@grain/binaryen.ml 0.34.0 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. binaryen-archive/CHANGELOG.md +37 -0
  2. binaryen-archive/binaryen.opam +3 -3
  3. binaryen-archive/esy.lock/index.json +366 -391
  4. binaryen-archive/esy.lock/opam/base.v0.17.3/opam +5 -3
  5. binaryen-archive/esy.lock/opam/{chrome-trace.3.20.2 → chrome-trace.3.24.1}/opam +6 -8
  6. binaryen-archive/esy.lock/opam/{cmdliner.1.3.0 → cmdliner.2.1.1}/opam +26 -10
  7. binaryen-archive/esy.lock/opam/{dune-build-info.3.20.2 → dune-build-info.3.24.2}/opam +6 -8
  8. binaryen-archive/esy.lock/opam/{dune-configurator.3.20.2 → dune-configurator.3.24.2}/opam +6 -8
  9. binaryen-archive/esy.lock/opam/{dune.3.20.2 → dune.3.24.2}/opam +15 -10
  10. binaryen-archive/esy.lock/opam/fix.20250919/opam +1 -0
  11. binaryen-archive/esy.lock/opam/js_of_ocaml-compiler.6.4.1/opam +67 -0
  12. binaryen-archive/esy.lock/opam/{menhir.20250912 → menhir.20260209}/opam +6 -4
  13. binaryen-archive/esy.lock/opam/{menhirCST.20250912 → menhirCST.20260209}/opam +4 -3
  14. binaryen-archive/esy.lock/opam/menhirGLR.20260209/opam +30 -0
  15. binaryen-archive/esy.lock/opam/{menhirLib.20250912 → menhirLib.20260209}/opam +5 -4
  16. binaryen-archive/esy.lock/opam/{menhirSdk.20250912 → menhirSdk.20260209}/opam +5 -4
  17. binaryen-archive/esy.lock/opam/ocaml-lsp-server.1.23.1/opam +2 -2
  18. binaryen-archive/esy.lock/opam/{ocaml-version.4.0.3 → ocaml-version.4.1.3}/opam +3 -3
  19. binaryen-archive/esy.lock/opam/{ocaml_intrinsics_kernel.v0.17.1 → ocaml_intrinsics_kernel.v0.17.2}/opam +3 -3
  20. binaryen-archive/esy.lock/opam/ocamlbuild.0.16.1/opam +1 -0
  21. binaryen-archive/esy.lock/opam/ocamlfind.1.9.8/opam +2 -1
  22. binaryen-archive/esy.lock/opam/{ocamlformat-lib.0.27.0 → ocamlformat-lib.0.29.0}/opam +7 -6
  23. binaryen-archive/esy.lock/opam/{ocamlformat-rpc-lib.0.28.1 → ocamlformat-rpc-lib.0.29.0}/opam +4 -4
  24. binaryen-archive/esy.lock/opam/{ocamlformat.0.27.0 → ocamlformat.0.29.0}/opam +6 -5
  25. binaryen-archive/esy.lock/opam/{ocp-indent.1.7.0 → ocp-indent.1.9.0}/opam +10 -9
  26. binaryen-archive/esy.lock/opam/ppx_yojson_conv_lib.v0.17.0/opam +2 -0
  27. binaryen-archive/esy.lock/opam/{ppxlib.0.37.0 → ppxlib.0.39.0~5.6preview}/opam +12 -13
  28. binaryen-archive/esy.lock/opam/sexplib0.v0.17.0/opam +2 -0
  29. binaryen-archive/esy.lock/opam/stdio.v0.17.0/opam +3 -1
  30. binaryen-archive/esy.lock/opam/{xdg.3.20.2 → xdg.3.24.2}/opam +6 -8
  31. binaryen-archive/package.json +9 -5
  32. binaryen-archive/src/dune +6 -1
  33. binaryen-archive/src/export.c +10 -0
  34. binaryen-archive/src/export.js +13 -0
  35. binaryen-archive/src/export.ml +4 -0
  36. binaryen-archive/src/export.mli +1 -0
  37. binaryen-archive/src/expression.c +636 -0
  38. binaryen-archive/src/expression.js +423 -7
  39. binaryen-archive/src/expression.ml +224 -0
  40. binaryen-archive/src/expression.mli +159 -0
  41. binaryen-archive/src/heap_type.c +19 -0
  42. binaryen-archive/src/heap_type.js +12 -0
  43. binaryen-archive/src/heap_type.ml +10 -1
  44. binaryen-archive/src/heap_type.mli +8 -1
  45. binaryen-archive/src/import.c +32 -0
  46. binaryen-archive/src/import.js +47 -0
  47. binaryen-archive/src/import.ml +12 -0
  48. binaryen-archive/src/import.mli +5 -0
  49. binaryen-archive/src/memory.c +6 -1
  50. binaryen-archive/src/memory.js +2 -2
  51. binaryen-archive/src/memory.ml +1 -1
  52. binaryen-archive/src/memory.mli +1 -1
  53. binaryen-archive/src/module.js +9 -22
  54. binaryen-archive/src/module.ml +4 -0
  55. binaryen-archive/src/module.mli +1 -0
  56. binaryen-archive/src/module_feature.c +6 -0
  57. binaryen-archive/src/module_feature.js +6 -0
  58. binaryen-archive/src/ocaml_helpers.c +17 -0
  59. binaryen-archive/src/ocaml_helpers.h +9 -0
  60. binaryen-archive/src/ocaml_helpers.js +11 -0
  61. binaryen-archive/src/passes.ml +13 -11
  62. binaryen-archive/src/passes.mli +13 -11
  63. binaryen-archive/src/tag.c +61 -0
  64. binaryen-archive/src/tag.js +43 -0
  65. binaryen-archive/src/tag.ml +11 -0
  66. binaryen-archive/src/tag.mli +8 -0
  67. binaryen-archive/src/type.c +5 -3
  68. binaryen-archive/src/type.js +5 -9
  69. binaryen-archive/src/type.ml +4 -1
  70. binaryen-archive/src/type.mli +1 -1
  71. binaryen-archive/src/type_builder.c +180 -0
  72. binaryen-archive/src/type_builder.js +116 -0
  73. binaryen-archive/src/type_builder.ml +67 -0
  74. binaryen-archive/src/type_builder.mli +28 -0
  75. binaryen-archive/test/test.expected +323 -33
  76. binaryen-archive/test/test.ml +220 -4
  77. binaryen-archive/esy.lock/opam/base-bytes.base/opam +0 -10
  78. binaryen-archive/esy.lock/opam/js_of_ocaml-compiler.6.2.0/opam +0 -56
  79. binaryen-archive/esy.lock/opam/stdlib-shims.0.3.0/opam +0 -31
