Implement RFC 11: Redesigning Wasmtime's APIs (#2897)

Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
This commit is contained in:
Alex Crichton
2021-06-03 09:10:53 -05:00
committed by GitHub
parent a5a28b1c5b
commit 7a1b7cdf92
233 changed files with 13349 additions and 11997 deletions

View File

@@ -26,7 +26,13 @@ to tweak the `-lpthread` and such annotations as well as the name of the
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_vec_t* args, wasm_val_vec_t* results) {
static wasm_trap_t* hello_callback(
void *env,
wasmtime_caller_t *caller,
const wasmtime_val_t* args,
size_t nargs,
wasmtime_val_t* results,
size_t nresults) {
printf("Calling back...\n");
printf("> Hello World!\n");
return NULL;
@@ -53,7 +59,7 @@ int serialize(wasm_byte_vec_t* buffer) {
// Parse the wat into the binary wasm format
wasm_byte_vec_t wasm;
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm);
wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
@@ -61,13 +67,13 @@ int serialize(wasm_byte_vec_t* buffer) {
// Now that we've got our binary webassembly we can compile our module
// and serialize into buffer.
printf("Compiling and serializing module...\n");
wasm_module_t *module = NULL;
error = wasmtime_module_new(engine, &wasm, &module);
wasmtime_module_t *module = NULL;
error = wasmtime_module_new(engine, (uint8_t*)wasm.data, wasm.size, &module);
wasm_byte_vec_delete(&wasm);
if (error != NULL)
exit_with_error("failed to compile module", error, NULL);
error = wasmtime_module_serialize(module, buffer);
wasm_module_delete(module);
wasmtime_module_delete(module);
if (error != NULL)
exit_with_error("failed to serialize module", error, NULL);
@@ -87,13 +93,14 @@ int deserialize(wasm_byte_vec_t* buffer) {
// With an engine we can create a *store* which is a long-lived group of wasm
// modules.
wasm_store_t *store = wasm_store_new(engine);
wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
assert(store != NULL);
wasmtime_context_t *context = wasmtime_store_context(store);
// Deserialize compiled module.
printf("Deserialize module...\n");
wasm_module_t *module = NULL;
wasmtime_error_t *error = wasmtime_module_deserialize(engine, buffer, &module);
wasmtime_module_t *module = NULL;
wasmtime_error_t *error = wasmtime_module_deserialize(engine, (uint8_t*) buffer->data, buffer->size, &module);
if (error != NULL)
exit_with_error("failed to compile module", error, NULL);
@@ -102,7 +109,8 @@ int deserialize(wasm_byte_vec_t* buffer) {
// function above.
printf("Creating callback...\n");
wasm_functype_t *hello_ty = wasm_functype_new_0_0();
wasm_func_t *hello = wasm_func_new(store, hello_ty, hello_callback);
wasmtime_func_t hello;
wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
// With our callback function we can now instantiate the compiled module,
// giving us an instance we can then execute exports from. Note that
@@ -110,36 +118,31 @@ int deserialize(wasm_byte_vec_t* buffer) {
// to handle that here too.
printf("Instantiating module...\n");
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
wasm_extern_vec_t imports_vec = WASM_ARRAY_VEC(imports);
error = wasmtime_instance_new(store, module, &imports_vec, &instance, &trap);
if (instance == NULL)
wasmtime_instance_t instance;
wasmtime_extern_t imports[1];
imports[0].kind = WASMTIME_EXTERN_FUNC;
imports[0].of.func = hello;
error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate", error, trap);
wasmtime_module_delete(module);
// Lookup our `run` export function
printf("Extracting export...\n");
wasm_extern_vec_t externs;
wasm_instance_exports(instance, &externs);
assert(externs.size == 1);
wasm_func_t *run = wasm_extern_as_func(externs.data[0]);
assert(run != NULL);
wasmtime_extern_t run;
bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
assert(ok);
assert(run.kind == WASMTIME_EXTERN_FUNC);
// And call it!
printf("Calling export...\n");
wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
error = wasmtime_func_call(run, &args_vec, &results_vec, &trap);
error = wasmtime_func_call(context, &run.of.func, 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");
wasm_extern_vec_delete(&externs);
wasm_instance_delete(instance);
wasm_module_delete(module);
wasm_store_delete(store);
wasmtime_store_delete(store);
wasm_engine_delete(engine);
return 0;
}