Add Wasmtime-specific C API functions to return errors (#1467)
* Add Wasmtime-specific C API functions to return errors This commit adds new `wasmtime_*` symbols to the C API, many of which mirror the existing counterparts in the `wasm.h` header. These APIs are enhanced in a number of respects: * Detailed error information is now available through a `wasmtime_error_t`. Currently this only exposes one function which is to extract a string version of the error. * There is a distinction now between traps and errors during instantiation and function calling. Traps only happen if wasm traps, and errors can happen for things like runtime type errors when interacting with the API. * APIs have improved safety with respect to embedders where the lengths of arrays are now taken as explicit parameters rather than assumed from other parameters. * Handle trap updates * Update C examples * Fix memory.c compile on MSVC * Update test assertions * Refactor C slightly * Bare-bones .NET update * Remove bogus nul handling
This commit is contained in:
@@ -24,7 +24,7 @@ to tweak the `-lpthread` and such annotations as well as the name of the
|
||||
#include <wasm.h>
|
||||
#include <wasmtime.h>
|
||||
|
||||
static void print_trap(wasm_trap_t *trap);
|
||||
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
|
||||
|
||||
static wasm_trap_t* hello_callback(const wasm_val_t args[], wasm_val_t results[]) {
|
||||
printf("Calling back...\n");
|
||||
@@ -58,18 +58,19 @@ int main() {
|
||||
fclose(file);
|
||||
|
||||
// Parse the wat into the binary wasm format
|
||||
wasm_byte_vec_t wasm, error;
|
||||
if (wasmtime_wat2wasm(&wat, &wasm, &error) == 0) {
|
||||
fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data);
|
||||
goto free_store;
|
||||
}
|
||||
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);
|
||||
|
||||
// Now that we've got our binary webassembly we can compile our module.
|
||||
printf("Compiling module...\n");
|
||||
wasm_module_t *module = wasm_module_new(store, &wasm);
|
||||
wasm_module_t *module = NULL;
|
||||
error = wasmtime_module_new(store, &wasm, &module);
|
||||
wasm_byte_vec_delete(&wasm);
|
||||
assert(module != NULL);
|
||||
if (error != NULL)
|
||||
exit_with_error("failed to compile module", error, NULL);
|
||||
|
||||
// Next up we need to create the function that the wasm module imports. Here
|
||||
// we'll be hooking up a thunk function to the `hello_callback` native
|
||||
@@ -84,12 +85,11 @@ int main() {
|
||||
// to handle that here too.
|
||||
printf("Instantiating module...\n");
|
||||
wasm_trap_t *trap = NULL;
|
||||
wasm_instance_t *instance = NULL;
|
||||
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
|
||||
wasm_instance_t *instance = wasm_instance_new(store, module, imports, &trap);
|
||||
if (instance == NULL) {
|
||||
print_trap(trap);
|
||||
goto free_module;
|
||||
}
|
||||
error = wasmtime_instance_new(module, imports, 1, &instance, &trap);
|
||||
if (instance == NULL)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
|
||||
// Lookup our `run` export function
|
||||
printf("Extracting export...\n");
|
||||
@@ -101,33 +101,33 @@ int main() {
|
||||
|
||||
// And call it!
|
||||
printf("Calling export...\n");
|
||||
trap = wasm_func_call(run, NULL, NULL);
|
||||
if (trap != NULL) {
|
||||
print_trap(trap);
|
||||
goto free_instance;
|
||||
}
|
||||
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
|
||||
if (error != NULL || trap != NULL)
|
||||
exit_with_error("failed to call function", error, trap);
|
||||
|
||||
// Clean up after ourselves at this point
|
||||
printf("All finished!\n");
|
||||
ret = 0;
|
||||
|
||||
free_instance:
|
||||
wasm_extern_vec_delete(&externs);
|
||||
wasm_instance_delete(instance);
|
||||
free_module:
|
||||
wasm_module_delete(module);
|
||||
free_store:
|
||||
wasm_store_delete(store);
|
||||
wasm_engine_delete(engine);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void print_trap(wasm_trap_t *trap) {
|
||||
assert(trap != NULL);
|
||||
wasm_message_t message;
|
||||
wasm_trap_message(trap, &message);
|
||||
fprintf(stderr, "failed to instantiate module %.*s\n", (int) message.size, message.data);
|
||||
wasm_byte_vec_delete(&message);
|
||||
wasm_trap_delete(trap);
|
||||
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);
|
||||
wasmtime_error_delete(error);
|
||||
} else {
|
||||
wasm_trap_message(trap, &error_message);
|
||||
wasm_trap_delete(trap);
|
||||
}
|
||||
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
|
||||
wasm_byte_vec_delete(&error_message);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user