Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binaryen.opam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ depends: [
"dune" {>= "3.0.0"}
"dune-configurator" {>= "3.0.0"}
"js_of_ocaml-compiler" {>= "6.3.0" < "7.0.0"}
"libbinaryen" {>= "128.0.0" < "129.0.0"}
"libbinaryen" {>= "129.0.0" < "130.0.0"}
]
x-maintenance-intent: ["0.(latest)"]
12 changes: 6 additions & 6 deletions esy.lock/index.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"resolutions": {
"@opam/ocp-indent": "1.9.0",
"@grain/libbinaryen": "git+https://github.com/grain-lang/libbinaryen.git#0c23f56e2d0b6d2207bc1b7a7e514881bc9c7816"
"@grain/libbinaryen": "git+https://github.com/grain-lang/libbinaryen.git#99ccf44e022248ad26203fb1eb1071e62f921704"
},
"esy": {
"build": "dune build -p binaryen"
Expand Down
81 changes: 81 additions & 0 deletions src/data_segment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#define CAML_NAME_SPACE
#include <caml/mlvalues.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/alloc.h>

#include "binaryen-c.h"
#include "ocaml_helpers.h"


CAMLprim value
caml_binaryen_get_num_memory_segments(value _module) {
CAMLparam1(_module);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
CAMLreturn(Val_int(BinaryenGetNumMemorySegments(module)));
}

CAMLprim value caml_binaryen_get_data_segment(value _module, value _name) {
CAMLparam2(_module, _name);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
BinaryenDataSegmentRef segment = BinaryenGetDataSegment(module, name);
if (segment == NULL) {
CAMLreturn(Val_none);
} else {
CAMLreturn(caml_alloc_some(alloc_BinaryenDataSegmentRef(segment)));
}
}

CAMLprim value caml_binaryen_get_data_segment_by_index(value _module, value _index) {
CAMLparam2(_module, _index);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
int index = Int_val(_index);
BinaryenDataSegmentRef segment = BinaryenGetDataSegmentByIndex(module, index);
CAMLreturn(alloc_BinaryenDataSegmentRef(segment));
}

CAMLprim value caml_binaryen_data_segment_get_name(value _module, value _segment) {
CAMLparam2(_module, _segment);
BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
const char* name = BinaryenDataSegmentGetName(segment);
CAMLreturn(caml_copy_string(name));
}

CAMLprim value
caml_binaryen_get_memory_segment_byte_offset(value _module, value _segment) {
CAMLparam2(_module, _segment);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
if (BinaryenGetMemorySegmentPassive(segment)) {
CAMLreturn(Val_none);
} else {
int offset = BinaryenGetMemorySegmentByteOffset(module, segment);
CAMLreturn(caml_alloc_some(Val_int(offset)));
}
}

CAMLprim value
caml_binaryen_get_memory_segment_byte_length(value _segment) {
CAMLparam1(_segment);
BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
int length = BinaryenGetMemorySegmentByteLength(segment);
CAMLreturn(Val_int(length));
}

CAMLprim value
caml_binaryen_get_memory_segment_passive(value _segment) {
CAMLparam1(_segment);
BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
CAMLreturn(Val_bool(BinaryenGetMemorySegmentPassive(segment)));
}

CAMLprim value
caml_binaryen_get_memory_segment_data(value _module, value _segment) {
CAMLparam2(_module, _segment);
BinaryenDataSegmentRef segment = BinaryenDataSegmentRef_val(_segment);
size_t size = BinaryenGetMemorySegmentByteLength(segment);
CAMLprim value bytes = caml_alloc_string(size);
BinaryenCopyMemorySegmentData(segment, (char*)Bytes_val(bytes));
CAMLreturn(bytes);
}
49 changes: 49 additions & 0 deletions src/data_segment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

//Provides: caml_binaryen_get_num_memory_segments
function caml_binaryen_get_num_memory_segments(wasm_mod) {
return wasm_mod.getNumMemorySegments();
}

//Provides: caml_binaryen_get_data_segment
//Requires: caml_jsstring_of_string, to_option
function caml_binaryen_get_data_segment(wasm_mod, name) {
return to_option(wasm_mod.getDataSegment(caml_jsstring_of_string(name)));
}

//Provides: caml_binaryen_get_data_segment_by_index
function caml_binaryen_get_data_segment_by_index(wasm_mod, index) {
return wasm_mod.getDataSegmentByIndex(index);
}

// Provides: caml_binaryen_data_segment_get_name
// Requires: caml_string_of_jsstring
function caml_binaryen_data_segment_get_name(wasm_mod, segment) {
var info = wasm_mod.getMemorySegmentInfo(segment);
return caml_string_of_jsstring(info.name);
}

//Provides: caml_binaryen_get_memory_segment_byte_offset
//Requires: to_option
function caml_binaryen_get_memory_segment_byte_offset(wasm_mod, segment) {
var info = wasm_mod.getMemorySegmentInfo(segment);
return to_option(info.offset);
}

//Provides: caml_binaryen_get_memory_segment_byte_length
//Requires: Binaryen
function caml_binaryen_get_memory_segment_byte_length(segment) {
return Binaryen._BinaryenGetMemorySegmentByteLength(segment);
}

//Provides: caml_binaryen_get_memory_segment_passive
//Requires: Binaryen
function caml_binaryen_get_memory_segment_passive(segment) {
return Binaryen._BinaryenGetMemorySegmentPassive(segment);
}

//Provides: caml_binaryen_get_memory_segment_data
//Requires: caml_bytes_of_array
function caml_binaryen_get_memory_segment_data(wasm_mod, segment) {
var info = wasm_mod.getMemorySegmentInfo(segment);
return caml_bytes_of_array(info.data);
}
26 changes: 26 additions & 0 deletions src/data_segment.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type t


external get_num_segments : Module.t -> int
= "caml_binaryen_get_num_memory_segments"

external get_segment : Module.t -> string -> t option
= "caml_binaryen_get_data_segment"

external get_segment_by_index: Module.t -> int -> t
= "caml_binaryen_get_data_segment_by_index"

external get_segment_name : Module.t -> t -> string
= "caml_binaryen_data_segment_get_name"

external get_segment_byte_offset : Module.t -> t -> int option
= "caml_binaryen_get_memory_segment_byte_offset"

external get_segment_byte_length : t -> int
= "caml_binaryen_get_memory_segment_byte_length"

external get_segment_passive : t -> bool
= "caml_binaryen_get_memory_segment_passive"

external get_segment_data : Module.t -> t -> bytes
= "caml_binaryen_get_memory_segment_data"
10 changes: 10 additions & 0 deletions src/data_segment.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type t

val get_num_segments : Module.t -> int
val get_segment : Module.t -> string -> t option
val get_segment_by_index : Module.t -> int -> t
val get_segment_name : Module.t -> t -> string
val get_segment_byte_offset : Module.t -> t -> int option
val get_segment_byte_length : t -> int
val get_segment_passive : t -> bool
val get_segment_data : Module.t -> t -> bytes
2 changes: 2 additions & 0 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(language c)
(names
array_type
data_segment
type
op
literal
Expand All @@ -29,6 +30,7 @@
(js_of_ocaml
(javascript_files
array_type.js
data_segment.js
type.js
op.js
literal.js
Expand Down
41 changes: 1 addition & 40 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,43 +107,4 @@ caml_binaryen_memory_is_64(value _module, value _memoryName) {
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* memoryName = Safe_String_val(_memoryName);
CAMLreturn(Val_bool(BinaryenMemoryIs64(module, memoryName)));
}

CAMLprim value
caml_binaryen_get_num_memory_segments(value _module) {
CAMLparam1(_module);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
CAMLreturn(Val_int(BinaryenGetNumMemorySegments(module)));
}

CAMLprim value
caml_binaryen_get_memory_segment_byte_offset(value _module, value _name) {
CAMLparam2(_module, _name);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
if (BinaryenGetMemorySegmentPassive(module, name)) {
CAMLreturn(Val_none);
} else {
int offset = BinaryenGetMemorySegmentByteOffset(module, name);
CAMLreturn(caml_alloc_some(Val_int(offset)));
}
}

CAMLprim value
caml_binaryen_get_memory_segment_passive(value _module, value _name) {
CAMLparam2(_module, _name);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
CAMLreturn(Val_bool(BinaryenGetMemorySegmentPassive(module, name)));
}

CAMLprim value
caml_binaryen_get_memory_segment_data(value _module, value _name) {
CAMLparam2(_module, _name);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
char* name = Safe_String_val(_name);
size_t size = BinaryenGetMemorySegmentByteLength(module, name);
CAMLprim value bytes = caml_alloc_string(size);
BinaryenCopyMemorySegmentData(module, name, (char*)Bytes_val(bytes));
CAMLreturn(bytes);
}
}
28 changes: 1 addition & 27 deletions src/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,4 @@ function caml_binaryen_memory_is_shared(mod, memoryName) {
function caml_binaryen_memory_is_64(mod, memoryName) {
var memory_info = mod.getMemoryInfo(caml_jsstring_of_string(memoryName));
return caml_js_to_bool(memory_info.is64);
}

//Provides: caml_binaryen_get_num_memory_segments
function caml_binaryen_get_num_memory_segments(wasm_mod) {
return wasm_mod.getNumMemorySegments();
}

//Provides: caml_binaryen_get_memory_segment_byte_offset
//Requires: caml_jsstring_of_string, to_option
function caml_binaryen_get_memory_segment_byte_offset(wasm_mod, name) {
var info = wasm_mod.getMemorySegmentInfo(caml_jsstring_of_string(name));
return to_option(info.offset);
}

//Provides: caml_binaryen_get_memory_segment_passive
//Requires: caml_jsstring_of_string
function caml_binaryen_get_memory_segment_passive(wasm_mod, name) {
var info = wasm_mod.getMemorySegmentInfo(caml_jsstring_of_string(name));
return info.passive;
}

//Provides: caml_binaryen_get_memory_segment_data
//Requires: caml_bytes_of_array, caml_jsstring_of_string
function caml_binaryen_get_memory_segment_data(wasm_mod, name) {
var info = wasm_mod.getMemorySegmentInfo(caml_jsstring_of_string(name));
return caml_bytes_of_array(info.data);
}
}
14 changes: 1 addition & 13 deletions src/memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,4 @@ external is_shared : Module.t -> string -> bool

external is_64 : Module.t -> string -> bool = "caml_binaryen_memory_is_64"

let unlimited = -1

external get_num_segments : Module.t -> int
= "caml_binaryen_get_num_memory_segments"

external get_segment_byte_offset : Module.t -> string -> int option
= "caml_binaryen_get_memory_segment_byte_offset"

external get_segment_passive : Module.t -> string -> bool
= "caml_binaryen_get_memory_segment_passive"

external get_segment_data : Module.t -> string -> bytes
= "caml_binaryen_get_memory_segment_data"
let unlimited = -1
4 changes: 0 additions & 4 deletions src/memory.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ val get_max : Module.t -> string -> int
val is_shared : Module.t -> string -> bool
val is_64 : Module.t -> string -> bool
val unlimited : int
val get_num_segments : Module.t -> int
val get_segment_byte_offset : Module.t -> string -> int option
val get_segment_passive : Module.t -> string -> bool
val get_segment_data : Module.t -> string -> bytes
10 changes: 10 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ caml_binaryen_module_read(value _bytes) {
CAMLreturn(alloc_BinaryenModuleRef(result));
}

CAMLprim value
caml_binaryen_module_read_with_features(value _bytes, value _features) {
CAMLparam2(_bytes, _features);
char* bytes = Safe_String_val(_bytes);
int length = caml_string_length(_bytes);
BinaryenFeatures features = Int_val(_features);
BinaryenModuleRef result = BinaryenModuleReadWithFeatures(bytes, length, features);
CAMLreturn(alloc_BinaryenModuleRef(result));
}

CAMLprim value
caml_binaryen_module_interpret(value _module) {
CAMLparam1(_module);
Expand Down
8 changes: 8 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ function caml_binaryen_module_read(bytes) {
return Binaryen.readBinary(data);
}

//Provides: caml_binaryen_module_read_with_features
//Requires: Binaryen
//Requires: caml_uint8_array_of_bytes
function caml_binaryen_module_read_with_features(bytes, features) {
var data = caml_uint8_array_of_bytes(bytes);
return Binaryen.readBinaryWithFeatures(data, features);
}

//Provides: caml_binaryen_module_interpret
function caml_binaryen_module_interpret(wasm_mod) {
return wasm_mod.interpret();
Expand Down
Loading