Skip to content

Commit

Permalink
fix(js): cast the call_indirect string arguments to JS strings
Browse files Browse the repository at this point in the history
Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
  • Loading branch information
phated and ospencer committed Mar 17, 2021
1 parent 6dc0fa8 commit 7b4136a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions js/expression.ml
Expand Up @@ -40,7 +40,7 @@ let call wasm_mod name params return_typ =
let call_indirect wasm_mod table target params params_typ return_typ =
meth_call wasm_mod "call_indirect"
[|
inject table;
inject (string table);
inject target;
inject (array (Array.of_list params));
inject params_typ;
Expand All @@ -58,7 +58,7 @@ let return_call wasm_mod name params return_typ =
let return_call_indirect wasm_mod table target params params_typ return_typ =
meth_call wasm_mod "return_call_indirect"
[|
inject table;
inject (string table);
inject target;
inject (array (Array.of_list params));
inject params_typ;
Expand Down
35 changes: 35 additions & 0 deletions test/test.expected
@@ -1,10 +1,12 @@
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_none (func))
(memory $0 1)
(table $table 1 1 funcref)
(elem (i32.const 0) $adder)
(export "adder" (func $adder))
(export "memory" (memory $0))
(start $start)
(func $adder (param $0 i32) (param $1 i32) (result i32)
(block $add (result i32)
(if
Expand All @@ -23,12 +25,25 @@
)
)
)
(func $start
(drop
(call_indirect (type $i32_i32_=>_i32)
(i32.const 3)
(i32.const 5)
(i32.const 0)
)
)
)
)
(module
(type $none_=>_none (func))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(memory $0 1)
(table $table 1 1 funcref)
(elem (i32.const 0) $adder)
(export "adder" (func $adder))
(export "memory" (memory $0))
(start $start)
(func $adder (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(select
Expand All @@ -41,12 +56,24 @@
(local.get $1)
)
)
(func $start (; has Stack IR ;)
(drop
(call $adder
(i32.const 3)
(i32.const 5)
)
)
)
)
(module
(type $none_=>_none (func))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(memory $0 1)
(table $0 1 1 funcref)
(elem (i32.const 0) $0)
(export "adder" (func $0))
(export "memory" (memory $0))
(start $1)
(func $0 (param $0 i32) (param $1 i32) (result i32)
(i32.add
(select
Expand All @@ -59,4 +86,12 @@
(local.get $1)
)
)
(func $1
(drop
(call $0
(i32.const 3)
(i32.const 5)
)
)
)
)
32 changes: 26 additions & 6 deletions test/test.ml
Expand Up @@ -3,7 +3,7 @@ open Binaryen
let wasm_mod = Module.create ()

(* Create function type for i32 (i32, i32) *)
let params = Type.create [| Type.int32; Type.int32 |]
let params () = Type.create [| Type.int32; Type.int32 |]

let results = Type.int32

Expand All @@ -20,20 +20,40 @@ let select =

let bin = Expression.binary wasm_mod Op.add_int32 select (y ())

let add = Expression.block wasm_mod ~return_type:Type.int32 "add" [
Expression.if_ wasm_mod (Expression.const wasm_mod (Literal.int32 0l)) (Expression.unreachable wasm_mod) (Expression.null ());
bin
]
let add =
Expression.block wasm_mod ~return_type:Type.int32 "add"
[
Expression.if_ wasm_mod
(Expression.const wasm_mod (Literal.int32 0l))
(Expression.unreachable wasm_mod)
(Expression.null ());
bin;
]

(* Create the add function *)
let adder = Function.add_function wasm_mod "adder" params results [||] add
let adder = Function.add_function wasm_mod "adder" (params ()) results [||] add

let call_adder =
Expression.call_indirect wasm_mod "table"
(Expression.const wasm_mod (Literal.int32 0l))
[
Expression.const wasm_mod (Literal.int32 3l);
Expression.const wasm_mod (Literal.int32 5l);
]
(params ()) Type.int32

let start =
Function.add_function wasm_mod "start" Type.none Type.none [||]
(Expression.drop wasm_mod call_adder)

let _ = Export.add_function_export wasm_mod "adder" "adder"

let _ =
Table.add_table wasm_mod "table" 1 1 [ "adder" ]
(Expression.const wasm_mod (Literal.int32 0l))

let _ = Function.set_start wasm_mod start

let _ = Memory.set_memory wasm_mod 1 Memory.unlimited "memory" [] false

let _ = Module.print wasm_mod
Expand Down

0 comments on commit 7b4136a

Please sign in to comment.