@@ -1058,10 +1058,10 @@ namespace jsb
10581058 JavaScriptModule* Environment::_load_module (const String& p_parent_id, const String& p_module_id)
10591059 {
10601060 JSB_BENCHMARK_SCOPE (JSRealm, _load_module);
1061- JavaScriptModule* existing_module = module_cache_.find (p_module_id);
1062- if (existing_module && !existing_module ->is_reloading ())
1061+ JavaScriptModule* resolved_module = module_cache_.find (p_module_id);
1062+ if (resolved_module && !resolved_module ->is_reloading ())
10631063 {
1064- return existing_module ;
1064+ return resolved_module ;
10651065 }
10661066
10671067 v8::Isolate* isolate = this ->isolate_ ;
@@ -1071,7 +1071,7 @@ namespace jsb
10711071 // find loader with the module id
10721072 if (IModuleLoader* loader = this ->find_module_loader (p_module_id))
10731073 {
1074- jsb_checkf (!existing_module , " module loader does not support reloading" );
1074+ jsb_checkf (!resolved_module , " module loader does not support reloading" );
10751075 JavaScriptModule& module = module_cache_.insert (isolate, context, p_module_id, false , false );
10761076
10771077 // NOTE the loader should throw error if failed
@@ -1107,35 +1107,37 @@ namespace jsb
11071107 const StringName module_id = source_info.source_filepath ;
11081108
11091109 // check again with the resolved module_id
1110- existing_module = module_cache_.find (module_id);
1111- if (existing_module && !existing_module->is_reloading ())
1112- {
1113- return existing_module;
1114- }
1110+ resolved_module = module_cache_.find (module_id);
1111+
1112+ v8::Local<v8::Object> module_obj;
11151113
11161114 // supported module properties: id, filename, cache, loaded, exports, children
1117- if (existing_module )
1115+ if (resolved_module )
11181116 {
1119- jsb_check (existing_module->id == module_id);
1120- jsb_check (existing_module->source_info .source_filepath == source_info.source_filepath );
1121-
1122- JSB_LOG (VeryVerbose, " reload module %s" , module_id);
1123- existing_module->mark_as_reloaded ();
1124- if (!resolver->load (this , source_info.source_filepath , *existing_module))
1117+ if (resolved_module->is_reloading ())
11251118 {
1126- return nullptr ;
1119+ jsb_check (resolved_module->id == module_id);
1120+ jsb_check (resolved_module->source_info .source_filepath == source_info.source_filepath );
1121+
1122+ JSB_LOG (VeryVerbose, " reload module %s" , module_id);
1123+ resolved_module->mark_as_reloaded ();
1124+ if (!resolver->load (this , source_info.source_filepath , *resolved_module))
1125+ {
1126+ return nullptr ;
1127+ }
1128+ ScriptClassInfo::_parse_script_class (context, *resolved_module);
11271129 }
1128- ScriptClassInfo::_parse_script_class (context, *existing_module);
1129- return existing_module ;
1130+
1131+ module_obj = resolved_module-> module . Get (isolate) ;
11301132 }
11311133 else
11321134 {
11331135 JSB_LOG (Verbose, " instantiating module %s" , module_id);
11341136 JavaScriptModule& module = module_cache_.insert (isolate, context, module_id, true , false );
11351137 v8::Local<v8::Object> exports_obj = v8::Object::New (isolate);
1136- v8::Local<v8::Object> module_obj = module .module .Get (isolate);
11371138
11381139 // init the new module obj
1140+ module_obj = module .module .Get (isolate);
11391141 module_obj->Set (context, jsb_name (this , children), v8::Array::New (isolate)).Check ();
11401142 module_obj->Set (context, jsb_name (this , exports), exports_obj).Check ();
11411143 module .source_info = source_info;
@@ -1148,29 +1150,6 @@ namespace jsb
11481150 return nullptr ;
11491151 }
11501152
1151- // build the module tree
1152- if (!p_parent_id.is_empty ())
1153- {
1154- if (const JavaScriptModule* parent_ptr = module_cache_.find (p_parent_id))
1155- {
1156- const v8::Local<v8::Object> parent_module = parent_ptr->module .Get (isolate);
1157- if (v8::Local<v8::Value> temp; parent_module->Get (context, jsb_name (this , children)).ToLocal (&temp) && temp->IsArray ())
1158- {
1159- const v8::Local<v8::Array> children = temp.As <v8::Array>();
1160- const uint32_t children_num = children->Length ();
1161- children->Set (context, children_num, module_obj).Check ();
1162- }
1163- else
1164- {
1165- JSB_LOG (Error, " can not access children on '%s'" , p_parent_id);
1166- }
1167- }
1168- else
1169- {
1170- JSB_LOG (Warning, " parent module not found with the name '%s'" , p_parent_id);
1171- }
1172- }
1173-
11741153 module .on_load (isolate, context);
11751154 {
11761155 const impl::TryCatch try_catch_run (isolate);
@@ -1180,8 +1159,34 @@ namespace jsb
11801159 JSB_LOG (Error, " something wrong when parsing '%s'\n %s" , module_id, BridgeHelper::get_exception (try_catch_run));
11811160 }
11821161 }
1183- return &module ;
1162+
1163+ resolved_module = &module ;
1164+ }
1165+
1166+ // build the module tree
1167+ if (!p_parent_id.is_empty ())
1168+ {
1169+ if (const JavaScriptModule* parent_ptr = module_cache_.find (p_parent_id))
1170+ {
1171+ const v8::Local<v8::Object> parent_module = parent_ptr->module .Get (isolate);
1172+ if (v8::Local<v8::Value> temp; parent_module->Get (context, jsb_name (this , children)).ToLocal (&temp) && temp->IsArray ())
1173+ {
1174+ const v8::Local<v8::Array> children = temp.As <v8::Array>();
1175+ const uint32_t children_num = children->Length ();
1176+ children->Set (context, children_num, module_obj).Check ();
1177+ }
1178+ else
1179+ {
1180+ JSB_LOG (Error, " can not access children on '%s'" , p_parent_id);
1181+ }
1182+ }
1183+ else
1184+ {
1185+ JSB_LOG (Warning, " parent module not found with the name '%s'" , p_parent_id);
1186+ }
11841187 }
1188+
1189+ return resolved_module;
11851190 }
11861191
11871192 impl::Helper::throw_error (isolate, jsb_format (" unknown module: %s" , normalized_id));
0 commit comments