Skip to content

Commit 37b09d0

Browse files
authored
Expose wasm_runtime_call_indirect (bytecodealliance#1969)
The function has been there for long. While what it does look a bit unsafe as it calls a function which may be not wasm-wise exported explicitly, it's useful and widely used when implementing callback-taking APIs, including our pthread_create's implementation.
1 parent e516de8 commit 37b09d0

File tree

6 files changed

+33
-36
lines changed

6 files changed

+33
-36
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,7 +4346,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
43464346
|| defined(BUILD_TARGET_RISCV64_LP64) */
43474347

43484348
bool
4349-
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
4349+
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
43504350
uint32 argc, uint32 argv[])
43514351
{
43524352
bool ret = false;
@@ -4362,11 +4362,11 @@ wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
43624362

43634363
#if WASM_ENABLE_INTERP != 0
43644364
if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
4365-
ret = wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
4365+
ret = wasm_call_indirect(exec_env, 0, element_index, argc, argv);
43664366
#endif
43674367
#if WASM_ENABLE_AOT != 0
43684368
if (exec_env->module_inst->module_type == Wasm_Module_AoT)
4369-
ret = aot_call_indirect(exec_env, 0, element_indices, argc, argv);
4369+
ret = aot_call_indirect(exec_env, 0, element_index, argc, argv);
43704370
#endif
43714371

43724372
if (!ret && clear_wasi_proc_exit_exception(exec_env->module_inst)) {

core/iwasm/common/wasm_runtime_common.h

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
615615
uint32 num_results, wasm_val_t *results,
616616
uint32 num_args, ...);
617617

618+
/* See wasm_export.h for description */
619+
WASM_RUNTIME_API_EXTERN bool
620+
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
621+
uint32 argc, uint32 argv[]);
622+
618623
#if WASM_ENABLE_DEBUG_INTERP != 0
619624
/* See wasm_export.h for description */
620625
WASM_RUNTIME_API_EXTERN uint32
@@ -626,27 +631,6 @@ WASM_RUNTIME_API_EXTERN uint32
626631
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
627632
#endif
628633

629-
/**
630-
* Call a function reference of a given WASM runtime instance with
631-
* arguments.
632-
*
633-
* @param exec_env the execution environment to call the function
634-
* which must be created from wasm_create_exec_env()
635-
* @param element_indices the function ference indicies, usually
636-
* prvovided by the caller of a registed native function
637-
* @param argc the number of arguments
638-
* @param argv the arguments. If the function method has return value,
639-
* the first (or first two in case 64-bit return value) element of
640-
* argv stores the return value of the called WASM function after this
641-
* function returns.
642-
*
643-
* @return true if success, false otherwise and exception will be thrown,
644-
* the caller can call wasm_runtime_get_exception to get exception info.
645-
*/
646-
bool
647-
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_indices,
648-
uint32 argc, uint32 argv[]);
649-
650634
bool
651635
wasm_runtime_create_exec_env_singleton(WASMModuleInstanceCommon *module_inst);
652636

core/iwasm/include/wasm_export.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,31 @@ wasm_runtime_call_wasm_v(wasm_exec_env_t exec_env,
800800
uint32_t num_results, wasm_val_t results[],
801801
uint32_t num_args, ...);
802802

803+
/**
804+
* Call a function reference of a given WASM runtime instance with
805+
* arguments.
806+
*
807+
* Note: this can be used to call a function which is not exported
808+
* by the module explicitly. You might consider it as an abstraction
809+
* violation.
810+
*
811+
* @param exec_env the execution environment to call the function
812+
* which must be created from wasm_create_exec_env()
813+
* @param element_index the function reference index, usually
814+
* prvovided by the caller of a registed native function
815+
* @param argc the number of arguments
816+
* @param argv the arguments. If the function method has return value,
817+
* the first (or first two in case 64-bit return value) element of
818+
* argv stores the return value of the called WASM function after this
819+
* function returns.
820+
*
821+
* @return true if success, false otherwise and exception will be thrown,
822+
* the caller can call wasm_runtime_get_exception to get exception info.
823+
*/
824+
WASM_RUNTIME_API_EXTERN bool
825+
wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_index,
826+
uint32_t argc, uint32_t argv[]);
827+
803828
/**
804829
* Find the unique main function from a WASM module instance
805830
* and execute that function.

core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
wasm_runtime_addr_native_to_app(module_inst, ptr)
4747
/* clang-format on */
4848

49-
extern bool
50-
wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32 element_indices,
51-
uint32 argc, uint32 argv[]);
52-
5349
enum {
5450
T_THREAD,
5551
T_MUTEX,

core/iwasm/libraries/libc-emcc/libc_emcc_wrapper.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
wasm_runtime_module_free(module_inst, offset)
3838
/* clang-format on */
3939

40-
extern bool
41-
wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32 element_idx,
42-
uint32 argc, uint32 argv[]);
43-
4440
static void
4541
invoke_viiii_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0,
4642
int arg1, int arg2, int arg3)

samples/basic/src/native_impl.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include "wasm_export.h"
88
#include "math.h"
99

10-
extern bool
11-
wasm_runtime_call_indirect(wasm_exec_env_t exec_env, uint32_t element_indices,
12-
uint32_t argc, uint32_t argv[]);
13-
1410
// The first parameter is not exec_env because it is invoked by native funtions
1511
void
1612
reverse(char *str, int len)

0 commit comments

Comments
 (0)