Skip to content

Commit c1a0e6d

Browse files
authored
Implement register/call native API with raw (unextracted) arguments (#222)
1 parent d9890d2 commit c1a0e6d

File tree

17 files changed

+344
-41
lines changed

17 files changed

+344
-41
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,9 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end,
786786
if (!(import_funcs[i].func_ptr_linked =
787787
wasm_native_resolve_symbol(module_name, field_name,
788788
import_funcs[i].func_type,
789-
&import_funcs[i].signature))) {
789+
&import_funcs[i].signature,
790+
&import_funcs[i].attachment,
791+
&import_funcs[i].call_conv_raw))) {
790792
LOG_WARNING("warning: fail to link import function (%s, %s)\n",
791793
module_name, field_name);
792794
}

core/iwasm/aot/aot_runtime.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ aot_call_function(WASMExecEnv *exec_env,
457457
AOTModuleInstance *module_inst = (AOTModuleInstance*)exec_env->module_inst;
458458
AOTFuncType *func_type = function->func_type;
459459
bool ret = wasm_runtime_invoke_native(exec_env, function->func_ptr,
460-
func_type, NULL, argv, argc, argv);
460+
func_type, NULL, NULL, argv, argc, argv);
461461
return ret && !aot_get_exception(module_inst) ? true : false;
462462
}
463463

@@ -824,7 +824,8 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
824824
void **func_ptrs = (void**)module_inst->func_ptrs.ptr;
825825
void *func_ptr = func_ptrs[func_idx];
826826
AOTImportFunc *import_func;
827-
const char *signature = NULL;
827+
const char *signature;
828+
void *attachment;
828829
char buf[128];
829830

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

841842
signature = import_func->signature;
842-
return wasm_runtime_invoke_native(exec_env, func_ptr,
843-
func_type, signature,
844-
frame_lp, argc, argv_ret);
843+
attachment = import_func->attachment;
844+
if (!import_func->call_conv_raw) {
845+
return wasm_runtime_invoke_native(exec_env, func_ptr,
846+
func_type, signature, attachment,
847+
frame_lp, argc, argv_ret);
848+
}
849+
else {
850+
return wasm_runtime_invoke_native_raw(exec_env, func_ptr,
851+
func_type, signature, attachment,
852+
frame_lp, argc, argv_ret);
853+
}
845854
}
846855

847856
bool
@@ -860,6 +869,7 @@ aot_call_indirect(WASMExecEnv *exec_env,
860869
uint32 func_idx, func_type_idx1;
861870
AOTImportFunc *import_func;
862871
const char *signature = NULL;
872+
void *attachment = NULL;
863873
char buf[128];
864874

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

882-
if (func_idx < aot_module->import_func_count) {
883-
/* Call native function */
884-
import_func = aot_module->import_funcs + func_idx;
885-
signature = import_func->signature;
886-
}
887-
888892
if (!(func_ptr = func_ptrs[func_idx])) {
889893
bh_assert(func_idx < aot_module->import_func_count);
890894
import_func = aot_module->import_funcs + func_idx;
@@ -895,8 +899,20 @@ aot_call_indirect(WASMExecEnv *exec_env,
895899
return false;
896900
}
897901

902+
if (func_idx < aot_module->import_func_count) {
903+
/* Call native function */
904+
import_func = aot_module->import_funcs + func_idx;
905+
signature = import_func->signature;
906+
if (import_func->call_conv_raw) {
907+
attachment = import_func->attachment;
908+
return wasm_runtime_invoke_native_raw(exec_env, func_ptr,
909+
func_type, signature, attachment,
910+
frame_lp, argc, argv_ret);
911+
}
912+
}
913+
898914
return wasm_runtime_invoke_native(exec_env, func_ptr,
899-
func_type, signature,
915+
func_type, signature, attachment,
900916
frame_lp, argc, argv_ret);
901917
}
902918

core/iwasm/common/wasm_exec_env.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ typedef struct WASMExecEnv {
3333
uint32 *argv_buf;
3434
#endif
3535

36+
/* attachment for native function */
37+
void *attachment;
38+
39+
void *user_data;
40+
3641
/* Current interpreter frame of current thread */
3742
struct WASMInterpFrame *cur_frame;
3843

core/iwasm/common/wasm_native.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
110110

111111
static void *
112112
lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
113-
const char *symbol, const char **p_signature)
113+
const char *symbol, const char **p_signature, void **p_attachment)
114114
{
115115
int low = 0, mid, ret;
116116
int high = n_native_symbols - 1;
@@ -120,6 +120,7 @@ lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
120120
ret = strcmp(symbol, native_symbols[mid].symbol);
121121
if (ret == 0) {
122122
*p_signature = native_symbols[mid].signature;
123+
*p_attachment = native_symbols[mid].attachment;
123124
return native_symbols[mid].func_ptr;
124125
}
125126
else if (ret < 0)
@@ -133,23 +134,25 @@ lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
133134

134135
void*
135136
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
136-
const WASMType *func_type, const char **p_signature)
137+
const WASMType *func_type, const char **p_signature,
138+
void **p_attachment, bool *p_call_conv_raw)
137139
{
138140
NativeSymbolsNode *node, *node_next;
139141
const char *signature = NULL;
140-
void *func_ptr = NULL;
142+
void *func_ptr = NULL, *attachment;
141143

142144
node = g_native_symbols_list;
143145
while (node) {
144146
node_next = node->next;
145147
if (!strcmp(node->module_name, module_name)) {
146148
if ((func_ptr = lookup_symbol(node->native_symbols,
147149
node->n_native_symbols,
148-
field_name, &signature))
150+
field_name, &signature, &attachment))
149151
|| (field_name[0] == '_'
150152
&& (func_ptr = lookup_symbol(node->native_symbols,
151153
node->n_native_symbols,
152-
field_name + 1, &signature))))
154+
field_name + 1,
155+
&signature, &attachment))))
153156
break;
154157
}
155158
node = node_next;
@@ -172,15 +175,19 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name,
172175
else
173176
/* signature is empty */
174177
*p_signature = NULL;
178+
179+
*p_attachment = attachment;
180+
*p_call_conv_raw = node->call_conv_raw;
175181
}
176182

