Update wasm_c_api to use vector types (#751)

The WASM C API now requires the use of vector types in certain apis.
Switching WAMR to use the new call signatures improves "drop in"
compilation compatibility between WAMR and other implementations
from a C-api embedding program's perspective.

* wasm_func_callback_t type has been updated to use wasm_val_vec_t
* wasm_func_callback_with_env_t type has been updated to use wasm_val_vec_t
* wasm_func_call() has been updated to use wasm_val_vec_t
* wasm_instance_new() has been updated to use wasm_extern_vec_t*
* wasm_instance_new_with_args() has been updated to use wasm_extern_vec_t*
* wasm_runtime_invoke_c_api_native() has been updated to support vector types
  in native callbacks without modifying the contract with the interpreter code.
* All users of the modified functions (including samples/wasm-c-api/src/*.c)
  have been appropriately updated.
This commit is contained in:
Saju Pillai
2021-09-15 23:54:25 -07:00
committed by GitHub
parent ed32693fc8
commit 76d641c7ea
13 changed files with 186 additions and 128 deletions

View File

@ -35,27 +35,27 @@ void wasm_val_print(wasm_val_t val) {
// A function to be called from Wasm code.
own wasm_trap_t* print_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> ");
wasm_val_print(args[0]);
wasm_val_print(args->data[0]);
printf("\n");
wasm_val_copy(&results[0], &args[0]);
wasm_val_copy(&results->data[0], &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;
}
@ -113,11 +113,13 @@ int main(int argc, const char* argv[]) {
// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {
wasm_extern_vec_t imports;
wasm_extern_vec_new(&imports, 2, (wasm_extern_t *[]) {
wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
};
});
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;
@ -145,9 +147,10 @@ int main(int argc, const char* argv[]) {
// Call.
printf("Calling export...\n");
wasm_val_t args[2] = { WASM_I32_VAL(3), WASM_I32_VAL(4) };
wasm_val_t results[1] = { WASM_INIT_VAL };
if (wasm_func_call(run_func, args, results)) {
wasm_val_vec_t args, results;
wasm_val_vec_new(&args, 2, (wasm_val_t[]){ WASM_I32_VAL(3), WASM_I32_VAL(4) });
wasm_val_vec_new(&results, 1, (wasm_val_t[]) { WASM_INIT_VAL });
if (wasm_func_call(run_func, &args, &results)) {
printf("> Error calling function!\n");
return 1;
}
@ -156,7 +159,7 @@ int main(int argc, const char* argv[]) {
// Print result.
printf("Printing result...\n");
printf("> %u\n", results[0].of.i32);
printf("> %u\n", results.data[0].of.i32);
// Shut down.
printf("Shutting down...\n");