@@ -106,6 +106,9 @@ let generate_global_effects = "generate-global-effects"
106
106
  (** refine the types of globals *)
107
107
  let global_refining = "global-refining"
108
108
 
109
+ (** globally optimize struct values, also emitting ref.cast_desc_eq *)
110
+ let gsi_desc_cast = "gsi-desc-cast"
111
+
109
112
  (** globally optimize struct values *)
110
113
  let gsi = "gsi"
111
114
 
@@ -152,9 +155,6 @@ let inlining_optimizing = "inlining-optimizing"
152
155
  (** lower away binaryen intrinsics *)
153
156
  let intrinsic_lowering = "intrinsic-lowering"
154
157
 
155
- (** wrap imports and exports for JavaScript promise integration *)
156
- let jspi = "jspi"
157
-
158
158
  (** legalizes i64 types on the import/export boundary *)
159
159
  let legalize_js_interface = "legalize-js-interface"
160
160
 
@@ -229,13 +229,6 @@ let minify_imports_and_exports_and_modules =
229
229
  (** Split types into minimal recursion groups *)
230
230
  let minimize_rec_groups = "minimize-rec-groups"
231
231
 
232
- (** apply the assumption that asyncify imports always unwind, and we never
233
- rewind *)
234
- let mod_asyncify_always_and_only_unwind = "mod-asyncify-always-and-only-unwind"
235
-
236
- (** apply the assumption that asyncify never unwinds *)
237
- let mod_asyncify_never_unwind = "mod-asyncify-never-unwind"
238
-
239
232
  (** creates specialized versions of functions *)
240
233
  let monomorphize = "monomorphize"
241
234
 
@@ -328,6 +321,9 @@ let symbolmap = "symbolmap"
328
321
  (** removes operations incompatible with js *)
329
322
  let remove_non_js_ops = "remove-non-js-ops"
330
323
 
324
+ (** replaces relaxed SIMD instructions with unreachable *)
325
+ let remove_relaxed_simd = "remove-relaxed-simd"
326
+
331
327
  (** removes imports and replaces them with nops *)
332
328
  let remove_imports = "remove-imports"
333
329
 
@@ -365,6 +361,9 @@ let reorder_globals = "reorder-globals"
365
361
  (** sorts locals by access frequency *)
366
362
  let reorder_locals = "reorder-locals"
367
363
 
364
+ (** sorts private types by access frequency *)
365
+ let reorder_types = "reorder-types"
366
+
368
367
  (** re-optimize control flow using the relooper algorithm *)
