Skip to content

Commit

Permalink
feat: Allow optional return_type parameter when creating blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Jan 9, 2021
1 parent f4338bd commit ab737bd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions js/expression.ml
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/binaryen_stubs_expressions.c
Expand Up @@ -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);
Expand All @@ -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));
}

Expand Down
4 changes: 3 additions & 1 deletion 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"
Expand Down
8 changes: 5 additions & 3 deletions 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)
)
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion test/test.ml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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

Expand Down

0 comments on commit ab737bd

Please sign in to comment.