177183
return func_ptr;
178184
}
179185

180-
bool
181-
wasm_native_register_natives(const char *module_name,
182-
NativeSymbol *native_symbols,
183-
uint32 n_native_symbols)
186+
static bool
187+
register_natives(const char *module_name,
188+
NativeSymbol *native_symbols,
189+
uint32 n_native_symbols,
190+
bool call_conv_raw)
184191
{
185192
NativeSymbolsNode *node;
186193

@@ -190,6 +197,7 @@ wasm_native_register_natives(const char *module_name,
190197
node->module_name = module_name;
191198
node->native_symbols = native_symbols;
192199
node->n_native_symbols = n_native_symbols;
200+
node->call_conv_raw = call_conv_raw;
193201
node->next = NULL;
194202

195203
if (g_native_symbols_list_end) {
@@ -204,6 +212,22 @@ wasm_native_register_natives(const char *module_name,
204212
return true;
205213
}
206214

215+
bool
216+
wasm_native_register_natives(const char *module_name,
217+
NativeSymbol *native_symbols,
218+
uint32 n_native_symbols)
219+
{
220+
return register_natives(module_name, native_symbols, n_native_symbols, false);
221+
}
222+
223+
bool
224+
wasm_native_register_natives_raw(const char *module_name,
225+
NativeSymbol *native_symbols,
226+
uint32 n_native_symbols)
227+
{
228+
return register_natives(module_name, native_symbols, n_native_symbols, true);
229+
}
230+
207231
bool
208232
wasm_native_init()
209233
{

core/iwasm/common/wasm_native.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct NativeSymbolsNode {
1919
const char *module_name;
2020
NativeSymbol *native_symbols;
2121
uint32 n_native_symbols;
22+
bool call_conv_raw;
2223
} NativeSymbolsNode, *NativeSymbolsList;
2324

2425
/**
@@ -50,13 +51,19 @@ wasm_native_lookup_libc_builtin_global(const char *module_name,
5051
*/
5152
void*
5253
wasm_native_resolve_symbol(const char *module_name, const char *field_name,
53-
const WASMType *func_type, const char **p_signature);
54+
const WASMType *func_type, const char **p_signature,
55+
void **p_attachment, bool *p_call_conv_raw);
5456

5557
bool
5658
wasm_native_register_natives(const char *module_name,
5759
NativeSymbol *native_symbols,
5860
uint32 n_native_symbols);
5961

62+
bool
63+
wasm_native_register_natives_raw(const char *module_name,
64+
NativeSymbol *native_symbols,
65+
uint32 n_native_symbols);
66+
6067
bool
6168
wasm_native_init();
6269

0 commit comments

Comments
 (0)