@@ -618,23 +618,23 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
618618 for (i = 0 ; i != module_inst -> table_count ; ++ i ) {
619619 if (i < module -> import_table_count ) {
620620 AOTImportTable * import_table = module -> import_tables + i ;
621- tbl_inst -> cur_size = import_table -> table_init_size ;
621+ tbl_inst -> cur_size = import_table -> table_type . init_size ;
622622 tbl_inst -> max_size =
623623 aot_get_imp_tbl_data_slots (import_table , false);
624+ tbl_inst -> elem_type = module -> tables [i ].table_type .elem_type ;
624625#if WASM_ENABLE_GC != 0
625- tbl_inst -> elem_type = module -> tables [i ].elem_type ;
626626 tbl_inst -> elem_ref_type .elem_ref_type =
627- module -> tables [i ].elem_ref_type ;
627+ module -> tables [i ].table_type . elem_ref_type ;
628628#endif
629629 }
630630 else {
631631 AOTTable * table = module -> tables + (i - module -> import_table_count );
632- tbl_inst -> cur_size = table -> table_init_size ;
632+ tbl_inst -> cur_size = table -> table_type . init_size ;
633633 tbl_inst -> max_size = aot_get_tbl_data_slots (table , false);
634+ tbl_inst -> elem_type = module -> tables [i ].table_type .elem_type ;
634635#if WASM_ENABLE_GC != 0
635- tbl_inst -> elem_type = module -> tables [i ].elem_type ;
636636 tbl_inst -> elem_ref_type .elem_ref_type =
637- module -> tables [i ].elem_ref_type ;
637+ module -> tables [i ].table_type . elem_ref_type ;
638638#endif
639639 }
640640
@@ -1183,6 +1183,75 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
11831183 return true;
11841184}
11851185
1186+ AOTFunctionInstance *
1187+ aot_get_function_instance (AOTModuleInstance * module_inst , uint32 func_idx )
1188+ {
1189+ AOTModule * module = (AOTModule * )module_inst -> module ;
1190+ AOTModuleInstanceExtra * extra = (AOTModuleInstanceExtra * )module_inst -> e ;
1191+ AOTFunctionInstance * export_funcs =
1192+ (AOTFunctionInstance * )module_inst -> export_functions ;
1193+ uint32 i ;
1194+
1195+ /* export functions are pre-instantiated */
1196+ for (i = 0 ; i < module_inst -> export_func_count ; i ++ ) {
1197+ if (export_funcs [i ].func_index == func_idx )
1198+ return & export_funcs [i ];
1199+ }
1200+
1201+ exception_lock (module_inst );
1202+
1203+ /* allocate functions array if needed */
1204+ if (!extra -> functions ) {
1205+ uint64 func_count =
1206+ ((uint64 )module -> import_func_count + module -> func_count );
1207+ uint64 total_size = func_count * (uint64 )sizeof (AOTFunctionInstance * );
1208+
1209+ if ((func_count == 0 )
1210+ || !(extra -> functions = runtime_malloc (total_size , NULL , 0 ))) {
1211+ exception_unlock (module_inst );
1212+ return NULL ;
1213+ }
1214+
1215+ extra -> function_count = func_count ;
1216+ }
1217+
1218+ /* instantiate function if needed */
1219+ bh_assert (func_idx < extra -> function_count );
1220+ if (!extra -> functions [func_idx ]) {
1221+ AOTFunctionInstance * function = (AOTFunctionInstance * )runtime_malloc (
1222+ sizeof (AOTFunctionInstance ), NULL , 0 );
1223+ if (!function ) {
1224+ exception_unlock (module_inst );
1225+ return NULL ;
1226+ }
1227+
1228+ if (func_idx < module -> import_func_count ) {
1229+ /* instantiate function from import section */
1230+ function -> is_import_func = true;
1231+ function -> func_name = module -> import_funcs [func_idx ].func_name ;
1232+ function -> func_index = func_idx ;
1233+ function -> u .func_import = & module -> import_funcs [func_idx ];
1234+ }
1235+ else {
1236+ /* instantiate non-import function */
1237+ uint32 ftype_index =
1238+ module -> func_type_indexes [func_idx - module -> import_func_count ];
1239+ function -> is_import_func = false;
1240+ function -> func_index = func_idx ;
1241+ function -> u .func .func_type =
1242+ (AOTFuncType * )module -> types [ftype_index ];
1243+ function -> u .func .func_ptr =
1244+ module -> func_ptrs [func_idx - module -> import_func_count ];
1245+ }
1246+
1247+ extra -> functions [func_idx ] = function ;
1248+ }
1249+
1250+ exception_unlock (module_inst );
1251+
1252+ return extra -> functions [func_idx ];
1253+ }
1254+
11861255static bool
11871256init_func_type_indexes (AOTModuleInstance * module_inst , AOTModule * module ,
11881257 char * error_buf , uint32 error_buf_size )
@@ -1490,6 +1559,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
14901559#if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
14911560 WASMModuleInstanceExtraCommon * common ;
14921561#endif
1562+ AOTModuleInstanceExtra * extra = NULL ;
14931563 const uint32 module_inst_struct_size =
14941564 offsetof(AOTModuleInstance , global_table_data .bytes );
14951565 const uint64 module_inst_mem_inst_size =
@@ -1543,14 +1613,13 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
15431613 module_inst -> module = (void * )module ;
15441614 module_inst -> e =
15451615 (WASMModuleInstanceExtra * )((uint8 * )module_inst + extra_info_offset );
1616+ extra = (AOTModuleInstanceExtra * )module_inst -> e ;
15461617
15471618#if WASM_ENABLE_GC != 0
15481619 /* Initialize gc heap first since it may be used when initializing
15491620 globals and others */
15501621 if (!is_sub_inst ) {
15511622 uint32 gc_heap_size = wasm_runtime_get_gc_heap_size_default ();
1552- AOTModuleInstanceExtra * extra =
1553- (AOTModuleInstanceExtra * )module_inst -> e ;
15541623
15551624 if (gc_heap_size < GC_HEAP_SIZE_MIN )
15561625 gc_heap_size = GC_HEAP_SIZE_MIN ;
@@ -1570,8 +1639,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
15701639#endif
15711640
15721641#if WASM_ENABLE_MULTI_MODULE != 0
1573- ((AOTModuleInstanceExtra * )module_inst -> e )-> sub_module_inst_list =
1574- & ((AOTModuleInstanceExtra * )module_inst -> e )-> sub_module_inst_list_head ;
1642+ extra -> sub_module_inst_list = & extra -> sub_module_inst_list_head ;
15751643 ret = wasm_runtime_sub_module_instantiate (
15761644 (WASMModuleCommon * )module , (WASMModuleInstanceCommon * )module_inst ,
15771645 stack_size , heap_size , max_memory_pages , error_buf , error_buf_size );
@@ -1587,7 +1655,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
15871655 goto fail ;
15881656
15891657#if WASM_ENABLE_BULK_MEMORY != 0 || WASM_ENABLE_REF_TYPES != 0
1590- common = & (( AOTModuleInstanceExtra * ) module_inst -> e ) -> common ;
1658+ common = & extra -> common ;
15911659#endif
15921660#if WASM_ENABLE_BULK_MEMORY != 0
15931661 if (module -> mem_init_data_count > 0 ) {
@@ -1682,7 +1750,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
16821750#endif
16831751 module_inst -> default_wasm_stack_size = stack_size ;
16841752
1685- (( AOTModuleInstanceExtra * ) module_inst -> e ) -> stack_sizes =
1753+ extra -> stack_sizes =
16861754 aot_get_data_section_addr (module , AOT_STACK_SIZES_SECTION_NAME , NULL );
16871755
16881756#if WASM_ENABLE_PERF_PROFILING != 0
@@ -1885,8 +1953,8 @@ destroy_c_api_frames(Vector *frames)
18851953void
18861954aot_deinstantiate (AOTModuleInstance * module_inst , bool is_sub_inst )
18871955{
1888- WASMModuleInstanceExtraCommon * common =
1889- & (( AOTModuleInstanceExtra * ) module_inst -> e ) -> common ;
1956+ AOTModuleInstanceExtra * extra = ( AOTModuleInstanceExtra * ) module_inst -> e ;
1957+ WASMModuleInstanceExtraCommon * common = & extra -> common ;
18901958 if (module_inst -> exec_env_singleton ) {
18911959 /* wasm_exec_env_destroy will call
18921960 wasm_cluster_wait_for_all_except_self to wait for other
@@ -1923,6 +1991,16 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
19231991 if (module_inst -> export_functions )
19241992 wasm_runtime_free (module_inst -> export_functions );
19251993
1994+ if (extra -> functions ) {
1995+ uint32 func_idx ;
1996+ for (func_idx = 0 ; func_idx < extra -> function_count ; ++ func_idx ) {
1997+ if (extra -> functions [func_idx ]) {
1998+ wasm_runtime_free (extra -> functions [func_idx ]);
1999+ }
2000+ }
2001+ wasm_runtime_free (extra -> functions );
2002+ }
2003+
19262004 if (module_inst -> func_ptrs )
19272005 wasm_runtime_free (module_inst -> func_ptrs );
19282006
@@ -1934,12 +2012,10 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
19342012
19352013#if WASM_ENABLE_GC != 0
19362014 if (!is_sub_inst ) {
1937- AOTModuleInstanceExtra * extra =
1938- (AOTModuleInstanceExtra * )module_inst -> e ;
1939- if (extra -> common .gc_heap_handle )
1940- mem_allocator_destroy (extra -> common .gc_heap_handle );
1941- if (extra -> common .gc_heap_pool )
1942- wasm_runtime_free (extra -> common .gc_heap_pool );
2015+ if (common -> gc_heap_handle )
2016+ mem_allocator_destroy (common -> gc_heap_handle );
2017+ if (common -> gc_heap_pool )
2018+ wasm_runtime_free (common -> gc_heap_pool );
19432019 }
19442020#endif
19452021
@@ -2021,7 +2097,9 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr,
20212097 void (* invoke_native )(void * func_ptr , void * exec_env , uint32 * argv ,
20222098 uint32 * argv_ret ) =
20232099 func_type -> quick_aot_entry ;
2100+ exec_env -> attachment = attachment ;
20242101 invoke_native (func_ptr , exec_env , argv , argv_ret );
2102+ exec_env -> attachment = NULL ;
20252103 ret = !aot_copy_exception (module_inst , NULL );
20262104 }
20272105 else
@@ -2099,6 +2177,7 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
20992177 unsigned argc , uint32 argv [])
21002178{
21012179 AOTModuleInstance * module_inst = (AOTModuleInstance * )exec_env -> module_inst ;
2180+ AOTModule * module = (AOTModule * )module_inst -> module ;
21022181 AOTFuncType * func_type = function -> is_import_func
21032182 ? function -> u .func_import -> func_type
21042183 : function -> u .func .func_type ;
@@ -2108,6 +2187,7 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
21082187 void * func_ptr = function -> is_import_func
21092188 ? function -> u .func_import -> func_ptr_linked
21102189 : function -> u .func .func_ptr ;
2190+ void * attachment = NULL ;
21112191#if WASM_ENABLE_MULTI_MODULE != 0
21122192 bh_list * sub_module_list_node = NULL ;
21132193 const char * sub_inst_name = NULL ;
@@ -2167,6 +2247,10 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
21672247 hw bound check is enabled */
21682248#endif
21692249
2250+ if (function -> func_index < module -> import_func_count ) {
2251+ attachment = function -> u .func_import -> attachment ;
2252+ }
2253+
21702254 /* Set exec env, so it can be later retrieved from instance */
21712255 module_inst -> cur_exec_env = exec_env ;
21722256
@@ -2217,7 +2301,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
22172301#endif
22182302
22192303 ret = invoke_native_internal (exec_env , function -> u .func .func_ptr ,
2220- func_type , NULL , NULL , argv1 , argc , argv );
2304+ func_type , NULL , attachment , argv1 , argc ,
2305+ argv );
22212306
22222307 if (!ret ) {
22232308#ifdef AOT_STACK_FRAME_DEBUG
@@ -2286,8 +2371,8 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
22862371 }
22872372#endif
22882373
2289- ret = invoke_native_internal (exec_env , func_ptr , func_type , NULL , NULL ,
2290- argv , argc , argv );
2374+ ret = invoke_native_internal (exec_env , func_ptr , func_type , NULL ,
2375+ attachment , argv , argc , argv );
22912376
22922377 if (aot_copy_exception (module_inst , NULL )) {
22932378#ifdef AOT_STACK_FRAME_DEBUG
@@ -2888,8 +2973,8 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
28882973 /* Call native function */
28892974 import_func = aot_module -> import_funcs + func_idx ;
28902975 signature = import_func -> signature ;
2976+ attachment = import_func -> attachment ;
28912977 if (import_func -> call_conv_raw ) {
2892- attachment = import_func -> attachment ;
28932978 ret = wasm_runtime_invoke_native_raw (exec_env , func_ptr , func_type ,
28942979 signature , attachment , argv ,
28952980 argc , argv );
0 commit comments