Bug 514077 - Get rid of MirrorWrappedNativeParent to simplify the parent chains of XPCNativeWrappers. r=jorendorff

This commit is contained in:
Blake Kaplan 2010-08-10 15:00:26 -07:00
parent 3e81f7fd38
commit df5f435eac

View File

@ -746,32 +746,6 @@ XPC_NW_HasInstance(JSContext *cx, JSObject *obj, const jsval *valp, JSBool *bp)
return JS_TRUE;
}
static JSBool
MirrorWrappedNativeParent(JSContext *cx, XPCWrappedNative *wrapper,
JSObject **result NS_OUTPARAM)
{
JSObject *wn_parent = wrapper->GetFlatJSObject()->getParent();
if (!wn_parent) {
*result = nsnull;
} else {
XPCWrappedNative *parent_wrapper =
XPCWrappedNative::GetAndMorphWrappedNativeOfJSObject(cx, wn_parent);
// parent_wrapper can be null if we're in a Components.utils.evalInSandbox
// scope. In that case, the best we can do is just use the
// non-native-wrapped sandbox global object for our parent.
if (parent_wrapper) {
*result = XPCNativeWrapper::GetNewOrUsed(cx, parent_wrapper, nsnull,
nsnull);
if (!*result)
return JS_FALSE;
} else {
*result = nsnull;
}
}
return JS_TRUE;
}
static JSBool
XPCNativeWrapperCtor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
@ -1109,38 +1083,15 @@ XPCNativeWrapper::GetNewOrUsed(JSContext *cx, XPCWrappedNative *wrapper,
return obj;
}
JSObject *nw_parent;
if (!MirrorWrappedNativeParent(cx, wrapper, &nw_parent)) {
return nsnull;
}
PRBool lock;
if (!nw_parent) {
nw_parent = wrapper->GetScope()->GetGlobalJSObject();
lock = PR_FALSE;
} else {
lock = PR_TRUE;
}
if (lock) {
// Make sure nw_parent doesn't get collected while we're creating
// the new wrapper.
::JS_LockGCThing(cx, nw_parent);
}
JSObject *nw_parent = wrapper->GetScope()->GetGlobalJSObject();
bool call = NATIVE_HAS_FLAG(wrapper, WantCall) ||
NATIVE_HAS_FLAG(wrapper, WantConstruct);
obj = ::JS_NewObjectWithGivenProto(cx, GetJSClass(call), nsnull, nw_parent);
if (lock) {
::JS_UnlockGCThing(cx, nw_parent);
}
obj = JS_NewObjectWithGivenProto(cx, GetJSClass(call), nsnull, nw_parent);
if (!obj ||
!::JS_SetPrivate(cx, obj, wrapper) ||
!::JS_SetReservedSlot(cx, obj, 0, JSVAL_ZERO)) {
!JS_SetPrivate(cx, obj, wrapper) ||
!JS_SetReservedSlot(cx, obj, 0, JSVAL_ZERO)) {
return nsnull;
}
@ -1185,30 +1136,10 @@ XPCNativeWrapper::CreateExplicitWrapper(JSContext *cx,
return JS_FALSE;
}
if (!::JS_SetReservedSlot(cx, wrapperObj, 0, INT_TO_JSVAL(FLAG_EXPLICIT))) {
if (!JS_SetReservedSlot(cx, wrapperObj, 0, INT_TO_JSVAL(FLAG_EXPLICIT))) {
return JS_FALSE;
}
JSObject *parent = nsnull;
// Make sure wrapperObj doesn't get collected while we're wrapping
// parents for it.
JS_LockGCThing(cx, wrapperObj);
// A deep XPCNativeWrapper has a parent chain that mirrors its
// XPCWrappedNative's chain.
if (!MirrorWrappedNativeParent(cx, wrappedNative, &parent))
return JS_FALSE;
JS_UnlockGCThing(cx, wrapperObj);
if (!parent) {
parent = wrappedNative->GetScope()->GetGlobalJSObject();
}
if (!JS_SetParent(cx, wrapperObj, parent))
return JS_FALSE;
// Set the XPCWrappedNative as private data in the native wrapper.
if (!JS_SetPrivate(cx, wrapperObj, wrappedNative)) {
return JS_FALSE;