feat(c-api) Update wasm-c-api repository.

This change is important because `wasm.h` contains new functions, like
`wasm_name_new_from_string_nt`, which are useful for the Go
implementation.
This commit is contained in:
Ivan Enderlin
2020-10-09 14:37:07 +02:00
parent 5315eef115
commit 18729709bf
31 changed files with 348 additions and 264 deletions

View File

@@ -9,31 +9,32 @@
// A function to be called from Wasm code.
own 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" %"PRIu64" %"PRIu32"\n",
args[0].of.i32, args[1].of.i64, args[2].of.i64, args[3].of.i32);
args->data[0].of.i32, args->data[1].of.i64,
args->data[2].of.i64, args->data[3].of.i32);
printf("\n");
wasm_val_copy(&results[0], &args[3]);
wasm_val_copy(&results[1], &args[1]);
wasm_val_copy(&results[2], &args[2]);
wasm_val_copy(&results[3], &args[0]);
wasm_val_copy(&results->data[0], &args->data[3]);
wasm_val_copy(&results->data[1], &args->data[1]);
wasm_val_copy(&results->data[2], &args->data[2]);
wasm_val_copy(&results->data[3], &args->data[0]);
return NULL;
}
// A function closure.
own wasm_trap_t* closure_callback(
void* env, const wasm_val_t args[], wasm_val_t results[]
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results
) {
int i = *(int*)env;
printf("Calling back closure...\n");
printf("> %d\n", i);
results[0].kind = WASM_I32;
results[0].of.i32 = (int32_t)i;
results->data[0].kind = WASM_I32;
results->data[0].of.i32 = (int32_t)i;
return NULL;
}
@@ -46,7 +47,7 @@ int main(int argc, const char* argv[]) {
// Load binary.
printf("Loading binary...\n");
FILE* file = fopen("multi.wasm", "r");
FILE* file = fopen("multi.wasm", "rb");
if (!file) {
printf("> Error loading module!\n");
return 1;
@@ -89,9 +90,10 @@ 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* externs[] = { wasm_func_as_extern(callback_func) };
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
own wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
wasm_instance_new(store, module, &imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
@@ -118,17 +120,15 @@ int main(int argc, const char* argv[]) {
// Call.
printf("Calling export...\n");
wasm_val_t args[4];
args[0].kind = WASM_I32;
args[0].of.i32 = 1;
args[1].kind = WASM_I64;
args[1].of.i64 = 2;
args[2].kind = WASM_I64;
args[2].of.i64 = 3;
args[3].kind = WASM_I32;
args[3].of.i32 = 4;
wasm_val_t results[4];
if (wasm_func_call(run_func, args, results)) {
wasm_val_t vals[4] = {
WASM_I32_VAL(1), WASM_I32_VAL(2), WASM_I32_VAL(3), WASM_I32_VAL(4)
};
wasm_val_t res[4] = {
WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
};
wasm_val_vec_t args = WASM_ARRAY_VEC(vals);
wasm_val_vec_t results = WASM_ARRAY_VEC(res);
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
}
@@ -138,13 +138,12 @@ int main(int argc, const char* argv[]) {
// Print result.
printf("Printing result...\n");
printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
results[0].of.i32, results[1].of.i64,
results[2].of.i64, results[3].of.i32);
res[0].of.i32, res[1].of.i64, res[2].of.i64, res[3].of.i32);
assert(results[0].of.i32 == 4);
assert(results[1].of.i64 == 3);
assert(results[2].of.i64 == 2);
assert(results[3].of.i32 == 1);
assert(res[0].of.i32 == 4);
assert(res[1].of.i64 == 3);
assert(res[2].of.i64 == 2);
assert(res[3].of.i32 == 1);
// Shut down.
printf("Shutting down...\n");