JSContext Inspection: Provide a way to use a non-Main RunLoop for Inspector JavaScript Evaluations
https://bugs.webkit.org/show_bug.cgi?id=134371

Reviewed by Timothy Hatcher.

* API/JSContextPrivate.h:
* API/JSContext.mm:
(-[JSContext _debuggerRunLoop]):
(-[JSContext _setDebuggerRunLoop:]):
Private API for setting the CFRunLoop for a debugger to evaluate in.

* API/JSContextRefInternal.h: Added.
* API/JSContextRef.cpp:
(JSGlobalContextGetDebuggerRunLoop):
(JSGlobalContextSetDebuggerRunLoop):
Internal API for setting a CFRunLoop on a JSContextRef.
Set this on the debuggable.

* inspector/remote/RemoteInspectorDebuggable.h:
* inspector/remote/RemoteInspectorDebuggableConnection.h:
(Inspector::RemoteInspectorBlock::RemoteInspectorBlock):
(Inspector::RemoteInspectorBlock::~RemoteInspectorBlock):
(Inspector::RemoteInspectorBlock::operator=):
(Inspector::RemoteInspectorBlock::operator()):
Moved into the header.

* runtime/JSGlobalObject.h:
(JSC::JSGlobalObject::inspectorDebuggable):
Lets store the RunLoop on the debuggable instead of this core
platform agnostic class, so expose the debuggable.

* inspector/remote/RemoteInspectorDebuggableConnection.mm:
(Inspector::RemoteInspectorHandleRunSourceGlobal):
(Inspector::RemoteInspectorQueueTaskOnGlobalQueue):
(Inspector::RemoteInspectorInitializeGlobalQueue):
Rename the global functions for clarity.

(Inspector::RemoteInspectorHandleRunSourceWithInfo):
Handler for private run loops.

(Inspector::RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection):
(Inspector::RemoteInspectorDebuggableConnection::~RemoteInspectorDebuggableConnection):
(Inspector::RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable):
(Inspector::RemoteInspectorDebuggableConnection::setupRunLoop):
(Inspector::RemoteInspectorDebuggableConnection::teardownRunLoop):
(Inspector::RemoteInspectorDebuggableConnection::queueTaskOnPrivateRunLoop):
Setup and teardown and use private run loop sources if the debuggable needs it.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@170589 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/API/JSContext.mm b/Source/JavaScriptCore/API/JSContext.mm
index ef59fef..701bdd1 100644
--- a/Source/JavaScriptCore/API/JSContext.mm
+++ b/Source/JavaScriptCore/API/JSContext.mm
@@ -29,7 +29,7 @@
 #import "JSCInlines.h"
 #import "JSContextInternal.h"
 #import "JSContextPrivate.h"
-#import "JSContextRefPrivate.h"
+#import "JSContextRefInternal.h"
 #import "JSGlobalObject.h"
 #import "JSValueInternal.h"
 #import "JSVirtualMachineInternal.h"
@@ -223,6 +223,16 @@
     JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(m_context, includeNativeCallStack);
 }
 
+- (CFRunLoopRef)_debuggerRunLoop
+{
+    return JSGlobalContextGetDebuggerRunLoop(m_context);
+}
+
+- (void)_setDebuggerRunLoop:(CFRunLoopRef)runLoop
+{
+    JSGlobalContextSetDebuggerRunLoop(m_context, runLoop);
+}
+
 @end
 
 @implementation JSContext(SubscriptSupport)
diff --git a/Source/JavaScriptCore/API/JSContextPrivate.h b/Source/JavaScriptCore/API/JSContextPrivate.h
index 8046019..7d1d0cb 100644
--- a/Source/JavaScriptCore/API/JSContextPrivate.h
+++ b/Source/JavaScriptCore/API/JSContextPrivate.h
@@ -44,6 +44,12 @@
 */
 @property (setter=_setIncludesNativeCallStackWhenReportingExceptions:) BOOL _includesNativeCallStackWhenReportingExceptions NS_AVAILABLE(10_10, 8_0);
 
