@@ -37,6 +37,7 @@ using v8::ScriptCompiler;
3737using v8::ScriptOrigin;
3838using v8::String;
3939using v8::TryCatch;
40+ using v8::Undefined;
4041using v8::Value;
4142
4243static const char * const EXTENSIONS[] = {" .mjs" , " .js" , " .json" , " .node" };
@@ -64,6 +65,19 @@ ModuleWrap::~ModuleWrap() {
6465 context_.Reset ();
6566}
6667
68+ ModuleWrap* ModuleWrap::GetFromModule (Environment* env,
69+ Local<Module> module ) {
70+ ModuleWrap* ret = nullptr ;
71+ auto range = env->module_map .equal_range (module ->GetIdentityHash ());
72+ for (auto it = range.first ; it != range.second ; ++it) {
73+ if (it->second ->module_ == module ) {
74+ ret = it->second ;
75+ break ;
76+ }
77+ }
78+ return ret;
79+ }
80+
6781void ModuleWrap::New (const FunctionCallbackInfo<Value>& args) {
6882 Environment* env = Environment::GetCurrent (args);
6983
@@ -133,9 +147,7 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
133147 }
134148 }
135149
136- Local<String> url_str = FIXED_ONE_BYTE_STRING (isolate, " url" );
137-
138- if (!that->Set (context, url_str, url).FromMaybe (false )) {
150+ if (!that->Set (context, env->url_string (), url).FromMaybe (false )) {
139151 return ;
140152 }
141153
@@ -361,14 +373,7 @@ MaybeLocal<Module> ModuleWrap::ResolveCallback(Local<Context> context,
361373 return MaybeLocal<Module>();
362374 }
363375
364- ModuleWrap* dependent = nullptr ;
365- auto range = env->module_map .equal_range (referrer->GetIdentityHash ());
366- for (auto it = range.first ; it != range.second ; ++it) {
367- if (it->second ->module_ == referrer) {
368- dependent = it->second ;
369- break ;
370- }
371- }
376+ ModuleWrap* dependent = ModuleWrap::GetFromModule (env, referrer);
372377
373378 if (dependent == nullptr ) {
374379 env->ThrowError (" linking error, null dep" );
@@ -728,6 +733,40 @@ void ModuleWrap::SetImportModuleDynamicallyCallback(
728733 iso->SetHostImportModuleDynamicallyCallback (ImportModuleDynamically);
729734}
730735
736+ void ModuleWrap::HostInitializeImportMetaObjectCallback (
737+ Local<Context> context, Local<Module> module , Local<Object> meta) {
738+ Isolate* isolate = context->GetIsolate ();
739+ Environment* env = Environment::GetCurrent (context);
740+ ModuleWrap* module_wrap = ModuleWrap::GetFromModule (env, module );
741+
742+ if (module_wrap == nullptr ) {
743+ return ;
744+ }
745+
746+ Local<Object> wrap = module_wrap->object ();
747+ Local<Function> callback =
748+ env->host_initialize_import_meta_object_callback ();
749+ Local<Value> args[] = { wrap, meta };
750+ callback->Call (context, Undefined (isolate), arraysize (args), args)
751+ .ToLocalChecked ();
752+ }
753+
754+ void ModuleWrap::SetInitializeImportMetaObjectCallback (
755+ const FunctionCallbackInfo<Value>& args) {
756+ Environment* env = Environment::GetCurrent (args);
757+ Isolate* isolate = env->isolate ();
758+ if (!args[0 ]->IsFunction ()) {
759+ env->ThrowError (" first argument is not a function" );
760+ return ;
761+ }
762+
763+ Local<Function> import_meta_callback = args[0 ].As <Function>();
764+ env->set_host_initialize_import_meta_object_callback (import_meta_callback);
765+
766+ isolate->SetHostInitializeImportMetaObjectCallback (
767+ HostInitializeImportMetaObjectCallback);
768+ }
769+
731770void ModuleWrap::Initialize (Local<Object> target,
732771 Local<Value> unused,
733772 Local<Context> context) {
@@ -752,6 +791,9 @@ void ModuleWrap::Initialize(Local<Object> target,
752791 env->SetMethod (target,
753792 " setImportModuleDynamicallyCallback" ,
754793 node::loader::ModuleWrap::SetImportModuleDynamicallyCallback);
794+ env->SetMethod (target,
795+ " setInitializeImportMetaObjectCallback" ,
796+ ModuleWrap::SetInitializeImportMetaObjectCallback);
755797
756798#define V (name ) \
757799 target->Set (context, \
0 commit comments