Bug 1300071 - Create an API to access DOM Promise's state. r=bz
authorKaku Kuo <tkuo@mozilla.com>
Wed, 07 Sep 2016 10:51:41 +0800
changeset 354508 ad2e3708134aa669bf2950671482f482254daf5d
parent 354507 9e4c73d21811d7b57ac952ae2891a7d7f412352a
child 354509 53735145cfbeede7164416318becd8e33dca53c9
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1300071
milestone51.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 1300071 - Create an API to access DOM Promise's state. r=bz MozReview-Commit-ID: LI2Dafkzq5x
dom/promise/Promise.cpp
dom/promise/Promise.h
--- a/dom/promise/Promise.cpp
+++ b/dom/promise/Promise.cpp
@@ -3241,10 +3241,35 @@ uint64_t
 Promise::GetID() {
   if (mID != 0) {
     return mID;
   }
   return mID = ++gIDGenerator;
 }
 #endif // SPIDERMONKEY_PROMISE
 
+#ifndef SPIDERMONKEY_PROMISE
+Promise::PromiseState
+Promise::State() const
+{
+  return mState;
+}
+#else // SPIDERMONKEY_PROMISE
+Promise::PromiseState
+Promise::State() const
+{
+  JS::Rooted<JSObject*> p(RootingCx(), PromiseObj());
+  const JS::PromiseState state = JS::GetPromiseState(p);
+
+  if (state == JS::PromiseState::Fulfilled) {
+      return PromiseState::Resolved;
+  }
+
+  if (state == JS::PromiseState::Rejected) {
+      return PromiseState::Rejected;
+  }
+
+  return PromiseState::Pending;
+}
+#endif // SPIDERMONKEY_PROMISE
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/promise/Promise.h
+++ b/dom/promise/Promise.h
@@ -308,16 +308,24 @@ public:
 #ifdef SPIDERMONKEY_PROMISE
   // Create a dom::Promise from a given SpiderMonkey Promise object.
   // aPromiseObj MUST be in the compartment of aGlobal's global JS object.
   static already_AddRefed<Promise>
   CreateFromExisting(nsIGlobalObject* aGlobal,
                      JS::Handle<JSObject*> aPromiseObj);
 #endif // SPIDERMONKEY_PROMISE
 
+  enum class PromiseState {
+    Pending,
+    Resolved,
+    Rejected
+  };
+
+  PromiseState State() const;
+
 protected:
   struct PromiseCapability;
 
   // Do NOT call this unless you're Promise::Create or
   // Promise::CreateFromExisting.  I wish we could enforce that from inside this
   // class too, somehow.
   explicit Promise(nsIGlobalObject* aGlobal);
 
@@ -364,22 +372,16 @@ protected:
 
   bool WasNotifiedAsUncaught() const
   {
     return mWasNotifiedAsUncaught;
   }
 #endif // SPIDERMONKEY_PROMISE
 
 private:
-  enum PromiseState {
-    Pending,
-    Resolved,
-    Rejected
-  };
-
 #ifndef SPIDERMONKEY_PROMISE
   friend class PromiseDebugging;
 
   void SetState(PromiseState aState)
   {
     MOZ_ASSERT(mState == Pending);
     MOZ_ASSERT(aState != Pending);
     mState = aState;