Bug 1257725 part 3. Get rid of ThreadsafeAutoJSContext usage in Promise code. r=bholley
authorBoris Zbarsky <bzbarsky@mit.edu>
Wed, 23 Mar 2016 11:02:57 -0400
changeset 290064 6c701e2a30470c3ae6102be68ff20edfd3ad685d
parent 290063 12c94a351403a56c89bf76a0e361116d34ab8458
child 290065 16ddfdbf6ff40842cca65951f0709d54980e7296
push id30114
push usercbook@mozilla.com
push dateThu, 24 Mar 2016 15:15:54 +0000
treeherdermozilla-central@24c5fbde4488 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1257725
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 1257725 part 3. Get rid of ThreadsafeAutoJSContext usage in Promise code. r=bholley
dom/promise/Promise.cpp
dom/promise/Promise.h
--- a/dom/promise/Promise.cpp
+++ b/dom/promise/Promise.cpp
@@ -74,20 +74,24 @@ public:
     MOZ_COUNT_DTOR(PromiseReactionJob);
   }
 
 protected:
   NS_IMETHOD
   Run() override
   {
     NS_ASSERT_OWNINGTHREAD(PromiseReactionJob);
-    ThreadsafeAutoJSContext cx;
-    JS::Rooted<JSObject*> wrapper(cx, mPromise->GetWrapper());
-    MOZ_ASSERT(wrapper); // It was preserved!
-    JSAutoCompartment ac(cx, wrapper);
+
+    MOZ_ASSERT(mPromise->GetWrapper()); // It was preserved!
+
+    AutoJSAPI jsapi;
+    if (!jsapi.Init(mPromise->GetWrapper())) {
+      return NS_ERROR_FAILURE;
+    }
+    JSContext* cx = jsapi.cx();
 
     JS::Rooted<JS::Value> value(cx, mValue);
     if (!MaybeWrapValue(cx, &value)) {
       NS_WARNING("Failed to wrap value into the right compartment.");
       JS_ClearPendingException(cx);
       return NS_OK;
     }
 
@@ -200,22 +204,26 @@ public:
     MOZ_COUNT_DTOR(PromiseResolveThenableJob);
   }
 
 protected:
   NS_IMETHOD
   Run() override
   {
     NS_ASSERT_OWNINGTHREAD(PromiseResolveThenableJob);
-    ThreadsafeAutoJSContext cx;
-    JS::Rooted<JSObject*> wrapper(cx, mPromise->GetWrapper());
-    MOZ_ASSERT(wrapper); // It was preserved!
+
+    MOZ_ASSERT(mPromise->GetWrapper()); // It was preserved!
+
+    AutoJSAPI jsapi;
     // If we ever change which compartment we're working in here, make sure to
     // fix the fast-path for resolved-with-a-Promise in ResolveInternal.
-    JSAutoCompartment ac(cx, wrapper);
+    if (!jsapi.Init(mPromise->GetWrapper())) {
+      return NS_ERROR_FAILURE;
+    }
+    JSContext* cx = jsapi.cx();
 
     JS::Rooted<JSObject*> resolveFunc(cx,
       mPromise->CreateThenableFunction(cx, mPromise, PromiseCallback::Resolve));
 
     if (!resolveFunc) {
       mPromise->HandleException(cx);
       return NS_OK;
     }
--- a/dom/promise/Promise.h
+++ b/dom/promise/Promise.h
@@ -417,21 +417,24 @@ private:
   void ResolveInternal(JSContext* aCx,
                        JS::Handle<JS::Value> aValue);
   void RejectInternal(JSContext* aCx,
                       JS::Handle<JS::Value> aValue);
 #endif // SPIDERMONKEY_PROMISE
 
   template <typename T>
   void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
-    ThreadsafeAutoJSContext cx;
-    JSObject* wrapper = PromiseObj();
-    MOZ_ASSERT(wrapper); // We preserved it!
+    MOZ_ASSERT(PromiseObj()); // It was preserved!
 
-    JSAutoCompartment ac(cx, wrapper);
+    AutoJSAPI jsapi;
+    if (!jsapi.Init(mGlobal)) {
+      return;
+    }
+    JSContext* cx = jsapi.cx();
+
     JS::Rooted<JS::Value> val(cx);
     if (!ToJSValue(cx, aArgument, &val)) {
       HandleException(cx);
       return;
     }
 
     (this->*aFunc)(cx, val);
   }