* Update WebAssembly C API submodule to latest commit. This commit updates the WebAssembly C API submodule (for `wasm.h`) to the latest commit out of master. This fixes the behavior of `wasm_name_new_from_string` such that it no longer copies the null character into the name, which caused unexpected failures when using the Wasmtime linker as imports wouldn't resolve when the null was present. Along with this change were breaking changes to `wasm_func_call`, the host callback signatures, and `wasm_instance_new` to take a vector type instead of a pointer to an unsized array. As a result, Wasmtime language bindings based on the C API will need to be updated once this change is pulled in. Fixes #2211. Fixes #2131. * Update Doxygen comments for wasm.h changes.
117 lines
3.4 KiB
C
117 lines
3.4 KiB
C
/*
|
|
Example of instantiating of the WebAssembly module and invoking its exported
|
|
function.
|
|
|
|
You can compile and run this example on Linux with:
|
|
|
|
cargo build --release -p wasmtime-c-api
|
|
cc examples/gcd.c \
|
|
-I crates/c-api/include \
|
|
-I crates/c-api/wasm-c-api/include \
|
|
target/release/libwasmtime.a \
|
|
-lpthread -ldl -lm \
|
|
-o gcd
|
|
./gcd
|
|
|
|
Note that on Windows and macOS the command will be similar, but you'll need
|
|
to tweak the `-lpthread` and such annotations.
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <wasm.h>
|
|
#include <wasmtime.h>
|
|
|
|
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
|
|
|
|
int main() {
|
|
int ret = 0;
|
|
// Set up our context
|
|
wasm_engine_t *engine = wasm_engine_new();
|
|
assert(engine != NULL);
|
|
wasm_store_t *store = wasm_store_new(engine);
|
|
assert(store != NULL);
|
|
|
|
// Load our input file to parse it next
|
|
FILE* file = fopen("examples/gcd.wat", "r");
|
|
if (!file) {
|
|
printf("> Error loading file!\n");
|
|
return 1;
|
|
}
|
|
fseek(file, 0L, SEEK_END);
|
|
size_t file_size = ftell(file);
|
|
fseek(file, 0L, SEEK_SET);
|
|
wasm_byte_vec_t wat;
|
|
wasm_byte_vec_new_uninitialized(&wat, file_size);
|
|
if (fread(wat.data, file_size, 1, file) != 1) {
|
|
printf("> Error loading module!\n");
|
|
return 1;
|
|
}
|
|
fclose(file);
|
|
|
|
// Parse the wat into the binary wasm format
|
|
wasm_byte_vec_t wasm;
|
|
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm);
|
|
if (error != NULL)
|
|
exit_with_error("failed to parse wat", error, NULL);
|
|
wasm_byte_vec_delete(&wat);
|
|
|
|
// Compile and instantiate our module
|
|
wasm_module_t *module = NULL;
|
|
error = wasmtime_module_new(engine, &wasm, &module);
|
|
if (module == NULL)
|
|
exit_with_error("failed to compile module", error, NULL);
|
|
wasm_byte_vec_delete(&wasm);
|
|
wasm_trap_t *trap = NULL;
|
|
wasm_instance_t *instance = NULL;
|
|
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
|
|
error = wasmtime_instance_new(store, module, &imports, &instance, &trap);
|
|
if (instance == NULL)
|
|
exit_with_error("failed to instantiate", error, trap);
|
|
|
|
// Lookup our `gcd` export function
|
|
wasm_extern_vec_t externs;
|
|
wasm_instance_exports(instance, &externs);
|
|
assert(externs.size == 1);
|
|
wasm_func_t *gcd = wasm_extern_as_func(externs.data[0]);
|
|
assert(gcd != NULL);
|
|
|
|
// And call it!
|
|
int a = 6;
|
|
int b = 27;
|
|
wasm_val_t params[2] = { WASM_I32_VAL(a), WASM_I32_VAL(b) };
|
|
wasm_val_t results[1];
|
|
wasm_val_vec_t params_vec = WASM_ARRAY_VEC(params);
|
|
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
|
|
error = wasmtime_func_call(gcd, ¶ms_vec, &results_vec, &trap);
|
|
if (error != NULL || trap != NULL)
|
|
exit_with_error("failed to call gcd", error, trap);
|
|
assert(results[0].kind == WASM_I32);
|
|
|
|
printf("gcd(%d, %d) = %d\n", a, b, results[0].of.i32);
|
|
|
|
// Clean up after ourselves at this point
|
|
ret = 0;
|
|
|
|
wasm_extern_vec_delete(&externs);
|
|
wasm_instance_delete(instance);
|
|
wasm_module_delete(module);
|
|
wasm_store_delete(store);
|
|
wasm_engine_delete(engine);
|
|
return ret;
|
|
}
|
|
|
|
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
|
|
fprintf(stderr, "error: %s\n", message);
|
|
wasm_byte_vec_t error_message;
|
|
if (error != NULL) {
|
|
wasmtime_error_message(error, &error_message);
|
|
} else {
|
|
wasm_trap_message(trap, &error_message);
|
|
}
|
|
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
|
|
wasm_byte_vec_delete(&error_message);
|
|
exit(1);
|
|
}
|