+/*!
+@property
+@discussion Set the run loop the Web Inspector debugger should use when evaluating JavaScript in the JSContext.
+*/
+@property (setter=_setDebuggerRunLoop:) CFRunLoopRef _debuggerRunLoop NS_AVAILABLE(10_10, 8_0);
+
 @end
 
 #endif
diff --git a/Source/JavaScriptCore/API/JSContextRef.cpp b/Source/JavaScriptCore/API/JSContextRef.cpp
index 23e11f6..637b99d 100644
--- a/Source/JavaScriptCore/API/JSContextRef.cpp
+++ b/Source/JavaScriptCore/API/JSContextRef.cpp
@@ -25,7 +25,7 @@
 
 #include "config.h"
 #include "JSContextRef.h"
-#include "JSContextRefPrivate.h"
+#include "JSContextRefInternal.h"
 
 #include "APICast.h"
 #include "CallFrame.h"
@@ -41,6 +41,7 @@
 #include <wtf/text/StringHash.h>
 
 #if ENABLE(REMOTE_INSPECTOR)
+#include "JSGlobalObjectDebuggable.h"
 #include "JSGlobalObjectInspectorController.h"
 #endif
 
@@ -367,3 +368,40 @@
 #endif
 }
 
+#if USE(CF)
+CFRunLoopRef JSGlobalContextGetDebuggerRunLoop(JSGlobalContextRef ctx)
+{
+#if ENABLE(REMOTE_INSPECTOR)
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return nullptr;
+    }
+
+    ExecState* exec = toJS(ctx);
+    JSLockHolder lock(exec);
+
+    return exec->vmEntryGlobalObject()->inspectorDebuggable().debuggerRunLoop();
+#else
+    UNUSED_PARAM(ctx);
+    return nullptr;
+#endif
+}
+
+void JSGlobalContextSetDebuggerRunLoop(JSGlobalContextRef ctx, CFRunLoopRef runLoop)
+{
+#if ENABLE(REMOTE_INSPECTOR)
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return;
+    }
+
+    ExecState* exec = toJS(ctx);
+    JSLockHolder lock(exec);
+
+    exec->vmEntryGlobalObject()->inspectorDebuggable().setDebuggerRunLoop(runLoop);
+#else
+    UNUSED_PARAM(ctx);
+    UNUSED_PARAM(runLoop);
+#endif
+}
+#endif
diff --git a/Source/JavaScriptCore/API/JSContextRefInternal.h b/Source/JavaScriptCore/API/JSContextRefInternal.h
new file mode 100644
index 0000000..85632b8
--- /dev/null
+++ b/Source/JavaScriptCore/API/JSContextRefInternal.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2014 Apple Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef JSContextRefInternal_h
+#define JSContextRefInternal_h
+
+#include "JSContextRefPrivate.h"
+
+#if USE(CF)
+#include <CoreFoundation/CFRunLoop.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if USE(CF)
+/*!
+@function
+@abstract Gets the run loop used by the Web Inspector debugger when evaluating JavaScript in this context.
+@param ctx The JSGlobalContext whose setting you want to get.
+*/
+JS_EXPORT CFRunLoopRef JSGlobalContextGetDebuggerRunLoop(JSGlobalContextRef ctx) CF_AVAILABLE(10_10, 8_0);
+
+/*!
+@function
+@abstract Sets the run loop used by the Web Inspector debugger when evaluating JavaScript in this context.
+@param ctx The JSGlobalContext that you want to change.
+@param runLoop The new value of the setting for the context.
+*/
+JS_EXPORT void JSGlobalContextSetDebuggerRunLoop(JSGlobalContextRef ctx, CFRunLoopRef) CF_AVAILABLE(10_10, 8_0);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // JSContextRefInternal_h
diff --git a/Source/JavaScriptCore/API/JSContextRefPrivate.h b/Source/JavaScriptCore/API/JSContextRefPrivate.h
index 1650905..5a5bebd 100644
--- a/Source/JavaScriptCore/API/JSContextRefPrivate.h
+++ b/Source/JavaScriptCore/API/JSContextRefPrivate.h
@@ -126,7 +126,7 @@
 @param ctx The JSGlobalContext that you want to change.
 @param includeNativeCallStack The new value of the setting for the context.
 */
