diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 81a9547b9071bc..de5acbef5b580a 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -361,6 +361,7 @@ process.emitWarning = emitWarning; // Preload modules so that they are included in the builtin snapshot. require('fs'); require('v8'); +require('vm'); function setupPrepareStackTrace() { const { diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index e52da0868e1ad2..62ba7da371d39e 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -434,6 +434,18 @@ function initializeESMLoader() { // track of for different ESM modules. setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject); setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback); + + // Patch the vm module when --experimental-vm-modules is on. + // Please update the comments in vm.js when this block changes. + if (getOptionValue('--experimental-vm-modules')) { + const { + Module, SourceTextModule, SyntheticModule, + } = require('internal/vm/module'); + const vm = require('vm'); + vm.Module = Module; + vm.SourceTextModule = SourceTextModule; + vm.SyntheticModule = SyntheticModule; + } } function initializeFrozenIntrinsics() { diff --git a/lib/vm.js b/lib/vm.js index 5c39429b3d1d5f..0b643110ae9465 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -422,11 +422,6 @@ module.exports = { measureMemory, }; -if (require('internal/options').getOptionValue('--experimental-vm-modules')) { - const { - Module, SourceTextModule, SyntheticModule, - } = require('internal/vm/module'); - module.exports.Module = Module; - module.exports.SourceTextModule = SourceTextModule; - module.exports.SyntheticModule = SyntheticModule; -} +// The vm module is patched to include vm.Module, vm.SourceTextModule +// and vm.SyntheticModule in the pre-execution phase when +// --experimental-vm-modules is on. diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 23895382b33c86..6e2b167da58046 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -21,13 +21,14 @@ #include "node_contextify.h" -#include "memory_tracker-inl.h" -#include "node_internals.h" -#include "node_watchdog.h" #include "base_object-inl.h" +#include "memory_tracker-inl.h" +#include "module_wrap.h" #include "node_context_data.h" #include "node_errors.h" -#include "module_wrap.h" +#include "node_external_reference.h" +#include "node_internals.h" +#include "node_watchdog.h" #include "util-inl.h" namespace node { @@ -255,6 +256,12 @@ void ContextifyContext::Init(Environment* env, Local target) { env->SetMethod(target, "compileFunction", CompileFunction); } +void ContextifyContext::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(MakeContext); + registry->Register(IsContext); + registry->Register(CompileFunction); +} // makeContext(sandbox, name, origin, strings, wasm); void ContextifyContext::MakeContext(const FunctionCallbackInfo& args) { @@ -665,6 +672,14 @@ void ContextifyScript::Init(Environment* env, Local target) { env->set_script_context_constructor_template(script_tmpl); } +void ContextifyScript::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(New); + registry->Register(CreateCachedData); + registry->Register(RunInContext); + registry->Register(RunInThisContext); +} + void ContextifyScript::New(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); @@ -1293,6 +1308,10 @@ void MicrotaskQueueWrap::Init(Environment* env, Local target) { env->SetConstructorFunction(target, "MicrotaskQueue", tmpl); } +void MicrotaskQueueWrap::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(New); +} void Initialize(Local target, Local unused, @@ -1347,7 +1366,19 @@ void Initialize(Local target, env->SetMethod(target, "measureMemory", MeasureMemory); } +void RegisterExternalReferences(ExternalReferenceRegistry* registry) { + ContextifyContext::RegisterExternalReferences(registry); + ContextifyScript::RegisterExternalReferences(registry); + MicrotaskQueueWrap::RegisterExternalReferences(registry); + + registry->Register(StartSigintWatchdog); + registry->Register(StopSigintWatchdog); + registry->Register(WatchdogHasPendingSigint); + registry->Register(MeasureMemory); +} } // namespace contextify } // namespace node NODE_MODULE_CONTEXT_AWARE_INTERNAL(contextify, node::contextify::Initialize) +NODE_MODULE_EXTERNAL_REFERENCE(contextify, + node::contextify::RegisterExternalReferences) diff --git a/src/node_contextify.h b/src/node_contextify.h index 357029d8e18312..08ee97df777a90 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -8,6 +8,8 @@ #include "node_errors.h" namespace node { +class ExternalReferenceRegistry; + namespace contextify { class MicrotaskQueueWrap : public BaseObject { @@ -17,6 +19,7 @@ class MicrotaskQueueWrap : public BaseObject { const std::shared_ptr& microtask_queue() const; static void Init(Environment* env, v8::Local target); + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); static void New(const v8::FunctionCallbackInfo& args); // This could have methods for running the microtask queue, if we ever decide @@ -52,6 +55,7 @@ class ContextifyContext { v8::Local sandbox_obj, const ContextOptions& options); static void Init(Environment* env, v8::Local target); + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); static ContextifyContext* ContextFromContextifiedSandbox( Environment* env, @@ -141,6 +145,7 @@ class ContextifyScript : public BaseObject { ~ContextifyScript() override; static void Init(Environment* env, v8::Local target); + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); static void New(const v8::FunctionCallbackInfo& args); static bool InstanceOf(Environment* env, const v8::Local& args); static void CreateCachedData( diff --git a/src/node_external_reference.h b/src/node_external_reference.h index 316d1d7b865dd8..5f64870178fe6d 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -50,6 +50,7 @@ class ExternalReferenceRegistry { V(async_wrap) \ V(binding) \ V(buffer) \ + V(contextify) \ V(credentials) \ V(env_var) \ V(errors) \