@@ -4282,15 +4282,13 @@ interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
42824282 return false;
42834283
42844284 imported_func_interp -> u .function .call_conv_wasm_c_api = true;
4285- imported_func_interp -> u .function .wasm_c_api_with_env = import -> with_env ;
4286- if (import -> with_env ) {
4285+ /* only set func_ptr_linked to avoid unlink warning during instantiation,
4286+ func_ptr_linked, with_env and env will be stored in module instance's
4287+ c_api_func_imports later and used when calling import function */
4288+ if (import -> with_env )
42874289 imported_func_interp -> u .function .func_ptr_linked = import -> u .cb_env .cb ;
4288- imported_func_interp -> u .function .attachment = import -> u .cb_env .env ;
4289- }
4290- else {
4290+ else
42914291 imported_func_interp -> u .function .func_ptr_linked = import -> u .cb ;
4292- imported_func_interp -> u .function .attachment = NULL ;
4293- }
42944292 import -> func_idx_rt = func_idx_rt ;
42954293
42964294 return true;
@@ -4496,15 +4494,13 @@ aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
44964494 return false;
44974495
44984496 import_aot_func -> call_conv_wasm_c_api = true;
4499- import_aot_func -> wasm_c_api_with_env = import -> with_env ;
4500- if (import -> with_env ) {
4497+ /* only set func_ptr_linked to avoid unlink warning during instantiation,
4498+ func_ptr_linked, with_env and env will be stored in module instance's
4499+ c_api_func_imports later and used when calling import function */
4500+ if (import -> with_env )
45014501 import_aot_func -> func_ptr_linked = import -> u .cb_env .cb ;
4502- import_aot_func -> attachment = import -> u .cb_env .env ;
4503- }
4504- else {
4502+ else
45054503 import_aot_func -> func_ptr_linked = import -> u .cb ;
4506- import_aot_func -> attachment = NULL ;
4507- }
45084504 import -> func_idx_rt = import_func_idx_rt ;
45094505
45104506 return true;
@@ -4718,10 +4714,12 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
47184714{
47194715 char sub_error_buf [128 ] = { 0 };
47204716 char error_buf [256 ] = { 0 };
4721- uint32 import_count = 0 ;
47224717 bool import_count_verified = false;
47234718 wasm_instance_t * instance = NULL ;
4724- uint32 i = 0 ;
4719+ WASMModuleInstance * inst_rt ;
4720+ CApiFuncImport * func_import = NULL , * * p_func_imports = NULL ;
4721+ uint32 i = 0 , import_count = 0 , import_func_count = 0 ;
4722+ uint64 total_size ;
47254723 bool processed = false;
47264724
47274725 bh_assert (singleton_engine );
@@ -4804,6 +4802,56 @@ wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
48044802 goto failed ;
48054803 }
48064804
4805+ inst_rt = (WASMModuleInstance * )instance -> inst_comm_rt ;
4806+ #if WASM_ENABLE_INTERP != 0
4807+ if (instance -> inst_comm_rt -> module_type == Wasm_Module_Bytecode ) {
4808+ p_func_imports = & inst_rt -> e -> c_api_func_imports ;
4809+ import_func_count = inst_rt -> module -> import_function_count ;
4810+ }
4811+ #endif
4812+ #if WASM_ENABLE_AOT != 0
4813+ if (instance -> inst_comm_rt -> module_type == Wasm_Module_AoT ) {
4814+ p_func_imports =
4815+ & ((AOTModuleInstanceExtra * )inst_rt -> e )-> c_api_func_imports ;
4816+ import_func_count = ((AOTModule * )inst_rt -> module )-> import_func_count ;
4817+ }
4818+ #endif
4819+ bh_assert (p_func_imports );
4820+
4821+ /* create the c-api func import list */
4822+ total_size = (uint64 )sizeof (CApiFuncImport ) * import_func_count ;
4823+ if (total_size > 0
4824+ && !(* p_func_imports = func_import = malloc_internal (total_size ))) {
4825+ snprintf (sub_error_buf , sizeof (sub_error_buf ),
4826+ "Failed to create wasm-c-api func imports" );
4827+ goto failed ;
4828+ }
4829+
4830+ /* fill in c-api func import list */
4831+ for (i = 0 ; i < import_count ; i ++ ) {
4832+ wasm_func_t * func_host ;
4833+ wasm_extern_t * in ;
4834+
4835+ in = imports -> data [i ];
4836+ if (wasm_extern_kind (in ) != WASM_EXTERN_FUNC )
4837+ continue ;
4838+
4839+ func_host = wasm_extern_as_func (in );
4840+
4841+ func_import -> with_env_arg = func_host -> with_env ;
4842+ if (func_host -> with_env ) {
4843+ func_import -> func_ptr_linked = func_host -> u .cb_env .cb ;
4844+ func_import -> env_arg = func_host -> u .cb_env .env ;
4845+ }
4846+ else {
4847+ func_import -> func_ptr_linked = func_host -> u .cb ;
4848+ func_import -> env_arg = NULL ;
4849+ }
4850+
4851+ func_import ++ ;
4852+ }
4853+ bh_assert ((uint32 )(func_import - * p_func_imports ) == import_func_count );
4854+
48074855 /* fill with inst */
48084856 for (i = 0 ; imports && imports -> data && i < (uint32 )import_count ; ++ i ) {
48094857 wasm_extern_t * import = imports -> data [i ];
0 commit comments