Bug 1328964 - Split WorkletJSContext into WorkletJSRuntime to catch up w/bug 1343396. draft
authorJan-Ivar Bruaroey <jib@mozilla.com>
Tue, 16 May 2017 14:19:43 -0400
changeset 697792 7955d507ed43a2190b4660029755975ef9cec028
parent 697791 3d92d48afde11c1dfefe371c2ede99d3f6aafef5
child 697793 e6201b8e4e26cbf1507e525756a31a05f00ebaa4
child 769206 c1701f67204f17b94d18e93b6aa8c9a66ea6437c
push id89093
push userjbruaroey@mozilla.com
push dateTue, 14 Nov 2017 17:33:26 +0000
bugs1328964, 1343396
milestone59.0a1
Bug 1328964 - Split WorkletJSContext into WorkletJSRuntime to catch up w/bug 1343396. MozReview-Commit-ID: HCHUOhsviV4
dom/worklet/WorkletThread.cpp
dom/worklet/WorkletThread.h
--- a/dom/worklet/WorkletThread.cpp
+++ b/dom/worklet/WorkletThread.cpp
@@ -95,17 +95,61 @@ Wrap(JSContext *cx, JS::HandleObject exi
 const JSWrapObjectCallbacks WrapObjectCallbacks =
 {
   Wrap,
   nullptr,
 };
 
 } // namespace
 
-// This class controls CC in the worklet thread.
+// This classes control CC in the worklet thread.
+
+class WorkletJSRuntime final : public mozilla::CycleCollectedJSRuntime
+{
+public:
+  explicit WorkletJSRuntime(JSContext* aCx)
+    : CycleCollectedJSRuntime(aCx)
+  {
+  }
+
+  ~WorkletJSRuntime()
+  {
+  }
+
+  virtual void
+  PrepareForForgetSkippable() override
+  {
+  }
+
+  virtual void
+  BeginCycleCollectionCallback() override
+  {
+  }
+
+  virtual void
+  EndCycleCollectionCallback(CycleCollectorResults &aResults) override
+  {
+  }
+
+  virtual void
+  DispatchDeferredDeletion(bool aContinuation, bool aPurge) override
+  {
+    MOZ_ASSERT(!aContinuation);
+    nsCycleCollector_doDeferredDeletion();
+  }
+
+  virtual void
+  CustomGCCallback(JSGCStatus aStatus) override
+  {
+    if (aStatus == JSGC_END) {
+      nsCycleCollector_collect(nullptr);
+    }
+  }
+};
+
 class WorkletJSContext final : public CycleCollectedJSContext
 {
 public:
   explicit WorkletJSContext(WorkletThread* aWorkletThread)
     : mWorkletThread(aWorkletThread)
   {
     MOZ_ASSERT(aWorkletThread);
     MOZ_ASSERT(!NS_IsMainThread());
@@ -123,23 +167,28 @@ public:
     }
 
     delete static_cast<WorkletThreadContextPrivate*>(JS_GetContextPrivate(cx));
     JS_SetContextPrivate(cx, nullptr);
 
     nsCycleCollector_shutdown();
   }
 
+  CycleCollectedJSRuntime* CreateRuntime(JSContext* aCx) override
+  {
+    return new WorkletJSRuntime(aCx);
+  }
+
   nsresult
