Bug 978042 - Make AutoEntryScript usable as a JSContext* and add some assertions. r=bz
authorBobby Holley <bobbyholley@gmail.com>
Thu, 20 Mar 2014 10:19:43 -0400
changeset 193091 cc154552b6a3d8cf05e7545f9c5340279a3081fe
parent 193090 28f1d36605dc33a41ca98862eeb59007495a51d0
child 193092 db2f6e6206aed940bbe815bd1f79bd5c8429ec32
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs978042
milestone31.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 978042 - Make AutoEntryScript usable as a JSContext* and add some assertions. r=bz
dom/base/ScriptSettings.cpp
dom/base/ScriptSettings.h
--- a/dom/base/ScriptSettings.cpp
+++ b/dom/base/ScriptSettings.cpp
@@ -186,41 +186,53 @@ GetWebIDLCallerPrincipal()
   // Once we fix bug 951991, this can all be simplified.
   if (!aes->mCxPusher.ref().IsStackTop()) {
     return nullptr;
   }
 
   return aes->mWebIDLCallerPrincipal;
 }
 
+static JSContext*
+FindJSContext(nsIGlobalObject* aGlobalObject)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  JSContext *cx = nullptr;
+  nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobalObject);
+  if (sgo && sgo->GetScriptContext()) {
+    cx = sgo->GetScriptContext()->GetNativeContext();
+  }
+  if (!cx) {
+    cx = nsContentUtils::GetSafeJSContext();
+  }
+  return cx;
+}
+
 AutoEntryScript::AutoEntryScript(nsIGlobalObject* aGlobalObject,
                                  bool aIsMainThread,
                                  JSContext* aCx)
   : ScriptSettingsStackEntry(aGlobalObject, /* aCandidate = */ true)
   , mStack(ScriptSettingsStack::Ref())
+  , mCx(aCx)
 {
   MOZ_ASSERT(aGlobalObject);
-  if (!aCx) {
+  MOZ_ASSERT_IF(!mCx, aIsMainThread); // cx is mandatory off-main-thread.
+  MOZ_ASSERT_IF(mCx && aIsMainThread, mCx == FindJSContext(aGlobalObject));
+  if (!mCx) {
     // If the caller didn't provide a cx, hunt one down. This isn't exactly
     // fast, but the callers that care about performance can pass an explicit
     // cx for now. Eventually, the whole cx pushing thing will go away
     // entirely.
-    MOZ_ASSERT(aIsMainThread, "cx is mandatory off-main-thread");
-    nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(aGlobalObject);
-    if (sgo && sgo->GetScriptContext()) {
-      aCx = sgo->GetScriptContext()->GetNativeContext();
-    }
-    if (!aCx) {
-      aCx = nsContentUtils::GetSafeJSContext();
-    }
+    mCx = FindJSContext(aGlobalObject);
+    MOZ_ASSERT(mCx);
   }
   if (aIsMainThread) {
-    mCxPusher.construct(aCx);
+    mCxPusher.construct(mCx);
   }
-  mAc.construct(aCx, aGlobalObject->GetGlobalJSObject());
+  mAc.construct(mCx, aGlobalObject->GetGlobalJSObject());
   mStack.Push(this);
 }
 
 AutoEntryScript::~AutoEntryScript()
 {
   MOZ_ASSERT(mStack.Incumbent() == this);
   mStack.Pop();
 }
--- a/dom/base/ScriptSettings.h
+++ b/dom/base/ScriptSettings.h
@@ -105,19 +105,22 @@ public:
                   // Note: aCx is mandatory off-main-thread.
                   JSContext* aCx = nullptr);
   ~AutoEntryScript();
 
   void SetWebIDLCallerPrincipal(nsIPrincipal *aPrincipal) {
     mWebIDLCallerPrincipal = aPrincipal;
   }
 
+  JSContext* cx() const { return mCx; }
+
 private:
   dom::ScriptSettingsStack& mStack;
   nsCOMPtr<nsIPrincipal> mWebIDLCallerPrincipal;
+  JSContext *mCx;
   mozilla::Maybe<AutoCxPusher> mCxPusher;
   mozilla::Maybe<JSAutoCompartment> mAc; // This can de-Maybe-fy when mCxPusher
                                          // goes away.
   friend nsIPrincipal* GetWebIDLCallerPrincipal();
 };
 
 /*
  * A class that can be used to force a particular incumbent script on the stack.