Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,9 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end,
if (!(import_funcs[i].func_ptr_linked =
wasm_native_resolve_symbol(module_name, field_name,
import_funcs[i].func_type,
&import_funcs[i].signature))) {
&import_funcs[i].signature,
&import_funcs[i].attachment,
&import_funcs[i].call_conv_raw))) {
LOG_WARNING("warning: fail to link import function (%s, %s)\n",
module_name, field_name);
}
Expand Down
40 changes: 28 additions & 12 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ aot_call_function(WASMExecEnv *exec_env,
AOTModuleInstance *module_inst = (AOTModuleInstance*)exec_env->module_inst;
AOTFuncType *func_type = function->func_type;
bool ret = wasm_runtime_invoke_native(exec_env, function->func_ptr,
func_type, NULL, argv, argc, argv);
func_type, NULL, NULL, argv, argc, argv);
return ret && !aot_get_exception(module_inst) ? true : false;
}

Expand Down Expand Up @@ -824,7 +824,8 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
void **func_ptrs = (void**)module_inst->func_ptrs.ptr;
void *func_ptr = func_ptrs[func_idx];
AOTImportFunc *import_func;
const char *signature = NULL;
const char *signature;
void *attachment;
char buf[128];

bh_assert(func_idx < aot_module->import_func_count);
Expand All @@ -839,9 +840,17 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
}

signature = import_func->signature;
return wasm_runtime_invoke_native(exec_env, func_ptr,
func_type, signature,
frame_lp, argc, argv_ret);
attachment = import_func->attachment;
if (!import_func->call_conv_raw) {
return wasm_runtime_invoke_native(exec_env, func_ptr,
func_type, signature, attachment,
frame_lp, argc, argv_ret);
}
else {
return wasm_runtime_invoke_native_raw(exec_env, func_ptr,
func_type, signature, attachment,
frame_lp, argc, argv_ret);
}
}

bool
Expand All @@ -860,6 +869,7 @@ aot_call_indirect(WASMExecEnv *exec_env,
uint32 func_idx, func_type_idx1;
AOTImportFunc *import_func;
const char *signature = NULL;
void *attachment = NULL;
char buf[128];

if (table_elem_idx >= table_size) {
Expand All @@ -879,12 +889,6 @@ aot_call_indirect(WASMExecEnv *exec_env,
return false;
}

if (func_idx < aot_module->import_func_count) {
/* Call native function */
import_func = aot_module->import_funcs + func_idx;
signature = import_func->signature;
}

if (!(func_ptr = func_ptrs[func_idx])) {
bh_assert(func_idx < aot_module->import_func_count);
import_func = aot_module->import_funcs + func_idx;
Expand All @@ -895,8 +899,20 @@ aot_call_indirect(WASMExecEnv *exec_env,
return false;
}

if (func_idx < aot_module->import_func_count) {
/* Call native function */
import_func = aot_module->import_funcs + func_idx;
signature = import_func->signature;
if (import_func->call_conv_raw) {
attachment = import_func->attachment;
return wasm_runtime_invoke_native_raw(exec_env, func_ptr,
func_type, signature, attachment,
frame_lp, argc, argv_ret);
}
}

return wasm_runtime_invoke_native(exec_env, func_ptr,
func_type, signature,
func_type, signature, attachment,
frame_lp, argc, argv_ret);
}

5 changes: 5 additions & 0 deletions core/iwasm/common/wasm_exec_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ typedef struct WASMExecEnv {
uint32 *argv_buf;
#endif

/* attachment for native function */
void *attachment;

void *user_data;

/* Current interpreter frame of current thread */
struct WASMInterpFrame *cur_frame;

Expand Down
42 changes: 33 additions & 9 deletions core/iwasm/common/wasm_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)

static void *
lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
const char *symbol, const char **p_signature)
const char *symbol, const char **p_signature, void **p_attachment)
{
int low = 0, mid, ret;
int high = n_native_symbols - 1;
Expand All @@ -120,6 +120,7 @@ lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
ret = strcmp(symbol, native_symbols[mid].symbol);
if (ret == 0) {
*p_signature = native_symbols[mid].signature;
*p_attachment = native_symbols[mid].attachment;
return native_symbols[mid].func_ptr;
}
else if (ret < 0)
Expand All @@ -133,23 +134,25 @@ lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,

void*
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMType *func_type, const char **p_signature)
const WASMType *func_type, const char **p_signature,
void **p_attachment, bool *p_call_conv_raw)
{
NativeSymbolsNode *node, *node_next;
const char *signature = NULL;
void *func_ptr = NULL;
void *func_ptr = NULL, *attachment;

node = g_native_symbols_list;
while (node) {
node_next = node->next;
if (!strcmp(node->module_name, module_name)) {
if ((func_ptr = lookup_symbol(node->native_symbols,
node->n_native_symbols,
field_name, &signature))
field_name, &signature, &attachment))
|| (field_name[0] == '_'
&& (func_ptr = lookup_symbol(node->native_symbols,
node->n_native_symbols,
field_name + 1, &signature))))
field_name + 1,
&signature, &attachment))))
break;
}
node = node_next;
Expand All @@ -172,15 +175,19 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name,
else
/* signature is empty */
*p_signature = NULL;

*p_attachment = attachment;
*p_call_conv_raw = node->call_conv_raw;
}

return func_ptr;
}

bool
wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
static bool
register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols,
bool call_conv_raw)
{
NativeSymbolsNode *node;

Expand All @@ -190,6 +197,7 @@ wasm_native_register_natives(const char *module_name,
node->module_name = module_name;
node->native_symbols = native_symbols;
node->n_native_symbols = n_native_symbols;
node->call_conv_raw = call_conv_raw;
node->next = NULL;

if (g_native_symbols_list_end) {
Expand All @@ -204,6 +212,22 @@ wasm_native_register_natives(const char *module_name,
return true;
}

bool
wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return register_natives(module_name, native_symbols, n_native_symbols, false);
}

bool
wasm_native_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols)
{
return register_natives(module_name, native_symbols, n_native_symbols, true);
}

bool
wasm_native_init()
{
Expand Down
9 changes: 8 additions & 1 deletion core/iwasm/common/wasm_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct NativeSymbolsNode {
const char *module_name;
NativeSymbol *native_symbols;
uint32 n_native_symbols;
bool call_conv_raw;
} NativeSymbolsNode, *NativeSymbolsList;

/**
Expand Down Expand Up @@ -50,13 +51,19 @@ wasm_native_lookup_libc_builtin_global(const char *module_name,
*/
void*
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
const WASMType *func_type, const char **p_signature);
const WASMType *func_type, const char **p_signature,
void **p_attachment, bool *p_call_conv_raw);

bool
wasm_native_register_natives(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols);

bool
wasm_native_register_natives_raw(const char *module_name,
NativeSymbol *native_symbols,
uint32 n_native_symbols);

bool
wasm_native_init();

Expand Down
Loading