Bug 887334 - Miscellaneous JSAutoCompartments. r=luke

This commit is contained in:
Bobby Holley 2013-07-17 11:53:53 -07:00
parent c045da868b
commit b6dac750a0
5 changed files with 25 additions and 13 deletions

View File

@ -2080,6 +2080,8 @@ nsGlobalWindow::CreateOuterObject(nsGlobalWindow* aNewInner)
nsresult
nsGlobalWindow::SetOuterObject(JSContext* aCx, JS::Handle<JSObject*> 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);

View File

@ -1322,6 +1322,7 @@ nsJSContext::CompileScript(const PRUnichar* aText,
AutoPushJSContext cx(mContext);
JSAutoRequest ar(cx);
JS::Rooted<JSObject*> scopeObject(mContext, GetNativeGlobal());
JSAutoCompartment ac(cx, scopeObject);
xpc_UnmarkGrayObject(scopeObject);
bool ok = false;
@ -2333,7 +2334,10 @@ nsJSContext::IsContextInitialized()
void
nsJSContext::ScriptEvaluated(bool aTerminated)
{
if (GetNativeGlobal()) {
JSAutoCompartment ac(mContext, GetNativeGlobal());
JS_MaybeGC(mContext);
}
if (aTerminated) {
mOperationCallbackTime = 0;

View File

@ -61,11 +61,9 @@ TestShellCommandParent::RunCallback(const nsString& aResponse)
NS_ENSURE_TRUE(*mCallback.ToJSValPtr() != JSVAL_NULL && mCx, JS_FALSE);
JSAutoRequest ar(mCx);
JS::Rooted<JSObject*> 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<JSObject*> global(mCx, JS_GetGlobalForScopeChain(mCx));
JSString* str = JS_NewUCStringCopyN(mCx, aResponse.get(), aResponse.Length());
NS_ENSURE_TRUE(str, JS_FALSE);

View File

@ -836,6 +836,7 @@ class AutoNewContext
JSContext *oldcx;
JSContext *newcx;
Maybe<JSAutoRequest> newRequest;
Maybe<AutoCompartment> 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<AutoCompartment> 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);

View File

@ -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());
}