Bug 887334 - Add a JSCompartment* to AutoCompartment and use it in JS_NewGlobalObject. r=luke
☠☠ backed out by b3d0c2498b42 ☠ ☠
authorBobby Holley <bobbyholley@gmail.com>
Fri, 26 Jul 2013 18:56:55 -0700
changeset 148097 0d59888536b54999cd4d9a6284a2c08c39873af2
parent 148096 101e407d040a669532f57eab945df6bbdb95bdf3
child 148098 508522bee36719b268482ae8379d3dfc7a942a9f
push idunknown
push userunknown
push dateunknown
reviewersluke
bugs887334
milestone24.0a2
Bug 887334 - Add a JSCompartment* to AutoCompartment and use it in JS_NewGlobalObject. r=luke This gets rid of the wonky setCompartment usage in that function.
js/src/jsapi.cpp
js/src/jscompartment.h
js/src/jscompartmentinlines.h
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -3264,20 +3264,22 @@ JS_NewGlobalObject(JSContext *cx, JSClas
 
     if (options.zoneSpec == JS::SystemZone) {
         rt->systemZone = compartment->zone();
         rt->systemZone->isSystem = true;
     }
 
     AutoHoldZone hold(compartment->zone());
 
-    JSCompartment *saved = cx->compartment();
-    cx->setCompartment(compartment);
-    Rooted<GlobalObject *> global(cx, GlobalObject::create(cx, Valueify(clasp)));
-    cx->setCompartment(saved);
+    Rooted<GlobalObject *> global(cx);
+    {
+        AutoCompartment ac(cx, compartment);
+        global = GlobalObject::create(cx, Valueify(clasp));
+    }
+
     if (!global)
         return NULL;
 
     if (!Debugger::onNewGlobalObject(cx, global))
         return NULL;
 
     return global;
 }
--- a/js/src/jscompartment.h
+++ b/js/src/jscompartment.h
@@ -450,16 +450,17 @@ class AssertCompartmentUnchanged
 
 class AutoCompartment
 {
     JSContext * const cx_;
     JSCompartment * const origin_;
 
   public:
     inline AutoCompartment(JSContext *cx, JSObject *target);
+    inline AutoCompartment(JSContext *cx, JSCompartment *target);
     inline ~AutoCompartment();
 
     JSContext *context() const { return cx_; }
     JSCompartment *origin() const { return origin_; }
 
   private:
     AutoCompartment(const AutoCompartment &) MOZ_DELETE;
     AutoCompartment & operator=(const AutoCompartment &) MOZ_DELETE;
--- a/js/src/jscompartmentinlines.h
+++ b/js/src/jscompartmentinlines.h
@@ -28,16 +28,23 @@ JSCompartment::maybeGlobal() const
 
 js::AutoCompartment::AutoCompartment(JSContext *cx, JSObject *target)
   : cx_(cx),
     origin_(cx->compartment())
 {
     cx_->enterCompartment(target->compartment());
 }
 
+js::AutoCompartment::AutoCompartment(JSContext *cx, JSCompartment *target)
+  : cx_(cx),
+    origin_(cx->compartment())
+{
+    cx_->enterCompartment(target);
+}
+
 js::AutoCompartment::~AutoCompartment()
 {
     cx_->leaveCompartment(origin_);
 }
 
 namespace js {
 
 /*