Skip to content

Commit 5ed770e

Browse files
XadillaXtargos
authored andcommitted
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 2e81ded commit 5ed770e

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/node_contextify.cc

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

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

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

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

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

404406
bool is_declared_on_sandbox = ctx->sandbox()
405-
->GetRealNamedPropertyAttributes(ctx->context(), property)
407+
->GetRealNamedPropertyAttributes(context, property)
406408
.To(&attributes);
407409
read_only = read_only ||
408410
(static_cast<int>(attributes) &
@@ -440,7 +442,7 @@ void ContextifyContext::PropertySetterCallback(
440442
args.GetReturnValue().Set(false);
441443
}
442444

443-
USE(ctx->sandbox()->Set(ctx->context(), property, value));
445+
USE(ctx->sandbox()->Set(context, property, value));
444446
}
445447

446448
// static
@@ -481,7 +483,7 @@ void ContextifyContext::PropertyDefinerCallback(
481483

482484
auto attributes = PropertyAttribute::None;
483485
bool is_declared =
484-
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
486+
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
485487
property)
486488
.To(&attributes);
487489
bool read_only =
@@ -655,8 +657,10 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
655657
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
656658
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext);
657659

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

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

783+
Local<Context> env_context = env->context();
779784
if (compile_options == ScriptCompiler::kConsumeCodeCache) {
780785
args.This()->Set(
781-
env->context(),
786+
env_context,
782787
env->cached_data_rejected_string(),
783788
Boolean::New(isolate, source.GetCachedData()->rejected)).Check();
784789
} else if (produce_cached_data) {
@@ -790,12 +795,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
790795
env,
791796
reinterpret_cast<const char*>(cached_data->data),
792797
cached_data->length);
793-
args.This()->Set(env->context(),
798+
args.This()->Set(env_context,
794799
env->cached_data_string(),
795800
buf.ToLocalChecked()).Check();
796801
}
797802
args.This()->Set(
798-
env->context(),
803+
env_context,
799804
env->cached_data_produced_string(),
800805
Boolean::New(isolate, cached_data_produced)).Check();
801806
}
@@ -885,7 +890,8 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
885890
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
886891
CHECK_NOT_NULL(contextify_context);
887892

888-
if (contextify_context->context().IsEmpty())
893+
Local<Context> context = contextify_context->context();
894+
if (context.IsEmpty())
889895
return;
890896

891897
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
@@ -904,7 +910,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
904910
bool break_on_first_line = args[4]->IsTrue();
905911

906912
// Do the eval within the context
907-
Context::Scope context_scope(contextify_context->context());
913+
Context::Scope context_scope(context);
908914
EvalMachine(contextify_context->env(),
909915
timeout,
910916
display_errors,

0 commit comments

Comments
 (0)