@@ -663,7 +663,6 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
663
663
script_tmpl->SetClassName (class_name);
664
664
env->SetProtoMethod (script_tmpl, " createCachedData" , CreateCachedData);
665
665
env->SetProtoMethod (script_tmpl, " runInContext" , RunInContext);
666
- env->SetProtoMethod (script_tmpl, " runInThisContext" , RunInThisContext);
667
666
668
667
Local<Context> context = env->context ();
669
668
@@ -677,7 +676,6 @@ void ContextifyScript::RegisterExternalReferences(
677
676
registry->Register (New);
678
677
registry->Register (CreateCachedData);
679
678
registry->Register (RunInContext);
680
- registry->Register (RunInThisContext);
681
679
}
682
680
683
681
void ContextifyScript::New (const FunctionCallbackInfo<Value>& args) {
@@ -844,59 +842,35 @@ void ContextifyScript::CreateCachedData(
844
842
}
845
843
}
846
844
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
-
882
845
void ContextifyScript::RunInContext (const FunctionCallbackInfo<Value>& args) {
883
846
Environment* env = Environment::GetCurrent (args);
884
847
885
848
ContextifyScript* wrapped_script;
886
849
ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.Holder ());
887
850
888
851
CHECK_EQ (args.Length (), 5 );
852
+ CHECK (args[0 ]->IsObject () || args[0 ]->IsNull ());
889
853
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;
896
857
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
+ }
900
874
901
875
TRACE_EVENT0 (TRACING_CATEGORY_NODE2 (vm, script), " RunInContext" );
902
876
@@ -914,12 +888,12 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
914
888
915
889
// Do the eval within the context
916
890
Context::Scope context_scope (context);
917
- EvalMachine (contextify_context-> env () ,
891
+ EvalMachine (eval_env ,
918
892
timeout,
919
893
display_errors,
920
894
break_on_sigint,
921
895
break_on_first_line,
922
- contextify_context-> microtask_queue () ,
896
+ microtask_queue,
923
897
args);
924
898
}
925
899
0 commit comments