Skip to content

Commit

Permalink
bootstrap: include vm and contextify binding into the snapshot
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and danielleadams committed May 31, 2021
1 parent 2268d1c commit a98d631
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 3 additions & 8 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
39 changes: 35 additions & 4 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -255,6 +256,12 @@ void ContextifyContext::Init(Environment* env, Local<Object> 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<Value>& args) {
Expand Down Expand Up @@ -665,6 +672,14 @@ void ContextifyScript::Init(Environment* env, Local<Object> 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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Expand Down Expand Up @@ -1293,6 +1308,10 @@ void MicrotaskQueueWrap::Init(Environment* env, Local<Object> target) {
env->SetConstructorFunction(target, "MicrotaskQueue", tmpl);
}

void MicrotaskQueueWrap::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
}

void Initialize(Local<Object> target,
Local<Value> unused,
Expand Down Expand Up @@ -1347,7 +1366,19 @@ void Initialize(Local<Object> 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)
5 changes: 5 additions & 0 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "node_errors.h"

namespace node {
class ExternalReferenceRegistry;

namespace contextify {

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

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

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

static ContextifyContext* ContextFromContextifiedSandbox(
Environment* env,
Expand Down Expand Up @@ -141,6 +145,7 @@ class ContextifyScript : public BaseObject {
~ContextifyScript() override;

static void Init(Environment* env, v8::Local<v8::Object> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
static void CreateCachedData(
Expand Down
1 change: 1 addition & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ExternalReferenceRegistry {
V(async_wrap) \
V(binding) \
V(buffer) \
V(contextify) \
V(credentials) \
V(env_var) \
V(errors) \
Expand Down

0 comments on commit a98d631

Please sign in to comment.