-JS_EXPORT void JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(JSGlobalContextRef ctx, bool includesNativeCallStack) CF_AVAILABLE(10_10, 8_0);    
+JS_EXPORT void JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(JSGlobalContextRef ctx, bool includesNativeCallStack) CF_AVAILABLE(10_10, 8_0);
 
 #ifdef __cplusplus
 }
diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog
index daa1606..6007579 100644
--- a/Source/JavaScriptCore/ChangeLog
+++ b/Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,53 @@
+2014-06-30  Joseph Pecoraro  <pecoraro@apple.com>
+
+        JSContext Inspection: Provide a way to use a non-Main RunLoop for Inspector JavaScript Evaluations
+        https://bugs.webkit.org/show_bug.cgi?id=134371
+
+        Reviewed by Timothy Hatcher.
+
+        * API/JSContextPrivate.h:
+        * API/JSContext.mm:
+        (-[JSContext _debuggerRunLoop]):
+        (-[JSContext _setDebuggerRunLoop:]):
+        Private API for setting the CFRunLoop for a debugger to evaluate in.
+        
+        * API/JSContextRefInternal.h: Added.
+        * API/JSContextRef.cpp:
+        (JSGlobalContextGetDebuggerRunLoop):
+        (JSGlobalContextSetDebuggerRunLoop):
+        Internal API for setting a CFRunLoop on a JSContextRef.
+        Set this on the debuggable.
+        
+        * inspector/remote/RemoteInspectorDebuggable.h:
+        * inspector/remote/RemoteInspectorDebuggableConnection.h:
+        (Inspector::RemoteInspectorBlock::RemoteInspectorBlock):
+        (Inspector::RemoteInspectorBlock::~RemoteInspectorBlock):
+        (Inspector::RemoteInspectorBlock::operator=):
+        (Inspector::RemoteInspectorBlock::operator()):
+        Moved into the header.
+
+        * runtime/JSGlobalObject.h:
+        (JSC::JSGlobalObject::inspectorDebuggable):
+        Lets store the RunLoop on the debuggable instead of this core
+        platform agnostic class, so expose the debuggable.
+
+        * inspector/remote/RemoteInspectorDebuggableConnection.mm:
+        (Inspector::RemoteInspectorHandleRunSourceGlobal):
+        (Inspector::RemoteInspectorQueueTaskOnGlobalQueue):
+        (Inspector::RemoteInspectorInitializeGlobalQueue):
+        Rename the global functions for clarity.
+
+        (Inspector::RemoteInspectorHandleRunSourceWithInfo):
+        Handler for private run loops.
+
+        (Inspector::RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection):
+        (Inspector::RemoteInspectorDebuggableConnection::~RemoteInspectorDebuggableConnection):
+        (Inspector::RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable):
+        (Inspector::RemoteInspectorDebuggableConnection::setupRunLoop):
+        (Inspector::RemoteInspectorDebuggableConnection::teardownRunLoop):
+        (Inspector::RemoteInspectorDebuggableConnection::queueTaskOnPrivateRunLoop):
+        Setup and teardown and use private run loop sources if the debuggable needs it.
+
 2014-06-30  Tibor Meszaros  <tmeszaros.u-szeged@partner.samsung.com>
 
         Add missing ENABLE(DFG_JIT) guards
