summaryrefslogtreecommitdiff
path: root/wasm2c/examples/callback/callback.wat
diff options
context:
space:
mode:
Diffstat (limited to 'wasm2c/examples/callback/callback.wat')
-rw-r--r--wasm2c/examples/callback/callback.wat19
1 files changed, 19 insertions, 0 deletions
diff --git a/wasm2c/examples/callback/callback.wat b/wasm2c/examples/callback/callback.wat
new file mode 100644
index 00000000..6a8ab233
--- /dev/null
+++ b/wasm2c/examples/callback/callback.wat
@@ -0,0 +1,19 @@
+;; Module demonstrating use of a host-installed callback function.
+
+;; The type of the callback function. The type ID can be looked up outside the module by calling
+;; Z_[modname]_get_func_type(1, 0, WASM_RT_I32) (indicating 1 param, 0 results, param type is i32).
+(type $print_type (func (param i32)))
+
+;; An indirect function table to hold the callback function
+(table $table 1 funcref)
+
+;; A memory holding the string to be printed
+(memory (export "memory") (data "Hello, world.\00"))
+
+;; Allow the host to set the callback function
+(func (export "set_print_function") (param funcref)
+ (table.set $table (i32.const 0) (local.get 0)))
+
+;; Call the callback function with the location of "Hello, world."
+(func (export "say_hello")
+ (call_indirect (type $print_type) (i32.const 0) (i32.const 0)))