Update WebAssembly C API submodule to latest commit. (#2579)

* 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.
This commit is contained in:
Peter Huene
2021-01-14 07:36:12 -08:00
committed by GitHub
parent cde07b9a79
commit f94db6556c
19 changed files with 207 additions and 144 deletions

View File

@@ -75,7 +75,8 @@ int main() {
printf("Instantiating module...\n");
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
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);
@@ -139,10 +140,11 @@ int main() {
assert(func != NULL);
// And call it!
wasm_val_t args[1];
wasm_val_copy(&args[0], &externref);
wasm_val_t args[1] = { externref };
wasm_val_t results[1];
error = wasmtime_func_call(func, args, 1, results, 1, &trap);
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
error = wasmtime_func_call(func, &args_vec, &results_vec, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
@@ -161,7 +163,6 @@ int main() {
ret = 0;
wasm_val_delete(&results[0]);
wasm_val_delete(&args[0]);
wasm_val_delete(&global_val);
wasm_val_delete(&elem);
wasm_extern_vec_delete(&externs);

View File

@@ -71,7 +71,8 @@ int main(int argc, const char* argv[]) {
printf("Instantiating module...\n");
wasm_instance_t* instance = NULL;
wasm_trap_t *trap = NULL;
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
error = wasmtime_instance_new(store, module, &imports, &instance, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate", error, trap);
wasm_module_delete(module);
@@ -95,9 +96,11 @@ int main(int argc, const char* argv[]) {
// Call.
printf("Calling fib...\n");
wasm_val_t params[1] = { {.kind = WASM_I32, .of = {.i32 = 6}} };
wasm_val_t params[1] = { WASM_I32_VAL(6) };
wasm_val_t results[1];
error = wasmtime_func_call(run_func, params, 1, results, 1, &trap);
wasm_val_vec_t params_vec = WASM_ARRAY_VEC(params);
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
error = wasmtime_func_call(run_func, &params_vec, &results_vec, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);

View File

@@ -65,7 +65,8 @@ int main() {
wasm_byte_vec_delete(&wasm);
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
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);
@@ -79,13 +80,11 @@ int main() {
// And call it!
int a = 6;
int b = 27;
wasm_val_t params[2];
wasm_val_t params[2] = { WASM_I32_VAL(a), WASM_I32_VAL(b) };
wasm_val_t results[1];
params[0].kind = WASM_I32;
params[0].of.i32 = a;
params[1].kind = WASM_I32;
params[1].of.i32 = b;
error = wasmtime_func_call(gcd, params, 2, results, 1, &trap);
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, &params_vec, &results_vec, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call gcd", error, trap);
assert(results[0].kind == WASM_I32);

View File

@@ -26,7 +26,7 @@ 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_t args[], wasm_val_t results[]) {
static wasm_trap_t* hello_callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
printf("Calling back...\n");
printf("> Hello World!\n");
return NULL;
@@ -86,8 +86,9 @@ int main() {
printf("Instantiating module...\n");
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
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)
exit_with_error("failed to instantiate", error, trap);
@@ -101,7 +102,9 @@ int main() {
// And call it!
printf("Calling export...\n");
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
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);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);

View File

@@ -26,7 +26,7 @@ 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_t args[], wasm_val_t results[]) {
static wasm_trap_t* hello_callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
printf("Calling back...\n");
printf("> Hello World!\n");
return NULL;
@@ -86,8 +86,9 @@ int main() {
printf("Instantiating module...\n");
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
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)
exit_with_error("failed to instantiate", error, trap);
@@ -101,7 +102,9 @@ int main() {
// And call it!
printf("Calling export...\n");
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
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);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);

View File

@@ -42,6 +42,7 @@ static void* helper(void *_handle) {
printf("Sending an interrupt\n");
wasmtime_interrupt_handle_interrupt(handle);
wasmtime_interrupt_handle_delete(handle);
return 0;
}
static void spawn_interrupt(wasmtime_interrupt_handle_t *handle) {
@@ -89,11 +90,12 @@ int main() {
wasm_module_t *module = NULL;
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
error = wasmtime_module_new(engine, &wasm, &module);
wasm_byte_vec_delete(&wasm);
if (error != NULL)
exit_with_error("failed to compile module", error, NULL);
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
error = wasmtime_instance_new(store, module, &imports, &instance, &trap);
if (instance == NULL)
exit_with_error("failed to instantiate", error, trap);
@@ -109,7 +111,9 @@ int main() {
// And call it!
printf("Entering infinite loop...\n");
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
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);
assert(error == NULL);
assert(trap != NULL);
printf("Got a trap!...\n");

View File

@@ -100,7 +100,9 @@ int main() {
assert(linking1_externs.size == 1);
wasm_func_t *run = wasm_extern_as_func(linking1_externs.data[0]);
assert(run != NULL);
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
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);
if (error != NULL || trap != NULL)
exit_with_error("failed to call run", error, trap);

View File

@@ -54,10 +54,11 @@ void check(bool success) {
}
}
void check_call(wasm_func_t* func, wasm_val_t args[], size_t num_args, int32_t expected) {
void check_call(wasm_func_t* func, const wasm_val_vec_t* args_vec, int32_t expected) {
wasm_val_t results[1];
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
wasm_trap_t *trap = NULL;
wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, results, 1, &trap);
wasmtime_error_t *error = wasmtime_func_call(func, args_vec, &results_vec, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
if (results[0].of.i32 != expected) {
@@ -67,42 +68,44 @@ void check_call(wasm_func_t* func, wasm_val_t args[], size_t num_args, int32_t e
}
void check_call0(wasm_func_t* func, int32_t expected) {
check_call(func, NULL, 0, expected);
wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
check_call(func, &args_vec, expected);
}
void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) {
wasm_val_t args[] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
check_call(func, args, 1, expected);
wasm_val_t args[] = { WASM_I32_VAL(arg) };
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
check_call(func, &args_vec, expected);
}
void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
wasm_val_t args[2] = {
{.kind = WASM_I32, .of = {.i32 = arg1}},
{.kind = WASM_I32, .of = {.i32 = arg2}}
};
check_call(func, args, 2, expected);
wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
check_call(func, &args_vec, expected);
}
void check_ok(wasm_func_t* func, wasm_val_t args[], size_t num_args) {
void check_ok(wasm_func_t* func, const wasm_val_vec_t* args_vec) {
wasm_trap_t *trap = NULL;
wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, NULL, 0, &trap);
wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
wasmtime_error_t *error = wasmtime_func_call(func, args_vec, &results_vec, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
}
void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
wasm_val_t args[2] = {
{.kind = WASM_I32, .of = {.i32 = arg1}},
{.kind = WASM_I32, .of = {.i32 = arg2}}
};
check_ok(func, args, 2);
wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
check_ok(func, &args_vec);
}
void check_trap(wasm_func_t* func, wasm_val_t args[], size_t num_args, size_t num_results) {
wasm_val_t results[1];
void check_trap(wasm_func_t* func, const wasm_val_vec_t* args_vec, size_t num_results) {
assert(num_results <= 1);
wasm_val_t results[1];
wasm_val_vec_t results_vec;
results_vec.data = results;
results_vec.size = num_results;
wasm_trap_t *trap = NULL;
wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, results, num_results, &trap);
wasmtime_error_t *error = wasmtime_func_call(func, args_vec, &results_vec, &trap);
if (error != NULL)
exit_with_error("failed to call function", error, NULL);
if (trap == NULL) {
@@ -113,16 +116,15 @@ void check_trap(wasm_func_t* func, wasm_val_t args[], size_t num_args, size_t nu
}
void check_trap1(wasm_func_t* func, int32_t arg) {
wasm_val_t args[1] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
check_trap(func, args, 1, 1);
wasm_val_t args[] = { WASM_I32_VAL(arg) };
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
check_trap(func, &args_vec, 1);
}
void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
wasm_val_t args[2] = {
{.kind = WASM_I32, .of = {.i32 = arg1}},
{.kind = WASM_I32, .of = {.i32 = arg2}}
};
check_trap(func, args, 2, 0);
wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
check_trap(func, &args_vec, 0);
}
int main(int argc, const char* argv[]) {
@@ -167,7 +169,8 @@ int main(int argc, const char* argv[]) {
printf("Instantiating module...\n");
wasm_instance_t* instance = NULL;
wasm_trap_t *trap = NULL;
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
wasm_extern_vec_t imports = WASM_EMPTY_VEC;
error = wasmtime_instance_new(store, module, &imports, &instance, &trap);
if (!instance)
exit_with_error("failed to instantiate", error, trap);

View File

@@ -32,14 +32,14 @@ static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_t
// A function to be called from Wasm code.
wasm_trap_t* callback(
const wasm_val_t args[], wasm_val_t results[]
const wasm_val_vec_t* args, wasm_val_vec_t* results
) {
printf("Calling back...\n");
printf("> %"PRIu32" %"PRIu64"\n", args[0].of.i32, args[1].of.i64);
printf("> %"PRIu32" %"PRIu64"\n", args->data[0].of.i32, args->data[1].of.i64);
printf("\n");
wasm_val_copy(&results[0], &args[1]);
wasm_val_copy(&results[1], &args[0]);
wasm_val_copy(&results->data[0], &args->data[1]);
wasm_val_copy(&results->data[1], &args->data[0]);
return NULL;
}
@@ -112,10 +112,11 @@ int main(int argc, const char* argv[]) {
// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {wasm_func_as_extern(callback_func)};
wasm_extern_t* imports[] = {wasm_func_as_extern(callback_func)};
wasm_extern_vec_t imports_vec = WASM_ARRAY_VEC(imports);
wasm_instance_t* instance = NULL;
wasm_trap_t* trap = NULL;
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
error = wasmtime_instance_new(store, module, &imports_vec, &instance, &trap);
if (!instance)
exit_with_error("failed to instantiate", error, trap);
@@ -140,13 +141,11 @@ int main(int argc, const char* argv[]) {
// Call.
printf("Calling export...\n");
wasm_val_t args[2];
args[0].kind = WASM_I32;
args[0].of.i32 = 1;
args[1].kind = WASM_I64;
args[1].of.i64 = 2;
wasm_val_t args[2] = { WASM_I32_VAL(1), WASM_I64_VAL(2) };
wasm_val_t results[2];
error = wasmtime_func_call(run_func, args, 2, results, 2, &trap);
wasm_val_vec_t args_vec = WASM_ARRAY_VEC(args);
wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
error = wasmtime_func_call(run_func, &args_vec, &results_vec, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call run", error, trap);

View File

@@ -26,7 +26,7 @@ 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_t args[], wasm_val_t results[]) {
static wasm_trap_t* hello_callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
printf("Calling back...\n");
printf("> Hello World!\n");
return NULL;
@@ -111,8 +111,9 @@ int deserialize(wasm_byte_vec_t* buffer) {
printf("Instantiating module...\n");
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
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)
exit_with_error("failed to instantiate", error, trap);
@@ -126,7 +127,9 @@ int deserialize(wasm_byte_vec_t* buffer) {
// And call it!
printf("Calling export...\n");
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
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);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);

View File

@@ -17,9 +17,9 @@ const int N_THREADS = 10;
const int N_REPS = 3;
// A function to be called from Wasm code.
own wasm_trap_t* callback(const wasm_val_t args[], wasm_val_t results[]) {
assert(args[0].kind == WASM_I32);
printf("> Thread %d running\n", args[0].of.i32);
own wasm_trap_t* callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
assert(args->data[0].kind == WASM_I32);
printf("> Thread %d running\n", args->data[0].of.i32);
return NULL;
}
@@ -53,11 +53,12 @@ void* run(void* args_abs) {
wasm_globaltype_delete(global_type);
// Instantiate.
const wasm_extern_t* imports[] = {
wasm_extern_t* imports[] = {
wasm_func_as_extern(func), wasm_global_as_extern(global),
};
wasm_extern_vec_t imports_vec = WASM_ARRAY_VEC(imports);
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, &imports_vec, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return NULL;
@@ -82,7 +83,9 @@ void* run(void* args_abs) {
wasm_instance_delete(instance);
// Call.
if (wasm_func_call(run_func, NULL, NULL)) {
wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
if (wasm_func_call(run_func, &args_vec, &results_vec)) {
printf("> Error calling function!\n");
return NULL;
}

View File

@@ -91,7 +91,10 @@ int main() {
wasmtime_linker_get_default(linker, &empty, &func);
if (error != NULL)
exit_with_error("failed to locate default export for module", error, NULL);
error = wasmtime_func_call(func, NULL, 0, NULL, 0, &trap);
wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
error = wasmtime_func_call(func, &args_vec, &results_vec, &trap);
if (error != NULL)
exit_with_error("error calling default export", error, trap);

View File

@@ -90,7 +90,10 @@ int main() {
wasmtime_linker_get_default(linker, &empty, &func);
if (error != NULL)
exit_with_error("failed to locate default export for module", error, NULL);
error = wasmtime_func_call(func, NULL, 0, NULL, 0, &trap);
wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
error = wasmtime_func_call(func, &args_vec, &results_vec, &trap);
if (error != NULL)
exit_with_error("error calling default export", error, trap);