summaryrefslogtreecommitdiff
path: root/wasm2c/examples/callback/main.c
diff options
context:
space:
mode:
authorKeith Winstein <keithw@cs.stanford.edu>2023-01-25 10:28:11 -0800
committerGitHub <noreply@github.com>2023-01-25 10:28:11 -0800
commit777e651edbe2a1931bd0d9746da5eaae8cc61ca9 (patch)
tree21ec1ac065b1ef7a6ef98ce36f3f26bec40b7813 /wasm2c/examples/callback/main.c
parent165d5d62485bf09ddac404ab908a7c0ebf6b9851 (diff)
downloadwabt-777e651edbe2a1931bd0d9746da5eaae8cc61ca9.tar.gz
wabt-777e651edbe2a1931bd0d9746da5eaae8cc61ca9.tar.bz2
wabt-777e651edbe2a1931bd0d9746da5eaae8cc61ca9.zip
wasm2c: serialize types at wasm2c-time (#2120)
This makes wasm2c serialize each function type, rather than registering function types at module-initialization time. The serialized function type is the SHA-256 of the mangled param and result types (with a space between params and results). At runtime in call_indirect, a known (immediate) function type is compared against the function type stored in a funcref structure. For call_indirects to functions local to the module, or for any call_indirect when the toolchain merges string constants across compilation units (generally, GCC and clang), this can be done by comparing the pointers to each function type. Otherwise, the actual 32-byte values are compared. The function type IDs can be looked up at runtime with `Z_[modname]_get_func_type`, which matches the API from `wasm_rt_register_func_type`. A new `callback` example demos this. wasm2c does the SHA-256 either by linking against libcrypto or, if not available or if requested via `cmake -DUSE_INTERNAL_SHA256=ON`, by using a vendored (header-only) PicoSHA2. There is no runtime dependency on SHA-256 in the wasm2c runtime or generated modules. This eliminates the last of the per-module state, so this commit also removes the [modname]_init_module() function and the s_module_initialized bool.
Diffstat (limited to 'wasm2c/examples/callback/main.c')
-rw-r--r--wasm2c/examples/callback/main.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/wasm2c/examples/callback/main.c b/wasm2c/examples/callback/main.c
new file mode 100644
index 00000000..b0b37d46
--- /dev/null
+++ b/wasm2c/examples/callback/main.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+
+#include "callback.h"
+
+/*
+ * The callback function. Prints the null-terminated string at the given
+ * location in the instance's exported memory.
+ */
+void print(Z_callback_instance_t* instance, uint32_t ptr) {
+ puts(Z_callbackZ_memory(instance)->data + ptr);
+}
+
+int main(int argc, char** argv) {
+ /* Initialize the Wasm runtime. */
+ wasm_rt_init();
+
+ /* Instantiate the callback module. */
+ Z_callback_instance_t inst;
+ Z_callback_instantiate(&inst);
+
+ /*
+ * Call the module's "set_print_function" function, which takes a funcref to
+ * the callback. A funcref has three members: the function type (which can be
+ * looked up with "Z_callback_get_func_type"), a pointer to the function, and
+ * a module instance pointer that will be passed to the function when called.
+ */
+ wasm_rt_func_type_t fn_type = Z_callback_get_func_type(1, 0, WASM_RT_I32);
+ wasm_rt_funcref_t fn_ref = {fn_type, (wasm_rt_function_ptr_t)print, &inst};
+ Z_callbackZ_set_print_function(&inst, fn_ref);
+
+ /* "say_hello" uses the previously installed callback. */
+ Z_callbackZ_say_hello(&inst);
+
+ /* Free the module instance and the Wasm runtime state. */
+ Z_callback_free(&inst);
+ wasm_rt_free();
+
+ return 0;
+}