From 2df3b953bf444e8b89a504e849d5cd321fbcb20b Mon Sep 17 00:00:00 2001 From: XadillaX Date: Mon, 22 Feb 2021 18:29:48 +0800 Subject: [PATCH 1/2] contextify: cache some context in locals Refs: https://github.com/nodejs/node/commit/66566df5773c8ef2f0e1181e8f9e47403e68c4b7 --- src/node_contextify.cc | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index a0acdb75eede98..79486132db5390 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -211,7 +211,8 @@ MaybeLocal ContextifyContext::CreateV8Context( return MaybeLocal(); } - ctx->SetSecurityToken(env->context()->GetSecurityToken()); + auto 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 @@ -220,7 +221,7 @@ MaybeLocal 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()); @@ -395,16 +396,17 @@ void ContextifyContext::PropertySetterCallback( if (ctx->context_.IsEmpty()) return; + Local 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(attributes) & static_cast(PropertyAttribute::ReadOnly); bool is_declared_on_sandbox = ctx->sandbox() - ->GetRealNamedPropertyAttributes(ctx->context(), property) + ->GetRealNamedPropertyAttributes(context, property) .To(&attributes); read_only = read_only || (static_cast(attributes) & @@ -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 @@ -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 = @@ -657,8 +659,10 @@ void ContextifyScript::Init(Environment* env, Local 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 = env->context(); + + target->Set(context, class_name, + script_tmpl->GetFunction(context).ToLocalChecked()).Check(); env->set_script_context_constructor_template(script_tmpl); } @@ -778,9 +782,10 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { } contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked()); + auto 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) { @@ -792,12 +797,12 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { env, reinterpret_cast(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(); } @@ -887,7 +892,8 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { ContextifyContext::ContextFromContextifiedSandbox(env, sandbox); CHECK_NOT_NULL(contextify_context); - if (contextify_context->context().IsEmpty()) + auto context = contextify_context->context(); + if (context.IsEmpty()) return; TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( @@ -906,7 +912,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& 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, From 0d624d88d167b864c71571920fc0e4444e7b898b Mon Sep 17 00:00:00 2001 From: XadillaX Date: Wed, 24 Feb 2021 16:49:36 +0800 Subject: [PATCH 2/2] f --- src/node_contextify.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 79486132db5390..fc6a73eda268dd 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -211,7 +211,7 @@ MaybeLocal ContextifyContext::CreateV8Context( return MaybeLocal(); } - auto context = env->context(); + Local context = env->context(); ctx->SetSecurityToken(context->GetSecurityToken()); // We need to tie the lifetime of the sandbox object with the lifetime of @@ -782,7 +782,7 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { } contextify_script->script_.Reset(isolate, v8_script.ToLocalChecked()); - auto env_context = env->context(); + Local env_context = env->context(); if (compile_options == ScriptCompiler::kConsumeCodeCache) { args.This()->Set( env_context, @@ -892,7 +892,7 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { ContextifyContext::ContextFromContextifiedSandbox(env, sandbox); CHECK_NOT_NULL(contextify_context); - auto context = contextify_context->context(); + Local context = contextify_context->context(); if (context.IsEmpty()) return;