@@ -671,7 +671,6 @@ void ContextifyScript::Init(Environment* env, Local<Object> target) {
671671 script_tmpl->SetClassName (class_name);
672672 env->SetProtoMethod (script_tmpl, " createCachedData" , CreateCachedData);
673673 env->SetProtoMethod (script_tmpl, " runInContext" , RunInContext);
674- env->SetProtoMethod (script_tmpl, " runInThisContext" , RunInThisContext);
675674
676675 Local<Context> context = env->context ();
677676
@@ -685,7 +684,6 @@ void ContextifyScript::RegisterExternalReferences(
685684 registry->Register (New);
686685 registry->Register (CreateCachedData);
687686 registry->Register (RunInContext);
688- registry->Register (RunInThisContext);
689687}
690688
691689void ContextifyScript::New (const FunctionCallbackInfo<Value>& args) {
@@ -852,59 +850,33 @@ void ContextifyScript::CreateCachedData(
852850 }
853851}
854852
855- void ContextifyScript::RunInThisContext (
856- const FunctionCallbackInfo<Value>& args) {
857- Environment* env = Environment::GetCurrent (args);
858-
859- ContextifyScript* wrapped_script;
860- ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.Holder ());
861-
862- TRACE_EVENT0 (TRACING_CATEGORY_NODE2 (vm, script), " RunInThisContext" );
863-
864- // TODO(addaleax): Use an options object or otherwise merge this with
865- // RunInContext().
866- CHECK_EQ (args.Length (), 4 );
867-
868- CHECK (args[0 ]->IsNumber ());
869- int64_t timeout = args[0 ]->IntegerValue (env->context ()).FromJust ();
870-
871- CHECK (args[1 ]->IsBoolean ());
872- bool display_errors = args[1 ]->IsTrue ();
873-
874- CHECK (args[2 ]->IsBoolean ());
875- bool break_on_sigint = args[2 ]->IsTrue ();
876-
877- CHECK (args[3 ]->IsBoolean ());
878- bool break_on_first_line = args[3 ]->IsTrue ();
879-
880- // Do the eval within this context
881- EvalMachine (env,
882- timeout,
883- display_errors,
884- break_on_sigint,
885- break_on_first_line,
886- nullptr , // microtask_queue
887- args);
888- }
889-
890853void ContextifyScript::RunInContext (const FunctionCallbackInfo<Value>& args) {
891854 Environment* env = Environment::GetCurrent (args);
892855
893856 ContextifyScript* wrapped_script;
894857 ASSIGN_OR_RETURN_UNWRAP (&wrapped_script, args.Holder ());
895858
896859 CHECK_EQ (args.Length (), 5 );
860+ CHECK (args[0 ]->IsObject () || args[0 ]->IsNull ());
897861
898- CHECK (args[0 ]->IsObject ());
899- Local<Object> sandbox = args[0 ].As <Object>();
900- // Get the context from the sandbox
901- ContextifyContext* contextify_context =
902- ContextifyContext::ContextFromContextifiedSandbox (env, sandbox);
903- CHECK_NOT_NULL (contextify_context);
862+ Local<Context> context;
863+ std::shared_ptr<v8::MicrotaskQueue> microtask_queue;
904864
905- Local<Context> context = contextify_context->context ();
906- if (context.IsEmpty ())
907- return ;
865+ if (args[0 ]->IsObject ()) {
866+ Local<Object> sandbox = args[0 ].As <Object>();
867+ // Get the context from the sandbox
868+ ContextifyContext* contextify_context =
869+ ContextifyContext::ContextFromContextifiedSandbox (env, sandbox);
870+ CHECK_NOT_NULL (contextify_context);
871+ CHECK_EQ (contextify_context->env (), env);
872+
873+ context = contextify_context->context ();
874+ if (context.IsEmpty ()) return ;
875+
876+ microtask_queue = contextify_context->microtask_queue ();
877+ } else {
878+ context = env->context ();
879+ }
908880
909881 TRACE_EVENT0 (TRACING_CATEGORY_NODE2 (vm, script), " RunInContext" );
910882
@@ -922,12 +894,12 @@ void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
922894
923895 // Do the eval within the context
924896 Context::Scope context_scope (context);
925- EvalMachine (contextify_context-> env () ,
897+ EvalMachine (env,
926898 timeout,
927899 display_errors,
928900 break_on_sigint,
929901 break_on_first_line,
930- contextify_context-> microtask_queue () ,
902+ microtask_queue,
931903 args);
932904}
933905
0 commit comments