From b6dac750a04d678ffc1e9c02175236954e199fe7 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Wed, 17 Jul 2013 11:53:53 -0700 Subject: [PATCH] Bug 887334 - Miscellaneous JSAutoCompartments. r=luke --- dom/base/nsGlobalWindow.cpp | 2 ++ dom/base/nsJSEnvironment.cpp | 6 +++++- ipc/testshell/TestShellParent.cpp | 8 +++----- js/src/shell/js.cpp | 18 +++++++++++------- netwerk/base/src/ProxyAutoConfig.cpp | 4 ++++ 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index f18d63ecdb8..a0a8a204bf6 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -2080,6 +2080,8 @@ nsGlobalWindow::CreateOuterObject(nsGlobalWindow* aNewInner) nsresult nsGlobalWindow::SetOuterObject(JSContext* aCx, JS::Handle aOuterObject) { + JSAutoCompartment ac(aCx, aOuterObject); + // Force our context's global object to be the outer. // NB: JS_SetGlobalObject sets aCx->compartment. JS_SetGlobalObject(aCx, aOuterObject); diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index 91fc6947105..8f3a431f3bb 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -1322,6 +1322,7 @@ nsJSContext::CompileScript(const PRUnichar* aText, AutoPushJSContext cx(mContext); JSAutoRequest ar(cx); JS::Rooted scopeObject(mContext, GetNativeGlobal()); + JSAutoCompartment ac(cx, scopeObject); xpc_UnmarkGrayObject(scopeObject); bool ok = false; @@ -2333,7 +2334,10 @@ nsJSContext::IsContextInitialized() void nsJSContext::ScriptEvaluated(bool aTerminated) { - JS_MaybeGC(mContext); + if (GetNativeGlobal()) { + JSAutoCompartment ac(mContext, GetNativeGlobal()); + JS_MaybeGC(mContext); + } if (aTerminated) { mOperationCallbackTime = 0; diff --git a/ipc/testshell/TestShellParent.cpp b/ipc/testshell/TestShellParent.cpp index 831959199db..d338c321cbb 100644 --- a/ipc/testshell/TestShellParent.cpp +++ b/ipc/testshell/TestShellParent.cpp @@ -61,11 +61,9 @@ TestShellCommandParent::RunCallback(const nsString& aResponse) NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, JS_FALSE); JSAutoRequest ar(mCx); - - JS::Rooted global(mCx, JS_GetGlobalForObject(mCx, mCallback.ToJSObject())); - NS_ENSURE_TRUE(global, JS_FALSE); - - JSAutoCompartment ac(mCx, global); + NS_ENSURE_TRUE(mCallback.ToJSObject(), JS_FALSE); + JSAutoCompartment ac(mCx, mCallback.ToJSObject()); + JS::Rooted global(mCx, JS_GetGlobalForScopeChain(mCx)); JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length()); NS_ENSURE_TRUE(str, JS_FALSE); diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 2959876d233..f37994dd195 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -836,6 +836,7 @@ class AutoNewContext JSContext *oldcx; JSContext *newcx; Maybe newRequest; + Maybe newCompartment; AutoNewContext(const AutoNewContext &) MOZ_DELETE; @@ -852,6 +853,7 @@ class AutoNewContext JS_SetGlobalObject(newcx, JS_GetGlobalForScopeChain(cx)); newRequest.construct(newcx); + newCompartment.construct(newcx, JS_GetGlobalForScopeChain(cx)); return true; } @@ -863,6 +865,7 @@ class AutoNewContext bool throwing = JS_IsExceptionPending(newcx); if (throwing) JS_GetPendingException(newcx, exc.address()); + newCompartment.destroy(); newRequest.destroy(); if (throwing) JS_SetPendingException(oldcx, exc); @@ -2620,9 +2623,13 @@ EvalInFrame(JSContext *cx, unsigned argc, jsval *vp) break; } - bool saved = false; - if (saveCurrent) - saved = JS_SaveFrameChain(cx); + AutoSaveFrameChain sfc(cx); + mozilla::Maybe ac; + if (saveCurrent) { + if (!sfc.save()) + return false; + ac.construct(cx, GetDefaultGlobalForContext(cx)); + } size_t length; const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length); @@ -2636,10 +2643,6 @@ EvalInFrame(JSContext *cx, unsigned argc, jsval *vp) JS_PCToLineNumber(cx, fpscript, fi.pc()), MutableHandleValue::fromMarkedLocation(vp)); - - if (saved) - JS_RestoreFrameChain(cx); - return ok; } @@ -5146,6 +5149,7 @@ Shell(JSContext *cx, OptionParser *op, char **envp) if (!glob) return 1; + JSAutoCompartment ac(cx, glob); JS_SetGlobalObject(cx, glob); JSObject *envobj = JS_DefineObject(cx, glob, "environment", &env_class, NULL, 0); diff --git a/netwerk/base/src/ProxyAutoConfig.cpp b/netwerk/base/src/ProxyAutoConfig.cpp index a70754d98be..2ab74cc9b0f 100644 --- a/netwerk/base/src/ProxyAutoConfig.cpp +++ b/netwerk/base/src/ProxyAutoConfig.cpp @@ -542,6 +542,7 @@ private: mGlobal = JS_NewGlobalObject(mContext, &sGlobalClass, nullptr, options); NS_ENSURE_TRUE(mGlobal, NS_ERROR_OUT_OF_MEMORY); + JSAutoCompartment ac(mContext, mGlobal); JS_SetGlobalObject(mContext, mGlobal); JS_InitStandardClasses(mContext, mGlobal); @@ -593,6 +594,7 @@ ProxyAutoConfig::SetupJS() return NS_ERROR_FAILURE; JSAutoRequest ar(mJSRuntime->Context()); + JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global()); sRunning = this; JSScript *script = JS_CompileScript(mJSRuntime->Context(), @@ -634,6 +636,7 @@ ProxyAutoConfig::GetProxyForURI(const nsCString &aTestURI, JSContext *cx = mJSRuntime->Context(); JSAutoRequest ar(cx); + JSAutoCompartment ac(cx, mJSRuntime->Global()); // the sRunning flag keeps a new PAC file from being installed // while the event loop is spinning on a DNS function. Don't early return. @@ -673,6 +676,7 @@ ProxyAutoConfig::GC() if (!mJSRuntime || !mJSRuntime->IsOK()) return; + JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global()); JS_MaybeGC(mJSRuntime->Context()); }