diff --git a/js/expression.ml b/js/expression.ml index a91c3b17..93e4be4e 100644 --- a/js/expression.ml +++ b/js/expression.ml @@ -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; @@ -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; diff --git a/test/test.expected b/test/test.expected index 91861847..06ab911e 100644 --- a/test/test.expected +++ b/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 @@ -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 @@ -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 @@ -59,4 +86,12 @@ (local.get $1) ) ) + (func $1 + (drop + (call $0 + (i32.const 3) + (i32.const 5) + ) + ) + ) ) diff --git a/test/test.ml b/test/test.ml index 557debe1..d5854274 100644 --- a/test/test.ml +++ b/test/test.ml @@ -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 @@ -20,13 +20,31 @@ 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" @@ -34,6 +52,8 @@ 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