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 nsresult
nsGlobalWindow::SetOuterObject(JSContext* aCx, JS::Handle<JSObject*> aOuterObject) nsGlobalWindow::SetOuterObject(JSContext* aCx, JS::Handle<JSObject*> aOuterObject)
{ {
JSAutoCompartment ac(aCx, aOuterObject);
// Force our context's global object to be the outer. // Force our context's global object to be the outer.
// NB: JS_SetGlobalObject sets aCx->compartment. // NB: JS_SetGlobalObject sets aCx->compartment.
JS_SetGlobalObject(aCx, aOuterObject); JS_SetGlobalObject(aCx, aOuterObject);

View File

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

View File

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

View File

@ -836,6 +836,7 @@ class AutoNewContext
JSContext *oldcx; JSContext *oldcx;
JSContext *newcx; JSContext *newcx;
Maybe<JSAutoRequest> newRequest; Maybe<JSAutoRequest> newRequest;
Maybe<AutoCompartment> newCompartment;
AutoNewContext(const AutoNewContext &) MOZ_DELETE; AutoNewContext(const AutoNewContext &) MOZ_DELETE;
@ -852,6 +853,7 @@ class AutoNewContext
JS_SetGlobalObject(newcx, JS_GetGlobalForScopeChain(cx)); JS_SetGlobalObject(newcx, JS_GetGlobalForScopeChain(cx));
newRequest.construct(newcx); newRequest.construct(newcx);
newCompartment.construct(newcx, JS_GetGlobalForScopeChain(cx));
return true; return true;
} }
@ -863,6 +865,7 @@ class AutoNewContext
bool throwing = JS_IsExceptionPending(newcx); bool throwing = JS_IsExceptionPending(newcx);
if (throwing) if (throwing)
JS_GetPendingException(newcx, exc.address()); JS_GetPendingException(newcx, exc.address());
newCompartment.destroy();
newRequest.destroy(); newRequest.destroy();
if (throwing) if (throwing)
JS_SetPendingException(oldcx, exc); JS_SetPendingException(oldcx, exc);
@ -2620,9 +2623,13 @@ EvalInFrame(JSContext *cx, unsigned argc, jsval *vp)
break; break;
} }
bool saved = false; AutoSaveFrameChain sfc(cx);
if (saveCurrent) mozilla::Maybe<AutoCompartment> ac;
saved = JS_SaveFrameChain(cx); if (saveCurrent) {
if (!sfc.save())
return false;
ac.construct(cx, GetDefaultGlobalForContext(cx));
}
size_t length; size_t length;
const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length); const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
@ -2636,10 +2643,6 @@ EvalInFrame(JSContext *cx, unsigned argc, jsval *vp)
JS_PCToLineNumber(cx, fpscript, JS_PCToLineNumber(cx, fpscript,
fi.pc()), fi.pc()),
MutableHandleValue::fromMarkedLocation(vp)); MutableHandleValue::fromMarkedLocation(vp));
if (saved)
JS_RestoreFrameChain(cx);
return ok; return ok;
} }
@ -5146,6 +5149,7 @@ Shell(JSContext *cx, OptionParser *op, char **envp)
if (!glob) if (!glob)
return 1; return 1;
JSAutoCompartment ac(cx, glob);
JS_SetGlobalObject(cx, glob); JS_SetGlobalObject(cx, glob);
JSObject *envobj = JS_DefineObject(cx, glob, "environment", &env_class, NULL, 0); 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); mGlobal = JS_NewGlobalObject(mContext, &sGlobalClass, nullptr, options);
NS_ENSURE_TRUE(mGlobal, NS_ERROR_OUT_OF_MEMORY); NS_ENSURE_TRUE(mGlobal, NS_ERROR_OUT_OF_MEMORY);
JSAutoCompartment ac(mContext, mGlobal);
JS_SetGlobalObject(mContext, mGlobal); JS_SetGlobalObject(mContext, mGlobal);
JS_InitStandardClasses(mContext, mGlobal); JS_InitStandardClasses(mContext, mGlobal);
@ -593,6 +594,7 @@ ProxyAutoConfig::SetupJS()
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
JSAutoRequest ar(mJSRuntime->Context()); JSAutoRequest ar(mJSRuntime->Context());
JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global());
sRunning = this; sRunning = this;
JSScript *script = JS_CompileScript(mJSRuntime->Context(), JSScript *script = JS_CompileScript(mJSRuntime->Context(),
@ -634,6 +636,7 @@ ProxyAutoConfig::GetProxyForURI(const nsCString &aTestURI,
JSContext *cx = mJSRuntime->Context(); JSContext *cx = mJSRuntime->Context();
JSAutoRequest ar(cx); JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, mJSRuntime->Global());
// the sRunning flag keeps a new PAC file from being installed // 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. // while the event loop is spinning on a DNS function. Don't early return.
@ -673,6 +676,7 @@ ProxyAutoConfig::GC()
if (!mJSRuntime || !mJSRuntime->IsOK()) if (!mJSRuntime || !mJSRuntime->IsOK())
return; return;
JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global());
JS_MaybeGC(mJSRuntime->Context()); JS_MaybeGC(mJSRuntime->Context());
} }