Files
electron/patches/node/chore_expose_importmoduledynamically_and.patch
electron-roller[bot] 54a617caab chore: bump node to v24.11.1 (main) (#48917)
* chore: bump node in DEPS to v24.11.1

* src: add a default branch for module phase

https://github.com/nodejs/node/pull/60261

* src: conditionally disable source phase imports by default

https://github.com/nodejs/node/pull/60364

* chore: update patches

* src: update locks to use DictionaryTemplate and other minor cleanups

https://github.com/nodejs/node/pull/60061

* deps: update simdjson to 4.0.7

https://github.com/nodejs/node/pull/59883

* test: move sea tests into test/sea

https://github.com/nodejs/node/pull/60250

* fixup deps: update simdjson to 4.0.7a

* src: conditionally disable source phase imports by default

https://github.com/nodejs/node/pull/60364

* module: handle null source from async loader hooks in sync hooks

https://github.com/nodejs/node/pull/59929

* Revert "src: conditionally disable source phase imports by default"

This reverts commit 5f85b84262.

* src: allow disabling JS source phase imports

https://github.com/nodejs/node/pull/60364

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2025-11-18 09:34:51 -05:00

139 lines
5.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <marshallofsound@electronjs.org>
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/modules/esm/utils.js b/lib/internal/modules/esm/utils.js
index a9076a7ae941284d4585829292e2ece25c2b90e4..7335fe20f34fdd822276575686379dd954f1c8e1 100644
--- a/lib/internal/modules/esm/utils.js
+++ b/lib/internal/modules/esm/utils.js
@@ -34,7 +34,7 @@ const {
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
ERR_INVALID_ARG_VALUE,
} = require('internal/errors').codes;
-const { getOptionValue } = require('internal/options');
+const { getOptionValue, getEmbedderOptions } = require('internal/options');
const {
emitExperimentalWarning,
kEmptyObject,
@@ -285,12 +285,13 @@ let _shouldSpawnLoaderHookWorker = true;
* should be spawned later.
*/
function initializeESM(shouldSpawnLoaderHookWorker = true) {
+ const shouldSetOnIsolate = !getEmbedderOptions().shouldNotRegisterESMLoader;
_shouldSpawnLoaderHookWorker = shouldSpawnLoaderHookWorker;
initializeDefaultConditions();
// Setup per-realm callbacks that locate data or callbacks that we keep
// track of for different ESM modules.
- setInitializeImportMetaObjectCallback(initializeImportMetaObject);
- setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
+ setInitializeImportMetaObjectCallback(initializeImportMetaObject, shouldSetOnIsolate);
+ setImportModuleDynamicallyCallback(importModuleDynamicallyCallback, shouldSetOnIsolate);
}
/**
diff --git a/src/module_wrap.cc b/src/module_wrap.cc
index 26e6bcfa30be8b22b20e66ffe2f8d0b7d60fc6cc..fa2f28989be19e8ea8f53b990ae96be92ef3c3a3 100644
--- a/src/module_wrap.cc
+++ b/src/module_wrap.cc
@@ -1098,7 +1098,7 @@ Maybe<ModuleWrap*> ModuleWrap::ResolveModule(
return Just(module_wrap);
}
-static MaybeLocal<Promise> ImportModuleDynamicallyWithPhase(
+MaybeLocal<Promise> ImportModuleDynamicallyWithPhase(
Local<Context> context,
Local<Data> host_defined_options,
Local<Value> resource_name,
@@ -1186,14 +1186,16 @@ void ModuleWrap::SetImportModuleDynamicallyCallback(
Realm* realm = Realm::GetCurrent(args);
HandleScope handle_scope(isolate);
- CHECK_EQ(args.Length(), 1);
+ CHECK_EQ(args.Length(), 2);
CHECK(args[0]->IsFunction());
Local<Function> import_callback = args[0].As<Function>();
realm->set_host_import_module_dynamically_callback(import_callback);
- isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
- isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
+ if (args[1]->IsBoolean() && args[1]->BooleanValue(isolate)) {
+ isolate->SetHostImportModuleDynamicallyCallback(ImportModuleDynamically);
+ isolate->SetHostImportModuleWithPhaseDynamicallyCallback(
ImportModuleDynamicallyWithPhase);
+ }
}
void ModuleWrap::HostInitializeImportMetaObjectCallback(
@@ -1235,13 +1237,14 @@ void ModuleWrap::SetInitializeImportMetaObjectCallback(
Realm* realm = Realm::GetCurrent(args);
Isolate* isolate = realm->isolate();
- CHECK_EQ(args.Length(), 1);
+ CHECK_EQ(args.Length(), 2);
CHECK(args[0]->IsFunction());
Local<Function> import_meta_callback = args[0].As<Function>();
realm->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<Value> ModuleWrap::SyntheticModuleEvaluationStepsCallback(
diff --git a/src/module_wrap.h b/src/module_wrap.h
index 03cf8d0e91d795aad6db9b11956d296ff4999f7e..45264c2ad58e37a73fd62addac7fb671eed6d502 100644
--- a/src/module_wrap.h
+++ b/src/module_wrap.h
@@ -8,6 +8,7 @@
#include <unordered_map>
#include <vector>
#include "base_object.h"
+#include "node.h"
#include "v8-script.h"
namespace node {
@@ -92,7 +93,15 @@ struct ModuleCacheKey : public MemoryRetainer {
hash(hash) {}
};
-class ModuleWrap : public BaseObject {
+NODE_EXTERN v8::MaybeLocal<v8::Promise> ImportModuleDynamicallyWithPhase(
+ v8::Local<v8::Context> context,
+ v8::Local<v8::Data> host_defined_options,
+ v8::Local<v8::Value> resource_name,
+ v8::Local<v8::String> specifier,
+ v8::ModuleImportPhase phase,
+ v8::Local<v8::FixedArray> import_attributes);
+
+class NODE_EXTERN ModuleWrap : public BaseObject {
using ResolveCache =
std::unordered_map<ModuleCacheKey, uint32_t, ModuleCacheKey::Hash>;
@@ -157,6 +166,8 @@ class ModuleWrap : public BaseObject {
static void CreateRequiredModuleFacade(
const v8::FunctionCallbackInfo<v8::Value>& args);
+ static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);
+
private:
ModuleWrap(Realm* realm,
v8::Local<v8::Object> object,
@@ -205,7 +216,6 @@ class ModuleWrap : public BaseObject {
v8::Local<v8::String> specifier,
v8::Local<v8::FixedArray> import_attributes,
v8::Local<v8::Module> referrer);
- static ModuleWrap* GetFromModule(node::Environment*, v8::Local<v8::Module>);
// This method may throw a JavaScript exception, so the return type is
// wrapped in a Maybe.