Skip to content

Commit a98d631

Browse files
joyeecheungdanielleadams
authored andcommittedMay 31, 2021
bootstrap: include vm and contextify binding into the snapshot
In addition, defer the patching of the vm module based on the value of --experimental-vm-modules to runtime. PR-URL: #38677 Refs: #35711 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 2268d1c commit a98d631

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed
 

‎lib/internal/bootstrap/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ process.emitWarning = emitWarning;
361361
// Preload modules so that they are included in the builtin snapshot.
362362
require('fs');
363363
require('v8');
364+
require('vm');
364365

365366
function setupPrepareStackTrace() {
366367
const {

‎lib/internal/bootstrap/pre_execution.js

+12
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,18 @@ function initializeESMLoader() {
434434
// track of for different ESM modules.
435435
setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject);
436436
setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback);
437+
438+
// Patch the vm module when --experimental-vm-modules is on.
439+
// Please update the comments in vm.js when this block changes.
440+
if (getOptionValue('--experimental-vm-modules')) {
441+
const {
442+
Module, SourceTextModule, SyntheticModule,
443+
} = require('internal/vm/module');
444+
const vm = require('vm');
445+
vm.Module = Module;
446+
vm.SourceTextModule = SourceTextModule;
447+
vm.SyntheticModule = SyntheticModule;
448+
}
437449
}
438450

439451
function initializeFrozenIntrinsics() {

‎lib/vm.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,6 @@ module.exports = {
422422
measureMemory,
423423
};
424424

425-
if (require('internal/options').getOptionValue('--experimental-vm-modules')) {
426-
const {
427-
Module, SourceTextModule, SyntheticModule,
428-
} = require('internal/vm/module');
429-
module.exports.Module = Module;
430-
module.exports.SourceTextModule = SourceTextModule;
431-
module.exports.SyntheticModule = SyntheticModule;
432-
}
425+
// The vm module is patched to include vm.Module, vm.SourceTextModule
426+
// and vm.SyntheticModule in the pre-execution phase when
427+
// --experimental-vm-modules is on.

‎src/node_contextify.cc

+35-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
#include "node_contextify.h"
2323

24-
#include "memory_tracker-inl.h"
25-
#include "node_internals.h"
26-
#include "node_watchdog.h"
2724
#include "base_object-inl.h"
25+
#include "memory_tracker-inl.h"
26+
#include "module_wrap.h"
2827
#include "node_context_data.h"
2928
#include "node_errors.h"
30-
#include "module_wrap.h"
29+
#include "node_external_reference.h"
30+
#include "node_internals.h"
31+
#include "node_watchdog.h"
3132
#include "util-inl.h"
3233

3334
namespace node {
@@ -255,6 +256,12 @@ void ContextifyContext::Init(Environment* env, Local<Object> target) {
255256
env->SetMethod(target, "compileFunction", CompileFunction);
256257
}
257258

259+
void ContextifyContext::RegisterExternalReferences(
260+
ExternalReferenceRegistry* registry) {
261+
registry->Register(MakeContext);
262+
registry->Register(IsContext);
263+
registry->Register(CompileFunction);
264+
}
258265

259266
// makeContext(sandbox, name, origin, strings, wasm);
260267
void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
@@ -665,6 +672,14 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
665672
env->set_script_context_constructor_template(script_tmpl);
666673
}
667674

675+
void ContextifyScript::RegisterExternalReferences(
676+
ExternalReferenceRegistry* registry) {
677+
registry->Register(New);
678+
registry->Register(CreateCachedData);
679+
registry->Register(RunInContext);
680+
registry->Register(RunInThisContext);
681+
}
682+
668683
void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
669684
Environment* env = Environment::GetCurrent(args);
670685
Isolate* isolate = env->isolate();
@@ -1293,6 +1308,10 @@ void MicrotaskQueueWrap::Init(Environment* env, Local<Object> target) {
12931308
env->SetConstructorFunction(target, "MicrotaskQueue", tmpl);
12941309
}
12951310

1311+
void MicrotaskQueueWrap::RegisterExternalReferences(
1312+
ExternalReferenceRegistry* registry) {
1313+
registry->Register(New);
1314+
}
12961315

12971316
void Initialize(Local<Object> target,
12981317
Local<Value> unused,
@@ -1347,7 +1366,19 @@ void Initialize(Local<Object> target,
13471366
env->SetMethod(target, "measureMemory", MeasureMemory);
13481367
}
13491368

1369+
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1370+
ContextifyContext::RegisterExternalReferences(registry);
1371+
ContextifyScript::RegisterExternalReferences(registry);
1372+
MicrotaskQueueWrap::RegisterExternalReferences(registry);
1373+
1374+
registry->Register(StartSigintWatchdog);
1375+
registry->Register(StopSigintWatchdog);
1376+
registry->Register(WatchdogHasPendingSigint);
1377+
registry->Register(MeasureMemory);
1378+
}
13501379
} // namespace contextify
13511380
} // namespace node
13521381

13531382
NODE_MODULE_CONTEXT_AWARE_INTERNAL(contextify, node::contextify::Initialize)
1383+
NODE_MODULE_EXTERNAL_REFERENCE(contextify,
1384+
node::contextify::RegisterExternalReferences)

‎src/node_contextify.h

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "node_errors.h"
99

1010
namespace node {
11+
class ExternalReferenceRegistry;
12+
1113
namespace contextify {
1214

1315
class MicrotaskQueueWrap : public BaseObject {
@@ -17,6 +19,7 @@ class MicrotaskQueueWrap : public BaseObject {
1719
const std::shared_ptr<v8::MicrotaskQueue>& microtask_queue() const;
1820

1921
static void Init(Environment* env, v8::Local<v8::Object> target);
22+
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
2023
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
2124

2225
// This could have methods for running the microtask queue, if we ever decide
@@ -52,6 +55,7 @@ class ContextifyContext {
5255
v8::Local<v8::Object> sandbox_obj,
5356
const ContextOptions& options);
5457
static void Init(Environment* env, v8::Local<v8::Object> target);
58+
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
5559

5660
static ContextifyContext* ContextFromContextifiedSandbox(
5761
Environment* env,
@@ -141,6 +145,7 @@ class ContextifyScript : public BaseObject {
141145
~ContextifyScript() override;
142146

143147
static void Init(Environment* env, v8::Local<v8::Object> target);
148+
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
144149
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
145150
static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
146151
static void CreateCachedData(

‎src/node_external_reference.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ExternalReferenceRegistry {
5050
V(async_wrap) \
5151
V(binding) \
5252
V(buffer) \
53+
V(contextify) \
5354
V(credentials) \
5455
V(env_var) \
5556
V(errors) \

0 commit comments

Comments
 (0)
Please sign in to comment.