mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
vm: lazily initialize primordials for vm contexts
Lazily initialize primordials when cross-context support for builtins is needed to fix the performance regression in context creation. PR-URL: https://github.com/nodejs/node/pull/31738 Fixes: https://github.com/nodejs/node/issues/29842 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: David Carlier <devnexen@gmail.com>
This commit is contained in:
1 parent
8a044cb9ae
commit
02de66a110
3 files changed
+47
-41
No files matched your search
+41
-39
@@ -382,7 +382,8 @@ MaybeLocal<Object> GetPerContextExports(Local<Context> context) {
|
||||
return handle_scope.Escape(existing_value.As<Object>());
|
||||
|
||||
Local<Object> exports = Object::New(isolate);
|
||||
if (context->Global()->SetPrivate(context, key, exports).IsNothing())
|
||||
if (context->Global()->SetPrivate(context, key, exports).IsNothing() ||
|
||||
!InitializePrimordials(context))
|
||||
return MaybeLocal<Object>();
|
||||
return handle_scope.Escape(exports);
|
||||
}
|
||||
@@ -438,49 +439,50 @@ bool InitializeContextForSnapshot(Local<Context> context) {
|
||||
|
||||
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration,
|
||||
True(isolate));
|
||||
return InitializePrimordials(context);
|
||||
}
|
||||
|
||||
{
|
||||
// Run per-context JS files.
|
||||
Context::Scope context_scope(context);
|
||||
Local<Object> exports;
|
||||
bool InitializePrimordials(Local<Context> context) {
|
||||
// Run per-context JS files.
|
||||
Isolate* isolate = context->GetIsolate();
|
||||
Context::Scope context_scope(context);
|
||||
Local<Object> exports;
|
||||
|
||||
Local<String> primordials_string =
|
||||
FIXED_ONE_BYTE_STRING(isolate, "primordials");
|
||||
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global");
|
||||
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports");
|
||||
Local<String> primordials_string =
|
||||
FIXED_ONE_BYTE_STRING(isolate, "primordials");
|
||||
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global");
|
||||
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports");
|
||||
|
||||
// Create primordials first and make it available to per-context scripts.
|
||||
Local<Object> primordials = Object::New(isolate);
|
||||
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() ||
|
||||
!GetPerContextExports(context).ToLocal(&exports) ||
|
||||
!exports->Set(context, primordials_string, primordials).FromJust()) {
|
||||
// Create primordials first and make it available to per-context scripts.
|
||||
Local<Object> primordials = Object::New(isolate);
|
||||
if (!primordials->SetPrototype(context, Null(isolate)).FromJust() ||
|
||||
!GetPerContextExports(context).ToLocal(&exports) ||
|
||||
!exports->Set(context, primordials_string, primordials).FromJust()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static const char* context_files[] = {"internal/per_context/primordials",
|
||||
"internal/per_context/domexception",
|
||||
"internal/per_context/messageport",
|
||||
nullptr};
|
||||
|
||||
for (const char** module = context_files; *module != nullptr; module++) {
|
||||
std::vector<Local<String>> parameters = {
|
||||
global_string, exports_string, primordials_string};
|
||||
Local<Value> arguments[] = {context->Global(), exports, primordials};
|
||||
MaybeLocal<Function> maybe_fn =
|
||||
native_module::NativeModuleEnv::LookupAndCompile(
|
||||
context, *module, ¶meters, nullptr);
|
||||
if (maybe_fn.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static const char* context_files[] = {"internal/per_context/primordials",
|
||||
"internal/per_context/domexception",
|
||||
"internal/per_context/messageport",
|
||||
nullptr};
|
||||
|
||||
for (const char** module = context_files; *module != nullptr; module++) {
|
||||
std::vector<Local<String>> parameters = {
|
||||
global_string, exports_string, primordials_string};
|
||||
Local<Value> arguments[] = {context->Global(), exports, primordials};
|
||||
MaybeLocal<Function> maybe_fn =
|
||||
native_module::NativeModuleEnv::LookupAndCompile(
|
||||
context, *module, ¶meters, nullptr);
|
||||
if (maybe_fn.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Local<Function> fn = maybe_fn.ToLocalChecked();
|
||||
MaybeLocal<Value> result =
|
||||
fn->Call(context, Undefined(isolate),
|
||||
arraysize(arguments), arguments);
|
||||
// Execution failed during context creation.
|
||||
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
|
||||
if (result.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
Local<Function> fn = maybe_fn.ToLocalChecked();
|
||||
MaybeLocal<Value> result =
|
||||
fn->Call(context, Undefined(isolate), arraysize(arguments), arguments);
|
||||
// Execution failed during context creation.
|
||||
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
|
||||
if (result.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -185,8 +185,11 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
|
||||
|
||||
object_template->SetHandler(config);
|
||||
object_template->SetHandler(indexed_config);
|
||||
|
||||
Local<Context> ctx = NewContext(env->isolate(), object_template);
|
||||
Local<Context> ctx = Context::New(env->isolate(), nullptr, object_template);
|
||||
if (ctx.IsEmpty()) return MaybeLocal<Context>();
|
||||
// Only partially initialize the context - the primordials are left out
|
||||
// and only initialized when necessary.
|
||||
InitializeContextRuntime(ctx);
|
||||
|
||||
if (ctx.IsEmpty()) {
|
||||
return MaybeLocal<Context>();
|
||||
|
||||
@@ -102,6 +102,7 @@ std::string GetProcessTitle(const char* default_title);
|
||||
std::string GetHumanReadableProcessName();
|
||||
|
||||
void InitializeContextRuntime(v8::Local<v8::Context>);
|
||||
bool InitializePrimordials(v8::Local<v8::Context> context);
|
||||
|
||||
namespace task_queue {
|
||||
void PromiseRejectCallback(v8::PromiseRejectMessage message);
|
||||
|
||||
Reference in new issue
Block a user