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:
@ -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
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user