Refine the wasm-c-api native func call process of aot mode (#640)
This commit is contained in:
@ -2335,14 +2335,12 @@ wasm_func_call(const wasm_func_t *func,
|
||||
/* a int64 or float64 parameter means 2 */
|
||||
uint32 argc = 0;
|
||||
/* a parameter list and a return value list */
|
||||
uint32 *argv = NULL;
|
||||
uint32 argv_buf[32], *argv = argv_buf;
|
||||
WASMFunctionInstanceCommon *func_comm_rt = NULL;
|
||||
WASMExecEnv *exec_env = NULL;
|
||||
size_t param_count, result_count, alloc_count;
|
||||
|
||||
if (!func || !func->type || !func->inst_comm_rt
|
||||
|| !valid_module_type(func->inst_comm_rt->module_type)) {
|
||||
return NULL;
|
||||
}
|
||||
bh_assert(func && func->type && func->inst_comm_rt);
|
||||
|
||||
cur_trap = NULL;
|
||||
|
||||
@ -2355,20 +2353,23 @@ wasm_func_call(const wasm_func_t *func,
|
||||
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (func->inst_comm_rt->module_type == Wasm_Module_AoT) {
|
||||
AOTModuleInstance *inst_aot = (AOTModuleInstance *)func->inst_comm_rt;
|
||||
AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
|
||||
if (!(func_comm_rt = func->func_comm_rt)) {
|
||||
AOTModuleInstance *inst_aot = (AOTModuleInstance *)func->inst_comm_rt;
|
||||
AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
|
||||
uint32 export_i = 0, export_func_j = 0;
|
||||
|
||||
uint32 export_i = 0, export_func_j = 0;
|
||||
for (; export_i < module_aot->export_count; ++export_i) {
|
||||
AOTExport *export = module_aot->exports + export_i;
|
||||
if (export->kind == EXPORT_KIND_FUNC) {
|
||||
if (export->index == func->func_idx_rt) {
|
||||
func_comm_rt =
|
||||
(AOTFunctionInstance *)inst_aot->export_funcs.ptr
|
||||
+ export_func_j;
|
||||
break;
|
||||
for (; export_i < module_aot->export_count; ++export_i) {
|
||||
AOTExport *export = module_aot->exports + export_i;
|
||||
if (export->kind == EXPORT_KIND_FUNC) {
|
||||
if (export->index == func->func_idx_rt) {
|
||||
func_comm_rt =
|
||||
(AOTFunctionInstance *)inst_aot->export_funcs.ptr
|
||||
+ export_func_j;
|
||||
((wasm_func_t*)func)->func_comm_rt = func_comm_rt;
|
||||
break;
|
||||
}
|
||||
export_func_j++;
|
||||
}
|
||||
export_func_j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2381,7 +2382,7 @@ wasm_func_call(const wasm_func_t *func,
|
||||
param_count = wasm_func_param_arity(func);
|
||||
result_count = wasm_func_result_arity(func);
|
||||
alloc_count = (param_count > result_count) ? param_count : result_count;
|
||||
if (alloc_count) {
|
||||
if (alloc_count > sizeof(argv_buf)/sizeof(uint64)) {
|
||||
if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
|
||||
goto failed;
|
||||
}
|
||||
@ -2394,8 +2395,12 @@ wasm_func_call(const wasm_func_t *func,
|
||||
goto failed;
|
||||
}
|
||||
|
||||
if (!wasm_runtime_create_exec_env_and_call_wasm(
|
||||
func->inst_comm_rt, func_comm_rt, argc, argv)) {
|
||||
exec_env = wasm_runtime_get_exec_env_singleton(func->inst_comm_rt);
|
||||
if (!exec_env) {
|
||||
goto failed;
|
||||
}
|
||||
|
||||
if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) {
|
||||
if (wasm_runtime_get_exception(func->inst_comm_rt)) {
|
||||
LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt));
|
||||
goto failed;
|
||||
@ -2410,11 +2415,13 @@ wasm_func_call(const wasm_func_t *func,
|
||||
}
|
||||
}
|
||||
|
||||
FREEIF(argv);
|
||||
if (argv != argv_buf)
|
||||
wasm_runtime_free(argv);
|
||||
return NULL;
|
||||
|
||||
failed:
|
||||
FREEIF(argv);
|
||||
if (argv != argv_buf)
|
||||
wasm_runtime_free(argv);
|
||||
if (cur_trap) {
|
||||
return cur_trap;
|
||||
}
|
||||
@ -3392,9 +3399,14 @@ aot_link_func(const wasm_instance_t *inst,
|
||||
return false;
|
||||
}
|
||||
|
||||
import_aot_func->call_conv_raw = true;
|
||||
import_aot_func->attachment = cloned;
|
||||
import_aot_func->func_ptr_linked = native_func_trampoline;
|
||||
import_aot_func->call_conv_wasm_c_api = true;
|
||||
import_aot_func->wasm_c_api_with_env = import->with_env;
|
||||
if (import->with_env) {
|
||||
import_aot_func->func_ptr_linked = import->u.cb_env.cb;
|
||||
import_aot_func->attachment = import->u.cb_env.env;
|
||||
}
|
||||
else
|
||||
import_aot_func->func_ptr_linked = import->u.cb;
|
||||
import->func_idx_rt = import_func_idx_rt;
|
||||
|
||||
return true;
|
||||
@ -3668,6 +3680,10 @@ wasm_instance_new(wasm_store_t *store,
|
||||
goto failed;
|
||||
}
|
||||
|
||||
if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
|
||||
goto failed;
|
||||
}
|
||||
|
||||
/* fill with inst */
|
||||
for (i = 0; imports && i < (uint32)import_count; ++i) {
|
||||
wasm_extern_t *import = (wasm_extern_t *)imports[i];
|
||||
|
||||
@ -112,6 +112,7 @@ struct wasm_func_t {
|
||||
*/
|
||||
uint16 func_idx_rt;
|
||||
WASMModuleInstanceCommon *inst_comm_rt;
|
||||
WASMFunctionInstanceCommon *func_comm_rt;
|
||||
};
|
||||
|
||||
struct wasm_global_t {
|
||||
|
||||
@ -1397,6 +1397,35 @@ wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst->module_type == Wasm_Module_Bytecode)
|
||||
return wasm_create_exec_env_singleton((WASMModuleInstance *)module_inst);
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst->module_type == Wasm_Module_AoT)
|
||||
return aot_create_exec_env_singleton((AOTModuleInstance *)module_inst);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
WASMExecEnv *
|
||||
wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst)
|
||||
{
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst->module_type == Wasm_Module_Bytecode)
|
||||
return ((WASMModuleInstance *)module_inst)->exec_env_singleton;
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst->module_type == Wasm_Module_AoT)
|
||||
return (WASMExecEnv *)
|
||||
((AOTModuleInstance *)module_inst)->exec_env_singleton.ptr;
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
|
||||
const char *exception)
|
||||
|
||||
@ -467,6 +467,12 @@ wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst
|
||||
WASMFunctionInstanceCommon *function,
|
||||
uint32 argc, uint32 argv[]);
|
||||
|
||||
bool
|
||||
wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
WASMExecEnv *
|
||||
wasm_runtime_get_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
WASM_RUNTIME_API_EXTERN bool
|
||||
wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
|
||||
|
||||
Reference in New Issue
Block a user