jvmBuilder.ml 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (*
  2. The Haxe Compiler
  3. Copyright (C) 2005-2019 Haxe Foundation
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *)
  16. open JvmGlobals
  17. open JvmSignature
  18. open JvmAttribute
  19. type annotation_kind =
  20. | AInt of Int32.t
  21. | ADouble of float
  22. | AString of string
  23. | ABool of bool
  24. | AEnum of jsignature * string
  25. | AArray of annotation_kind list
  26. | AAnnotation of jsignature * annotation
  27. and annotation = (string * annotation_kind) list
  28. type export_config = {
  29. export_debug : bool;
  30. }
  31. let convert_annotations pool annotations =
  32. let rec process_annotation (jsig, l) =
  33. let offset = pool#add_string (generate_signature false jsig) in
  34. let l = List.map (fun (name,ak) ->
  35. let offset = pool#add_string name in
  36. let rec loop ak = match ak with
  37. | AInt i32 ->
  38. 'I',ValConst(pool#add (ConstInt i32))
  39. | ADouble f ->
  40. 'D',ValConst(pool#add (ConstDouble f))
  41. | AString s ->
  42. 's',ValConst(pool#add_string s)
  43. | ABool b ->
  44. 'Z',ValConst(pool#add (ConstInt (if b then Int32.one else Int32.zero)))
  45. | AEnum(jsig,name) ->
  46. 'e',ValEnum(pool#add_string (generate_signature false jsig),pool#add_string name)
  47. | AArray l ->
  48. let l = List.map (fun ak -> loop ak) l in
  49. '[',ValArray(Array.of_list l)
  50. | AAnnotation (jsig, a) ->
  51. let ann = process_annotation (jsig, a) in
  52. '@',ValAnnotation(ann)
  53. in
  54. offset,loop ak
  55. ) l in
  56. {
  57. ann_type = offset;
  58. ann_elements = Array.of_list l;
  59. }
  60. in
  61. let a = Array.map process_annotation annotations in
  62. a
  63. class base_builder = object(self)
  64. val mutable access_flags = 0
  65. val attributes = DynArray.create ()
  66. val annotations = DynArray.create ()
  67. val mutable was_exported = false
  68. method add_access_flag i =
  69. access_flags <- i lor access_flags
  70. method add_attribute (a : j_attribute) =
  71. DynArray.add attributes a
  72. method add_annotation (path : jpath) (a : annotation) =
  73. DynArray.add annotations ((TObject(path,[])),a)
  74. method private commit_annotations pool =
  75. if DynArray.length annotations > 0 then begin
  76. let open JvmAttribute in
  77. let a = convert_annotations pool (DynArray.to_array annotations) in
  78. self#add_attribute (AttributeRuntimeVisibleAnnotations a)
  79. end
  80. method export_attributes (pool : JvmConstantPool.constant_pool) =
  81. DynArray.to_array (DynArray.map (write_attribute pool) attributes)
  82. end