diff --git a/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj b/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
index 81ccb1c..9a892ff 100644
--- a/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
+++ b/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
@@ -1332,6 +1332,7 @@
 		A5C3A1A618C0490200C9593A /* JSConsoleClient.h in Headers */ = {isa = PBXBuildFile; fileRef = A5C3A1A418C0490200C9593A /* JSConsoleClient.h */; };
 		A5CEEE14187F3BAD00E55C99 /* InspectorAgent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A5CEEE12187F3BAD00E55C99 /* InspectorAgent.cpp */; };
 		A5D0A1BB1862301B00C7B496 /* InspectorEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = A5D0A1BA1862301B00C7B496 /* InspectorEnvironment.h */; settings = {ATTRIBUTES = (Private, ); }; };
+		A5D2E665195E174000A518E7 /* JSContextRefInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = A5D2E664195E173800A518E7 /* JSContextRefInternal.h */; };
 		A5FD0067189AFE9C00633231 /* ScriptArguments.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A5FD0065189AFE9C00633231 /* ScriptArguments.cpp */; };
 		A5FD0068189AFE9C00633231 /* ScriptArguments.h in Headers */ = {isa = PBXBuildFile; fileRef = A5FD0066189AFE9C00633231 /* ScriptArguments.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		A5FD006D189B00AA00633231 /* ScriptCallFrame.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A5FD0069189B00A900633231 /* ScriptCallFrame.cpp */; };
@@ -2933,6 +2934,7 @@
 		A5CEEE12187F3BAD00E55C99 /* InspectorAgent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorAgent.cpp; sourceTree = "<group>"; };
 		A5CEEE13187F3BAD00E55C99 /* InspectorAgent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorAgent.h; sourceTree = "<group>"; };
 		A5D0A1BA1862301B00C7B496 /* InspectorEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorEnvironment.h; sourceTree = "<group>"; };
+		A5D2E664195E173800A518E7 /* JSContextRefInternal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSContextRefInternal.h; sourceTree = "<group>"; };
 		A5FD0065189AFE9C00633231 /* ScriptArguments.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScriptArguments.cpp; sourceTree = "<group>"; };
 		A5FD0066189AFE9C00633231 /* ScriptArguments.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScriptArguments.h; sourceTree = "<group>"; };
 		A5FD0069189B00A900633231 /* ScriptCallFrame.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScriptCallFrame.cpp; sourceTree = "<group>"; };
@@ -3965,6 +3967,7 @@
 				86E3C609167BAB87006D760A /* JSContextInternal.h */,
 				14BD5A290A3E91F600BAF59C /* JSContextRef.cpp */,
 				14BD5A2A0A3E91F600BAF59C /* JSContextRef.h */,
+				A5D2E664195E173800A518E7 /* JSContextRefInternal.h */,
 				148CD1D7108CF902008163C6 /* JSContextRefPrivate.h */,
 				A72028B41797601E0098028C /* JSCTestRunnerUtils.cpp */,
 				A72028B51797601E0098028C /* JSCTestRunnerUtils.h */,
@@ -5537,6 +5540,7 @@
 				14816E1C154CC56C00B8054C /* BlockAllocator.h in Headers */,
 				BC18C3EC0E16F5CD00B34460 /* BooleanObject.h in Headers */,
 				FEA08620182B7A0400F6D851 /* Breakpoint.h in Headers */,
+				A5D2E665195E174000A518E7 /* JSContextRefInternal.h in Headers */,
 				A7D801A51880D66E0026C39B /* BuiltinExecutables.h in Headers */,
 				A75EE9B218AAB7E200AAD043 /* BuiltinNames.h in Headers */,
 				0FB7F39715ED8E4600F167B2 /* Butterfly.h in Headers */,
diff --git a/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggable.h b/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggable.h
index 90274ad..d480968 100644
--- a/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggable.h
+++ b/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggable.h
@@ -28,6 +28,8 @@
 #ifndef RemoteInspectorDebuggable_h
 #define RemoteInspectorDebuggable_h
 
