@@ -811,7 +811,7 @@ aot_is_wasm_type_equal(AOTModuleInstance *module_inst,
811811 return wasm_type_equal (type1 , type2 );
812812}
813813
814- void
814+ bool
815815aot_invoke_native (WASMExecEnv * exec_env , uint32 func_idx ,
816816 uint32 * frame_lp , uint32 argc , uint32 * argv_ret )
817817{
@@ -827,18 +827,76 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
827827 const char * signature = NULL ;
828828 char buf [128 ];
829829
830+ bh_assert (func_idx < aot_module -> import_func_count );
831+
832+ import_func = aot_module -> import_funcs + func_idx ;
833+ if (!func_ptr ) {
834+ snprintf (buf , sizeof (buf ),
835+ "fail to call unlinked import function (%s, %s)" ,
836+ import_func -> module_name , import_func -> func_name );
837+ aot_set_exception (module_inst , buf );
838+ return false;
839+ }
840+
841+ signature = import_func -> signature ;
842+ return wasm_runtime_invoke_native (exec_env , func_ptr ,
843+ func_type , signature ,
844+ frame_lp , argc , argv_ret );
845+ }
846+
847+ bool
848+ aot_call_indirect (WASMExecEnv * exec_env ,
849+ uint32 func_type_idx , uint32 table_elem_idx ,
850+ uint32 * frame_lp , uint32 argc , uint32 * argv_ret )
851+ {
852+ AOTModuleInstance * module_inst = (AOTModuleInstance * )
853+ wasm_runtime_get_module_inst (exec_env );
854+ AOTModule * aot_module = (AOTModule * )module_inst -> aot_module .ptr ;
855+ uint32 * func_type_indexes = (uint32 * )module_inst -> func_type_indexes .ptr ;
856+ uint32 * table_data = (uint32 * )module_inst -> table_data .ptr ;
857+ AOTFuncType * func_type = aot_module -> func_types [func_type_idx ];;
858+ void * * func_ptrs = (void * * )module_inst -> func_ptrs .ptr , * func_ptr ;
859+ uint32 table_size = module_inst -> table_size ;
860+ uint32 func_idx , func_type_idx1 ;
861+ AOTImportFunc * import_func ;
862+ const char * signature = NULL ;
863+ char buf [128 ];
864+
865+ if (table_elem_idx >= table_size ) {
866+ aot_set_exception_with_id (module_inst , EXCE_UNDEFINED_ELEMENT );
867+ return false;
868+ }
869+
870+ func_idx = table_data [table_elem_idx ];
871+ if (func_idx == (uint32 )- 1 ) {
872+ aot_set_exception_with_id (module_inst , EXCE_UNINITIALIZED_ELEMENT );
873+ return false;
874+ }
875+
876+ func_type_idx1 = func_type_indexes [func_idx ];
877+ if (!aot_is_wasm_type_equal (module_inst , func_type_idx , func_type_idx1 )) {
878+ aot_set_exception_with_id (module_inst , EXCE_INVALID_FUNCTION_TYPE_INDEX );
879+ return false;
880+ }
881+
830882 if (func_idx < aot_module -> import_func_count ) {
883+ /* Call native function */
831884 import_func = aot_module -> import_funcs + func_idx ;
832- if (!func_ptr ) {
833- snprintf (buf , sizeof (buf ),
834- "fail to call unlinked import function (%s, %s)" ,
835- import_func -> module_name , import_func -> func_name );
836- aot_set_exception (module_inst , buf );
837- return ;
838- }
839885 signature = import_func -> signature ;
840886 }
841- wasm_runtime_invoke_native (exec_env , func_ptr ,
842- func_type , signature , frame_lp , argc , argv_ret );
887+
888+ if (!(func_ptr = func_ptrs [func_idx ])) {
889+ bh_assert (func_idx < aot_module -> import_func_count );
890+ import_func = aot_module -> import_funcs + func_idx ;
891+ snprintf (buf , sizeof (buf ),
892+ "fail to call unlinked import function (%s, %s)" ,
893+ import_func -> module_name , import_func -> func_name );
894+ aot_set_exception (module_inst , buf );
895+ return false;
896+ }
897+
898+ return wasm_runtime_invoke_native (exec_env , func_ptr ,
899+ func_type , signature ,
900+ frame_lp , argc , argv_ret );
843901}
844902
0 commit comments