Bug 711859 - Add an IsObjectInContextCompartment API; seems-better-than-the-alternative-all-things-considered=Waldo

This commit is contained in:
Ms2ger 2011-12-24 09:28:55 +01:00
parent 4bf0b93208
commit 15fef19c0d
7 changed files with 18 additions and 9 deletions

View File

@ -214,6 +214,12 @@ js::GetObjectSlotSpan(const JSObject *obj)
return obj->slotSpan();
}
JS_FRIEND_API(bool)
js::IsObjectInContextCompartment(const JSObject *obj, const JSContext *cx)
{
return obj->compartment() == cx->compartment;
}
JS_FRIEND_API(bool)
js::IsOriginalScriptFunction(JSFunction *fun)
{

View File

@ -411,6 +411,9 @@ StringIsArrayIndex(JSLinearString *str, jsuint *indexp);
JS_FRIEND_API(void)
SetPreserveWrapperCallback(JSRuntime *rt, PreserveWrapperCallback callback);
JS_FRIEND_API(bool)
IsObjectInContextCompartment(const JSObject *obj, const JSContext *cx);
/*
* NB: these flag bits are encoded into the bytecode stream in the immediate
* operand of JSOP_ITER, so don't change them without advancing jsxdrapi.h's

View File

@ -160,7 +160,7 @@ XPCConvert::NativeData2JS(XPCLazyCallContext& lccx, jsval* d, const void* s,
// Allow wrong compartment or unset ScopeForNewObject when the caller knows
// the value is primitive (viz., XPCNativeMember::GetConstantValue).
NS_ABORT_IF_FALSE(type.IsArithmetic() ||
cx->compartment == js::GetObjectCompartment(lccx.GetScopeForNewJSObjects()),
js::IsObjectInContextCompartment(lccx.GetScopeForNewJSObjects(), cx),
"bad scope for new JSObjects");
if (pErr)
@ -933,7 +933,7 @@ XPCConvert::NativeInterface2JSObject(XPCLazyCallContext& lccx,
// optimal -- we could detect this and roll the functionality into a
// single wrapper, but the current solution is good enough for now.
JSContext* cx = lccx.GetJSContext();
NS_ABORT_IF_FALSE(js::GetObjectCompartment(lccx.GetScopeForNewJSObjects()) == cx->compartment,
NS_ABORT_IF_FALSE(js::IsObjectInContextCompartment(lccx.GetScopeForNewJSObjects(), cx),
"bad scope for new JSObjects");
JSObject *jsscope = lccx.GetScopeForNewJSObjects();
@ -979,7 +979,7 @@ XPCConvert::NativeInterface2JSObject(XPCLazyCallContext& lccx,
if (!flat) {
tryConstructSlimWrapper = true;
} else if (IS_SLIM_WRAPPER_OBJECT(flat)) {
if (js::GetObjectCompartment(flat) == cx->compartment) {
if (js::IsObjectInContextCompartment(flat, cx)) {
*d = OBJECT_TO_JSVAL(flat);
return true;
}
@ -1150,7 +1150,7 @@ XPCConvert::NativeInterface2JSObject(XPCLazyCallContext& lccx,
} else {
flat = JS_ObjectToOuterObject(cx, flat);
NS_ASSERTION(flat, "bad outer object hook!");
NS_ASSERTION(js::GetObjectCompartment(flat) == cx->compartment,
NS_ASSERTION(js::IsObjectInContextCompartment(flat, cx),
"bad compartment");
}
}
@ -1584,7 +1584,7 @@ XPCConvert::NativeArray2JS(XPCLazyCallContext& lccx,
return false;
JSContext* cx = ccx.GetJSContext();
NS_ABORT_IF_FALSE(js::GetObjectCompartment(lccx.GetScopeForNewJSObjects()) == cx->compartment,
NS_ABORT_IF_FALSE(js::IsObjectInContextCompartment(lccx.GetScopeForNewJSObjects(), cx),
"bad scope for new JSObjects");
// XXX add support for putting chars in a string rather than an array

View File

@ -164,7 +164,7 @@ inline void
XPCCallContext::SetScopeForNewJSObjects(JSObject *scope)
{
NS_ABORT_IF_FALSE(mState == HAVE_CONTEXT, "wrong call context state");
NS_ABORT_IF_FALSE(js::GetObjectCompartment(scope) == mJSContext->compartment, "wrong compartment");
NS_ABORT_IF_FALSE(js::IsObjectInContextCompartment(scope, mJSContext), "wrong compartment");
mScopeForNewJSObjects = scope;
mState = HAVE_SCOPE;
}

View File

@ -466,7 +466,7 @@ XPCVariant::VariantDataToJS(XPCLazyCallContext& lccx,
JSBool success;
JSContext* cx = lccx.GetJSContext();
NS_ABORT_IF_FALSE(js::GetObjectCompartment(lccx.GetScopeForNewJSObjects()) == cx->compartment,
NS_ABORT_IF_FALSE(js::IsObjectInContextCompartment(lccx.GetScopeForNewJSObjects(), cx),
"bad scope for new JSObjects");
switch (type) {

View File

@ -3683,7 +3683,7 @@ ConstructSlimWrapper(XPCCallContext &ccx,
return false;
}
if (ccx.GetJSContext()->compartment != js::GetObjectCompartment(parent)) {
if (!js::IsObjectInContextCompartment(parent, ccx.GetJSContext())) {
SLIM_LOG_NOT_CREATED(ccx, identityObj, "wrong compartment");
return false;

View File

@ -437,7 +437,7 @@ WrapperFactory::WaiveXrayAndWrap(JSContext *cx, jsval *vp)
JSObject *obj = js::UnwrapObject(JSVAL_TO_OBJECT(*vp));
obj = GetCurrentOuter(cx, obj);
if (js::GetObjectCompartment(obj) == cx->compartment) {
if (js::IsObjectInContextCompartment(obj, cx)) {
*vp = OBJECT_TO_JSVAL(obj);
return true;
}