Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contextify: cache some context in locals #37473

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
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
// newly created context. We do this by making them hold references to each
Expand All @@ -220,7 +221,7 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
// directly in an Object, we instead hold onto the new context's global
// object instead (which then has a reference to the context).
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj);
sandbox_obj->SetPrivate(env->context(),
sandbox_obj->SetPrivate(context,
env->contextify_global_private_symbol(),
ctx->Global());

Expand Down Expand Up @@ -395,16 +396,17 @@ void ContextifyContext::PropertySetterCallback(
if (ctx->context_.IsEmpty())
return;

Local<Context> context = ctx->context();
auto attributes = PropertyAttribute::None;
bool is_declared_on_global_proxy = ctx->global_proxy()
->GetRealNamedPropertyAttributes(ctx->context(), property)
->GetRealNamedPropertyAttributes(context, property)
.To(&attributes);
bool read_only =
static_cast<int>(attributes) &
static_cast<int>(PropertyAttribute::ReadOnly);

bool is_declared_on_sandbox = ctx->sandbox()
->GetRealNamedPropertyAttributes(ctx->context(), property)
->GetRealNamedPropertyAttributes(context, property)
.To(&attributes);
read_only = read_only ||
(static_cast<int>(attributes) &
Expand Down Expand Up @@ -442,7 +444,7 @@ void ContextifyContext::PropertySetterCallback(
args.GetReturnValue().Set(false);
}

USE(ctx->sandbox()->Set(ctx->context(), property, value));
USE(ctx->sandbox()->Set(context, property, value));
}

// static
Expand Down Expand Up @@ -483,7 +485,7 @@ void ContextifyContext::PropertyDefinerCallback(

auto attributes = PropertyAttribute::None;
bool is_declared =
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
property)
.To(&attributes);
bool read_only =
Expand Down Expand Up @@ -657,8 +659,10 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext);

target->Set(env->context(), class_name,
script_tmpl->GetFunction(env->context()).ToLocalChecked()).Check();
Local<Context> context = env->context();

target->Set(context, class_name,
script_tmpl->GetFunction(context).ToLocalChecked()).Check();
env->set_script_context_constructor_template(script_tmpl);
}

Expand Down Expand Up @@ -778,9 +782,10 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
}
contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked());

Local<Context> env_context = env->context();
if (compile_options == ScriptCompiler::kConsumeCodeCache) {
args.This()->Set(
env->context(),
env_context,
env->cached_data_rejected_string(),
Boolean::New(isolate, source.GetCachedData()->rejected)).Check();
} else if (produce_cached_data) {
Expand All @@ -792,12 +797,12 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
env,
reinterpret_cast<const char*>(cached_data->data),
cached_data->length);
args.This()->Set(env->context(),
args.This()->Set(env_context,
env->cached_data_string(),
buf.ToLocalChecked()).Check();
}
args.This()->Set(
env->context(),
env_context,
env->cached_data_produced_string(),
Boolean::New(isolate, cached_data_produced)).Check();
}
Expand Down Expand Up @@ -887,7 +892,8 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
CHECK_NOT_NULL(contextify_context);

if (contextify_context->context().IsEmpty())
Local<Context> context = contextify_context->context();
if (context.IsEmpty())
return;

TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
Expand All @@ -906,7 +912,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
bool break_on_first_line = args[4]->IsTrue();

// Do the eval within the context
Context::Scope context_scope(contextify_context->context());
Context::Scope context_scope(context);
EvalMachine(contextify_context->env(),
timeout,
display_errors,
Expand Down