@@ -161,6 +161,9 @@ bool ShouldRewriteSourceMapURLs() {
161161 do {
162162 CFRunLoopRunInMode (kCFRunLoopDefaultMode , 0.1 , false );
163163 } while (this ->isWaitingForDebugger_ );
164+ if (this ->debuggerAttachedWhileWaiting_ ) {
165+ this ->pauseBeforeApp ();
166+ }
164167 });
165168 CFRunLoopWakeUp (CFRunLoopGetMain ());
166169 });
@@ -216,14 +219,13 @@ bool ShouldRewriteSourceMapURLs() {
216219 std::function<void (const std::string&)> sender) {
217220 std::lock_guard<std::mutex> lifecycle (this ->frontendLifecycleMutex_ );
218221
219- if (this ->isWaitingForDebugger_ ) {
220- this ->isWaitingForDebugger_ = NO ;
221- CFRunLoopRef runloop = CFRunLoopGetMain ();
222- CFRunLoopPerformBlock (runloop, (__bridge CFTypeRef)( NSRunLoopCommonModes ), ^{
223- CFRunLoopRunInMode ( kCFRunLoopDefaultMode , 1 , false );
224- this ->scheduleBreak () ;
222+ if (this ->isWaitingForDebugger_ && ! this -> debuggerAttachedWhileWaiting_ ) {
223+ this ->debuggerAttachedWhileWaiting_ = true ;
224+ // Frontends that don't send Runtime.runIfWaitingForDebugger still get the app
225+ // going, once they've had time to enable the debugger.
226+ dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , NSEC_PER_SEC * 3 ), dispatch_get_main_queue (), ^{
227+ this ->isWaitingForDebugger_ = NO ;
225228 });
226- CFRunLoopWakeUp (runloop);
227229 }
228230
229231 bool replacesFrontend;
@@ -748,6 +750,13 @@ bool ShouldRewriteSourceMapURLs() {
748750 auto json_message = json::parse (message);
749751 std::string method = json_message[" method" ];
750752
753+ // The frontend's last message before it expects the app to run, so it arrives
754+ // after the Debugger.enable that the pause depends on. The Runtime domain still
755+ // answers it below.
756+ if (method == " Runtime.runIfWaitingForDebugger" ) {
757+ this ->isWaitingForDebugger_ = NO ;
758+ }
759+
751760 // Note: the Tracing domain is handled in onFrontendMessageReceived, so it
752761 // never waits on the message queue or takes the Locker below.
753762
@@ -1097,26 +1106,21 @@ json WithSessionId(json message, const std::string& sessionId) {
10971106 return result;
10981107}
10991108
1100- void JsV8InspectorClient::scheduleBreak () {
1109+ void JsV8InspectorClient::pauseBeforeApp () {
11011110 Isolate* isolate = isolate_;
11021111 v8::Locker locker (isolate);
11031112 Isolate::Scope isolate_scope (isolate);
11041113 HandleScope handle_scope (isolate);
1105- auto context = isolate-> GetCurrentContext ();
1114+ Local<Context> context = tns::Caches::Get ( isolate)-> GetContext ();
11061115 Context::Scope context_scope (context);
11071116
1108- if (!this ->hasScheduledDebugBreak_ ) {
1109- this ->hasScheduledDebugBreak_ = true ;
1110- // hack: force a debugger; statement in ModuleInternal to actually break before loading the next
1111- // (main) script...
1112- // FIXME: find a proper fix to not need to resort to this hack.
1113- context->Global ()
1114- ->Set (context, tns::ToV8String (isolate, " __pauseOnNextRequire" ),
1115- v8::Boolean::New (isolate, true ))
1116- .ToChecked ();
1117+ // schedulePauseOnNextStatement doesn't break on the evaluation of the main
1118+ // module, and an ES module has no require call to stop at either. This stops
1119+ // before any app code has run, so its breakpoints can still be set.
1120+ Local<Script> script;
1121+ if (Script::Compile (context, tns::ToV8String (isolate, " debugger;" )).ToLocal (&script)) {
1122+ script->Run (context).IsEmpty ();
11171123 }
1118-
1119- this ->session_ ->schedulePauseOnNextStatement ({}, {});
11201124}
11211125
11221126void JsV8InspectorClient::registerModules () {
0 commit comments