Bug 593294 - TM: various other defaultCompartment fixes r=gal
authorGregor Wagner <anygregor@gmail.com>
Wed, 08 Sep 2010 16:09:23 -0700
changeset 53609 0fadb269655411688d7bfcbcc5cdacfc2940f01b
parent 53608 82e35dc17b714904f7b5fb4a9e5ee82552e1565c
child 53610 bc7cd0c9b6d03846efa9724855e01d20ec85eb45
push idunknown
push userunknown
push dateunknown
reviewersgal
bugs593294
milestone2.0b6pre
Bug 593294 - TM: various other defaultCompartment fixes r=gal
js/src/xpconnect/shell/xpcshell.cpp
js/src/xpconnect/src/xpcjsruntime.cpp
js/src/xpconnect/src/xpcwrappednativescope.cpp
--- a/js/src/xpconnect/shell/xpcshell.cpp
+++ b/js/src/xpconnect/shell/xpcshell.cpp
@@ -1963,58 +1963,64 @@ main(int argc, char **argv)
 
         rv = holder->GetJSObject(&glob);
         if (NS_FAILED(rv)) {
             NS_ASSERTION(glob == nsnull, "bad GetJSObject?");
             return 1;
         }
 
         JS_BeginRequest(cx);
-
-        if (!JS_DefineFunctions(cx, glob, glob_functions)) {
-            JS_EndRequest(cx);
-            return 1;
-        }
+        {
+            JSAutoCrossCompartmentCall ac;
+            if (!ac.enter(cx, glob)) {
+                JS_EndRequest(cx);
+                return 1;
+            }
 
-        envobj = JS_DefineObject(cx, glob, "environment", &env_class, NULL, 0);
-        if (!envobj || !JS_SetPrivate(cx, envobj, envp)) {
-            JS_EndRequest(cx);
-            return 1;
-        }
+            if (!JS_DefineFunctions(cx, glob, glob_functions)) {
+                JS_EndRequest(cx);
+                return 1;
+            }
 
-        nsAutoString workingDirectory;
-        if (GetCurrentWorkingDirectory(workingDirectory))
-            gWorkingDirectory = &workingDirectory;
+            envobj = JS_DefineObject(cx, glob, "environment", &env_class, NULL, 0);
+            if (!envobj || !JS_SetPrivate(cx, envobj, envp)) {
+                JS_EndRequest(cx);
+                return 1;
+            }
 
-        JS_DefineProperty(cx, glob, "__LOCATION__", JSVAL_VOID,
-                          GetLocationProperty, NULL, 0);
+            nsAutoString workingDirectory;
+            if (GetCurrentWorkingDirectory(workingDirectory))
+                gWorkingDirectory = &workingDirectory;
 
-        argc--;
-        argv++;
+            JS_DefineProperty(cx, glob, "__LOCATION__", JSVAL_VOID,
+                              GetLocationProperty, NULL, 0);
 
-        result = ProcessArgs(cx, glob, argv, argc);
+            argc--;
+            argv++;
+
+            result = ProcessArgs(cx, glob, argv, argc);
 
 
 //#define TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN 1
 
 #ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN
-        // test of late call and release (see below)
-        nsCOMPtr<nsIJSContextStack> bogus;
-        xpc->WrapJS(cx, glob, NS_GET_IID(nsIJSContextStack),
-                    (void**) getter_AddRefs(bogus));
+            // test of late call and release (see below)
+            nsCOMPtr<nsIJSContextStack> bogus;
+            xpc->WrapJS(cx, glob, NS_GET_IID(nsIJSContextStack),
+                        (void**) getter_AddRefs(bogus));
 #endif
-
-        JSPRINCIPALS_DROP(cx, gJSPrincipals);
-        JS_ClearScope(cx, glob);
-        JS_GC(cx);
-        JSContext *oldcx;
-        cxstack->Pop(&oldcx);
-        NS_ASSERTION(oldcx == cx, "JS thread context push/pop mismatch");
-        cxstack = nsnull;
-        JS_GC(cx);
+            JSPRINCIPALS_DROP(cx, gJSPrincipals);
+            JS_ClearScope(cx, glob);
+            JS_GC(cx);
+            JSContext *oldcx;
+            cxstack->Pop(&oldcx);
+            NS_ASSERTION(oldcx == cx, "JS thread context push/pop mismatch");
+            cxstack = nsnull;
+            JS_GC(cx);
+        } //this scopes the JSAutoCrossCompartmentCall
         JS_DestroyContext(cx);
     } // this scopes the nsCOMPtrs
 
 #ifdef MOZ_IPC
     if (!XRE_ShutdownTestShell())
         NS_ERROR("problem shutting down testshell");
 #endif
 
--- a/js/src/xpconnect/src/xpcjsruntime.cpp
+++ b/js/src/xpconnect/src/xpcjsruntime.cpp
@@ -86,16 +86,17 @@ WrappedJSDyingJSObjectFinder(JSDHashTabl
     nsXPCWrappedJS* wrapper = ((JSObject2WrappedJSMap::Entry*)hdr)->value;
     NS_ASSERTION(wrapper, "found a null JS wrapper!");
 
     // walk the wrapper chain and find any whose JSObject is to be finalized
     while(wrapper)
     {
         if(wrapper->IsSubjectToFinalization())
         {
+            JSAutoEnterCompartment ac(data->cx, wrapper->GetJSObject());
             if(JS_IsAboutToBeFinalized(data->cx, wrapper->GetJSObject()))
                 data->array->AppendElement(wrapper);
         }
         wrapper = wrapper->GetNextWrapper();
     }
     return JS_DHASH_NEXT;
 }
 
--- a/js/src/xpconnect/src/xpcwrappednativescope.cpp
+++ b/js/src/xpconnect/src/xpcwrappednativescope.cpp
@@ -464,16 +464,17 @@ XPCWrappedNativeScope::FinishedMarkPhase
                  "JSGC_MARK_END without JSGC_FINALIZE_END");
 
     XPCWrappedNativeScope* prev = nsnull;
     XPCWrappedNativeScope* cur = gScopes;
 
     while(cur)
     {
         XPCWrappedNativeScope* next = cur->mNext;
+        JSAutoEnterCompartment ac(cx, cur->mGlobalJSObject);
         if(cur->mGlobalJSObject &&
            JS_IsAboutToBeFinalized(cx, cur->mGlobalJSObject))
         {
             cur->mGlobalJSObject = nsnull;
 #ifndef XPCONNECT_STANDALONE
             cur->mScriptObjectPrincipal = nsnull;
 #endif
             // Move this scope from the live list to the dying list.