@@ -254,6 +254,9 @@ bool GetInt32(const json& value, long long& out) {
254254 do {
255255 CFRunLoopRunInMode (kCFRunLoopDefaultMode , 0.1 , false );
256256 } while (this ->isWaitingForDebugger_ );
257+ if (this ->debuggerAttachedWhileWaiting_ ) {
258+ this ->pauseBeforeApp ();
259+ }
257260 });
258261 CFRunLoopWakeUp (CFRunLoopGetMain ());
259262 });
@@ -309,14 +312,13 @@ bool GetInt32(const json& value, long long& out) {
309312 std::function<void (const std::string&)> sender) {
310313 std::lock_guard<std::mutex> lifecycle (this ->frontendLifecycleMutex_ );
311314
312- if (this ->isWaitingForDebugger_ ) {
313- this ->isWaitingForDebugger_ = NO ;
314- CFRunLoopRef runloop = CFRunLoopGetMain ();
315- CFRunLoopPerformBlock (runloop, (__bridge CFTypeRef)( NSRunLoopCommonModes ), ^{
316- CFRunLoopRunInMode ( kCFRunLoopDefaultMode , 1 , false );
317- this ->scheduleBreak () ;
315+ if (this ->isWaitingForDebugger_ && ! this -> debuggerAttachedWhileWaiting_ ) {
316+ this ->debuggerAttachedWhileWaiting_ = true ;
317+ // Frontends that don't send Runtime.runIfWaitingForDebugger still get the app
318+ // going, once they've had time to enable the debugger.
319+ dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW , NSEC_PER_SEC * 3 ), dispatch_get_main_queue (), ^{
320+ this ->isWaitingForDebugger_ = NO ;
318321 });
319- CFRunLoopWakeUp (runloop);
320322 }
321323
322324 bool replacesFrontend;
@@ -854,6 +856,13 @@ bool GetInt32(const json& value, long long& out) {
854856 auto json_message = json::parse (message);
855857 std::string method = json_message[" method" ];
856858
859+ // The frontend's last message before it expects the app to run, so it arrives
860+ // after the Debugger.enable that the pause depends on. The Runtime domain still
861+ // answers it below.
862+ if (method == " Runtime.runIfWaitingForDebugger" ) {
863+ this ->isWaitingForDebugger_ = NO ;
864+ }
865+
857866 // Note: the Tracing domain is handled in onFrontendMessageReceived, so it
858867 // never waits on the message queue or takes the Locker below.
859868
@@ -1242,26 +1251,21 @@ bool GetInt32(const json& value, long long& out) {
12421251 return result;
12431252}
12441253
1245- void JsV8InspectorClient::scheduleBreak () {
1254+ void JsV8InspectorClient::pauseBeforeApp () {
12461255 Isolate* isolate = isolate_;
12471256 v8::Locker locker (isolate);
12481257 Isolate::Scope isolate_scope (isolate);
12491258 HandleScope handle_scope (isolate);
1250- auto context = isolate-> GetCurrentContext ();
1259+ Local<Context> context = tns::Caches::Get ( isolate)-> GetContext ();
12511260 Context::Scope context_scope (context);
12521261
1253- if (!this ->hasScheduledDebugBreak_ ) {
1254- this ->hasScheduledDebugBreak_ = true ;
1255- // hack: force a debugger; statement in ModuleInternal to actually break before loading the next
1256- // (main) script...
1257- // FIXME: find a proper fix to not need to resort to this hack.
1258- context->Global ()
1259- ->Set (context, tns::ToV8String (isolate, " __pauseOnNextRequire" ),
1260- v8::Boolean::New (isolate, true ))
1261- .ToChecked ();
1262+ // schedulePauseOnNextStatement doesn't break on the evaluation of the main
1263+ // module, and an ES module has no require call to stop at either. This stops
1264+ // before any app code has run, so its breakpoints can still be set.
1265+ Local<Script> script;
1266+ if (Script::Compile (context, tns::ToV8String (isolate, " debugger;" )).ToLocal (&script)) {
1267+ script->Run (context).IsEmpty ();
12621268 }
1263-
1264- this ->session_ ->schedulePauseOnNextStatement ({}, {});
12651269}
12661270
12671271void JsV8InspectorClient::registerModules () {
0 commit comments