Skip to content

Commit

Permalink
chore: add some externref usage to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jozanza committed Apr 24, 2021
1 parent ea4f0cc commit 3583b55
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
33 changes: 33 additions & 0 deletions test/test.expected
@@ -1,11 +1,15 @@
(module
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $none_=>_none (func))
(type $externref_=>_i32 (func (param externref) (result i32)))
(type $externref_i32_i32_=>_i32 (func (param externref i32 i32) (result i32)))
(import "future-wasi" "write" (func $write (param externref i32 i32) (result i32)))
(memory $0 1)
(table $table 1 1 funcref)
(elem $elem (i32.const 0) $adder)
(export "adder" (func $adder))
(export "memory" (memory $0))
(export "hello" (func $hello))
(start $start)
(func $adder (param $0 i32) (param $1 i32) (result i32)
(block $add (result i32)
Expand Down Expand Up @@ -34,15 +38,26 @@
)
)
)
(func $hello (param $0 externref) (result i32)
(call $write
(local.get $0)
(i32.const 0)
(i32.const 1)
)
)
)
(module
(type $none_=>_none (func))
(type $externref_=>_i32 (func (param externref) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $externref_i32_i32_=>_i32 (func (param externref i32 i32) (result i32)))
(import "future-wasi" "write" (func $write (param externref i32 i32) (result i32)))
(memory $0 1)
(table $table 1 1 funcref)
(elem $elem (i32.const 0) $adder)
(export "adder" (func $adder))
(export "memory" (memory $0))
(export "hello" (func $hello))
(start $start)
(func $adder (; has Stack IR ;) (param $0 i32) (param $1 i32) (result i32)
(i32.add
Expand All @@ -64,15 +79,26 @@
)
)
)
(func $hello (; has Stack IR ;) (param $0 externref) (result i32)
(call $write
(local.get $0)
(i32.const 0)
(i32.const 1)
)
)
)
(module
(type $none_=>_none (func))
(type $externref_=>_i32 (func (param externref) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $externref_i32_i32_=>_i32 (func (param externref i32 i32) (result i32)))
(import "future-wasi" "write" (func $fimport$0 (param externref 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))
(export "hello" (func $2))
(start $1)
(func $0 (param $0 i32) (param $1 i32) (result i32)
(i32.add
Expand All @@ -94,4 +120,11 @@
)
)
)
(func $2 (param $0 externref) (result i32)
(call $fimport$0
(local.get $0)
(i32.const 0)
(i32.const 1)
)
)
)
32 changes: 31 additions & 1 deletion 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 Down Expand Up @@ -58,12 +58,40 @@ let _ = Function.set_start wasm_mod start

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


(* Create an imported "write" function i32 (externref, i32, i32) *)
(* Similar to the example here: https://bytecodealliance.org/articles/reference-types-in-wasmtime *)

let _ = Import.add_function_import wasm_mod "write" "future-wasi" "write"
(Type.create [| Type.externref; Type.int32; Type.int32 |])
Type.int32

(* Create a function that calls the imported write function *)
let _ = Function.add_function wasm_mod "hello" Type.externref Type.int32 [||]
(Expression.call wasm_mod "write"
[
Expression.local_get wasm_mod 0 Type.externref;
Expression.const wasm_mod (Literal.int32 0l);
Expression.const wasm_mod (Literal.int32 1l);
]
Type.int32)

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

(* Finally, we print 3 versions of the module to be checked against test.expected *)

(* 1. Print the the module as-is *)

let _ = Module.print wasm_mod

(* 2. Optimize, then print the module *)

let _ = Module.optimize wasm_mod

let _ = Module.print wasm_mod

(* 3. Copy previous module bytes into new module, validate, and print *)

let byts, _ = Module.write wasm_mod None

let new_mod = Module.read byts
Expand All @@ -72,6 +100,8 @@ let _ = Module.validate new_mod

let _ = Module.print new_mod

(* Dispose the modules 👋 *)

let _ = Module.dispose wasm_mod

let _ = Module.dispose new_mod

0 comments on commit 3583b55

Please sign in to comment.