mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-06 06:55:07 +00:00
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:
parent
ed32693fc8
commit
76d641c7ea
|
@ -2739,8 +2739,8 @@ failed:
|
|||
|
||||
wasm_trap_t *
|
||||
wasm_func_call(const wasm_func_t *func,
|
||||
const wasm_val_t params[],
|
||||
wasm_val_t results[])
|
||||
const wasm_val_vec_t *params,
|
||||
wasm_val_vec_t *results)
|
||||
{
|
||||
/* parameters count as if all are uint32 */
|
||||
/* a int64 or float64 parameter means 2 */
|
||||
|
@ -2804,7 +2804,7 @@ wasm_func_call(const wasm_func_t *func,
|
|||
|
||||
/* copy parametes */
|
||||
if (param_count
|
||||
&& !(argc = params_to_argv(func->inst_comm_rt, params,
|
||||
&& !(argc = params_to_argv(func->inst_comm_rt, params->data,
|
||||
wasm_functype_params(func->type),
|
||||
param_count, argv))) {
|
||||
goto failed;
|
||||
|
@ -2826,9 +2826,11 @@ wasm_func_call(const wasm_func_t *func,
|
|||
/* copy results */
|
||||
if (result_count) {
|
||||
if (!(argc = argv_to_results(argv, wasm_functype_results(func->type),
|
||||
result_count, results))) {
|
||||
result_count, results->data))) {
|
||||
goto failed;
|
||||
}
|
||||
results->num_elems = result_count;
|
||||
results->size = result_count;
|
||||
}
|
||||
|
||||
if (argv != argv_buf)
|
||||
|
@ -4261,7 +4263,7 @@ failed:
|
|||
wasm_instance_t *
|
||||
wasm_instance_new(wasm_store_t *store,
|
||||
const wasm_module_t *module,
|
||||
const wasm_extern_t *const imports[],
|
||||
const wasm_extern_vec_t *imports,
|
||||
own wasm_trap_t **traps)
|
||||
{
|
||||
return wasm_instance_new_with_args(store, module, imports, traps,
|
||||
|
@ -4271,7 +4273,7 @@ wasm_instance_new(wasm_store_t *store,
|
|||
wasm_instance_t *
|
||||
wasm_instance_new_with_args(wasm_store_t *store,
|
||||
const wasm_module_t *module,
|
||||
const wasm_extern_t *const imports[],
|
||||
const wasm_extern_vec_t *imports,
|
||||
own wasm_trap_t **traps,
|
||||
const uint32 stack_size,
|
||||
const uint32 heap_size)
|
||||
|
@ -4305,7 +4307,7 @@ wasm_instance_new_with_args(wasm_store_t *store,
|
|||
|
||||
if (import_count) {
|
||||
uint32 actual_link_import_count = interp_link(
|
||||
instance, MODULE_INTERP(module), (wasm_extern_t **)imports);
|
||||
instance, MODULE_INTERP(module), (wasm_extern_t **)imports->data);
|
||||
/* make sure a complete import list */
|
||||
if ((int32)import_count < 0
|
||||
|| import_count != actual_link_import_count) {
|
||||
|
@ -4327,7 +4329,7 @@ wasm_instance_new_with_args(wasm_store_t *store,
|
|||
|
||||
if (import_count) {
|
||||
import_count = aot_link(instance, MODULE_AOT(module),
|
||||
(wasm_extern_t **)imports);
|
||||
(wasm_extern_t **)imports->data);
|
||||
if ((int32)import_count < 0) {
|
||||
goto failed;
|
||||
}
|
||||
|
@ -4356,8 +4358,8 @@ wasm_instance_new_with_args(wasm_store_t *store,
|
|||
}
|
||||
|
||||
/* fill with inst */
|
||||
for (i = 0; imports && i < (uint32)import_count; ++i) {
|
||||
wasm_extern_t *import = (wasm_extern_t *)imports[i];
|
||||
for (i = 0; imports && imports->data && i < (uint32)import_count; ++i) {
|
||||
wasm_extern_t *import = imports->data[i];
|
||||
switch (import->kind) {
|
||||
case WASM_EXTERN_FUNC:
|
||||
wasm_extern_as_func(import)->inst_comm_rt =
|
||||
|
|
|
@ -4104,6 +4104,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
|
|||
wasm_val_t *params = params_buf, *results = results_buf;
|
||||
wasm_trap_t *trap = NULL;
|
||||
bool ret = false;
|
||||
wasm_val_vec_t params_vec, results_vec;
|
||||
|
||||
if (func_type->param_count > 16
|
||||
&& !(params = wasm_runtime_malloc(sizeof(wasm_val_t)
|
||||
|
@ -4124,14 +4125,24 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
|
|||
goto fail;
|
||||
}
|
||||
|
||||
params_vec.data = params;
|
||||
params_vec.num_elems = func_type->param_count;
|
||||
params_vec.size = func_type->param_count;
|
||||
params_vec.size_of_elem = sizeof(wasm_val_t);
|
||||
|
||||
results_vec.data = results;
|
||||
results_vec.num_elems = 0;
|
||||
results_vec.size = func_type->result_count;
|
||||
results_vec.size_of_elem = sizeof(wasm_val_t);
|
||||
|
||||
if (!with_env) {
|
||||
wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr;
|
||||
trap = callback(params, results);
|
||||
trap = callback(¶ms_vec, &results_vec);
|
||||
}
|
||||
else {
|
||||
wasm_func_callback_with_env_t callback =
|
||||
(wasm_func_callback_with_env_t)func_ptr;
|
||||
trap = callback(wasm_c_api_env, params, results);
|
||||
trap = callback(wasm_c_api_env, ¶ms_vec, &results_vec);
|
||||
}
|
||||
|
||||
if (trap) {
|
||||
|
@ -4155,7 +4166,7 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst,
|
|||
wasm_runtime_set_exception(module_inst, "unsupported result type");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
results_vec.num_elems = func_type->result_count;
|
||||
ret = true;
|
||||
|
||||
fail:
|
||||
|
|
|
@ -467,9 +467,9 @@ WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const
|
|||
WASM_DECLARE_REF(func)
|
||||
|
||||
typedef own wasm_trap_t* (*wasm_func_callback_t)(
|
||||
const wasm_val_t args[], own wasm_val_t results[]);
|
||||
const wasm_val_vec_t* args, own wasm_val_vec_t *results);
|
||||
typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
|
||||
void* env, const wasm_val_t args[], wasm_val_t results[]);
|
||||
void* env, const wasm_val_vec_t *args, wasm_val_vec_t *results);
|
||||
|
||||
WASM_API_EXTERN own wasm_func_t* wasm_func_new(
|
||||
wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
|
||||
|
@ -482,7 +482,7 @@ WASM_API_EXTERN size_t wasm_func_param_arity(const wasm_func_t*);
|
|||
WASM_API_EXTERN size_t wasm_func_result_arity(const wasm_func_t*);
|
||||
|
||||
WASM_API_EXTERN own wasm_trap_t* wasm_func_call(
|
||||
const wasm_func_t*, const wasm_val_t args[], wasm_val_t results[]);
|
||||
const wasm_func_t*, const wasm_val_vec_t* args, wasm_val_vec_t* results);
|
||||
|
||||
|
||||
// Global Instances
|
||||
|
@ -569,13 +569,13 @@ WASM_API_EXTERN const wasm_memory_t* wasm_extern_as_memory_const(const wasm_exte
|
|||
WASM_DECLARE_REF(instance)
|
||||
|
||||
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(
|
||||
wasm_store_t*, const wasm_module_t*, const wasm_extern_t *const imports[],
|
||||
wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
|
||||
own wasm_trap_t**
|
||||
);
|
||||
|
||||
// please refer to wasm_runtime_instantiate(...) in core/iwasm/include/wasm_export.h
|
||||
WASM_API_EXTERN own wasm_instance_t* wasm_instance_new_with_args(
|
||||
wasm_store_t*, const wasm_module_t*, const wasm_extern_t *const imports[],
|
||||
wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t *imports,
|
||||
own wasm_trap_t**, const uint32_t stack_size, const uint32_t heap_size
|
||||
);
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -17,8 +17,8 @@ get_memory_data(uint32_t offset, uint32_t length);
|
|||
|
||||
static bool
|
||||
call_wasm_function(uint32_t export_id,
|
||||
const wasm_val_t *args,
|
||||
wasm_val_t *results,
|
||||
const wasm_val_vec_t *args,
|
||||
wasm_val_vec_t *results,
|
||||
const char *name);
|
||||
|
||||
/************************ IMPORTED FUNCTIONS **************************/
|
||||
|
@ -49,7 +49,7 @@ enum EXPORT_ITEM_NAME {
|
|||
};
|
||||
|
||||
#define DEFINE_FUNCTION(name) \
|
||||
wasm_trap_t *STUB_##name(const wasm_val_t args[], wasm_val_t results[])
|
||||
wasm_trap_t *STUB_##name(const wasm_val_vec_t* args, wasm_val_vec_t* results)
|
||||
|
||||
#define DEFINE_EMPTY_FUNCTION(name) \
|
||||
DEFINE_FUNCTION(name) \
|
||||
|
@ -61,16 +61,17 @@ enum EXPORT_ITEM_NAME {
|
|||
|
||||
DEFINE_FUNCTION(get_pairs)
|
||||
{
|
||||
wasm_val_t ret[1] = { WASM_INIT_VAL };
|
||||
call_wasm_function(e_malloc, (wasm_val_t[]){ WASM_I32_VAL(24) }, ret,
|
||||
"malloc");
|
||||
wasm_val_vec_t arg, ret;
|
||||
wasm_val_vec_new(&ret, 1, (wasm_val_t []){ WASM_INIT_VAL });
|
||||
wasm_val_vec_new(&arg, 1, (wasm_val_t []){ WASM_I32_VAL(24) });
|
||||
call_wasm_function(e_malloc, &arg, &ret, "malloc");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DEFINE_FUNCTION(log)
|
||||
{
|
||||
wasm_val_t offset = args[0];
|
||||
wasm_val_t length = args[1];
|
||||
wasm_val_t offset = args->data[0];
|
||||
wasm_val_t length = args->data[1];
|
||||
const byte_t *data = NULL;
|
||||
|
||||
printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__);
|
||||
|
@ -120,8 +121,8 @@ get_memory_data(uint32_t offset, uint32_t length)
|
|||
|
||||
static bool
|
||||
call_wasm_function(uint32_t export_id,
|
||||
const wasm_val_t *args,
|
||||
wasm_val_t *results,
|
||||
const wasm_val_vec_t *args,
|
||||
wasm_val_vec_t *results,
|
||||
const char *name)
|
||||
{
|
||||
const wasm_func_t *function;
|
||||
|
@ -203,7 +204,8 @@ main(int argc, const char *argv[])
|
|||
IMPORT_FUNCTION_LIST(IMPORT_FUNCTION_VARIABLE_NAME)
|
||||
#undef IMPORT_FUNCTION_VARIABLE_NAME
|
||||
|
||||
const wasm_extern_t *imports[10] = { 0 };
|
||||
wasm_extern_vec_t imports;
|
||||
wasm_extern_vec_new_uninitialized(&imports, 10);
|
||||
|
||||
#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE) \
|
||||
{ \
|
||||
|
@ -218,13 +220,14 @@ main(int argc, const char *argv[])
|
|||
#undef CREATE_WASM_FUNCTION
|
||||
|
||||
#define ADD_TO_FUNCTION_LIST(name, index, ...) \
|
||||
imports[index] = wasm_func_as_extern(function_##name);
|
||||
imports.data[index] = wasm_func_as_extern(function_##name); \
|
||||
imports.num_elems += 1;
|
||||
IMPORT_FUNCTION_LIST(ADD_TO_FUNCTION_LIST)
|
||||
#undef CREATE_IMPORT_FUNCTION
|
||||
|
||||
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -52,9 +52,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); \
|
||||
}
|
||||
|
||||
wasm_module_t * create_module_from_file(wasm_store_t* store, const char * filename)
|
||||
|
@ -146,16 +147,18 @@ int main(int argc, const char* argv[]) {
|
|||
|
||||
// Modify variables through calls and check again.
|
||||
printf("Modify the variable to 77.0...\n");
|
||||
wasm_val_t args77[] = { {.kind = WASM_F32, .of = {.f32 = 77.}} };
|
||||
wasm_func_call(set_var_f32_export, args77, NULL); //Call to module export
|
||||
wasm_val_vec_t args77;
|
||||
wasm_val_vec_new(&args77, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 77.0}} });
|
||||
wasm_func_call(set_var_f32_export, &args77, NULL); //Call to module export
|
||||
check_call(get_var_f32_export, f32, 77.0); //Call to module export
|
||||
check_global(var_f32_export, f32, 77.0); //Failed here, still 37
|
||||
check_call(get_var_f32_import, f32, 77.0); //Call to module import Failed here, still 37
|
||||
|
||||
|
||||
printf("Modify the variable to 78.0...\n");
|
||||
wasm_val_t args78[] = { {.kind = WASM_F32, .of = {.f32 = 78.0}} };
|
||||
wasm_func_call(set_var_f32_import, args78, NULL);
|
||||
wasm_val_vec_t args78;
|
||||
wasm_val_vec_new(&args78, 1, (wasm_val_t []){ {.kind = WASM_F32, .of = {.f32 = 78.0}} });
|
||||
wasm_func_call(set_var_f32_import, &args78, NULL);
|
||||
check_global(var_f32_export, f32, 78.0);
|
||||
check_call(get_var_f32_export, f32, 78.0); //Call to module export Failed here, still 77
|
||||
check_call(get_var_f32_import, f32, 78.0); //Call to module import
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* hello_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("> Hello World!\n");
|
||||
|
@ -66,9 +66,11 @@ int main(int argc, const char* argv[]) {
|
|||
|
||||
// Instantiate.
|
||||
printf("Instantiating module...\n");
|
||||
const wasm_extern_t* imports[] = { wasm_func_as_extern(hello_func) };
|
||||
wasm_extern_vec_t imports;
|
||||
wasm_extern_vec_new(&imports, 1, (wasm_extern_t* []) { wasm_func_as_extern(hello_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;
|
||||
|
|
|
@ -10,12 +10,12 @@
|
|||
|
||||
// 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("> %p\n",
|
||||
args[0].of.ref ? wasm_ref_get_host_info(args[0].of.ref) : NULL);
|
||||
wasm_val_copy(&results[0], &args[0]);
|
||||
args->data[0].of.ref ? wasm_ref_get_host_info(args->data[0].of.ref) : NULL);
|
||||
wasm_val_copy(&results->data[0], &args->data[0]);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -47,19 +47,21 @@ wasm_table_t* get_export_table(const wasm_extern_vec_t* exports, size_t i) {
|
|||
|
||||
own wasm_ref_t* call_v_r(const wasm_func_t* func) {
|
||||
printf("call_v_r... "); fflush(stdout);
|
||||
wasm_val_t rs[] = { WASM_INIT_VAL };
|
||||
if (wasm_func_call(func, NULL, rs)) {
|
||||
wasm_val_vec_t rs;
|
||||
wasm_val_vec_new_uninitialized(&rs, 1);
|
||||
if (wasm_func_call(func, NULL, &rs)) {
|
||||
printf("> Error calling function!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("okay\n");
|
||||
return rs[0].of.ref;
|
||||
return rs.data[0].of.ref;
|
||||
}
|
||||
|
||||
void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) {
|
||||
printf("call_r_v... "); fflush(stdout);
|
||||
wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
|
||||
if (wasm_func_call(func, vs, NULL)) {
|
||||
wasm_val_vec_t vs;
|
||||
wasm_val_vec_new(&vs, 1, (wasm_val_t []){ WASM_REF_VAL(ref) });
|
||||
if (wasm_func_call(func, &vs, NULL)) {
|
||||
printf("> Error calling function!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -68,20 +70,22 @@ void call_r_v(const wasm_func_t* func, wasm_ref_t* ref) {
|
|||
|
||||
own wasm_ref_t* call_r_r(const wasm_func_t* func, wasm_ref_t* ref) {
|
||||
printf("call_r_r... "); fflush(stdout);
|
||||
wasm_val_t vs[1] = { WASM_REF_VAL(ref) };
|
||||
wasm_val_t rs[1] = { WASM_INIT_VAL };
|
||||
if (wasm_func_call(func, vs, rs)) {
|
||||
wasm_val_vec_t vs, rs;
|
||||
wasm_val_vec_new(&vs, 1, (wasm_val_t []){ WASM_REF_VAL(ref) });
|
||||
wasm_val_vec_new_uninitialized(&rs, 1);
|
||||
if (wasm_func_call(func, &vs, &rs)) {
|
||||
printf("> Error calling function!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("okay\n");
|
||||
return rs[0].of.ref;
|
||||
return rs.data[0].of.ref;
|
||||
}
|
||||
|
||||
void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) {
|
||||
printf("call_ir_v... "); fflush(stdout);
|
||||
wasm_val_t vs[2] = { WASM_I32_VAL(i), WASM_REF_VAL(ref) };
|
||||
if (wasm_func_call(func, vs, NULL)) {
|
||||
wasm_val_vec_t vs;
|
||||
wasm_val_vec_new(&vs, 2, (wasm_val_t []){ WASM_I32_VAL(i), WASM_REF_VAL(ref) });
|
||||
if (wasm_func_call(func, &vs, NULL)) {
|
||||
printf("> Error calling function!\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -90,14 +94,15 @@ void call_ir_v(const wasm_func_t* func, int32_t i, wasm_ref_t* ref) {
|
|||
|
||||
own wasm_ref_t* call_i_r(const wasm_func_t* func, int32_t i) {
|
||||
printf("call_i_r... "); fflush(stdout);
|
||||
wasm_val_t vs[1] = { WASM_I32_VAL(i) };
|
||||
wasm_val_t rs[1] = { WASM_INIT_VAL };
|
||||
if (wasm_func_call(func, vs, rs)) {
|
||||
wasm_val_vec_t vs, rs;
|
||||
wasm_val_vec_new(&vs, 1, (wasm_val_t []){ WASM_I32_VAL(i) });
|
||||
wasm_val_vec_new_uninitialized(&rs, 1);
|
||||
if (wasm_func_call(func, &vs, &rs)) {
|
||||
printf("> Error calling function!\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("okay\n");
|
||||
return rs[0].of.ref;
|
||||
return rs.data[0].of.ref;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -164,9 +169,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_vec_t imports;
|
||||
wasm_extern_vec_new(&imports, 1, (wasm_extern_t* []) { wasm_func_as_extern(callback_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;
|
||||
|
|
|
@ -33,8 +33,13 @@ void check(bool success) {
|
|||
}
|
||||
|
||||
void check_call(wasm_func_t* func, int i, wasm_val_t args[], int32_t expected) {
|
||||
wasm_val_t results[1] = { WASM_INIT_VAL };
|
||||
if (wasm_func_call(func, args, results) || results[0].of.i32 != expected) {
|
||||
wasm_val_vec_t args_vec;
|
||||
wasm_val_vec_t results_vec;
|
||||
if (args)
|
||||
wasm_val_vec_new(&args_vec, i, args);
|
||||
wasm_val_vec_new(&results_vec, 1, (wasm_val_t []){ WASM_INIT_VAL });
|
||||
if (wasm_func_call(func, args ? &args_vec : NULL, &results_vec)
|
||||
|| results_vec.data[0].of.i32 != expected) {
|
||||
printf("> Error on result\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -55,7 +60,9 @@ void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected
|
|||
}
|
||||
|
||||
void check_ok(wasm_func_t* func, int i, wasm_val_t args[]) {
|
||||
if (wasm_func_call(func, args, NULL)) {
|
||||
wasm_val_vec_t args_vec;
|
||||
wasm_val_vec_new(&args_vec, i, args);
|
||||
if (wasm_func_call(func, &args_vec, NULL)) {
|
||||
printf("> Error on result, expected empty\n");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -67,8 +74,10 @@ void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
|
|||
}
|
||||
|
||||
void check_trap(wasm_func_t* func, int i, wasm_val_t args[]) {
|
||||
wasm_val_t results[1] = { WASM_INIT_VAL };
|
||||
own wasm_trap_t* trap = wasm_func_call(func, args, results);
|
||||
wasm_val_vec_t args_vec, results_vec;
|
||||
wasm_val_vec_new(&args_vec, i, args);
|
||||
wasm_val_vec_new(&results_vec, 1, (wasm_val_t []){ WASM_INIT_VAL });
|
||||
own wasm_trap_t* trap = wasm_func_call(func, &args_vec, &results_vec);
|
||||
if (! trap) {
|
||||
printf("> Error on result, expected trap\n");
|
||||
exit(1);
|
||||
|
|
|
@ -9,18 +9,18 @@
|
|||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -91,9 +91,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_vec_t imports;
|
||||
wasm_extern_vec_new(&imports, 1, (wasm_extern_t *[]) { wasm_func_as_extern(callback_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;
|
||||
|
@ -120,13 +121,14 @@ int main(int argc, const char* argv[]) {
|
|||
|
||||
// Call.
|
||||
printf("Calling export...\n");
|
||||
wasm_val_t args[4] = {
|
||||
WASM_I32_VAL(1), WASM_I64_VAL(2), WASM_I64_VAL(3), WASM_I32_VAL(4)
|
||||
};
|
||||
wasm_val_t results[4] = {
|
||||
WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
|
||||
};
|
||||
if (wasm_func_call(run_func, args, results)) {
|
||||
wasm_val_vec_t args, results;
|
||||
wasm_val_vec_new(&args, 4, (wasm_val_t []){
|
||||
WASM_I32_VAL(1), WASM_I64_VAL(2), WASM_I64_VAL(3), WASM_I32_VAL(4)
|
||||
});
|
||||
wasm_val_vec_new(&results, 4, (wasm_val_t []) {
|
||||
WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL, WASM_INIT_VAL
|
||||
});
|
||||
if (wasm_func_call(run_func, &args, &results)) {
|
||||
printf("> Error calling function!\n");
|
||||
return 1;
|
||||
}
|
||||
|
@ -136,12 +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);
|
||||
results.data[0].of.i32, results.data[1].of.i64, results.data[2].of.i64, results.data[3].of.i32);
|
||||
|
||||
assert(results[0].of.i32 == 1);
|
||||
assert(results[1].of.i64 == 2);
|
||||
assert(results[2].of.i64 == 3);
|
||||
assert(results[3].of.i32 == 4);
|
||||
assert(results.data[0].of.i32 == 1);
|
||||
assert(results.data[1].of.i64 == 2);
|
||||
assert(results.data[2].of.i64 == 3);
|
||||
assert(results.data[3].of.i32 == 4);
|
||||
|
||||
// Shut down.
|
||||
printf("Shutting down...\n");
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* neg_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");
|
||||
results[0].kind = WASM_I32;
|
||||
results[0].of.i32 = -args[0].of.i32;
|
||||
results->data[0].kind = WASM_I32;
|
||||
results->data[0].of.i32 = -args->data[0].of.i32;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -49,18 +49,20 @@ void check_table(wasm_table_t* table, int32_t i, bool expect_set) {
|
|||
}
|
||||
|
||||
void check_call(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
|
||||
wasm_val_t args[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
|
||||
wasm_val_t results[1] = { WASM_INIT_VAL };
|
||||
if (wasm_func_call(func, args, results) || results[0].of.i32 != expected) {
|
||||
wasm_val_vec_t args, results;
|
||||
wasm_val_vec_new(&args, 2, (wasm_val_t []){ WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) });
|
||||
wasm_val_vec_new(&results, 1, (wasm_val_t []){ WASM_INIT_VAL });
|
||||
if (wasm_func_call(func, &args, &results) || results.data[0].of.i32 != expected) {
|
||||
printf("> Error on result\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void check_trap(wasm_func_t* func, int32_t arg1, int32_t arg2) {
|
||||
wasm_val_t args[2] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
|
||||
wasm_val_t results[1] = { WASM_INIT_VAL };
|
||||
own wasm_trap_t* trap = wasm_func_call(func, args, results);
|
||||
wasm_val_vec_t args, results;
|
||||
wasm_val_vec_new(&args, 2, (wasm_val_t []){ WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) });
|
||||
wasm_val_vec_new(&results, 1, (wasm_val_t []){ WASM_INIT_VAL });
|
||||
own wasm_trap_t* trap = wasm_func_call(func, &args, &results);
|
||||
if (! trap) {
|
||||
printf("> Error on result, expected trap\n");
|
||||
exit(1);
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* fail_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
|
||||
) {
|
||||
printf("Calling back...\n");
|
||||
own wasm_name_t message;
|
||||
|
@ -80,9 +80,10 @@ int main(int argc, const char* argv[]) {
|
|||
|
||||
// Instantiate.
|
||||
printf("Instantiating module...\n");
|
||||
const wasm_extern_t* imports[] = { wasm_func_as_extern(fail_func) };
|
||||
wasm_extern_vec_t imports;
|
||||
wasm_extern_vec_new(&imports, 1, (wasm_extern_t* []) { wasm_func_as_extern(fail_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;
|
||||
|
@ -111,8 +112,10 @@ int main(int argc, const char* argv[]) {
|
|||
}
|
||||
|
||||
printf("Calling export %d...\n", i);
|
||||
wasm_val_t results[1]; \
|
||||
own wasm_trap_t* trap = wasm_func_call(func, NULL, results);
|
||||
|
||||
wasm_val_vec_t results;
|
||||
wasm_val_vec_new_uninitialized(&results, 1);
|
||||
own wasm_trap_t* trap = wasm_func_call(func, NULL, &results);
|
||||
if (!trap) {
|
||||
printf("> Error calling function, expected trap!\n");
|
||||
return 1;
|
||||
|
|
Loading…
Reference in New Issue
Block a user