Skip to content

Commit 86207bc

Browse files
committed
src: merge RunInThisContext() with RunInContext()
This commit resolves TODO in `RunInThisContext()` by merging `RunInThisContext()` with `RunInContext()`. Signed-off-by: Daeyeon Jeong [email protected]
1 parent 331088f commit 86207bc

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

lib/vm.js

Lines changed: 15 additions & 8 deletions
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,
@@ -122,17 +121,19 @@ class Script extends ContextifyScript {
122121
}
123122

124123
runInThisContext(options) {
125-
const { breakOnSigint, args } = getRunInContextArgs(options);
124+
const { breakOnSigint, args } = getRunInContextArgs(null, options);
126125
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
127-
return sigintHandlersWrap(super.runInThisContext, this, args);
126+
return sigintHandlersWrap(super.runInContext, this, args);
128127
}
129-
return ReflectApply(super.runInThisContext, this, args);
128+
return ReflectApply(super.runInContext, this, args);
130129
}
131130

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

155-
function getRunInContextArgs(options = {}) {
156+
function getRunInContextArgs(contextifiedObject, options = {}) {
156157
validateObject(options, 'options');
157158

158159
let timeout = options.timeout;
@@ -173,7 +174,13 @@ function getRunInContextArgs(options = {}) {
173174

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

src/node_contextify.cc

Lines changed: 22 additions & 48 deletions
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,35 @@ 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+
Environment* eval_env = nullptr;
856+
std::shared_ptr<v8::MicrotaskQueue> microtask_queue;
896857

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

901875
TRACE_EVENT0(TRACING_CATEGORY_NODE2(vm, script), "RunInContext");
902876

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

915889
// Do the eval within the context
916890
Context::Scope context_scope(context);
917-
EvalMachine(contextify_context->env(),
891+
EvalMachine(eval_env,
918892
timeout,
919893
display_errors,
920894
break_on_sigint,
921895
break_on_first_line,
922-
contextify_context->microtask_queue(),
896+
microtask_queue,
923897
args);
924898
}
925899

0 commit comments

Comments
 (0)