diff --git a/src/env.h b/src/env.h index c204834d25b407..5a3466b1686cb5 100644 --- a/src/env.h +++ b/src/env.h @@ -1002,7 +1002,8 @@ struct SnapshotData { enum class DataOwnership { kOwned, kNotOwned }; static const uint32_t kMagic = 0x143da19; - static const SnapshotIndex kNodeBaseContextIndex = 0; + static const SnapshotIndex kNodeVMContextIndex = 0; + static const SnapshotIndex kNodeBaseContextIndex = kNodeVMContextIndex + 1; static const SnapshotIndex kNodeMainContextIndex = kNodeBaseContextIndex + 1; DataOwnership data_ownership = DataOwnership::kOwned; diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 19d2f81c604674..da8d88e06e60e2 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -28,6 +28,7 @@ #include "node_errors.h" #include "node_external_reference.h" #include "node_internals.h" +#include "node_snapshot_builder.h" #include "node_watchdog.h" #include "util-inl.h" @@ -118,6 +119,9 @@ ContextifyContext::ContextifyContext( object_template = CreateGlobalTemplate(env->isolate()); env->set_contextify_global_template(object_template); } + bool use_node_snapshot = per_process::cli_options->node_snapshot; + const SnapshotData* snapshot_data = + use_node_snapshot ? SnapshotBuilder::GetEmbeddedSnapshotData() : nullptr; MicrotaskQueue* queue = microtask_queue() @@ -125,7 +129,7 @@ ContextifyContext::ContextifyContext( : env->isolate()->GetCurrentContext()->GetMicrotaskQueue(); Local v8_context; - if (!(CreateV8Context(env->isolate(), object_template, queue) + if (!(CreateV8Context(env->isolate(), object_template, snapshot_data, queue) .ToLocal(&v8_context)) || !InitializeContext(v8_context, env, sandbox_obj, options)) { // Allocation failure, maximum call stack size reached, termination, etc. @@ -190,17 +194,28 @@ Local ContextifyContext::CreateGlobalTemplate( MaybeLocal ContextifyContext::CreateV8Context( Isolate* isolate, Local object_template, + const SnapshotData* snapshot_data, MicrotaskQueue* queue) { EscapableHandleScope scope(isolate); - Local ctx = Context::New(isolate, - nullptr, // extensions - object_template, - {}, // global object - {}, // deserialization callback - queue); - if (ctx.IsEmpty()) return MaybeLocal(); - + Local ctx; + if (snapshot_data == nullptr) { + ctx = Context::New(isolate, + nullptr, // extensions + object_template, + {}, // global object + {}, // deserialization callback + queue); + if (ctx.IsEmpty()) return MaybeLocal(); + } else if (!Context::FromSnapshot(isolate, + SnapshotData::kNodeVMContextIndex, + {}, // deserialization callback + nullptr, // extensions + {}, // global object + queue) + .ToLocal(&ctx)) { + return MaybeLocal(); + } return scope.Escape(ctx); } diff --git a/src/node_contextify.h b/src/node_contextify.h index 8aabf080255516..ee1580c7d2b494 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -53,6 +53,7 @@ class ContextifyContext { static v8::MaybeLocal CreateV8Context( v8::Isolate* isolate, v8::Local object_template, + const SnapshotData* snapshot_data, v8::MicrotaskQueue* queue); bool InitializeContext(v8::Local ctx, Environment* env, diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index b9675b0aa9bb2f..4d087d658fbb36 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -7,6 +7,7 @@ #include "env-inl.h" #include "node_blob.h" #include "node_builtins.h" +#include "node_contextify.h" #include "node_errors.h" #include "node_external_reference.h" #include "node_file.h" @@ -31,6 +32,7 @@ using v8::HandleScope; using v8::Isolate; using v8::Local; using v8::Object; +using v8::ObjectTemplate; using v8::ScriptCompiler; using v8::ScriptOrigin; using v8::SnapshotCreator; @@ -1030,6 +1032,19 @@ int SnapshotBuilder::Generate(SnapshotData* out, // The default context with only things created by V8. Local default_context = Context::New(isolate); + // The context used by the vm module. + Local vm_context; + { + Local global_template = + main_instance->isolate_data()->contextify_global_template(); + CHECK(!global_template.IsEmpty()); + if (!contextify::ContextifyContext::CreateV8Context( + isolate, global_template, nullptr, nullptr) + .ToLocal(&vm_context)) { + return SNAPSHOT_ERROR; + } + } + // The Node.js-specific context with primodials, can be used by workers // TODO(joyeecheung): investigate if this can be used by vm contexts // without breaking compatibility. @@ -1111,7 +1126,9 @@ int SnapshotBuilder::Generate(SnapshotData* out, // blob is created. So initialize all the contexts before adding them. // TODO(joyeecheung): figure out how to remove this restriction. creator.SetDefaultContext(default_context); - size_t index = creator.AddContext(base_context); + size_t index = creator.AddContext(vm_context); + CHECK_EQ(index, SnapshotData::kNodeVMContextIndex); + index = creator.AddContext(base_context); CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex); index = creator.AddContext(main_context, {SerializeNodeContextInternalFields, env});