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

@ -39,9 +39,10 @@ wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
#define check_call(func, type, expected) \
{ \
wasm_val_t results[1]; \
wasm_func_call(func, NULL, results); \
check(results[0], type, expected); \
wasm_val_vec_t results; \
wasm_val_vec_new_uninitialized(&results, 1); \
wasm_func_call(func, NULL, &results); \
check(results.data[0], type, expected); \
}
@ -115,14 +116,21 @@ int main(int argc, const char* argv[]) {
// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {
/*const wasm_extern_t* imports1[] = {
wasm_global_as_extern(const_f32_import),
wasm_global_as_extern(const_i64_import),
wasm_global_as_extern(var_f32_import),
wasm_global_as_extern(var_i64_import)
};
};*/
wasm_extern_vec_t imports;
wasm_extern_vec_new(&imports, 4, (wasm_extern_t* []) {
wasm_global_as_extern(const_f32_import),
wasm_global_as_extern(const_i64_import),
wasm_global_as_extern(var_f32_import),
wasm_global_as_extern(var_i64_import)
});
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;
@ -200,14 +208,18 @@ int main(int argc, const char* argv[]) {
check_call(get_var_i64_export, i64, 38);
// Modify variables through calls and check again.
wasm_val_t args73[] = { WASM_F32_VAL(73) };
wasm_func_call(set_var_f32_import, args73, NULL);
wasm_val_t args74[] = { WASM_I64_VAL(74) };
wasm_func_call(set_var_i64_import, args74, NULL);
wasm_val_t args77[] = { WASM_F32_VAL(77) };
wasm_func_call(set_var_f32_export, args77, NULL);
wasm_val_t args78[] = { WASM_I64_VAL(78) };
wasm_func_call(set_var_i64_export, args78, NULL);
wasm_val_vec_t args73;
wasm_val_vec_new(&args73, 1, (wasm_val_t []){ WASM_F32_VAL(73) });
wasm_func_call(set_var_f32_import, &args73, NULL);
wasm_val_vec_t args74;
wasm_val_vec_new(&args74, 1, (wasm_val_t []){ WASM_I64_VAL(74) });
wasm_func_call(set_var_i64_import, &args74, NULL);
wasm_val_vec_t args77;
wasm_val_vec_new(&args77, 1, (wasm_val_t []){ WASM_F32_VAL(77) });
wasm_func_call(set_var_f32_export, &args77, NULL);
wasm_val_vec_t args78;
wasm_val_vec_new(&args78, 1, (wasm_val_t []){ WASM_I64_VAL(78) });
wasm_func_call(set_var_i64_export, &args78, NULL);
check_global(var_f32_import, f32, 73);
check_global(var_i64_import, i64, 74);