Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap: include vm and contextify binding into the snapshot #38677

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -427,11 +427,6 @@ module.exports = {
measureMemory,
};

if (require('internal/options').getOptionValue('--experimental-vm-modules')) {
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
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