369
368
  let rereloop = "rereloop"
370
369
 
@@ -472,6 +471,9 @@ let strip_eh = "strip-eh"
472
471
  (** strip the wasm target features section *)
473
472
  let strip_target_features = "strip-target-features"
474
473
 
474
+ (** strip all toolchain-specific code annotations *)
475
+ let strip_toolchain_annotations = "strip-toolchain-annotations"
476
+
475
477
  (** translate old Phase 3 EH instructions to new ones with exnref *)
476
478
  let translate_to_exnref = "translate-to-exnref"
477
479
 
@@ -490,7 +492,7 @@ let type_finalizing = "type-finalizing"
490
492
  (** merge types to their supertypes where possible *)
491
493
  let type_merging = "type-merging"
492
494
 
493
- (** create new nominal types to help other optimizations *)
495
+ (** create new types to help other optimizations *)
494
496
  let type_ssa = "type-ssa"
495
497
 
496
498
  (** mark all types as non-final (open) *)
@@ -106,6 +106,9 @@ val generate_global_effects : t
106
106
  val global_refining : t
107
107
  (** refine the types of globals *)
108
108
 
109
+ val gsi_desc_cast : t
110
+ (** globally optimize struct values, also emitting ref.cast_desc_eq *)
111
+
109
112
  val gsi : t
110
113
  (** globally optimize struct values *)
111
114
 
@@ -149,9 +152,6 @@ val inlining_optimizing : t
149
152
  val intrinsic_lowering : t
150
153
  (** lower away binaryen intrinsics *)
151
154
 
152
- val jspi : t
153
- (** wrap imports and exports for JavaScript promise integration *)
154
-
155
155
  val legalize_js_interface : t
156
156
  (** legalizes i64 types on the import/export boundary *)
157
157
 
@@ -225,13 +225,6 @@ val minify_imports_and_exports_and_modules : t
225
225
  val minimize_rec_groups : t
226
226
  (** Split types into minimal recursion groups *)
227
227
 
228
- val mod_asyncify_always_and_only_unwind : t
229
- (** apply the assumption that asyncify imports always unwind, and we never
230
- rewind *)
231
-
232
- val mod_asyncify_never_unwind : t
233
- (** apply the assumption that asyncify never unwinds *)
234
-
235
228
  val monomorphize : t
236
229
  (** creates specialized versions of functions *)
237
230
 
@@ -323,6 +316,9 @@ val symbolmap : t
323
316
  val remove_non_js_ops : t
324
317
  (** removes operations incompatible with js *)
325
318
 
319
+ val remove_relaxed_simd : t
320
+ (** replaces relaxed SIMD instructions with unreachable *)
321
+
326
322
  val remove_imports : t
327
323
  (** removes imports and replaces them with nops *)
328
324
 
@@ -359,6 +355,9 @@ val reorder_globals : t
359
355
  val reorder_locals : t
360
356
  (** sorts locals by access frequency *)
361
357
 
358
+ val reorder_types : t
359
+ (** sorts private types by access frequency *)
360
+
362
361
  val rereloop : t
363
362
  (** re-optimize control flow using the relooper algorithm *)
364
363
 
@@ -464,6 +463,9 @@ val strip_eh : t
464
463
  val strip_target_features : t
465
464
  (** strip the wasm target features section *)
466
465
 
466
+ val strip_toolchain_annotations : t
467
+ (** strip all toolchain-specific code annotations *)
468
+
467
469
  val translate_to_exnref : t
468
470
  (** translate old Phase 3 EH instructions to new ones with exnref *)
469
471
 
@@ -483,7 +485,7 @@ val type_merging : t
483
485
  (** merge types to their supertypes where possible *)
484
486
 
485
487
  val type_ssa : t
486
- (** create new nominal types to help other optimizations *)
488
+ (** create new types to help other optimizations *)
487
489
 
488
490
  val type_unfinalizing : t
489
491
  (** mark all types as non-final (open) *)