-  Initialize(JSContext* aParentContext)
+  Initialize(JSRuntime* aParentRuntime)
   {
     MOZ_ASSERT(!NS_IsMainThread());
 
     nsresult rv =
-      CycleCollectedJSContext::Initialize(aParentContext,
+      CycleCollectedJSContext::Initialize(aParentRuntime,
                                           WORKLET_DEFAULT_RUNTIME_HEAPSIZE,
                                           WORKLET_DEFAULT_NURSERY_SIZE);
      if (NS_WARN_IF(NS_FAILED(rv))) {
        return rv;
      }
 
     JSContext* cx = Context();
 
@@ -149,46 +198,16 @@ public:
     JS_InitDestroyPrincipalsCallback(cx, DestroyWorkletPrincipals);
     JS_SetWrapObjectCallbacks(cx, &WrapObjectCallbacks);
     JS_SetFutexCanWait(cx);
 
     return NS_OK;
   }
 
   void
-  PrepareForForgetSkippable() override
-  {
-  }
-
-  void
-  BeginCycleCollectionCallback() override
-  {
-  }
-
-  void
-  EndCycleCollectionCallback(CycleCollectorResults &aResults) override
-  {
-  }
-
-  void
-  DispatchDeferredDeletion(bool aContinuation, bool aPurge) override
-  {
-    MOZ_ASSERT(!aContinuation);
-    nsCycleCollector_doDeferredDeletion();
-  }
-
-  void
-  CustomGCCallback(JSGCStatus aStatus) override
-  {
-    if (aStatus == JSGC_END) {
-      nsCycleCollector_collect(nullptr);
-    }
-  }
-
-  void
   AfterProcessTask(uint32_t aRecursionDepth) override
   {
     CycleCollectedJSContext::AfterProcessTask(aRecursionDepth);
   }
 
   void
   DispatchToMicroTask(already_AddRefed<nsIRunnable> aRunnable) override
   {
@@ -223,31 +242,31 @@ class WorkletThread::PrimaryRunnable fin
 public:
   explicit PrimaryRunnable(WorkletThread* aWorkletThread)
     : Runnable("WorkletThread::PrimaryRunnable")
     , mWorkletThread(aWorkletThread)
   {
     MOZ_ASSERT(aWorkletThread);
     MOZ_ASSERT(NS_IsMainThread());
 
-    mParentContext =
-      JS_GetParentContext(CycleCollectedJSContext::Get()->Context());
-    MOZ_ASSERT(mParentContext);
+    mParentRuntime =
+      JS_GetParentRuntime(CycleCollectedJSContext::Get()->Context());
+    MOZ_ASSERT(mParentRuntime);
   }
 
   NS_IMETHOD
   Run() override
   {
-    mWorkletThread->RunEventLoop(mParentContext);
+    mWorkletThread->RunEventLoop(mParentRuntime);
     return NS_OK;
   }
 
 private:
   RefPtr<WorkletThread> mWorkletThread;
-  JSContext* mParentContext;
+  JSRuntime* mParentRuntime;
 };
 
 // This is the last runnable to be dispatched. It calls the TerminateInternal()
 class WorkletThread::TerminateRunnable final : public Runnable
 {
 public:
   explicit TerminateRunnable(WorkletThread* aWorkletThread)
     : Runnable("WorkletThread::TerminateRunnable")
@@ -329,24 +348,24 @@ WorkletThread::Dispatch(already_AddRefed
 
 NS_IMETHODIMP
 WorkletThread::DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t aFlags)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 void
-WorkletThread::RunEventLoop(JSContext* aParentContext)
+WorkletThread::RunEventLoop(JSRuntime* aParentRuntime)
 {
   MOZ_ASSERT(!NS_IsMainThread());
 
   PR_SetCurrentThreadName("worklet");
 
   WorkletJSContext context(this);
-  nsresult rv = context.Initialize(aParentContext);
+  nsresult rv = context.Initialize(aParentRuntime);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     // TODO: error propagation
     return;
   }
 
   // FIXME: JS_SetDefaultLocale
   // FIXME: JSSettings
   // FIXME: JS_SetNativeStackQuota
--- a/dom/worklet/WorkletThread.h
+++ b/dom/worklet/WorkletThread.h
@@ -8,18 +8,16 @@
 #define mozilla_dom_worklet_WorkletThread_h
 
 #include "mozilla/Attributes.h"
 #include "mozilla/CondVar.h"
 #include "mozilla/RefPtr.h"
 #include "mozilla/UniquePtr.h"
 #include "nsThread.h"
 
-struct JSContext;
-struct JSPrincipals;
 class nsIRunnable;
 
 namespace mozilla {
 namespace dom {
 
 class WorkletThread final : public nsThread
 {
 public:
@@ -49,17 +47,17 @@ public:
   void
   Terminate();
 
 private:
   WorkletThread();
   ~WorkletThread();
 
   void
-  RunEventLoop(JSContext* aParentContext);
+  RunEventLoop(JSRuntime* aParentRuntime);
   class PrimaryRunnable;
 
   void
   TerminateInternal();
   class TerminateRunnable;
 
   // This should only be called by consumers that have an
   // nsIEventTarget/nsIThread pointer.