Skip to content

Commit fe1f740

Browse files
daeyeontargos
authored andcommitted
src: merge RunInThisContext() with RunInContext()
This commit resolves a TODO in `RunInThisContext()` by merging `RunInThisContext()` with `RunInContext()`. Signed-off-by: Daeyeon Jeong [email protected] PR-URL: #43225 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 5d8ee51 commit fe1f740

File tree

4 files changed

+36
-60
lines changed

4 files changed

+36
-60
lines changed

lib/vm.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
const {
2525
ArrayPrototypeForEach,
26-
ArrayPrototypeUnshift,
2726
Symbol,
2827
PromiseReject,
2928
ReflectApply,
@@ -123,17 +122,19 @@ class Script extends ContextifyScript {
123122
}
124123

125124
runInThisContext(options) {
126-
const { breakOnSigint, args } = getRunInContextArgs(options);
125+
const { breakOnSigint, args } = getRunInContextArgs(null, options);
127126
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
128-
return sigintHandlersWrap(super.runInThisContext, this, args);
127+
return sigintHandlersWrap(super.runInContext, this, args);
129128
}
130-
return ReflectApply(super.runInThisContext, this, args);
129+
return ReflectApply(super.runInContext, this, args);
131130
}
132131

133132
runInContext(contextifiedObject, options) {
134133
validateContext(contextifiedObject);
135-
const { breakOnSigint, args } = getRunInContextArgs(options);
136-
ArrayPrototypeUnshift(args, contextifiedObject);
134+
const { breakOnSigint, args } = getRunInContextArgs(
135+
contextifiedObject,
136+
options,
137+
);
137138
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
138139
return sigintHandlersWrap(super.runInContext, this, args);
139140
}
@@ -153,7 +154,7 @@ function validateContext(contextifiedObject) {
153154
}
154155
}
155156

156-
function getRunInContextArgs(options = kEmptyObject) {
157+
function getRunInContextArgs(contextifiedObject, options = kEmptyObject) {
157158
validateObject(options, 'options');
158159

159160
let timeout = options.timeout;
@@ -174,7 +175,13 @@ function getRunInContextArgs(options = kEmptyObject) {
174175

175176
return {
176177
breakOnSigint,
177-
args: [timeout, displayErrors, breakOnSigint, breakFirstLine]
178+
args: [
179+
contextifiedObject,
180+
timeout,
181+
displayErrors,
182+
breakOnSigint,
183+
breakFirstLine,
184+
],
178185
};
179186
}
180187

src/node_contextify.cc

+20-48
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
663663
script_tmpl->SetClassName(class_name);
664664
env->SetProtoMethod(script_tmpl, "createCachedData", CreateCachedData);
665665
env->SetProtoMethod(script_tmpl, "runInContext", RunInContext);
666-
env->SetProtoMethod(script_tmpl, "runInThisContext", RunInThisContext);
667666

668667
Local<Context> context = env->context();
669668

@@ -677,7 +676,6 @@ void ContextifyScript::RegisterExternalReferences(
677676
registry->Register(New);
678677
registry->Register(CreateCachedData);
679678
registry->Register(RunInContext);
680-
registry->Register(RunInThisContext);
681679
}
682680

683681
void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
@@ -844,59 +842,33 @@ void ContextifyScript::CreateCachedData(
844842
}
845843
}
846844

847-
void ContextifyScript::RunInThisContext(
848-
const FunctionCallbackInfo<Value>& args) {
849-
Environment* env = Environment::GetCurrent(args);
850-
851-
ContextifyScript* wrapped_script;
852-
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder());
853-
854-
TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInThisContext");
855-
856-
// TODO(addaleax): Use an options object or otherwise merge this with
857-
// RunInContext().
858-
CHECK_EQ(args.Length(), 4);
859-
860-
CHECK(args[0]->IsNumber());
861-
int64_t timeout = args[0]->IntegerValue(env->context()).FromJust();
862-
863-
CHECK(args[1]->IsBoolean());
864-
bool display_errors = args[1]->IsTrue();
865-
866-
CHECK(args[2]->IsBoolean());
867-
bool break_on_sigint = args[2]->IsTrue();
868-
869-
CHECK(args[3]->IsBoolean());
870-
bool break_on_first_line = args[3]->IsTrue();
871-
872-
// Do the eval within this context
873-
EvalMachine(env,
874-
timeout,
875-
display_errors,
876-
break_on_sigint,
877-
break_on_first_line,
878-
nullptr, // microtask_queue
879-
args);
880-
}
881-
882845
void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
883846
Environment* env = Environment::GetCurrent(args);
884847

885848
ContextifyScript* wrapped_script;
886849
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder());
887850

888851
CHECK_EQ(args.Length(), 5);
852+
CHECK(args[0]->IsObject() || args[0]->IsNull());
889853

890-
CHECK(args[0]->IsObject());
891-
Local<Object> sandbox = args[0].As<Object>();
892-
// Get the context from the sandbox
893-
ContextifyContext* contextify_context =
894-
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
895-
CHECK_NOT_NULL(contextify_context);
854+
Local<Context> context;
855+
std::shared_ptr<v8::MicrotaskQueue> microtask_queue;
896856

897-
Local<Context> context = contextify_context->context();
898-
if (context.IsEmpty())
899-
return;
857+
if (args[0]->IsObject()) {
858+
Local<Object> sandbox = args[0].As<Object>();
859+
// Get the context from the sandbox
860+
ContextifyContext* contextify_context =
861+
ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
862+
CHECK_NOT_NULL(contextify_context);
863+
CHECK_EQ(contextify_context->env(), env);
864+
865+
context = contextify_context->context();
866+
if (context.IsEmpty()) return;
867+
868+
microtask_queue = contextify_context->microtask_queue();
869+
} else {
870+
context = env->context();
871+
}
900872

901873
TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInContext");
902874

@@ -914,12 +886,12 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
914886

915887
// Do the eval within the context
916888
Context::Scope context_scope(context);
917-
EvalMachine(contextify_context->env(),
889+
EvalMachine(env,
918890
timeout,
919891
display_errors,
920892
break_on_sigint,
921893
break_on_first_line,
922-
contextify_context->microtask_queue(),
894+
microtask_queue,
923895
args);
924896
}
925897

src/node_contextify.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ class ContextifyScript : public BaseObject {
148148
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
149149
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
150150
static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
151-
static void CreateCachedData(
152-
const v8::FunctionCallbackInfo<v8::Value>& args);
153-
static void RunInThisContext(const v8::FunctionCallbackInfo<v8::Value>& args);
151+
static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
154152
static void RunInContext(const v8::FunctionCallbackInfo<v8::Value>& args);
155153
static bool EvalMachine(Environment* env,
156154
const int64_t timeout,

test/parallel/test-trace-events-vm.js

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const tmpdir = require('../common/tmpdir');
88

99
const names = [
1010
'ContextifyScript::New',
11-
'RunInThisContext',
1211
'RunInContext',
1312
];
1413

0 commit comments

Comments
 (0)