Bug 887334 - Add a JSCompartment* to AutoCompartment and use it in JS_NewGlobalObject. r=luke
authorBobby Holley <bobbyholley@gmail.com>
Wed, 17 Jul 2013 11:53:52 -0700
changeset 138955 31d4bde6b786ebd637b8b93c3c48440630be449e
parent 138954 957a80f3ddab52d2cd0a792e1988c930d8281037
child 138956 e8125c01b1b0e27bd4ca773053fb623b1dc18a0c
push id24977
push userryanvm@gmail.com
push dateFri, 19 Jul 2013 00:35:38 +0000
treeherdermozilla-central@0d0263a58f06 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs887334
milestone25.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 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
@@ -3313,20 +3313,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
@@ -455,16 +455,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 {
 
 /*