Skip to content

Commit

Permalink
chore: Convert bindings to operate on string segment names
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Sep 17, 2023
1 parent 7aa233c commit 05f6cab
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 48 deletions.
36 changes: 18 additions & 18 deletions src/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ caml_binaryen_memory_init(value _module, value _segment, value _dest, value _off
CAMLparam5(_module, _segment, _dest, _offset, _size);
CAMLxparam1(_memoryName);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
uint32_t segment = Int_val(_segment);
char* segment = Safe_String_val(_segment);
BinaryenExpressionRef dest = BinaryenExpressionRef_val(_dest);
BinaryenExpressionRef offset = BinaryenExpressionRef_val(_offset);
BinaryenExpressionRef size = BinaryenExpressionRef_val(_size);
Expand All @@ -373,7 +373,7 @@ CAMLprim value
caml_binaryen_data_drop(value _module, value _segment) {
CAMLparam2(_module, _segment);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
uint32_t segment = Int_val(_segment);
char* segment = Safe_String_val(_segment);
BinaryenExpressionRef exp = BinaryenDataDrop(module, segment);
CAMLreturn(alloc_BinaryenExpressionRef(exp));
}
Expand Down Expand Up @@ -1596,14 +1596,14 @@ CAMLprim value
caml_binaryen_memory_init_get_segment(value _exp) {
CAMLparam1(_exp);
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
CAMLreturn(Val_int(BinaryenMemoryInitGetSegment(exp)));
CAMLreturn(caml_copy_string(BinaryenMemoryInitGetSegment(exp)));
}

CAMLprim value
caml_binaryen_memory_init_set_segment(value _exp, value _value) {
CAMLparam2(_exp, _value);
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
uint32_t val = Int_val(_value);
char* val = Safe_String_val(_value);
BinaryenMemoryInitSetSegment(exp, val);
CAMLreturn(Val_unit);
}
Expand Down Expand Up @@ -1648,27 +1648,27 @@ caml_binaryen_memory_init_get_size(value _exp) {
}

CAMLprim value
caml_binaryen_memory_init_set_size(value _exp, value _value) {
CAMLparam2(_exp, _value);
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
BinaryenExpressionRef val = BinaryenExpressionRef_val(_value);
BinaryenMemoryInitSetSize(exp, val);
caml_binaryen_memory_init_set_size(value _expr, value _segment) {
CAMLparam2(_expr, _segment);
BinaryenExpressionRef expr = BinaryenExpressionRef_val(_expr);
BinaryenExpressionRef segment = BinaryenExpressionRef_val(_segment);
BinaryenMemoryInitSetSize(expr, segment);
CAMLreturn(Val_unit);
}

CAMLprim value
caml_binaryen_data_drop_get_segment(value _exp) {
CAMLparam1(_exp);
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
CAMLreturn(Val_int(BinaryenDataDropGetSegment(exp)));
caml_binaryen_data_drop_get_segment(value _expr) {
CAMLparam1(_expr);
BinaryenExpressionRef expr = BinaryenExpressionRef_val(_expr);
CAMLreturn(caml_copy_string(BinaryenDataDropGetSegment(expr)));
}

CAMLprim value
caml_binaryen_data_drop_set_segment(value _exp, value _value) {
CAMLparam2(_exp, _value);
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
uint32_t val = Int_val(_value);
BinaryenDataDropSetSegment(exp, val);
caml_binaryen_data_drop_set_segment(value _expr, value _segment) {
CAMLparam2(_expr, _segment);
BinaryenExpressionRef expr = BinaryenExpressionRef_val(_expr);
char* segment = Safe_String_val(_segment);
BinaryenDataDropSetSegment(expr, segment);
CAMLreturn(Val_unit);
}

Expand Down
25 changes: 15 additions & 10 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ function caml_binaryen_memory_init(
memoryName
) {
return wasm_mod.memory.init(
segment,
caml_jsstring_of_string(segment),
dest,
offset,
size,
Expand All @@ -478,8 +478,9 @@ function caml_binaryen_memory_init__bytecode() {
}

//Provides: caml_binaryen_data_drop
//Requires: caml_jsstring_of_string
function caml_binaryen_data_drop(wasm_mod, segment) {
return wasm_mod.data.drop(segment);
return wasm_mod.data.drop(caml_jsstring_of_string(segment));
}

//Provides: caml_binaryen_memory_copy
Expand Down Expand Up @@ -1493,14 +1494,16 @@ function caml_binaryen_return_set_value(exp, value) {

//Provides: caml_binaryen_memory_init_get_segment
//Requires: Binaryen
function caml_binaryen_memory_init_get_segment(exp) {
return Binaryen.MemoryInit.getSegment(exp);
//Requires: caml_string_of_jsstring
function caml_binaryen_memory_init_get_segment(expr) {
return caml_string_of_jsstring(Binaryen.MemoryInit.getSegment(expr));
}

//Provides: caml_binaryen_memory_init_set_segment
//Requires: Binaryen
function caml_binaryen_memory_init_set_segment(exp, value) {
return Binaryen.MemoryInit.setSegment(exp, value);
//Requires: caml_jsstring_of_string
function caml_binaryen_memory_init_set_segment(expr, segment) {
return Binaryen.MemoryInit.setSegment(expr, caml_jsstring_of_string(segment));
}

//Provides: caml_binaryen_memory_init_get_dest
Expand Down Expand Up @@ -1541,14 +1544,16 @@ function caml_binaryen_memory_init_set_size(exp, value) {

//Provides: caml_binaryen_data_drop_get_segment
//Requires: Binaryen
function caml_binaryen_data_drop_get_segment(exp) {
return Binaryen.DataDrop.getSegment(exp);
//Requires: caml_string_of_jsstring
function caml_binaryen_data_drop_get_segment(expr) {
return caml_string_of_jsstring(Binaryen.DataDrop.getSegment(expr));
}

//Provides: caml_binaryen_data_drop_set_segment
//Requires: Binaryen
function caml_binaryen_data_drop_set_segment(exp, value) {
return Binaryen.DataDrop.setSegment(exp, value);
//Requires: caml_jsstring_of_string
function caml_binaryen_data_drop_set_segment(expr, segment) {
return Binaryen.DataDrop.setSegment(expr, caml_jsstring_of_string(segment));
}

//Provides: caml_binaryen_memory_copy_get_dest
Expand Down
31 changes: 25 additions & 6 deletions src/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,25 @@ module Memory_grow = struct
end

module Memory_init = struct
external make : Module.t -> int -> t -> t -> t -> string -> t
external make : Module.t -> string -> t -> t -> t -> string -> t
= "caml_binaryen_memory_init__bytecode" "caml_binaryen_memory_init"
(** Module, segment, destination, offset, size, memory_name *)

external get_segment : t -> int = "caml_binaryen_memory_init_get_segment"
(* Binaryen v113 still uses indexes for data segements, so we only want to change the binding itself, not our interface *)
let make wasm_mod segment dest offset size memory_name =
make wasm_mod (string_of_int segment) dest offset size memory_name

external set_segment : t -> int -> unit
external get_segment : t -> string = "caml_binaryen_memory_init_get_segment"

(* Binaryen v113 still uses indexes for data segements, so we only want to change the binding itself, not our interface *)
let get_segment expr = int_of_string (get_segment expr)

external set_segment : t -> string -> unit
= "caml_binaryen_memory_init_set_segment"

(* Binaryen v113 still uses indexes for data segements, so we only want to change the binding itself, not our interface *)
let set_segment expr segment = set_segment expr (string_of_int segment)

external get_dest : t -> t = "caml_binaryen_memory_init_get_dest"
external set_dest : t -> t -> unit = "caml_binaryen_memory_init_set_dest"
external get_offset : t -> t = "caml_binaryen_memory_init_get_offset"
Expand All @@ -721,13 +731,22 @@ module Memory_init = struct
end

module Data_drop = struct
external make : Module.t -> int -> t = "caml_binaryen_data_drop"
external make : Module.t -> string -> t = "caml_binaryen_data_drop"
(** Module, segment. *)

external get_segment : t -> int = "caml_binaryen_data_drop_get_segment"
(* Binaryen v113 still uses indexes for data segements, so we only want to change the binding itself, not our interface *)
let make wasm_mod segment = make wasm_mod (string_of_int segment)

external set_segment : t -> int -> unit
external get_segment : t -> string = "caml_binaryen_data_drop_get_segment"

(* Binaryen v113 still uses indexes for data segements, so we only want to change the binding itself, not our interface *)
let get_segment expr = int_of_string (get_segment expr)

external set_segment : t -> string -> unit
= "caml_binaryen_data_drop_set_segment"

(* Binaryen v113 still uses indexes for data segements, so we only want to change the binding itself, not our interface *)
let set_segment expr segment = set_segment expr (string_of_int segment)
end

module Memory_copy = struct
Expand Down
28 changes: 14 additions & 14 deletions test/test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
(global $max_int64_mut (mut i64) (i64.const 9223372036854775807))
(global $test_float64_bits f64 (f64.const 1.23))
(memory $0 1)
(data (i32.const 0) "hello")
(data "world")
(data $0 (i32.const 0) "hello")
(data $1 "world")
(table $table 1 1 funcref)
(elem $elem (i32.const 0) $adder)
(export "adder" (func $adder))
Expand All @@ -44,7 +44,7 @@
)
(func $start (type $none_=>_none)
(block $start
(memory.init 1
(memory.init $1
(i32.const 2048)
(i32.const 0)
(i32.const 5)
Expand Down Expand Up @@ -73,8 +73,8 @@
(type $anyref_=>_i32 (func (param anyref) (result i32)))
(import "future-wasi" "write" (func $write (param anyref i32 i32) (result i32)))
(memory $0 1)
(data (i32.const 0) "hello")
(data "world")
(data $0 (i32.const 0) "hello")
(data $1 "world")
(table $table 1 1 funcref)
(elem $elem (i32.const 0) $adder)
(export "adder" (func $adder))
Expand All @@ -94,7 +94,7 @@
)
)
(func $start (type $none_=>_none) (; has Stack IR ;)
(memory.init 1
(memory.init $1
(i32.const 2048)
(i32.const 0)
(i32.const 5)
Expand All @@ -121,10 +121,10 @@
(type $type$3 (func (param anyref) (result i32)))
(import "future-wasi" "write" (func $fimport$0 (param anyref i32 i32) (result i32)))
(memory $0 1)
(data (i32.const 0) "hello")
(data "world")
(data $0 (i32.const 0) "hello")
(data $1 "world")
(table $0 1 1 funcref)
(elem (i32.const 0) $0)
(elem $0 (i32.const 0) $0)
(export "adder" (func $0))
(export "memory" (memory $0))
(export "hello" (func $2))
Expand All @@ -142,7 +142,7 @@
)
)
(func $1 (type $type$2)
(memory.init 1
(memory.init $1
(i32.const 2048)
(i32.const 0)
(i32.const 5)
Expand All @@ -169,10 +169,10 @@
(type $type$3 (func (param anyref) (result i32)))
(import "future-wasi" "write" (func $fimport$0 (param anyref i32 i32) (result i32)))
(memory $0 1)
(data (i32.const 0) "hello")
(data "world")
(data $0 (i32.const 0) "hello")
(data $1 "world")
(table $0 1 1 funcref)
(elem (i32.const 0) $0)
(elem $0 (i32.const 0) $0)
(export "adder" (func $0))
(export "memory" (memory $0))
(export "hello" (func $2))
Expand All @@ -190,7 +190,7 @@
i32.const 2048
i32.const 0
i32.const 5
memory.init $0 1
memory.init $0 $1
i32.const 3
i32.const 5
call $0
Expand Down

0 comments on commit 05f6cab

Please sign in to comment.