electron/patches/node/chore_expose_importmoduledynamically_and.patch
electron-roller[bot] 1766511e34
chore: bump node to v18.18.0 (main) (#39915)
* chore: bump node in DEPS to v18.18.0

* child_process: harden against prototype pollution

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

* deps: upgrade to libuv 1.46.0

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

* module: reduce url invocations in esm/load.js

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

* Revert "test: remove test-crypto-keygen flaky designation"

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

* fix: FTBTFS in ada dep

https://github.com/ada-url/ada/pull/464
https://github.com/ada-url/idna/pull/31

* fix: force_colors snapshot line number

* chore: fixup patch indices

* chore: update filenames.json

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-09-20 16:13:43 -04:00

103 lines
4.5 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/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<Module> ModuleWrap::ResolveModuleCallback(
return module->module_.Get(isolate);
}
-static MaybeLocal<Promise> ImportModuleDynamically(
+MaybeLocal<Promise> ImportModuleDynamically(
Local<Context> context,
Local<v8::Data> host_defined_options,
Local<Value> 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<Function> import_callback = args[0].As<Function>();
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<Function> import_meta_callback = args[0].As<Function>();
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<Value> 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<v8::Promise> ImportModuleDynamically(
+ v8::Local<v8::Context> context,
+ v8::Local<v8::Data> host_defined_options,
+ v8::Local<v8::Value> resource_name,
+ v8::Local<v8::String> specifier,
+ v8::Local<v8::FixedArray> import_assertions);
+
+class NODE_EXTERN ModuleWrap : public BaseObject {
public:
enum InternalFields {
kModuleWrapBaseField = BaseObject::kInternalFieldCount,