Bug 1256424. Get rid of ThreadsafeAutoSafeJSContext. r=bholley
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 14 Mar 2016 20:48:39 -0400
changeset 288672 21e5497ebb7269dde3315ec97d28243fe09f72de
parent 288671 dca912a377eac9a3f2a149a9edb08c84631acea4
child 288673 a5365af3f8c6b1b308286328818ed273cb1a24c7
push id30087
push usercbook@mozilla.com
push dateTue, 15 Mar 2016 09:43:43 +0000
treeherdermozilla-central@5e14887312d4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1256424
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1256424. Get rid of ThreadsafeAutoSafeJSContext. r=bholley
dom/base/DOMRequest.cpp
dom/base/ScriptSettings.cpp
dom/base/ScriptSettings.h
dom/promise/Promise.cpp
--- a/dom/base/DOMRequest.cpp
+++ b/dom/base/DOMRequest.cpp
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "DOMRequest.h"
 
 #include "DOMError.h"
 #include "nsThreadUtils.h"
 #include "DOMCursor.h"
+#include "mozilla/CycleCollectedJSRuntime.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/dom/Event.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "jsfriendapi.h"
 
 using mozilla::dom::AnyCallback;
 using mozilla::dom::DOMError;
@@ -292,35 +293,34 @@ DOMRequestService::FireDetailedError(nsI
   static_cast<DOMRequest*>(aRequest)->FireDetailedError(err);
 
   return NS_OK;
 }
 
 class FireSuccessAsyncTask : public nsRunnable
 {
 
-  FireSuccessAsyncTask(JSContext* aCx,
-                       DOMRequest* aRequest,
+  FireSuccessAsyncTask(DOMRequest* aRequest,
                        const JS::Value& aResult) :
     mReq(aRequest),
-    mResult(aCx, aResult)
+    mResult(CycleCollectedJSRuntime::Get()->Runtime(), aResult)
   {
   }
 
 public:
 
   // Due to the fact that initialization can fail during shutdown (since we
   // can't fetch a js context), set up an initiatization function to make sure
   // we can return the failure appropriately
   static nsresult
   Dispatch(DOMRequest* aRequest,
            const JS::Value& aResult)
   {
-    mozilla::ThreadsafeAutoSafeJSContext cx;
-    RefPtr<FireSuccessAsyncTask> asyncTask = new FireSuccessAsyncTask(cx, aRequest, aResult);
+    RefPtr<FireSuccessAsyncTask> asyncTask =
+      new FireSuccessAsyncTask(aRequest, aResult);
     MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(asyncTask)));
     return NS_OK;
   }
 
   NS_IMETHODIMP
   Run()
   {
     mReq->FireSuccess(JS::Handle<JS::Value>::fromMarkedLocation(mResult.address()));
--- a/dom/base/ScriptSettings.cpp
+++ b/dom/base/ScriptSettings.cpp
@@ -820,31 +820,9 @@ ThreadsafeAutoJSContext::operator JSCont
 }
 
 AutoSafeJSContext::AutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
   : AutoJSContext(true MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT)
   , mAc(mCx, xpc::UnprivilegedJunkScope())
 {
 }
 
-ThreadsafeAutoSafeJSContext::ThreadsafeAutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
-{
-  MOZ_GUARD_OBJECT_NOTIFIER_INIT;
-
-  if (NS_IsMainThread()) {
-    mCx = nullptr;
-    mAutoSafeJSContext.emplace();
-  } else {
-    mCx = mozilla::dom::workers::GetCurrentThreadJSContext();
-    mRequest.emplace(mCx);
-  }
-}
-
-ThreadsafeAutoSafeJSContext::operator JSContext*() const
-{
-  if (mCx) {
-    return mCx;
-  } else {
-    return *mAutoSafeJSContext;
-  }
-}
-
 } // namespace mozilla
--- a/dom/base/ScriptSettings.h
+++ b/dom/base/ScriptSettings.h
@@ -463,27 +463,11 @@ private:
  */
 class MOZ_RAII AutoSafeJSContext : public AutoJSContext {
 public:
   explicit AutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM);
 private:
   JSAutoCompartment mAc;
 };
 
-/**
- * Like AutoSafeJSContext but can be used safely on worker threads.
- */
-class MOZ_RAII ThreadsafeAutoSafeJSContext {
-public:
-  explicit ThreadsafeAutoSafeJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM);
-  operator JSContext*() const;
-
-private:
-  JSContext* mCx; // Used on workers.  Null means mainthread.
-  Maybe<JSAutoRequest> mRequest; // Used on workers.
-  Maybe<AutoSafeJSContext> mAutoSafeJSContext; // Used on main thread.
-  MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
-};
-
-
 } // namespace mozilla
 
 #endif // mozilla_dom_ScriptSettings_h
--- a/dom/promise/Promise.cpp
+++ b/dom/promise/Promise.cpp
@@ -1757,31 +1757,32 @@ private:
     mozilla::DropJSObjects(this);
   }
 
 public:
   void SetValue(uint32_t index, const JS::Handle<JS::Value> aValue)
   {
     MOZ_ASSERT(mCountdown > 0);
 
-    ThreadsafeAutoSafeJSContext cx;
-    JSAutoCompartment ac(cx, mValues);
-    {
-
-      AutoDontReportUncaught silenceReporting(cx);
-      JS::Rooted<JS::Value> value(cx, aValue);
-      JS::Rooted<JSObject*> values(cx, mValues);
-      if (!JS_WrapValue(cx, &value) ||
-          !JS_DefineElement(cx, values, index, value, JSPROP_ENUMERATE)) {
-        MOZ_ASSERT(JS_IsExceptionPending(cx));
-        JS::Rooted<JS::Value> exn(cx);
-        JS_GetPendingException(cx, &exn);
-
-        mPromise->MaybeReject(cx, exn);
-      }
+    AutoJSAPI jsapi;
+    if (!jsapi.Init(mValues)) {
+      // Now what?
+      return;
+    }
+    jsapi.TakeOwnershipOfErrorReporting();
+    JSContext* cx = jsapi.cx();
+
+    JS::Rooted<JS::Value> value(cx, aValue);
+    JS::Rooted<JSObject*> values(cx, mValues);
+    if (!JS_WrapValue(cx, &value) ||
+        !JS_DefineElement(cx, values, index, value, JSPROP_ENUMERATE)) {
+      MOZ_ASSERT(JS_IsExceptionPending(cx));
+      JS::Rooted<JS::Value> exn(cx);
+      jsapi.StealException(&exn);
+      mPromise->MaybeReject(cx, exn);
     }
 
     --mCountdown;
     if (mCountdown == 0) {
       JS::Rooted<JS::Value> result(cx, JS::ObjectValue(*mValues));
       mPromise->MaybeResolve(cx, result);
     }
   }