Bug 887334 - Add a JSAutoCompartment to AutoCxPusher. r=luke,gabor
☠☠ backed out by 659b0d61fbc6 ☠ ☠
authorBobby Holley <bobbyholley@gmail.com>
Thu, 25 Jul 2013 11:29:23 -0700
changeset 148062 1e48252b5a1d42934c5cee9135f8437a8abf1f4b
parent 148061 30b19452ea7afe0cda3203e3b05d97b867bac7e6
child 148063 2c36942312c84139579644d36ef5f8fc4fa2086a
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke, gabor
bugs887334
milestone24.0a2
Bug 887334 - Add a JSAutoCompartment to AutoCxPusher. r=luke,gabor This should hopefully take care of any cases where consumers expect to be in the default compartment.
js/xpconnect/src/nsCxPusher.cpp
js/xpconnect/src/nsCxPusher.h
--- a/js/xpconnect/src/nsCxPusher.cpp
+++ b/js/xpconnect/src/nsCxPusher.cpp
@@ -120,34 +120,39 @@ AutoCxPusher::AutoCxPusher(JSContext* cx
     // is running at the moment in the context.
     mScriptIsRunning = true;
   }
 
   if (!stack->Push(cx)) {
     MOZ_CRASH();
   }
 
-  // Enter a request for the duration that the cx is on the stack if non-null.
+#ifdef DEBUG
+  mPushedContext = cx;
+  mCompartmentDepthOnEntry = cx ? js::GetEnterCompartmentDepth(cx) : 0;
+#endif
+
+  // Enter a request and a compartment for the duration that the cx is on the
+  // stack if non-null.
+  //
   // NB: We call UnmarkGrayContext so that this can obsolete the need for the
   // old XPCAutoRequest as well.
   if (cx) {
     mAutoRequest.construct(cx);
+    if (js::GetDefaultGlobalForContext(cx))
+      mAutoCompartment.construct(cx, js::GetDefaultGlobalForContext(cx));
     xpc_UnmarkGrayContext(cx);
   }
-
-#ifdef DEBUG
-  mPushedContext = cx;
-  mCompartmentDepthOnEntry = cx ? js::GetEnterCompartmentDepth(cx) : 0;
-#endif
 }
 
 NS_EXPORT
 AutoCxPusher::~AutoCxPusher()
 {
-  // Leave the request before popping.
+  // Leave the compartment and request before popping.
+  mAutoCompartment.destroyIfConstructed();
   mAutoRequest.destroyIfConstructed();
 
   // When we push a context, we may save the frame chain and pretend like we
   // haven't entered any compartment. This gets restored on Pop(), but we can
   // run into trouble if a Push/Pop are interleaved with a
   // JSAutoEnterCompartment. Make sure the compartment depth right before we
   // pop is the same as it was right after we pushed.
   MOZ_ASSERT_IF(mPushedContext, mCompartmentDepthOnEntry ==
--- a/js/xpconnect/src/nsCxPusher.h
+++ b/js/xpconnect/src/nsCxPusher.h
@@ -28,16 +28,17 @@ public:
   AutoCxPusher(JSContext *aCx, bool aAllowNull = false);
   // XPCShell uses an nsCxPusher, which contains an AutoCxPusher.
   NS_EXPORT ~AutoCxPusher();
 
   nsIScriptContext* GetScriptContext() { return mScx; }
 
 private:
   mozilla::Maybe<JSAutoRequest> mAutoRequest;
+  mozilla::Maybe<JSAutoCompartment> mAutoCompartment;
   nsCOMPtr<nsIScriptContext> mScx;
   bool mScriptIsRunning;
 #ifdef DEBUG
   JSContext* mPushedContext;
   unsigned mCompartmentDepthOnEntry;
 #endif
 };