From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 8 Mar 2023 13:02:17 -0800 Subject: chore: expose ImportModuleDynamically and HostInitializeImportMetaObjectCallback to embedders This also subtly changes the behavior of shouldNotRegisterESMLoader to ensure that node sets up the handlers internally but simply avoids setting its own handlers on the Isolate. This is so that Electron can set it to its own blended handler between Node and Blink. Not upstreamable. diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index cbfb6e3620a7e77658c86a4730c50661b8a937f7..ccd48027e3dfebd563fcbe83239174c79c693dd7 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -567,7 +567,7 @@ function initializeESMLoader() { // Create this WeakMap in js-land because V8 has no C++ API for WeakMap. internalBinding('module_wrap').callbackMap = new SafeWeakMap(); - if (getEmbedderOptions().shouldNotRegisterESMLoader) return; + const shouldSetOnIsolate = !getEmbedderOptions().shouldNotRegisterESMLoader; const { setImportModuleDynamicallyCallback, @@ -576,8 +576,8 @@ function initializeESMLoader() { const esm = require('internal/process/esm_loader'); // Setup per-isolate callbacks that locate data or callbacks that we keep // track of for different ESM modules. - setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject); - setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback); + setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject, shouldSetOnIsolate); + setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback, shouldSetOnIsolate); // Patch the vm module when --experimental-vm-modules is on. // Please update the comments in vm.js when this block changes. diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 0645b3ddf506df2a76f5661f0ec6bb35d5d8b94e..e0f1b2d51f3055b2250f2c0dc1dfd1048b645dd9 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -547,7 +547,7 @@ MaybeLocal ModuleWrap::ResolveModuleCallback( return module->module_.Get(isolate); } -static MaybeLocal ImportModuleDynamically( +MaybeLocal ImportModuleDynamically( Local context, Local host_defined_options, Local resource_name, @@ -629,12 +629,13 @@ void ModuleWrap::SetImportModuleDynamicallyCallback( Environment* env = Environment::GetCurrent(args); HandleScope handle_scope(isolate); - CHECK_EQ(args.Length(), 1); + CHECK_EQ(args.Length(), 2); CHECK(args[0]->IsFunction()); Local import_callback = args[0].As(); env->set_host_import_module_dynamically_callback(import_callback); - isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically); + if (args[1]->IsBoolean() && args[1]->BooleanValue(isolate)) + isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically); } void ModuleWrap::HostInitializeImportMetaObjectCallback( @@ -665,13 +666,14 @@ void ModuleWrap::SetInitializeImportMetaObjectCallback( Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); - CHECK_EQ(args.Length(), 1); + CHECK_EQ(args.Length(), 2); CHECK(args[0]->IsFunction()); Local import_meta_callback = args[0].As(); env->set_host_initialize_import_meta_object_callback(import_meta_callback); - isolate->SetHostInitializeImportMetaObjectCallback( - HostInitializeImportMetaObjectCallback); + if (args[1]->IsBoolean() && args[1]->BooleanValue(isolate)) + isolate->SetHostInitializeImportMetaObjectCallback( + HostInitializeImportMetaObjectCallback); } MaybeLocal ModuleWrap::SyntheticModuleEvaluationStepsCallback( diff --git a/src/module_wrap.h b/src/module_wrap.h index 58b233d036515c52d9bd5574c776c2ea65d2ecb1..5f7ef75480a76761c6fa62061c8700c812a3fc6f 100644 --- a/src/module_wrap.h +++ b/src/module_wrap.h @@ -30,7 +30,14 @@ enum HostDefinedOptions : int { kLength = 10, }; -class ModuleWrap : public BaseObject { +NODE_EXTERN v8::MaybeLocal ImportModuleDynamically( + v8::Local context, + v8::Local host_defined_options, + v8::Local resource_name, + v8::Local specifier, + v8::Local import_assertions); + +class NODE_EXTERN ModuleWrap : public BaseObject { public: enum InternalFields { kModuleWrapBaseField = BaseObject::kInternalFieldCount,