diff --git a/js/expression.ml b/js/expression.ml index bc280436..da4f4a8e 100644 --- a/js/expression.ml +++ b/js/expression.ml @@ -3,12 +3,12 @@ open Js_of_ocaml.Js.Unsafe type t = int -let block wasm_mod name children = +let block ?(return_type=Type.auto) wasm_mod name children = meth_call wasm_mod "block" [| inject (string name); inject (array (Array.of_list children)); - inject Type.auto; + inject return_type; |] let if_ wasm_mod cond if_true if_false = diff --git a/package.json b/package.json index fa453386..e716227c 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "scripts": { "test": "esy b dune runtest", - "format": "dune build @fmt --auto-promote" + "format": "esy b dune build @fmt --auto-promote" }, "installConfig": { "pnp": false diff --git a/src/binaryen_stubs_expressions.c b/src/binaryen_stubs_expressions.c index 6ea702aa..453cdc44 100644 --- a/src/binaryen_stubs_expressions.c +++ b/src/binaryen_stubs_expressions.c @@ -22,8 +22,8 @@ caml_binaryen_null_expression(value unit) { } CAMLprim value -caml_binaryen_block(value _module, value _name, value _children) { - CAMLparam3(_module, _name, _children); +caml_binaryen_block(value _module, value _name, value _children, value _ty) { + CAMLparam4(_module, _name, _children, _ty); BinaryenModuleRef module = BinaryenModuleRef_val(_module); char* name = Safe_String_val(_name); _children = array_of_list(_children); @@ -32,7 +32,8 @@ caml_binaryen_block(value _module, value _name, value _children) { for (int i = 0; i < childLen; i++) { children[i] = BinaryenExpressionRef_val(Field(_children, i)); } - BinaryenExpressionRef block = BinaryenBlock(module, name, children, childLen, BinaryenTypeAuto()); + BinaryenType ty = BinaryenType_val(_ty); + BinaryenExpressionRef block = BinaryenBlock(module, name, children, childLen, ty); CAMLreturn(alloc_BinaryenExpressionRef(block)); } diff --git a/src/expression.ml b/src/expression.ml index b5ad7123..cb5ffff9 100644 --- a/src/expression.ml +++ b/src/expression.ml @@ -1,6 +1,8 @@ type t -external block : Module.t -> string -> t list -> t = "caml_binaryen_block" +external block : Module.t -> string -> t list -> Type.t -> t = "caml_binaryen_block" +let block ?(return_type=Type.auto) wasm_mod name exprs = + block wasm_mod name exprs return_type (** Module, block name, expression list. *) external if_ : Module.t -> t -> t -> t -> t = "caml_binaryen_if" diff --git a/test/test.expected b/test/test.expected index 0fafc738..8a1750b5 100644 --- a/test/test.expected +++ b/test/test.expected @@ -1,9 +1,11 @@ (module (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) (func $adder (param $0 i32) (param $1 i32) (result i32) - (i32.add - (local.get $0) - (local.get $1) + (block $add (result i32) + (i32.add + (local.get $0) + (local.get $1) + ) ) ) ) diff --git a/test/test.ml b/test/test.ml index 48ea9ba3..2d05dcec 100644 --- a/test/test.ml +++ b/test/test.ml @@ -12,7 +12,7 @@ let x = Expression.local_get wasm_mod 0 Type.int32 let y = Expression.local_get wasm_mod 1 Type.int32 -let add = Expression.binary wasm_mod Op.add_int32 x y +let add = Expression.block wasm_mod ~return_type:Type.int32 "add" [Expression.binary wasm_mod Op.add_int32 x y] (* Create the add function *) let adder = Function.add_function wasm_mod "adder" params results [||] add diff --git a/virtual/expression.mli b/virtual/expression.mli index 9e9f50fd..72220554 100644 --- a/virtual/expression.mli +++ b/virtual/expression.mli @@ -1,6 +1,6 @@ type t -val block : Module.t -> string -> t list -> t +val block : ?return_type:Type.t -> Module.t -> string -> t list -> t val if_ : Module.t -> t -> t -> t -> t