src: cache some context in locals

Refs: 66566df577

PR-URL: https://github.com/nodejs/node/pull/37473
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
XadillaX 2021-02-22 18:29:48 +08:00 committed by 死月
parent bfee9daaa5
commit 4e9212bb7b

View File

@ -210,7 +210,8 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
return MaybeLocal<Context>(); return MaybeLocal<Context>();
} }
ctx->SetSecurityToken(env->context()->GetSecurityToken()); Local<Context> context = env->context();
ctx->SetSecurityToken(context->GetSecurityToken());
// We need to tie the lifetime of the sandbox object with the lifetime of // We need to tie the lifetime of the sandbox object with the lifetime of
// newly created context. We do this by making them hold references to each // newly created context. We do this by making them hold references to each
@ -219,7 +220,7 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
// directly in an Object, we instead hold onto the new context's global // directly in an Object, we instead hold onto the new context's global
// object instead (which then has a reference to the context). // object instead (which then has a reference to the context).
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj); ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj);
sandbox_obj->SetPrivate(env->context(), sandbox_obj->SetPrivate(context,
env->contextify_global_private_symbol(), env->contextify_global_private_symbol(),
ctx->Global()); ctx->Global());
@ -394,16 +395,17 @@ void ContextifyContext::PropertySetterCallback(
if (ctx->context_.IsEmpty()) if (ctx->context_.IsEmpty())
return; return;
Local<Context> context = ctx->context();
auto attributes = PropertyAttribute::None; auto attributes = PropertyAttribute::None;
bool is_declared_on_global_proxy = ctx->global_proxy() bool is_declared_on_global_proxy = ctx->global_proxy()
->GetRealNamedPropertyAttributes(ctx->context(), property) ->GetRealNamedPropertyAttributes(context, property)
.To(&attributes); .To(&attributes);
bool read_only = bool read_only =
static_cast<int>(attributes) & static_cast<int>(attributes) &
static_cast<int>(PropertyAttribute::ReadOnly); static_cast<int>(PropertyAttribute::ReadOnly);
bool is_declared_on_sandbox = ctx->sandbox() bool is_declared_on_sandbox = ctx->sandbox()
->GetRealNamedPropertyAttributes(ctx->context(), property) ->GetRealNamedPropertyAttributes(context, property)
.To(&attributes); .To(&attributes);
read_only = read_only || read_only = read_only ||
(static_cast<int>(attributes) & (static_cast<int>(attributes) &
@ -441,7 +443,7 @@ void ContextifyContext::PropertySetterCallback(
args.GetReturnValue().Set(false); args.GetReturnValue().Set(false);
} }
USE(ctx->sandbox()->Set(ctx->context(), property, value)); USE(ctx->sandbox()->Set(context, property, value));
} }
// static // static
@ -482,7 +484,7 @@ void ContextifyContext::PropertyDefinerCallback(
auto attributes = PropertyAttribute::None; auto attributes = PropertyAttribute::None;
bool is_declared = bool is_declared =
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(), ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
property) property)
.To(&attributes); .To(&attributes);
bool read_only = bool read_only =
@ -656,8 +658,10 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext); env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext); env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext);
target->Set(env->context(), class_name, Local<Context> context = env->context();
script_tmpl->GetFunction(env->context()).ToLocalChecked()).Check();
target->Set(context, class_name,
script_tmpl->GetFunction(context).ToLocalChecked()).Check();
env->set_script_context_constructor_template(script_tmpl); env->set_script_context_constructor_template(script_tmpl);
} }
@ -775,9 +779,10 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
} }
contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked()); contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked());
Local<Context> env_context = env->context();
if (compile_options == ScriptCompiler::kConsumeCodeCache) { if (compile_options == ScriptCompiler::kConsumeCodeCache) {
args.This()->Set( args.This()->Set(
env->context(), env_context,
env->cached_data_rejected_string(), env->cached_data_rejected_string(),
Boolean::New(isolate, source.GetCachedData()->rejected)).Check(); Boolean::New(isolate, source.GetCachedData()->rejected)).Check();
} else if (produce_cached_data) { } else if (produce_cached_data) {
@ -789,12 +794,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
env, env,
reinterpret_cast<const char*>(cached_data->data), reinterpret_cast<const char*>(cached_data->data),
cached_data->length); cached_data->length);
args.This()->Set(env->context(), args.This()->Set(env_context,
env->cached_data_string(), env->cached_data_string(),
buf.ToLocalChecked()).Check(); buf.ToLocalChecked()).Check();
} }
args.This()->Set( args.This()->Set(
env->context(), env_context,
env->cached_data_produced_string(), env->cached_data_produced_string(),
Boolean::New(isolate, cached_data_produced)).Check(); Boolean::New(isolate, cached_data_produced)).Check();
} }
@ -884,7 +889,8 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox); ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
CHECK_NOT_NULL(contextify_context); CHECK_NOT_NULL(contextify_context);
if (contextify_context->context().IsEmpty()) Local<Context> context = contextify_context->context();
if (context.IsEmpty())
return; return;
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
@ -903,7 +909,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
bool break_on_first_line = args[4]->IsTrue(); bool break_on_first_line = args[4]->IsTrue();
// Do the eval within the context // Do the eval within the context
Context::Scope context_scope(contextify_context->context()); Context::Scope context_scope(context);
EvalMachine(contextify_context->env(), EvalMachine(contextify_context->env(),
timeout, timeout,
display_errors, display_errors,