+#include <CoreFoundation/CFRunLoop.h>
+#include <wtf/RetainPtr.h>
 #include <wtf/text/WTFString.h>
 
 namespace Inspector {
@@ -49,6 +51,9 @@
     bool remoteDebuggingAllowed() const { return m_allowed; }
     void setRemoteDebuggingAllowed(bool);
 
+    CFRunLoopRef debuggerRunLoop() { return m_runLoop.get(); }
+    void setDebuggerRunLoop(CFRunLoopRef runLoop) { m_runLoop = runLoop; }
+
     RemoteInspectorDebuggableInfo info() const;
 
     enum DebuggableType { JavaScript, Web };
@@ -65,6 +70,7 @@
 private:
     unsigned m_identifier;
     bool m_allowed;
+    RetainPtr<CFRunLoopRef> m_runLoop;
 };
 
 struct RemoteInspectorDebuggableInfo {
diff --git a/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h b/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h
index 9d39a9a..2bec437 100644
--- a/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h
+++ b/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.h
@@ -38,6 +38,42 @@
 OBJC_CLASS NSString;
 
 namespace Inspector {
+    
+class RemoteInspectorBlock {
+public:
+    RemoteInspectorBlock(void (^task)())
+        : m_task(Block_copy(task))
+    {
+    }
+
+    RemoteInspectorBlock(const RemoteInspectorBlock& other)
+        : m_task(Block_copy(other.m_task))
+    {
+    }
+
+    ~RemoteInspectorBlock()
+    {
+        Block_release(m_task);
+    }
+
+    RemoteInspectorBlock& operator=(const RemoteInspectorBlock& other)
+    {
+        void (^oldTask)() = m_task;
+        m_task = Block_copy(other.m_task);
+        Block_release(oldTask);
+        return *this;
+    }
+
+    void operator()() const
+    {
+        m_task();
+    }
+
+private:
+    void (^m_task)();
+};
+
+typedef Vector<RemoteInspectorBlock> RemoteInspectorQueue;
 
 class RemoteInspectorDebuggableConnection final : public ThreadSafeRefCounted<RemoteInspectorDebuggableConnection>, public InspectorFrontendChannel {
 public:
@@ -56,14 +92,29 @@
     void sendMessageToBackend(NSString *);
     virtual bool sendMessageToFrontend(const String&) override;
 
+    std::mutex& queueMutex() { return m_queueMutex; }
+    RemoteInspectorQueue queue() const { return m_queue; }
+    void clearQueue() { m_queue.clear(); }
+
 private:
     void dispatchAsyncOnDebuggable(void (^block)());
 
+    void setupRunLoop();
+    void teardownRunLoop();
+    void queueTaskOnPrivateRunLoop(void (^block)());
+
     // This connection from the RemoteInspector singleton to the Debuggable
     // can be used on multiple threads. So any access to the debuggable
     // itself must take this mutex to ensure m_debuggable is valid.
     std::mutex m_debuggableMutex;
 
+    // If a debuggable has a specific run loop it wants to evaluate on
+    // we setup our run loop sources on that specific run loop.
+    RetainPtr<CFRunLoopRef> m_runLoop;
+    RetainPtr<CFRunLoopSourceRef> m_runLoopSource;
+    RemoteInspectorQueue m_queue;
+    std::mutex m_queueMutex;
+
     RemoteInspectorDebuggable* m_debuggable;
     RetainPtr<NSString> m_connectionIdentifier;
     RetainPtr<NSString> m_destination;
diff --git a/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm b/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm
index 4995dc1..9b2d008 100644
--- a/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm
+++ b/Source/JavaScriptCore/inspector/remote/RemoteInspectorDebuggableConnection.mm
@@ -38,46 +38,11 @@
 
 namespace Inspector {
 
-class RemoteInspectorBlock {
-public:
-    RemoteInspectorBlock(void (^task)())
-        : m_task(Block_copy(task))
-    {
-    }
-
-    RemoteInspectorBlock(const RemoteInspectorBlock& other)
-        : m_task(Block_copy(other.m_task))
-    {
-    }
-
-    ~RemoteInspectorBlock()
-    {
-        Block_release(m_task);
-    }
-
-    RemoteInspectorBlock& operator=(const RemoteInspectorBlock& other)
-    {
-        void (^oldTask)() = m_task;
-        m_task = Block_copy(other.m_task);
-        Block_release(oldTask);
-        return *this;
-    }
-
-    void operator()() const
-    {
-        m_task();
-    }
-
-private:
-    void (^m_task)();
-};
-
-typedef Vector<RemoteInspectorBlock> RemoteInspectorQueue;
 static std::mutex* rwiQueueMutex;
 static CFRunLoopSourceRef rwiRunLoopSource;
 static RemoteInspectorQueue* rwiQueue;
 
-static void RemoteInspectorHandleRunSource(void*)
+static void RemoteInspectorHandleRunSourceGlobal(void*)
 {
     ASSERT(CFRunLoopGetCurrent() == CFRunLoopGetMain());
     ASSERT(rwiQueueMutex);
@@ -95,7 +60,7 @@
         block();
 }
 
-static void RemoteInspectorQueueTask(void (^task)())
+static void RemoteInspectorQueueTaskOnGlobalQueue(void (^task)())
 {
     ASSERT(rwiQueueMutex);
     ASSERT(rwiRunLoopSource);
@@ -110,21 +75,38 @@
     CFRunLoopWakeUp(CFRunLoopGetMain());
 }
 
-static void RemoteInspectorInitializeQueue()
+static void RemoteInspectorInitializeGlobalQueue()
 {
     static dispatch_once_t pred;
     dispatch_once(&pred, ^{
         rwiQueue = new RemoteInspectorQueue;
         rwiQueueMutex = std::make_unique<std::mutex>().release();
 
-        CFRunLoopSourceContext runLoopSourceContext = {0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, RemoteInspectorHandleRunSource};
+        CFRunLoopSourceContext runLoopSourceContext = {0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, RemoteInspectorHandleRunSourceGlobal};
         rwiRunLoopSource = CFRunLoopSourceCreate(nullptr, 1, &runLoopSourceContext);
+
         // Add to the default run loop mode for default handling, and the JSContext remote inspector run loop mode when paused.
         CFRunLoopAddSource(CFRunLoopGetMain(), rwiRunLoopSource, kCFRunLoopDefaultMode);
         CFRunLoopAddSource(CFRunLoopGetMain(), rwiRunLoopSource, EventLoop::remoteInspectorRunLoopMode());
     });
 }
 
+static void RemoteInspectorHandleRunSourceWithInfo(void* info)
+{
+    RemoteInspectorDebuggableConnection *debuggableConnection = static_cast<RemoteInspectorDebuggableConnection*>(info);
+
+    RemoteInspectorQueue queueCopy;
+    {
+        std::lock_guard<std::mutex> lock(debuggableConnection->queueMutex());
+        queueCopy = debuggableConnection->queue();
+        debuggableConnection->clearQueue();
+    }
+
+    for (const auto& block : queueCopy)
+        block();
+}
+
+
 RemoteInspectorDebuggableConnection::RemoteInspectorDebuggableConnection(RemoteInspectorDebuggable* debuggable, NSString *connectionIdentifier, NSString *destination, RemoteInspectorDebuggable::DebuggableType)
     : m_debuggable(debuggable)
     , m_connectionIdentifier(connectionIdentifier)
@@ -132,11 +114,12 @@
     , m_identifier(debuggable->identifier())
     , m_connected(false)
 {
-    RemoteInspectorInitializeQueue();
+    setupRunLoop();
 }
 
 RemoteInspectorDebuggableConnection::~RemoteInspectorDebuggableConnection()
 {
+    teardownRunLoop();
 }
 
 NSString *RemoteInspectorDebuggableConnection::destination() const
@@ -151,6 +134,11 @@
 
 void RemoteInspectorDebuggableConnection::dispatchAsyncOnDebuggable(void (^block)())
 {
+    if (m_runLoop) {
+        queueTaskOnPrivateRunLoop(block);
+        return;
+    }
+
 #if PLATFORM(IOS)
     if (WebCoreWebThreadIsEnabled && WebCoreWebThreadIsEnabled()) {
         WebCoreWebThreadRun(block);
@@ -158,7 +146,7 @@
     }
 #endif
 
-    RemoteInspectorQueueTask(block);
+    RemoteInspectorQueueTaskOnGlobalQueue(block);
 }
 
 bool RemoteInspectorDebuggableConnection::setup()
@@ -237,6 +225,48 @@
     return true;
 }
 
+void RemoteInspectorDebuggableConnection::setupRunLoop()
+{
+    CFRunLoopRef debuggerRunLoop = m_debuggable->debuggerRunLoop();
+    if (!debuggerRunLoop) {
+        RemoteInspectorInitializeGlobalQueue();
+        return;
+    }
+
+    m_runLoop = debuggerRunLoop;
+
+    CFRunLoopSourceContext runLoopSourceContext = {0, this, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, RemoteInspectorHandleRunSourceWithInfo};
+    m_runLoopSource = CFRunLoopSourceCreate(nullptr, 1, &runLoopSourceContext);
+
+    CFRunLoopAddSource(m_runLoop.get(), m_runLoopSource.get(), kCFRunLoopDefaultMode);
+    CFRunLoopAddSource(m_runLoop.get(), m_runLoopSource.get(), EventLoop::remoteInspectorRunLoopMode());
+}
+
+void RemoteInspectorDebuggableConnection::teardownRunLoop()
+{
+    if (!m_runLoop)
+        return;
+
+    CFRunLoopRemoveSource(m_runLoop.get(), m_runLoopSource.get(), kCFRunLoopDefaultMode);
+    CFRunLoopRemoveSource(m_runLoop.get(), m_runLoopSource.get(), EventLoop::remoteInspectorRunLoopMode());
+
+    m_runLoop = nullptr;
+    m_runLoopSource = nullptr;
+}
+
+void RemoteInspectorDebuggableConnection::queueTaskOnPrivateRunLoop(void (^block)())
+{
+    ASSERT(m_runLoop);
+
+    {
+        std::lock_guard<std::mutex> lock(m_queueMutex);
+        m_queue.append(RemoteInspectorBlock(block));
+    }
+
+    CFRunLoopSourceSignal(m_runLoopSource.get());
+    CFRunLoopWakeUp(m_runLoop.get());
+}
+
 } // namespace Inspector
 
 #endif // ENABLE(REMOTE_INSPECTOR)
diff --git a/Source/JavaScriptCore/runtime/JSGlobalObject.h b/Source/JavaScriptCore/runtime/JSGlobalObject.h
index 094fad0..7bdcb72 100644
--- a/Source/JavaScriptCore/runtime/JSGlobalObject.h
+++ b/Source/JavaScriptCore/runtime/JSGlobalObject.h
@@ -456,6 +456,7 @@
 
 #if ENABLE(REMOTE_INSPECTOR)
     Inspector::JSGlobalObjectInspectorController& inspectorController() const { return *m_inspectorController.get(); }
+    JSGlobalObjectDebuggable& inspectorDebuggable() { return *m_inspectorDebuggable.get(); }
 #endif
 
     JS_EXPORT_PRIVATE void setConsoleClient(ConsoleClient* consoleClient) { m_consoleClient = consoleClient; }