@grain/binaryen.ml 0.38.0 → 0.39.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.
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.39.0](https://github.com/grain-lang/binaryen.ml/compare/v0.38.0...v0.39.0) (2026-09-06)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * Upgrade to Binaryen v129 ([#276](https://github.com/grain-lang/binaryen.ml/issues/276))
9
+
10
+ ### Features
11
+
12
+ * Upgrade to Binaryen v129 ([#276](https://github.com/grain-lang/binaryen.ml/issues/276)) ([4784ff8](https://github.com/grain-lang/binaryen.ml/commit/4784ff8afbeab8870b72ab84a0bfbb0bf3a4451c))
13
+
3
14
  ## [0.38.0](https://github.com/grain-lang/binaryen.ml/compare/v0.37.0...v0.38.0) (2026-09-05)
4
15
 
5
16
 
@@ -1,5 +1,5 @@
1
1
  opam-version: "2.0"
2
- version: "0.38.0"
2
+ version: "0.39.0"
3
3
  synopsis: "OCaml bindings for Binaryen"
4
4
  maintainer: "oscar@grain-lang.org"
5
5
  author: "Oscar Spencer"
@@ -16,6 +16,6 @@ depends: [
16
16
  "dune" {>= "3.0.0"}
17
17
  "dune-configurator" {>= "3.0.0"}
18
18
  "js_of_ocaml-compiler" {>= "6.4.0" < "7.0.0"}
19
- "libbinaryen" {>= "128.0.0" < "129.0.0"}
19
+ "libbinaryen" {>= "129.0.0" < "130.0.0"}
20
20
  ]
21
21
  x-maintenance-intent: ["0.(latest)"]
@@ -1,5 +1,5 @@
1
1
  {
2
- "checksum": "e0ac1b977a50c857edf45c8c6b5c6a02",
2
+ "checksum": "20ec2a11257be87e1aa8fef3d188392e",
3
3
  "root": "@grain/binaryen.ml@link-dev:./package.json",
4
4
  "node": {
5
5
  "ocaml@5.3.0@d41d8cd9": {
@@ -1934,14 +1934,14 @@
1934
1934
  [ "windows", "x86_64" ]
1935
1935
  ]
1936
1936
  },
1937
- "@grain/libbinaryen@128.0.0@d41d8cd9": {
1938
- "id": "@grain/libbinaryen@128.0.0@d41d8cd9",
1937
+ "@grain/libbinaryen@129.0.0@d41d8cd9": {
1938
+ "id": "@grain/libbinaryen@129.0.0@d41d8cd9",
1939
1939
  "name": "@grain/libbinaryen",
1940
- "version": "128.0.0",
1940
+ "version": "129.0.0",
1941
1941
  "source": {
1942
1942
  "type": "install",
1943
1943
  "source": [
1944
- "archive:https://registry.npmjs.org/@grain/libbinaryen/-/libbinaryen-128.0.0.tgz#sha1:d2e540a3177f9f6f0f27e6125efdffa5bf670814"
1944
+ "archive:https://registry.npmjs.org/@grain/libbinaryen/-/libbinaryen-129.0.0.tgz#sha1:6d927815b071a528ee314ad02c3279d86428aec6"
1945
1945
  ]
1946
1946
  },
1947
1947
  "overrides": [],
@@ -1974,7 +1974,7 @@
1974
1974
  "ocaml@5.3.0@d41d8cd9",
1975
1975
  "@opam/dune-configurator@opam:3.24.2@7928eaef",
1976
1976
  "@opam/dune@opam:3.24.2@dc9091c2",
1977
- "@grain/libbinaryen@128.0.0@d41d8cd9"
1977
+ "@grain/libbinaryen@129.0.0@d41d8cd9"
1978
1978
  ],
1979
1979
  "devDependencies": [
1980
1980
  "@opam/ocamlformat@opam:0.29.0@966c16ee",
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@grain/binaryen.ml",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "OCaml bindings for Binaryen.",
5
5
  "author": "Oscar Spencer <oscar@grain-lang.org>",
6
6
  "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/grain-lang/binaryen.ml.git"
10
+ },
7
11
  "installConfig": {
8
12
  "pnp": false
9
13
  },
10
14
  "dependencies": {
11
15
  "ocaml": ">= 4.14.0 < 6.0.0",
12
- "@grain/libbinaryen": ">= 128.0.0 < 129.0.0",
16
+ "@grain/libbinaryen": ">= 129.0.0 < 130.0.0",
13
17
  "@opam/dune": ">= 3.0.0",
14
18
  "@opam/dune-configurator": ">= 3.0.0"
15
19
  },
@@ -36,4 +40,4 @@
36
40
  "binaryen",
37
41
  "ocaml"
38
42
  ]
39
- }
43
+ }
@@ -0,0 +1,81 @@
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
+
11
+ CAMLprim value
12
+ caml_binaryen_get_num_memory_segments(value _module) {
13
+ CAMLparam1(_module);
14
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
15
+ CAMLreturn(Val_int(BinaryenGetNumMemorySegments(module)));
16
+ }
17
+
18
+ CAMLprim value caml_binaryen_get_data_segment(value _module, value _name) {
19
+ CAMLparam2(_module, _name);
20
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
21
+ char* name = Safe_String_val(_name);
22
+ BinaryenDataSegmentRef segment = BinaryenGetDataSegment(module, name);
23
+ if (segment == NULL) {
24
+ CAMLreturn(Val_none);
25
+ } else {
26
+ CAMLreturn(caml_alloc_some(alloc_BinaryenDataSegmentRef(segment)));
27
+ }
28
+ }
29
+
30
+ CAMLprim value caml_binaryen_get_data_segment_by_index(value _module, value _index) {
31
+ CAMLparam2(_module, _index);
32
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
33
+ int index = Int_val(_index);
34
+ BinaryenDataSegmentRef segment = BinaryenGetDataSegmentByIndex(module, index);
35
+ CAMLreturn(alloc_BinaryenDataSegmentRef(segment));
36
+ }
37
+
38
+ CAMLprim value caml_binaryen_data_segment_get_name(value _module, value _segment) {
39
+ CAMLparam2(_module, _segment);
40
+ BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
41
+ const char* name = BinaryenDataSegmentGetName(segment);
42
+ CAMLreturn(caml_copy_string(name));
43
+ }
44
+
45
+ CAMLprim value
46
+ caml_binaryen_get_memory_segment_byte_offset(value _module, value _segment) {
47
+ CAMLparam2(_module, _segment);
48
+ BinaryenModuleRef module = BinaryenModuleRef_val(_module);
49
+ BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
50
+ if (BinaryenGetMemorySegmentPassive(segment)) {
51
+ CAMLreturn(Val_none);
52
+ } else {
53
+ int offset = BinaryenGetMemorySegmentByteOffset(module, segment);
54
+ CAMLreturn(caml_alloc_some(Val_int(offset)));
55
+ }
56
+ }
57
+
58
+ CAMLprim value
59
+ caml_binaryen_get_memory_segment_byte_length(value _segment) {
60
+ CAMLparam1(_segment);
61
+ BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
62
+ int length = BinaryenGetMemorySegmentByteLength(segment);
63
+ CAMLreturn(Val_int(length));
64
+ }
65
+
66
+ CAMLprim value
67
+ caml_binaryen_get_memory_segment_passive(value _segment) {
68
+ CAMLparam1(_segment);
69
+ BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
70
+ CAMLreturn(Val_bool(BinaryenGetMemorySegmentPassive(segment)));
71
+ }
72
+
73
+ CAMLprim value
74
+ caml_binaryen_get_memory_segment_data(value _module, value _segment) {
75
+ CAMLparam2(_module, _segment);
76
+ BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
77
+ size_t size = BinaryenGetMemorySegmentByteLength(segment);
78
+ CAMLprim value bytes = caml_alloc_string(size);
79
+ BinaryenCopyMemorySegmentData(segment, (char*)Bytes_val(bytes));
80
+ CAMLreturn(bytes);
81
+ }
@@ -0,0 +1,49 @@
1
+
2
+ //Provides: caml_binaryen_get_num_memory_segments
3
+ function caml_binaryen_get_num_memory_segments(wasm_mod) {
4
+ return wasm_mod.getNumMemorySegments();
5
+ }
6
+
7
+ //Provides: caml_binaryen_get_data_segment
8
+ //Requires: caml_jsstring_of_string, to_option
9
+ function caml_binaryen_get_data_segment(wasm_mod, name) {
10
+ return to_option(wasm_mod.getDataSegment(caml_jsstring_of_string(name)));
11
+ }
12
+
13
+ //Provides: caml_binaryen_get_data_segment_by_index
14
+ function caml_binaryen_get_data_segment_by_index(wasm_mod, index) {
15
+ return wasm_mod.getDataSegmentByIndex(index);
16
+ }
17
+
18
+ // Provides: caml_binaryen_data_segment_get_name
19
+ // Requires: caml_string_of_jsstring
20
+ function caml_binaryen_data_segment_get_name(wasm_mod, segment) {
21
+ var info = wasm_mod.getMemorySegmentInfo(segment);
22
+ return caml_string_of_jsstring(info.name);
23
+ }
24
+
25
+ //Provides: caml_binaryen_get_memory_segment_byte_offset
26
+ //Requires: to_option
27
+ function caml_binaryen_get_memory_segment_byte_offset(wasm_mod, segment) {
28
+ var info = wasm_mod.getMemorySegmentInfo(segment);
29
+ return to_option(info.offset);
30
+ }
31
+
32
+ //Provides: caml_binaryen_get_memory_segment_byte_length
33
+ //Requires: Binaryen
34
+ function caml_binaryen_get_memory_segment_byte_length(segment) {
35
+ return Binaryen._BinaryenGetMemorySegmentByteLength(segment);
36
+ }
37
+
38
+ //Provides: caml_binaryen_get_memory_segment_passive
39
+ //Requires: Binaryen
40
+ function caml_binaryen_get_memory_segment_passive(segment) {
41
+ return Binaryen._BinaryenGetMemorySegmentPassive(segment);
42
+ }
43
+
44
+ //Provides: caml_binaryen_get_memory_segment_data
45
+ //Requires: caml_bytes_of_array
46
+ function caml_binaryen_get_memory_segment_data(wasm_mod, segment) {
47
+ var info = wasm_mod.getMemorySegmentInfo(segment);
48
+ return caml_bytes_of_array(info.data);
49
+ }
@@ -0,0 +1,25 @@
1
+ type t
2
+
3
+ external get_num_segments : Module.t -> int
4
+ = "caml_binaryen_get_num_memory_segments"
5
+
6
+ external get_segment : Module.t -> string -> t option
7
+ = "caml_binaryen_get_data_segment"
8
+
9
+ external get_segment_by_index : Module.t -> int -> t
10
+ = "caml_binaryen_get_data_segment_by_index"
11
+
12
+ external get_segment_name : Module.t -> t -> string
13
+ = "caml_binaryen_data_segment_get_name"
14
+
15
+ external get_segment_byte_offset : Module.t -> t -> int option
16
+ = "caml_binaryen_get_memory_segment_byte_offset"
17
+
18
+ external get_segment_byte_length : t -> int
19
+ = "caml_binaryen_get_memory_segment_byte_length"
20
+
21
+ external get_segment_passive : t -> bool
22
+ = "caml_binaryen_get_memory_segment_passive"
23
+
24
+ external get_segment_data : Module.t -> t -> bytes
25
+ = "caml_binaryen_get_memory_segment_data"
@@ -0,0 +1,10 @@
1
+ type t
2
+
3
+ val get_num_segments : Module.t -> int
4
+ val get_segment : Module.t -> string -> t option
5
+ val get_segment_by_index : Module.t -> int -> t
6
+ val get_segment_name : Module.t -> t -> string
7
+ val get_segment_byte_offset : Module.t -> t -> int option
8
+ val get_segment_byte_length : t -> int
9
+ val get_segment_passive : t -> bool
10
+ val get_segment_data : Module.t -> t -> bytes
binaryen-archive/src/dune CHANGED
@@ -6,6 +6,7 @@
6
6
  (language c)
7
7
  (names
8
8
  array_type
9
+ data_segment
9
10
  type
10
11
  op
11
12
  literal
@@ -30,6 +31,7 @@
30
31
  (js_of_ocaml
31
32
  (javascript_files
32
33
  array_type.js
34
+ data_segment.js
33
35
  type.js
34
36
  op.js
35
37
  literal.js
@@ -107,43 +107,4 @@ caml_binaryen_memory_is_64(value _module, value _memoryName) {
107
107
  BinaryenModuleRef module = BinaryenModuleRef_val(_module);
108
108
  char* memoryName = Safe_String_val(_memoryName);
109
109
  CAMLreturn(Val_bool(BinaryenMemoryIs64(module, memoryName)));
110
- }
111
-
112
- CAMLprim value
113
- caml_binaryen_get_num_memory_segments(value _module) {
114
- CAMLparam1(_module);
115
- BinaryenModuleRef module = BinaryenModuleRef_val(_module);
116
- CAMLreturn(Val_int(BinaryenGetNumMemorySegments(module)));
117
- }
118
-
119
- CAMLprim value
120
- caml_binaryen_get_memory_segment_byte_offset(value _module, value _name) {
121
- CAMLparam2(_module, _name);
122
- BinaryenModuleRef module = BinaryenModuleRef_val(_module);
123
- char* name = Safe_String_val(_name);
124
- if (BinaryenGetMemorySegmentPassive(module, name)) {
125
- CAMLreturn(Val_none);
126
- } else {
127
- int offset = BinaryenGetMemorySegmentByteOffset(module, name);
128
- CAMLreturn(caml_alloc_some(Val_int(offset)));
129
- }
130
- }
131
-
132
- CAMLprim value
133
- caml_binaryen_get_memory_segment_passive(value _module, value _name) {
134
- CAMLparam2(_module, _name);
135
- BinaryenModuleRef module = BinaryenModuleRef_val(_module);
136
- char* name = Safe_String_val(_name);
137
- CAMLreturn(Val_bool(BinaryenGetMemorySegmentPassive(module, name)));
138
- }
139
-
140
- CAMLprim value
141
- caml_binaryen_get_memory_segment_data(value _module, value _name) {
142
- CAMLparam2(_module, _name);
143
- BinaryenModuleRef module = BinaryenModuleRef_val(_module);
144
- char* name = Safe_String_val(_name);
145
- size_t size = BinaryenGetMemorySegmentByteLength(module, name);
146
- CAMLprim value bytes = caml_alloc_string(size);
147
- BinaryenCopyMemorySegmentData(module, name, (char*)Bytes_val(bytes));
148
- CAMLreturn(bytes);
149
- }
110
+ }
@@ -104,30 +104,4 @@ function caml_binaryen_memory_is_shared(mod, memoryName) {
104
104
  function caml_binaryen_memory_is_64(mod, memoryName) {
105
105
  var memory_info = mod.getMemoryInfo(caml_jsstring_of_string(memoryName));
106
106
  return caml_js_to_bool(memory_info.is64);
107
- }
108
-
109
- //Provides: caml_binaryen_get_num_memory_segments
110
- function caml_binaryen_get_num_memory_segments(wasm_mod) {
111
- return wasm_mod.getNumMemorySegments();
112
- }
113
-
114
- //Provides: caml_binaryen_get_memory_segment_byte_offset
115
- //Requires: caml_jsstring_of_string, to_option
116
- function caml_binaryen_get_memory_segment_byte_offset(wasm_mod, name) {
117
- var info = wasm_mod.getMemorySegmentInfo(caml_jsstring_of_string(name));
118
- return to_option(info.offset);
119
- }
120
-
121
- //Provides: caml_binaryen_get_memory_segment_passive
122
- //Requires: caml_jsstring_of_string
123
- function caml_binaryen_get_memory_segment_passive(wasm_mod, name) {
124
- var info = wasm_mod.getMemorySegmentInfo(caml_jsstring_of_string(name));
125
- return info.passive;
126
- }
127
-
128
- //Provides: caml_binaryen_get_memory_segment_data
129
- //Requires: caml_bytes_of_array, caml_jsstring_of_string
130
- function caml_binaryen_get_memory_segment_data(wasm_mod, name) {
131
- var info = wasm_mod.getMemorySegmentInfo(caml_jsstring_of_string(name));
132
- return caml_bytes_of_array(info.data);
133
- }
107
+ }
@@ -67,15 +67,3 @@ external is_shared : Module.t -> string -> bool
67
67
  external is_64 : Module.t -> string -> bool = "caml_binaryen_memory_is_64"
68
68
 
69
69
  let unlimited = -1
70
-
71
- external get_num_segments : Module.t -> int
72
- = "caml_binaryen_get_num_memory_segments"
73
-
74
- external get_segment_byte_offset : Module.t -> string -> int option
75
- = "caml_binaryen_get_memory_segment_byte_offset"
76
-
77
- external get_segment_passive : Module.t -> string -> bool
78
- = "caml_binaryen_get_memory_segment_passive"
79
-
80
- external get_segment_data : Module.t -> string -> bytes
81
- = "caml_binaryen_get_memory_segment_data"
@@ -19,7 +19,3 @@ val get_max : Module.t -> string -> int
19
19
  val is_shared : Module.t -> string -> bool
20
20
  val is_64 : Module.t -> string -> bool
21
21
  val unlimited : int
22
- val get_num_segments : Module.t -> int
23
- val get_segment_byte_offset : Module.t -> string -> int option
24
- val get_segment_passive : Module.t -> string -> bool
25
- val get_segment_data : Module.t -> string -> bytes
@@ -137,6 +137,16 @@ caml_binaryen_module_read(value _bytes) {
137
137
  CAMLreturn(alloc_BinaryenModuleRef(result));
138
138
  }
139
139
 
140
+ CAMLprim value
141
+ caml_binaryen_module_read_with_features(value _bytes, value _features) {
142
+ CAMLparam2(_bytes, _features);
143
+ char* bytes = Safe_String_val(_bytes);
144
+ int length = caml_string_length(_bytes);
145
+ BinaryenFeatures features = Int_val(_features);
146
+ BinaryenModuleRef result = BinaryenModuleReadWithFeatures(bytes, length, features);
147
+ CAMLreturn(alloc_BinaryenModuleRef(result));
148
+ }
149
+
140
150
  CAMLprim value
141
151
  caml_binaryen_module_interpret(value _module) {
142
152
  CAMLparam1(_module);
@@ -93,6 +93,14 @@ function caml_binaryen_module_read(bytes) {
93
93
  return Binaryen.readBinary(data);
94
94
  }
95
95
 
96
+ //Provides: caml_binaryen_module_read_with_features
97
+ //Requires: Binaryen
98
+ //Requires: caml_uint8_array_of_bytes
99
+ function caml_binaryen_module_read_with_features(bytes, features) {
100
+ var data = caml_uint8_array_of_bytes(bytes);
101
+ return Binaryen.readBinaryWithFeatures(data, features);
102
+ }
103
+
96
104
  //Provides: caml_binaryen_module_interpret
97
105
  function caml_binaryen_module_interpret(wasm_mod) {
98
106
  return wasm_mod.interpret();
@@ -102,6 +102,10 @@ module Feature = struct
102
102
 
103
103
  let relaxed_atomics = relaxed_atomics ()
104
104
 
105
+ external multibyte : unit -> t = "caml_binaryen_feature_multibyte"
106
+
107
+ let multibyte = multibyte ()
108
+
105
109
  external custom_page_sizes : unit -> t
106
110
  = "caml_binaryen_feature_custom_page_sizes"
107
111
 
@@ -152,6 +156,13 @@ external write : t -> string option -> bytes * string option
152
156
  external write_text : t -> string = "caml_binaryen_module_write_text"
153
157
  external write_stack_ir : t -> string = "caml_binaryen_module_write_stack_ir"
154
158
  external read : bytes -> t = "caml_binaryen_module_read"
159
+
160
+ external read_with_features : bytes -> int -> t
161
+ = "caml_binaryen_module_read_with_features"
162
+
163
+ let read_with_features buf features =
164
+ read_with_features buf (List.fold_left ( lor ) 0 features)
165
+
155
166
  external interpret : t -> unit = "caml_binaryen_module_interpret"
156
167
 
157
168
  external add_debug_info_filename : t -> string -> int
@@ -26,6 +26,7 @@ module Feature : sig
26
26
  val bulk_memory_opt : t
27
27
  val call_indirect_overlong : t
28
28
  val relaxed_atomics : t
29
+ val multibyte : t
29
30
  val custom_page_sizes : t
30
31
  val all : t
31
32
  end
@@ -46,6 +47,7 @@ val write : t -> string option -> bytes * string option
46
47
  val write_text : t -> string
47
48
  val write_stack_ir : t -> string
48
49
  val read : bytes -> t
50
+ val read_with_features : bytes -> Feature.t list -> t
49
51
  val interpret : t -> unit
50
52
  val add_debug_info_filename : t -> string -> int
51
53
  val get_debug_info_filename : t -> int -> string
@@ -161,6 +161,12 @@ caml_binaryen_feature_relaxed_atomics(value unit) {
161
161
  CAMLreturn(Val_int(BinaryenFeatureRelaxedAtomics()));
162
162
  }
163
163
 
164
+ CAMLprim value
165
+ caml_binaryen_feature_multibyte(value unit) {
166
+ CAMLparam1(unit);
167
+ CAMLreturn(Val_int(BinaryenFeatureMultibyte()));
168
+ }
169
+
164
170
  CAMLprim value
165
171
  caml_binaryen_feature_custom_page_sizes(value unit) {
166
172
  CAMLparam1(unit);
@@ -146,6 +146,12 @@ function caml_binaryen_feature_relaxed_atomics() {
146
146
  return Binaryen.Features.RelaxedAtomics;
147
147
  }
148
148
 
149
+ //Provides: caml_binaryen_feature_multibyte
150
+ //Requires: Binaryen
151
+ function caml_binaryen_feature_multibyte() {
152
+ return Binaryen._BinaryenFeatureMultibyte;
153
+ }
154
+
149
155
  //Provides: caml_binaryen_feature_custom_page_sizes
150
156
  //Requires: Binaryen
151
157
  function caml_binaryen_feature_custom_page_sizes() {
@@ -111,6 +111,14 @@ value alloc_TypeBuilderRef(TypeBuilderRef builder)
111
111
  return v;
112
112
  }
113
113
 
114
+ /* Allocating an OCaml custom block to hold the given BinaryenDataSegmentRef */
115
+ value alloc_BinaryenDataSegmentRef(BinaryenDataSegmentRef seg)
116
+ {
117
+ value v = caml_alloc_custom(&binaryen_ops, sizeof(BinaryenDataSegmentRef), 0, 1);
118
+ BinaryenDataSegmentRef_val(v) = seg;
119
+ return v;
120
+ }
121
+
114
122
  CAMLprim value
115
123
  array_of_list(value list) {
116
124
  CAMLparam1(list);
@@ -35,6 +35,7 @@ static struct custom_operations binaryen_ops = {
35
35
  #define BinaryenTableRef_val(v) (*((BinaryenTableRef*) Data_custom_val(v)))
36
36
  #define BinaryenElementSegmentRef_val(v) (*((BinaryenElementSegmentRef*) Data_custom_val(v)))
37
37
  #define TypeBuilderRef_val(v) (*((TypeBuilderRef*) Data_custom_val(v)))
38
+ #define BinaryenDataSegmentRef_val(v) (*((BinaryenDataSegmentRef*) Data_custom_val(v)))
38
39
 
39
40
  #define Val_none Val_int(0)
40
41
  #define Some_val(v) Field(v, 0)
@@ -83,6 +84,9 @@ value alloc_BinaryenElementSegmentRef(BinaryenElementSegmentRef elem);
83
84
  /* Allocating an OCaml custom block to hold the given TypeBuilderRef */
84
85
  value alloc_TypeBuilderRef(TypeBuilderRef builder);
85
86
 
87
+ /* Allocating an OCaml custom block to hold the given BinaryenDataSegmentRef */
88
+ value alloc_BinaryenDataSegmentRef(BinaryenDataSegmentRef seg);
89
+
86
90
  CAMLprim value
87
91
  array_of_list(value list);
88
92
 
@@ -8,14 +8,25 @@
8
8
  #include "ocaml_helpers.h"
9
9
 
10
10
  CAMLprim value
11
- caml_binaryen_add_table(value _module, value _table, value _initial, value _maximum, value _tableType) {
11
+ caml_binaryen_add_table(value _module, value _table, value _initial, value _maximum, value _tableType, value _init) {
12
12
  CAMLparam5(_module, _table, _initial, _maximum, _tableType);
13
+ CAMLxparam1(_init);
13
14
  BinaryenModuleRef module = BinaryenModuleRef_val(_module);
14
15
  char* table = Safe_String_val(_table);
15
16
  BinaryenIndex initial = Int_val(_initial);
16
17
  BinaryenIndex maximum = Int_val(_maximum);
17
18
  BinaryenType tableType = BinaryenType_val(_tableType);
18
- CAMLreturn(alloc_BinaryenTableRef(BinaryenAddTable(module, table, initial, maximum, tableType)));
19
+ BinaryenExpressionRef init;
20
+ if (Is_none(_init)) {
21
+ init = NULL;
22
+ } else {
23
+ init = BinaryenExpressionRef_val(Some_val(_init));
24
+ }
25
+ CAMLreturn(alloc_BinaryenTableRef(BinaryenAddTable(module, table, initial, maximum, tableType, init)));
26
+ }
27
+ CAMLprim value
28
+ caml_binaryen_add_table__bytecode(value * argv) {
29
+ return caml_binaryen_add_table(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
19
30
  }
20
31
 
21
32
  CAMLprim value
@@ -1,14 +1,26 @@
1
1
  //Provides: caml_binaryen_add_table
2
2
  //Requires: caml_jsstring_of_string
3
- function caml_binaryen_add_table(wasm_mod, table, initial, maximum, tableType) {
3
+ function caml_binaryen_add_table(wasm_mod, table, initial, maximum, tableType, init) {
4
4
  return wasm_mod.addTable(
5
5
  caml_jsstring_of_string(table),
6
6
  initial,
7
7
  maximum,
8
- tableType
8
+ tableType,
9
+ init ? init : null
10
+ );
11
+ }
12
+ //Provides: caml_binaryen_add_table__bytecode
13
+ //Requires: caml_binaryen_add_table
14
+ function caml_binaryen_add_table__bytecode() {
15
+ return caml_binaryen_add_table(
16
+ arguments[0],
17
+ arguments[1],
18
+ arguments[2],
19
+ arguments[3],
20
+ arguments[4],
21
+ arguments[5]
9
22
  );
10
23
  }
11
-
12
24
  //Provides: caml_binaryen_add_active_element_segment
13
25
  //Requires: caml_jsstring_of_string, caml_list_to_js_array
14
26
  function caml_binaryen_add_active_element_segment(
@@ -1,7 +1,8 @@
1
1
  type t
2
2
 
3
- external add_table : Module.t -> string -> int -> int -> Type.t -> t
4
- = "caml_binaryen_add_table"
3
+ external add_table :
4
+ Module.t -> string -> int -> int -> Type.t -> Expression.t option -> t
5
+ = "caml_binaryen_add_table__bytecode" "caml_binaryen_add_table"
5
6
  (** Module, name, initial size, maximum size, table type *)
6
7
 
7
8
  external add_active_element_segment :
@@ -1,6 +1,7 @@
1
1
  type t
2
2
 
3
- val add_table : Module.t -> string -> int -> int -> Type.t -> t
3
+ val add_table :
4
+ Module.t -> string -> int -> int -> Type.t -> Expression.t option -> t
4
5
 
5
6
  val add_active_element_segment :
6
7
  Module.t ->
@@ -169,7 +169,7 @@ let start =
169
169
  ])
170
170
 
171
171
  let _ = Export.add_function_export wasm_mod "adder" "adder"
172
- let _ = Table.add_table wasm_mod "table" 1 1 Type.funcref
172
+ let _ = Table.add_table wasm_mod "table" 1 1 Type.funcref None
173
173
 
174
174
  let adder_type =
175
175
  let builder = Type_builder.make 1 in
@@ -254,13 +254,43 @@ let _ = Memory.set_memory max_memory_wasm_mod 1 2 "memory" [] false false "0"
254
254
  let _ = assert (Memory.has_max max_memory_wasm_mod "0" = true)
255
255
  let _ = assert (Memory.get_max max_memory_wasm_mod "0" = 2)
256
256
 
257
- (* Memory.get_segment_byte_offset Passive *)
258
- let _ = assert (Memory.get_segment_byte_offset wasm_mod "world" = None)
257
+ (* Data_segment.get_num_segments *)
258
+ let _ = assert (Data_segment.get_num_segments wasm_mod = 2)
259
259
 
260
+ (* Data_segment.get_segment *)
261
+ let segment_hello = Data_segment.get_segment wasm_mod "hello"
262
+ let segment_world = Data_segment.get_segment wasm_mod "world"
263
+ let _ = assert (segment_hello <> None)
264
+ let _ = assert (segment_world <> None)
265
+ let _ = assert (Data_segment.get_segment wasm_mod "nonexistent" = None)
266
+ let segment_hello = Option.get segment_hello
267
+ let segment_world = Option.get segment_world
268
+
269
+ (* Data_segment.get_segment_by_index *)
270
+ let _ =
271
+ assert (
272
+ Data_segment.get_segment_name wasm_mod
273
+ (Data_segment.get_segment_by_index wasm_mod 0)
274
+ = "hello")
275
+
276
+ (* Data_segment.get_segment_name *)
277
+ let _ = assert (Data_segment.get_segment_name wasm_mod segment_hello = "hello")
278
+
279
+ (* Data_segment.get_segment_byte_offset *)
280
+ let _ =
281
+ assert (Data_segment.get_segment_byte_offset wasm_mod segment_world = None)
282
+
283
+ (* Data_segment.get_segment_byte_length *)
284
+ let _ = assert (Data_segment.get_segment_byte_length segment_hello = 5)
285
+
286
+ (* Data_segment.get_segment_passive *)
287
+ let _ = assert (Data_segment.get_segment_passive segment_hello = false)
288
+
289
+ (* Data_segment.get_segment_data *)
260
290
  let _ =
261
291
  assert (
262
292
  Bytes.equal
263
- (Memory.get_segment_data wasm_mod "world")
293
+ (Data_segment.get_segment_data wasm_mod segment_world)
264
294
  (Bytes.of_string "world"))
265
295
 
266
296
  (* Call_ref *)
@@ -518,6 +548,9 @@ let _ =
518
548
 
519
549
  let new_mod = Module.read byts
520
550
 
551
+ let new_mod_with_features =
552
+ Module.read_with_features byts [ Module.Feature.all ]
553
+
521
554
  let _ =
522
555
  Module.run_passes new_mod
523
556
  [
@@ -567,8 +600,10 @@ let _ =
567
600
  let _ = assert (Module.validate new_mod == 1)
568
601
  let _ = Module.print new_mod
569
602
  let _ = Module.print_stack_ir new_mod
603
+ let _ = Module.validate new_mod_with_features
570
604
 
571
605
  (* Dispose the modules 👋 *)
572
606
 
573
607
  let _ = Module.dispose wasm_mod
574
608
  let _ = Module.dispose new_mod
609
+ let _ = Module.dispose new_mod_with_features