diff options
Diffstat (limited to 'wasm2c/examples')
-rw-r--r-- | wasm2c/examples/callback/callback.wat | 2 | ||||
-rw-r--r-- | wasm2c/examples/callback/main.c | 17 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.c | 20 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.h | 12 | ||||
-rw-r--r-- | wasm2c/examples/fac/main.c | 8 | ||||
-rw-r--r-- | wasm2c/examples/rot13/main.c | 48 |
6 files changed, 47 insertions, 60 deletions
diff --git a/wasm2c/examples/callback/callback.wat b/wasm2c/examples/callback/callback.wat index 6a8ab233..9786209a 100644 --- a/wasm2c/examples/callback/callback.wat +++ b/wasm2c/examples/callback/callback.wat @@ -1,7 +1,7 @@ ;; 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). +;; wasm2c_[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 diff --git a/wasm2c/examples/callback/main.c b/wasm2c/examples/callback/main.c index b0b37d46..8c6f9db0 100644 --- a/wasm2c/examples/callback/main.c +++ b/wasm2c/examples/callback/main.c @@ -6,8 +6,8 @@ * 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); +void print(w2c_callback* instance, uint32_t ptr) { + puts(w2c_callback_memory(instance)->data + ptr); } int main(int argc, char** argv) { @@ -15,8 +15,8 @@ int main(int argc, char** argv) { wasm_rt_init(); /* Instantiate the callback module. */ - Z_callback_instance_t inst; - Z_callback_instantiate(&inst); + w2c_callback inst; + wasm2c_callback_instantiate(&inst); /* * Call the module's "set_print_function" function, which takes a funcref to @@ -24,15 +24,16 @@ int main(int argc, char** argv) { * 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_func_type_t fn_type = + wasm2c_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); + w2c_callback_set_print_function(&inst, fn_ref); /* "say_hello" uses the previously installed callback. */ - Z_callbackZ_say_hello(&inst); + w2c_callback_say_hello(&inst); /* Free the module instance and the Wasm runtime state. */ - Z_callback_free(&inst); + wasm2c_callback_free(&inst); wasm_rt_free(); return 0; diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c index c00a60d0..40cb43b2 100644 --- a/wasm2c/examples/fac/fac.c +++ b/wasm2c/examples/fac/fac.c @@ -724,11 +724,11 @@ DEFINE_TABLE_FILL(externref) #define FUNC_TYPE_T(x) static const char x[] #endif -FUNC_TYPE_T(w2c_t0) = "\x07\x80\x96\x7a\x42\xf7\x3e\xe6\x70\x5c\x2f\xac\x83\xf5\x67\xd2\xa2\xa0\x69\x41\x5f\xf8\xe7\x96\x7f\x23\xab\x00\x03\x5f\x4a\x3c"; +FUNC_TYPE_T(w2c_fac_t0) = "\x07\x80\x96\x7a\x42\xf7\x3e\xe6\x70\x5c\x2f\xac\x83\xf5\x67\xd2\xa2\xa0\x69\x41\x5f\xf8\xe7\x96\x7f\x23\xab\x00\x03\x5f\x4a\x3c"; -static u32 w2c_fac(Z_fac_instance_t*, u32); +static u32 w2c_fac_fac_0(w2c_fac*, u32); -static u32 w2c_fac(Z_fac_instance_t* instance, u32 w2c_p0) { +static u32 w2c_fac_fac_0(w2c_fac* instance, u32 w2c_p0) { FUNC_PROLOGUE; u32 w2c_i0, w2c_i1, w2c_i2; w2c_i0 = w2c_p0; @@ -741,7 +741,7 @@ static u32 w2c_fac(Z_fac_instance_t* instance, u32 w2c_p0) { w2c_i1 = w2c_p0; w2c_i2 = 1u; w2c_i1 -= w2c_i2; - w2c_i1 = w2c_fac(instance, w2c_i1); + w2c_i1 = w2c_fac_fac_0(instance, w2c_i1); w2c_i0 *= w2c_i1; } FUNC_EPILOGUE; @@ -749,25 +749,25 @@ static u32 w2c_fac(Z_fac_instance_t* instance, u32 w2c_p0) { } /* export: 'fac' */ -u32 Z_facZ_fac(Z_fac_instance_t* instance, u32 w2c_p0) { - return w2c_fac(instance, w2c_p0); +u32 w2c_fac_fac(w2c_fac* instance, u32 w2c_p0) { + return w2c_fac_fac_0(instance, w2c_p0); } -void Z_fac_instantiate(Z_fac_instance_t* instance) { +void wasm2c_fac_instantiate(w2c_fac* instance) { assert(wasm_rt_is_initialized()); } -void Z_fac_free(Z_fac_instance_t* instance) { +void wasm2c_fac_free(w2c_fac* instance) { } -wasm_rt_func_type_t Z_fac_get_func_type(uint32_t param_count, uint32_t result_count, ...) { +wasm_rt_func_type_t wasm2c_fac_get_func_type(uint32_t param_count, uint32_t result_count, ...) { va_list args; if (param_count == 1 && result_count == 1) { va_start(args, result_count); if (true && va_arg(args, wasm_rt_type_t) == WASM_RT_I32 && va_arg(args, wasm_rt_type_t) == WASM_RT_I32) { va_end(args); - return w2c_t0; + return w2c_fac_t0; } va_end(args); } diff --git a/wasm2c/examples/fac/fac.h b/wasm2c/examples/fac/fac.h index 1354411a..5450fe42 100644 --- a/wasm2c/examples/fac/fac.h +++ b/wasm2c/examples/fac/fac.h @@ -34,16 +34,16 @@ typedef simde_v128_t v128; extern "C" { #endif -typedef struct Z_fac_instance_t { +typedef struct w2c_fac { char dummy_member; -} Z_fac_instance_t; +} w2c_fac; -void Z_fac_instantiate(Z_fac_instance_t*); -void Z_fac_free(Z_fac_instance_t*); -wasm_rt_func_type_t Z_fac_get_func_type(uint32_t param_count, uint32_t result_count, ...); +void wasm2c_fac_instantiate(w2c_fac*); +void wasm2c_fac_free(w2c_fac*); +wasm_rt_func_type_t wasm2c_fac_get_func_type(uint32_t param_count, uint32_t result_count, ...); /* export: 'fac' */ -u32 Z_facZ_fac(Z_fac_instance_t*, u32); +u32 w2c_fac_fac(w2c_fac*, u32); #ifdef __cplusplus } diff --git a/wasm2c/examples/fac/main.c b/wasm2c/examples/fac/main.c index 37593f6e..eb85376e 100644 --- a/wasm2c/examples/fac/main.c +++ b/wasm2c/examples/fac/main.c @@ -18,19 +18,19 @@ int main(int argc, char** argv) { wasm_rt_init(); /* Declare an instance of the `fac` module. */ - Z_fac_instance_t instance; + w2c_fac fac; /* Construct the module instance. */ - Z_fac_instantiate(&instance); + wasm2c_fac_instantiate(&fac); /* Call `fac`, using the mangled name. */ - u32 result = Z_facZ_fac(&instance, x); + u32 result = w2c_fac_fac(&fac, x); /* Print the result. */ printf("fac(%u) -> %u\n", x, result); /* Free the fac module. */ - Z_fac_free(&instance); + wasm2c_fac_free(&fac); /* Free the Wasm runtime state. */ wasm_rt_free(); diff --git a/wasm2c/examples/rot13/main.c b/wasm2c/examples/rot13/main.c index d6517f39..f0164fba 100644 --- a/wasm2c/examples/rot13/main.c +++ b/wasm2c/examples/rot13/main.c @@ -20,49 +20,33 @@ #include "rot13.h" /* Define structure to hold the imports */ -struct Z_host_instance_t { +struct w2c_host { wasm_rt_memory_t memory; char* input; }; /* Accessor to access the memory member of the host */ -wasm_rt_memory_t* Z_hostZ_mem(struct Z_host_instance_t* instance) { +wasm_rt_memory_t* w2c_host_mem(struct w2c_host* instance) { return &instance->memory; } -/* Declare the implementations of the imports. */ -static u32 fill_buf(struct Z_host_instance_t* instance, u32 ptr, u32 size); -static void buf_done(struct Z_host_instance_t* instance, u32 ptr, u32 size); - -/* Define host-provided functions under the names imported by the `rot13` - * instance */ -u32 Z_hostZ_fill_buf(struct Z_host_instance_t* instance, u32 ptr, u32 size) { - return fill_buf(instance, ptr, size); -} - -void Z_hostZ_buf_done(struct Z_host_instance_t* instance, u32 ptr, u32 size) { - return buf_done(instance, ptr, size); -} - int main(int argc, char** argv) { /* Make sure there is at least one command-line argument. */ if (argc < 2) { - printf("Invalid argument. Expected '%s WORD'\n", argv[0]); + printf("Invalid argument. Expected '%s WORD...'\n", argv[0]); return 1; } /* Initialize the Wasm runtime. */ wasm_rt_init(); - /* Declare an instance of the `rot13` module. */ - Z_rot13_instance_t rot13_instance; - - /* Create a `host` module instance to store the memory and current string */ - struct Z_host_instance_t host_instance; - /* Allocate 1 page of wasm memory (64KiB). */ - wasm_rt_allocate_memory(&host_instance.memory, 1, 1, false); + /* Create a structure to store the memory and current string, allocating 1 + page of Wasm memory (64 KiB) that the rot13 module instance will import. */ + struct w2c_host host; + wasm_rt_allocate_memory(&host.memory, 1, 1, false); - /* Construct the module instance */ - Z_rot13_instantiate(&rot13_instance, &host_instance); + // Construct an instance of the `rot13` module, which imports from the host. + w2c_rot13 rot13; + wasm2c_rot13_instantiate(&rot13, &host); /* Call `rot13` on each argument. */ while (argc > 1) { @@ -70,12 +54,12 @@ int main(int argc, char** argv) { argc--; argv++; - host_instance.input = argv[0]; - Z_rot13Z_rot13(&rot13_instance); + host.input = argv[0]; + w2c_rot13_rot13(&rot13); } /* Free the rot13 module. */ - Z_rot13_free(&rot13_instance); + wasm2c_rot13_free(&rot13); /* Free the Wasm runtime state. */ wasm_rt_free(); @@ -86,12 +70,13 @@ int main(int argc, char** argv) { /* Fill the wasm buffer with the input to be rot13'd. * * params: + * instance: An instance of the w2c_host structure * ptr: The wasm memory address of the buffer to fill data. * size: The size of the buffer in wasm memory. * result: * The number of bytes filled into the buffer. (Must be <= size). */ -u32 fill_buf(struct Z_host_instance_t* instance, u32 ptr, u32 size) { +u32 w2c_host_fill_buf(struct w2c_host* instance, u32 ptr, u32 size) { for (size_t i = 0; i < size; ++i) { if (instance->input[i] == 0) { return i; @@ -104,10 +89,11 @@ u32 fill_buf(struct Z_host_instance_t* instance, u32 ptr, u32 size) { /* Called when the wasm buffer has been rot13'd. * * params: + * w2c_host: An instance of the w2c_host structure * ptr: The wasm memory address of the buffer. * size: The size of the buffer in wasm memory. */ -void buf_done(struct Z_host_instance_t* instance, u32 ptr, u32 size) { +void w2c_host_buf_done(struct w2c_host* instance, u32 ptr, u32 size) { /* The output buffer is not necessarily null-terminated, so use the %*.s * printf format to limit the number of characters printed. */ printf("%s -> %.*s\n", instance->input, (int)size, |