diff --git a/js/src/jsfriendapi.cpp b/js/src/jsfriendapi.cpp index 4b7ed8efd578..1a58096577e9 100644 --- a/js/src/jsfriendapi.cpp +++ b/js/src/jsfriendapi.cpp @@ -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) { diff --git a/js/src/jsfriendapi.h b/js/src/jsfriendapi.h index ad2cb545fac9..5a382c87d98f 100644 --- a/js/src/jsfriendapi.h +++ b/js/src/jsfriendapi.h @@ -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 diff --git a/js/xpconnect/src/XPCConvert.cpp b/js/xpconnect/src/XPCConvert.cpp index 0ac11b4d2b7f..d3578c223462 100644 --- a/js/xpconnect/src/XPCConvert.cpp +++ b/js/xpconnect/src/XPCConvert.cpp @@ -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 diff --git a/js/xpconnect/src/XPCInlines.h b/js/xpconnect/src/XPCInlines.h index 973a1bc42348..8ea20eef87eb 100644 --- a/js/xpconnect/src/XPCInlines.h +++ b/js/xpconnect/src/XPCInlines.h @@ -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; } diff --git a/js/xpconnect/src/XPCVariant.cpp b/js/xpconnect/src/XPCVariant.cpp index c25c476a300c..c61ef42b3c59 100644 --- a/js/xpconnect/src/XPCVariant.cpp +++ b/js/xpconnect/src/XPCVariant.cpp @@ -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) { diff --git a/js/xpconnect/src/XPCWrappedNative.cpp b/js/xpconnect/src/XPCWrappedNative.cpp index 9d6e8fb1b880..d6f6bdf9c61a 100644 --- a/js/xpconnect/src/XPCWrappedNative.cpp +++ b/js/xpconnect/src/XPCWrappedNative.cpp @@ -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; diff --git a/js/xpconnect/wrappers/WrapperFactory.cpp b/js/xpconnect/wrappers/WrapperFactory.cpp index 863693941971..5efafbf886d2 100644 --- a/js/xpconnect/wrappers/WrapperFactory.cpp +++ b/js/xpconnect/wrappers/WrapperFactory.cpp @@ -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; }