@@ -0,0 +1,61 @@
1
+ #define CAML_NAME_SPACE
2
+ #include <caml/mlvalues.h>
3
+ #include <caml/fail.h>
4
+ #include <caml/memory.h>
5
+ #include <caml/alloc.h>
6
+
7
+ #include "binaryen-c.h"
8
+ #include "ocaml_helpers.h"
9
+
10
+ CAMLprim value
11
+ caml_binaryen_add_tag(value _module, value _name, value _params, value _results) {
12
+ CAMLparam4(_module, _name, _params, _results);
13
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
14
+ char* name = Safe_String_val(_name);
15
+ BinaryenType params = BinaryenType_val(_params);
16
+ BinaryenType results = BinaryenType_val(_results);
17
+ BinaryenTagRef tag = BinaryenAddTag(module, name, params, results);
18
+ CAMLreturn(alloc_BinaryenTagRef(tag));
19
+ }
20
+
21
+ CAMLprim value
22
+ caml_binaryen_get_tag(value _module, value _name) {
23
+ CAMLparam2(_module, _name);
24
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
25
+ char* name = Safe_String_val(_name);
26
+ BinaryenTagRef tag = BinaryenGetTag(module, name);
27
+ CAMLreturn(alloc_BinaryenTagRef(tag));
28
+ }
29
+
30
+ CAMLprim value
31
+ caml_binaryen_remove_tag(value _module, value _name) {
32
+ CAMLparam2(_module, _name);
33
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
34
+ char* name = Safe_String_val(_name);
35
+ BinaryenRemoveTag(module, name);
36
+ CAMLreturn(Val_unit);
37
+ }
38
+
39
+ CAMLprim value
40
+ caml_binaryen_tag_get_name(value _tag) {
41
+ CAMLparam1(_tag);
42
+ BinaryenTagRef tag = BinaryenTagRef_val(_tag);
43
+ const char* name = BinaryenTagGetName(tag);
44
+ CAMLreturn(caml_copy_string(name));
45
+ }
46
+
47
+ CAMLprim value
48
+ caml_binaryen_tag_get_params(value _tag) {
49
+ CAMLparam1(_tag);
50
+ BinaryenTagRef tag = BinaryenTagRef_val(_tag);
51
+ BinaryenType ty = BinaryenTagGetParams(tag);
52
+ CAMLreturn(alloc_BinaryenType(ty));
53
+ }
54
+
55
+ CAMLprim value
56
+ caml_binaryen_tag_get_results(value _tag) {
57
+ CAMLparam1(_tag);
58
+ BinaryenTagRef tag = BinaryenTagRef_val(_tag);
59
+ BinaryenType ty = BinaryenTagGetResults(tag);
60
+ CAMLreturn(alloc_BinaryenType(ty));
61
+ }
@@ -0,0 +1,43 @@
1
+ //Provides: caml_binaryen_add_tag
2
+ //Requires: caml_jsstring_of_string
3
+ function caml_binaryen_add_tag(wasm_mod, name, params, results) {
4
+ return wasm_mod.addTag(
5
+ caml_jsstring_of_string(name),
6
+ params,
7
+ results,
8
+ );
9
+ }
10
+
11
+ //Provides: caml_binaryen_get_tag
12
+ //Requires: caml_jsstring_of_string
13
+ function caml_binaryen_get_tag(wasm_mod, name) {
14
+ return wasm_mod.getTag(caml_jsstring_of_string(name));
15
+ }
16
+
17
+ //Provides: caml_binaryen_remove_tag
18
+ //Requires: caml_jsstring_of_string
19
+ function caml_binaryen_remove_tag(wasm_mod, name) {
20
+ return wasm_mod.removeTag(caml_jsstring_of_string(name));
21
+ }
22
+
23
+ //Provides: caml_binaryen_tag_get_name
24
+ //Requires: Binaryen
25
+ //Requires: caml_string_of_jsstring
26
+ function caml_binaryen_tag_get_name(tag) {
27
+ var tag_info = Binaryen.getTagInfo(tag);
28
+ return caml_string_of_jsstring(tag_info.name);
29
+ }
30
+
31
+ //Provides: caml_binaryen_tag_get_params
32
+ //Requires: Binaryen
33
+ function caml_binaryen_tag_get_params(tag) {
34
+ var tag_info = Binaryen.getTagInfo(tag);
35
+ return tag_info.params;
36
+ }
37
+
38
+ //Provides: caml_binaryen_tag_get_results
39
+ //Requires: Binaryen
40
+ function caml_binaryen_tag_get_results(tag) {
41
+ var tag_info = Binaryen.getTagInfo(tag);
42
+ return tag_info.results;
43
+ }
@@ -0,0 +1,11 @@
1
+ type t
2
+
3
+ external add_tag : Module.t -> string -> Type.t -> Type.t -> t
4
+ = "caml_binaryen_add_tag"
5
+ (** Module, name, params, result *)
6
+
7
+ external get_tag : Module.t -> string -> t = "caml_binaryen_get_tag"
8
+ external remove_tag : Module.t -> string -> unit = "caml_binaryen_remove_tag"
9
+ external get_name : t -> string = "caml_binaryen_tag_get_name"
10
+ external get_params : t -> Type.t = "caml_binaryen_tag_get_params"
11
+ external get_results : t -> Type.t = "caml_binaryen_tag_get_results"
@@ -0,0 +1,8 @@
1
+ type t
2
+
3
+ val add_tag : Module.t -> string -> Type.t -> Type.t -> t
4
+ val get_tag : Module.t -> string -> t
5
+ val remove_tag : Module.t -> string -> unit
6
+ val get_name : t -> string
7
+ val get_params : t -> Type.t
8
+ val get_results : t -> Type.t
@@ -147,14 +147,16 @@ caml_binaryen_type_create(value tys) {
147
147
  CAMLprim value
148
148
  caml_binaryen_type_expand(value _ty) {
149
149
  CAMLparam1(_ty);
150
+ CAMLlocal2(typeArray, field);
150
151
  BinaryenType ty = BinaryenType_val(_ty);
151
152
  int arity = BinaryenTypeArity(ty);
152
153
  BinaryenType* buf = malloc(sizeof(BinaryenType) * arity);
153
154
  BinaryenTypeExpand(ty, buf);
154
155
 
155
- value typeArray = caml_alloc(arity, 0);
156
+ typeArray = caml_alloc(arity, 0);
156
157
  for (int i = 0; i < arity; i++) {
157
- Field(typeArray, i) = alloc_BinaryenType(buf[i]);
158
+ field = alloc_BinaryenType(buf[i]);
159
+ Store_field(typeArray, i, field);
158
160
  }
159
161
 
160
162
  free(buf);
@@ -176,4 +178,4 @@ caml_binaryen_type_from_heap_type(value _ty, value _nullable) {
176
178
  bool nullable = Bool_val(_nullable);
177
179
  BinaryenType ty = BinaryenTypeFromHeapType(heapTy, nullable);
178
180
  CAMLreturn(alloc_BinaryenType(ty));
179
- }
181
+ }
@@ -67,8 +67,7 @@ function caml_binaryen_type_structref() {
67
67
  //Provides: caml_binaryen_type_arrayref
68
68
  //Requires: Binaryen
69
69
  function caml_binaryen_type_arrayref() {
70
- // TODO: Binaryen v111 didn't expose this directly
71
- return Binaryen._BinaryenTypeArrayref();
70
+ return Binaryen.arrayref;
72
71
  }
73
72
 
74
73
  //Provides: caml_binaryen_type_stringref
@@ -80,22 +79,19 @@ function caml_binaryen_type_stringref() {
80
79
  //Provides: caml_binaryen_type_nullref
81
80
  //Requires: Binaryen
82
81
  function caml_binaryen_type_nullref() {
83
- // TODO: Binaryen v111 didn't expose this directly
84
- return Binaryen._BinaryenTypeNullref();
82
+ return Binaryen.nullref;
85
83
  }
86
84
 
87
85
  //Provides: caml_binaryen_type_null_externref
88
86
  //Requires: Binaryen
89
87
  function caml_binaryen_type_null_externref() {
90
- // TODO: Binaryen v111 didn't expose this directly
91
- return Binaryen._BinaryenTypeNullExternref();
88
+ return Binaryen.nullexternref;
92
89
  }
93
90
 
94
91
  //Provides: caml_binaryen_type_null_funcref
95
92
  //Requires: Binaryen
96
93
  function caml_binaryen_type_null_funcref() {
97
- // TODO: Binaryen v111 didn't expose this directly
98
- return Binaryen._BinaryenTypeNullFuncref();
94
+ return Binaryen.nullfuncref;
99
95
  }
100
96
 
101
97
  //Provides: caml_binaryen_type_unreachable
@@ -134,4 +130,4 @@ function caml_binaryen_type_is_nullable(typ) {
134
130
  //Requires: Binaryen
135
131
  function caml_binaryen_type_from_heap_type(typ, nullable) {
136
132
  return Binaryen._BinaryenTypeFromHeapType(typ, nullable);
137
- }
133
+ }
@@ -75,5 +75,8 @@ let auto = auto ()
75
75
  external create : t array -> t = "caml_binaryen_type_create"
76
76
  external expand : t -> t array = "caml_binaryen_type_expand"
77
77
  external is_nullable : t -> bool = "caml_binaryen_type_is_nullable"
78
- external from_heap_type : Heap_type.t -> t = "caml_binaryen_type_from_heap_type"
78
+
79
+ external from_heap_type : Heap_type.t -> bool -> t
80
+ = "caml_binaryen_type_from_heap_type"
81
+
79
82
  external get_heap_type : t -> Heap_type.t = "caml_binaryen_type_get_heap_type"
@@ -21,5 +21,5 @@ val auto : t
21
21
  val create : t array -> t
22
22
  val expand : t -> t array
23
23
  val is_nullable : t -> bool
24
- val from_heap_type : Heap_type.t -> t
24
+ val from_heap_type : Heap_type.t -> bool -> t
25
25
  val get_heap_type : t -> Heap_type.t
@@ -0,0 +1,180 @@
1
+ #define CAML_NAME_SPACE
2
+ #include <caml/alloc.h>
3
+ #include <caml/mlvalues.h>
4
+ #include <caml/fail.h>
5
+ #include <caml/memory.h>
6
+
7
+ #include "binaryen-c.h"
8
+ #include "ocaml_helpers.h"
9
+
10
+ CAMLprim value
11
+ caml_type_builder_create(value _size) {
12
+ CAMLparam1(_size);
13
+ BinaryenIndex size = Int_val(_size);
14
+ CAMLreturn(alloc_TypeBuilderRef(TypeBuilderCreate(size)));
15
+ }
16
+
17
+ CAMLprim value
18
+ caml_type_builder_grow(value _builder, value _count) {
19
+ CAMLparam2(_builder, _count);
20
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
21
+ BinaryenIndex count = Int_val(_count);
22
+ TypeBuilderGrow(builder, count);
23
+ CAMLreturn(Val_unit);
24
+ }
25
+
26
+ CAMLprim value
27
+ caml_type_builder_get_size(value _builder) {
28
+ CAMLparam1(_builder);
29
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
30
+ CAMLreturn(Val_int(TypeBuilderGetSize(builder)));
31
+ }
32
+
33
+ CAMLprim value
34
+ caml_type_builder_set_signature_type(value _builder, value _index, value _paramTypes, value _resultTypes) {
35
+ CAMLparam4(_builder, _index, _paramTypes, _resultTypes);
36
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
37
+ BinaryenIndex index = Int_val(_index);
38
+ BinaryenType paramTypes = BinaryenType_val(_paramTypes);
39
+ BinaryenType resultTypes = BinaryenType_val(_resultTypes);
40
+ TypeBuilderSetSignatureType(builder, index, paramTypes, resultTypes);
41
+ CAMLreturn(Val_unit);
42
+ }
43
+
44
+ CAMLprim value
45
+ caml_type_builder_set_struct_type(value _builder, value _index, value _fieldTypes, value _fieldPackedTypes, value _fieldMutables, value _numFields) {
46
+ CAMLparam5(_builder, _index, _fieldTypes, _fieldPackedTypes, _fieldMutables);
47
+ CAMLxparam1(_numFields);
48
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
49
+ BinaryenIndex index = Int_val(_index);
50
+ _fieldTypes = array_of_list(_fieldTypes);
51
+ int fieldTypesLen = array_length(_fieldTypes);
52
+ BinaryenType fieldTypes[fieldTypesLen];
53
+ for (int i = 0; i < fieldTypesLen; i++) {
54
+ fieldTypes[i] = BinaryenType_val(Field(_fieldTypes, i));
55
+ }
56
+ _fieldPackedTypes = array_of_list(_fieldPackedTypes);
57
+ int fieldPackedTypesLen = array_length(_fieldPackedTypes);
58
+ BinaryenPackedType fieldPackedTypes[fieldPackedTypesLen];
59
+ for (int i = 0; i < fieldPackedTypesLen; i++) {
60
+ fieldPackedTypes[i] = BinaryenPackedType_val(Field(_fieldPackedTypes, i));
61
+ }
62
+ _fieldMutables = array_of_list(_fieldMutables);
63
+ int fieldMutablesLen = array_length(_fieldMutables);
64
+ bool fieldMutables[fieldMutablesLen];
65
+ for (int i = 0; i < fieldMutablesLen; i++) {
66
+ fieldMutables[i] = Bool_val(Field(_fieldMutables, i));
67
+ }
68
+ int numFields = Int_val(_numFields);
69
+ TypeBuilderSetStructType(builder, index, fieldTypes, fieldPackedTypes, fieldMutables, numFields);
70
+ CAMLreturn(Val_unit);
71
+ }
72
+ CAMLprim value
73
+ caml_type_builder_set_struct_type__bytecode(value * argv) {
74
+ return caml_type_builder_set_struct_type(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
75
+ }
76
+
77
+ CAMLprim value
78
+ caml_type_builder_set_array_type(value _builder, value _index, value _elementType, value _elementPackedType, value _elementMutable) {
79
+ CAMLparam5(_builder, _index, _elementType, _elementPackedType, _elementMutable);
80
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
81
+ BinaryenIndex index = Int_val(_index);
82
+ BinaryenType elementType = BinaryenType_val(_elementType);
83
+ BinaryenPackedType elementPackedType = BinaryenPackedType_val(_elementPackedType);
84
+ bool elementMutable = Bool_val(_elementMutable);
85
+ TypeBuilderSetArrayType(builder, index, elementType, elementPackedType, elementMutable);
86
+ CAMLreturn(Val_unit);
87
+ }
88
+
89
+ CAMLprim value
90
+ caml_type_builder_get_temp_heap_type(value _builder, value _index) {
91
+ CAMLparam2(_builder, _index);
92
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
93
+ BinaryenIndex index = Int_val(_index);
94
+ CAMLreturn(alloc_BinaryenHeapType(TypeBuilderGetTempHeapType(builder, index)));
95
+ }
96
+
97
+ CAMLprim value
98
+ caml_type_builder_get_temp_tuple_type(value _builder, value _types) {
99
+ CAMLparam2(_builder, _types);
100
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
101
+ _types = array_of_list(_types);
102
+ int typesLen = array_length(_types);
103
+ BinaryenType types[typesLen];
104
+ for (int i = 0; i < typesLen; i++) {
105
+ types[i] = BinaryenType_val(Field(_types, i));
106
+ }
107
+ CAMLreturn(alloc_BinaryenType(TypeBuilderGetTempTupleType(builder, types, typesLen)));
108
+ }
109
+
110
+ CAMLprim value
111
+ caml_type_builder_get_temp_ref_type(value _builder, value _heapType, value _nullable) {
112
+ CAMLparam3(_builder, _heapType, _nullable);
113
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
114
+ BinaryenHeapType heapType = BinaryenHeapType_val(_heapType);
115
+ bool nullable = Bool_val(_nullable);
116
+ CAMLreturn(alloc_BinaryenType(TypeBuilderGetTempRefType(builder, heapType, nullable)));
117
+ }
118
+
119
+ CAMLprim value
120
+ caml_type_builder_set_sub_type(value _builder, value _index, value _superType) {
121
+ CAMLparam3(_builder, _index, _superType);
122
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
123
+ BinaryenIndex index = Int_val(_index);
124
+ BinaryenHeapType superType = BinaryenHeapType_val(_superType);
125
+ TypeBuilderSetSubType(builder, index, superType);
126
+ CAMLreturn(Val_unit);
127
+ }
128
+
129
+ CAMLprim value
130
+ caml_type_builder_set_open(value _builder, value _index) {
131
+ CAMLparam2(_builder, _index);
132
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
133
+ BinaryenIndex index = Int_val(_index);
134
+ TypeBuilderSetOpen(builder, index);
135
+ CAMLreturn(Val_unit);
136
+ }
137
+
138
+ CAMLprim value
139
+ caml_type_builder_create_rec_group(value _builder, value _index, value _length) {
140
+ CAMLparam3(_builder, _index, _length);
141
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
142
+ BinaryenIndex index = Int_val(_index);
143
+ BinaryenIndex length = Int_val(_length);
144
+ TypeBuilderCreateRecGroup(builder, index, length);
145
+ CAMLreturn(Val_unit);
146
+ }
147
+
148
+ CAMLprim value
149
+ caml_conv_heap_type(BinaryenHeapType heapType) {
150
+ return alloc_BinaryenHeapType(heapType);
151
+ }
152
+
153
+ CAMLprim value caml_type_builder_build_and_dispose(value _builder) {
154
+ CAMLparam1(_builder);
155
+ CAMLlocal5(arr, ok, error, tuple, field);
156
+ TypeBuilderRef builder = TypeBuilderRef_val(_builder);
157
+ BinaryenIndex size = TypeBuilderGetSize(builder);
158
+ BinaryenHeapType heapTypes[size + 1];
159
+ BinaryenIndex errorIndex;
160
+ TypeBuilderErrorReason errorReason;
161
+ bool success =
162
+ TypeBuilderBuildAndDispose(builder, heapTypes, &errorIndex, &errorReason);
163
+ if (success) {
164
+ arr = caml_alloc(size, 0);
165
+ for (mlsize_t i = 0; i < (mlsize_t)size; i++) {
166
+ field = alloc_BinaryenHeapType(heapTypes[i]);
167
+ Store_field(arr, i, field);
168
+ }
169
+ ok = caml_alloc_small(1, 0);
170
+ Store_field(ok, 0, arr);
171
+ CAMLreturn(ok);
172
+ } else {
173
+ error = caml_alloc_small(1, 1);
174
+ tuple = caml_alloc_small(2, 0);
175
+ Field(tuple, 0) = Val_int(errorIndex);
176
+ Field(tuple, 1) = Val_int(errorReason);
177
+ Store_field(error, 0, tuple);
178
+ CAMLreturn(error);
179
+ }
180
+ }
@@ -0,0 +1,116 @@
1
+ //Provides: caml_type_builder_create
2
+ //Requires: Binaryen
3
+ function caml_type_builder_create(size) {
4
+ return new Binaryen.TypeBuilder(size);
5
+ }
6
+
7
+ //Provides: caml_type_builder_grow
8
+ function caml_type_builder_grow(builder, count) {
9
+ builder.grow(count);
10
+ }
11
+
12
+ //Provides: caml_type_builder_get_size
13
+ function caml_type_builder_get_size(builder) {
14
+ return builder.getSize();
15
+ }
16
+
17
+ //Provides: caml_type_builder_set_signature_type
18
+ function caml_type_builder_set_signature_type(
19
+ builder,
20
+ index,
21
+ paramTypes,
22
+ resultTypes
23
+ ) {
24
+ builder.setSignatureType(index, paramTypes, resultTypes);
25
+ }
26
+
27
+ //Provides: caml_type_builder_set_struct_type
28
+ //Requires: caml_list_to_js_array
29
+ //Requires: caml_js_from_bool
30
+ function caml_type_builder_set_struct_type(
31
+ builder,
32
+ index,
33
+ fieldTypes,
34
+ fieldPackedTypes,
35
+ fieldMutables,
36
+ numFields
37
+ ) {
38
+ var types = caml_list_to_js_array(fieldTypes);
39
+ var packedTypes = caml_list_to_js_array(fieldPackedTypes);
40
+ var mutables = caml_list_to_js_array(fieldMutables);
41
+ var fields = types.map(function (type, idx) {
42
+ return {
43
+ type,
44
+ packedType: packedTypes[idx],
45
+ mutable: caml_js_from_bool(mutables[idx]),
46
+ };
47
+ });
48
+ builder.setStructType(index, fields);
49
+ }
50
+ //Provides: caml_type_builder_set_struct_type__bytecode
51
+ //Requires: caml_type_builder_set_struct_type
52
+ function caml_type_builder_set_struct_type__bytecode() {
53
+ return caml_type_builder_set_struct_type(
54
+ arguments[0],
55
+ arguments[1],
56
+ arguments[2],
57
+ arguments[3],
58
+ arguments[4],
59
+ arguments[5]
60
+ );
61
+ }
62
+
63
+ //Provides: caml_type_builder_set_array_type
64
+ function caml_type_builder_set_array_type(
65
+ builder,
66
+ index,
67
+ elementType,
68
+ elementPackedType,
69
+ elementMutable
70
+ ) {
71
+ builder.setArrayType(index, elementType, elementPackedType, elementMutable);
72
+ }
73
+
74
+ //Provides: caml_type_builder_get_temp_heap_type
75
+ function caml_type_builder_get_temp_heap_type(builder, index) {
76
+ return builder.getTempHeapType(index);
77
+ }
78
+
79
+ //Provides: caml_type_builder_get_temp_tuple_type
80
+ //Requires: caml_list_to_js_array
81
+ function caml_type_builder_get_temp_tuple_type(builder, types) {
82
+ var tupleTypes = caml_list_to_js_array(types);
83
+ return builder.getTempTupleType(tupleTypes);
84
+ }
85
+
86
+ //Provides: caml_type_builder_get_temp_ref_type
87
+ //Requires: caml_js_from_bool
88
+ function caml_type_builder_get_temp_ref_type(builder, heapType, nullable) {
89
+ return builder.getTempRefType(heapType, caml_js_from_bool(nullable));
90
+ }
91
+
92
+ //Provides: caml_type_builder_set_sub_type
93
+ function caml_type_builder_set_sub_type(builder, index, superType) {
94
+ builder.setSubType(index, superType);
95
+ }
96
+
97
+ //Provides: caml_type_builder_set_open
98
+ function caml_type_builder_set_open(builder, index) {
99
+ builder.setOpen(index);
100
+ }
101
+
102
+ //Provides: caml_type_builder_create_rec_group
103
+ function caml_type_builder_create_rec_group(builder, index, length) {
104
+ builder.createRecGroup(index, length);
105
+ }
106
+
107
+ //Provides: caml_type_builder_build_and_dispose
108
+ //Requires: caml_js_to_array
109
+ function caml_type_builder_build_and_dispose(builder) {
110
+ try {
111
+ var types = builder.buildAndDispose();
112
+ return [0, caml_js_to_array(types)];
113
+ } catch (e) {
114
+ return [1, [0, e.index, e.reason]];
115
+ }
116
+ }