Skip to content

Commit 4e9212b

Browse files
committed
src: cache some context in locals
Refs: 66566df PR-URL: #37473 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent bfee9da commit 4e9212b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Diff for: src/node_contextify.cc

+19-13
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
210210
return MaybeLocal<Context>();
211211
}
212212

213-
ctx->SetSecurityToken(env->context()->GetSecurityToken());
213+
Local<Context> context = env->context();
214+
ctx->SetSecurityToken(context->GetSecurityToken());
214215

215216
// We need to tie the lifetime of the sandbox object with the lifetime of
216217
// newly created context. We do this by making them hold references to each
@@ -219,7 +220,7 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
219220
// directly in an Object, we instead hold onto the new context's global
220221
// object instead (which then has a reference to the context).
221222
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj);
222-
sandbox_obj->SetPrivate(env->context(),
223+
sandbox_obj->SetPrivate(context,
223224
env->contextify_global_private_symbol(),
224225
ctx->Global());
225226

@@ -394,16 +395,17 @@ void ContextifyContext::PropertySetterCallback(
394395
if (ctx->context_.IsEmpty())
395396
return;
396397

398+
Local<Context> context = ctx->context();
397399
auto attributes = PropertyAttribute::None;
398400
bool is_declared_on_global_proxy = ctx->global_proxy()
399-
->GetRealNamedPropertyAttributes(ctx->context(), property)
401+
->GetRealNamedPropertyAttributes(context, property)
400402
.To(&attributes);
401403
bool read_only =
402404
static_cast<int>(attributes) &
403405
static_cast<int>(PropertyAttribute::ReadOnly);
404406

405407
bool is_declared_on_sandbox = ctx->sandbox()
406-
->GetRealNamedPropertyAttributes(ctx->context(), property)
408+
->GetRealNamedPropertyAttributes(context, property)
407409
.To(&attributes);
408410
read_only = read_only ||
409411
(static_cast<int>(attributes) &
@@ -441,7 +443,7 @@ void ContextifyContext::PropertySetterCallback(
441443
args.GetReturnValue().Set(false);
442444
}
443445

444-
USE(ctx->sandbox()->Set(ctx->context(), property, value));
446+
USE(ctx->sandbox()->Set(context, property, value));
445447
}
446448

447449
// static
@@ -482,7 +484,7 @@ void ContextifyContext::PropertyDefinerCallback(
482484

483485
auto attributes = PropertyAttribute::None;
484486
bool is_declared =
485-
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
487+
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
486488
property)
487489
.To(&attributes);
488490
bool read_only =
@@ -656,8 +658,10 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
656658
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
657659
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext);
658660

659-
target->Set(env->context(), class_name,
660-
script_tmpl->GetFunction(env->context()).ToLocalChecked()).Check();
661+
Local<Context> context = env->context();
662+
663+
target->Set(context, class_name,
664+
script_tmpl->GetFunction(context).ToLocalChecked()).Check();
661665
env->set_script_context_constructor_template(script_tmpl);
662666
}
663667

@@ -775,9 +779,10 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
775779
}
776780
contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked());
777781

782+
Local<Context> env_context = env->context();
778783
if (compile_options == ScriptCompiler::kConsumeCodeCache) {
779784
args.This()->Set(
780-
env->context(),
785+
env_context,
781786
env->cached_data_rejected_string(),
782787
Boolean::New(isolate, source.GetCachedData()->rejected)).Check();
783788
} else if (produce_cached_data) {
@@ -789,12 +794,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
789794
env,
790795
reinterpret_cast<const char*>(cached_data->data),
791796
cached_data->length);
792-
args.This()->Set(env->context(),
797+
args.This()->Set(env_context,
793798
env->cached_data_string(),
794799
buf.ToLocalChecked()).Check();
795800
}
796801
args.This()->Set(
797-
env->context(),
802+
env_context,
798803
env->cached_data_produced_string(),
799804
Boolean::New(isolate, cached_data_produced)).Check();
800805
}
@@ -884,7 +889,8 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
884889
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
885890
CHECK_NOT_NULL(contextify_context);
886891

887-
if (contextify_context->context().IsEmpty())
892+
Local<Context> context = contextify_context->context();
893+
if (context.IsEmpty())
888894
return;
889895

890896
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
@@ -903,7 +909,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
903909
bool break_on_first_line = args[4]->IsTrue();
904910

905911
// Do the eval within the context
906-
Context::Scope context_scope(contextify_context->context());
912+
Context::Scope context_scope(context);
907913
EvalMachine(contextify_context->env(),
908914
timeout,
909915
display_errors,

0 commit comments

Comments
 (0)