Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a98d631

Browse filesBrowse files
joyeecheungdanielleadams
authored andcommitted
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 <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 2268d1c commit a98d631
Copy full SHA for a98d631

File tree

Expand file treeCollapse file tree

6 files changed

+57
-12
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+57
-12
lines changed
Open diff view settings
Collapse file

‎lib/internal/bootstrap/node.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js
+1Lines changed: 1 addition & 0 deletions
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 {
Collapse file

‎lib/internal/bootstrap/pre_execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/pre_execution.js
+12Lines changed: 12 additions & 0 deletions
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() {
Collapse file

‎lib/vm.js‎

Copy file name to clipboardExpand all lines: lib/vm.js
+3-8Lines changed: 3 additions & 8 deletions
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.
Collapse file

‎src/node_contextify.cc‎

Copy file name to clipboardExpand all lines: src/node_contextify.cc
+35-4Lines changed: 35 additions & 4 deletions
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)
Collapse file

‎src/node_contextify.h‎

Copy file name to clipboardExpand all lines: src/node_contextify.h
+5Lines changed: 5 additions & 0 deletions
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(
Collapse file

‎src/node_external_reference.h‎

Copy file name to clipboardExpand all lines: src/node_external_reference.h
+1Lines changed: 1 addition & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.