From 985a0f1a7f5995c4eb7b4103191786d2480d8b98 Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Fri, 5 Apr 2013 15:21:02 +0200 Subject: [PATCH] Bug 855411 - InstallMember. r=bz,terrence --- content/xbl/src/nsXBLProtoImpl.cpp | 8 +++---- content/xbl/src/nsXBLProtoImplMember.h | 2 +- content/xbl/src/nsXBLProtoImplMethod.cpp | 17 +++++++------ content/xbl/src/nsXBLProtoImplMethod.h | 4 ++-- content/xbl/src/nsXBLProtoImplProperty.cpp | 28 ++++++++++++---------- content/xbl/src/nsXBLProtoImplProperty.h | 2 +- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/content/xbl/src/nsXBLProtoImpl.cpp b/content/xbl/src/nsXBLProtoImpl.cpp index 619fbbb3e87..aaba329edc0 100644 --- a/content/xbl/src/nsXBLProtoImpl.cpp +++ b/content/xbl/src/nsXBLProtoImpl.cpp @@ -55,7 +55,7 @@ nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aPrototypeBinding, // If the way this gets the script context changes, fix // nsXBLProtoImplAnonymousMethod::Execute nsIDocument* document = aBinding->GetBoundElement()->OwnerDoc(); - + nsCOMPtr global = do_QueryInterface(document->GetScopeObject()); if (!global) return NS_OK; @@ -67,7 +67,7 @@ nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aPrototypeBinding, // This function also has the side effect of building up the prototype implementation if it has // not been built already. nsCOMPtr holder; - JSObject* targetClassObject = nullptr; + JS::Rooted targetClassObject(context->GetNativeContext(), nullptr); bool targetObjectIsNew = false; nsresult rv = InitTargetObjects(aPrototypeBinding, context, aBinding->GetBoundElement(), @@ -83,8 +83,8 @@ nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aPrototypeBinding, if (!targetObjectIsNew) return NS_OK; - JSObject * targetScriptObject; - holder->GetJSObject(&targetScriptObject); + JS::Rooted targetScriptObject(context->GetNativeContext()); + holder->GetJSObject(targetScriptObject.address()); AutoPushJSContext cx(context->GetNativeContext()); JSAutoRequest ar(cx); diff --git a/content/xbl/src/nsXBLProtoImplMember.h b/content/xbl/src/nsXBLProtoImplMember.h index 3ccdef7793b..5646bf6effc 100644 --- a/content/xbl/src/nsXBLProtoImplMember.h +++ b/content/xbl/src/nsXBLProtoImplMember.h @@ -74,7 +74,7 @@ public: const PRUnichar* GetName() { return mName; } virtual nsresult InstallMember(JSContext* aCx, - JSObject* aTargetClassObject) = 0; + JS::Handle aTargetClassObject) = 0; virtual nsresult CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr, JSObject* aClassObject) = 0; diff --git a/content/xbl/src/nsXBLProtoImplMethod.cpp b/content/xbl/src/nsXBLProtoImplMethod.cpp index 66a79f001e9..a69752fb036 100644 --- a/content/xbl/src/nsXBLProtoImplMethod.cpp +++ b/content/xbl/src/nsXBLProtoImplMethod.cpp @@ -96,14 +96,14 @@ nsXBLProtoImplMethod::SetLineNumber(uint32_t aLineNumber) nsresult nsXBLProtoImplMethod::InstallMember(JSContext* aCx, - JSObject* aTargetClassObject) + JS::Handle aTargetClassObject) { NS_PRECONDITION(IsCompiled(), "Should not be installing an uncompiled method"); MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx)); - JSObject* globalObject = JS_GetGlobalForObject(aCx, aTargetClassObject); - JSObject* scopeObject = xpc::GetXBLScope(aCx, globalObject); + JS::Rooted globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject)); + JS::Rooted scopeObject(aCx, xpc::GetXBLScope(aCx, globalObject)); // now we want to reevaluate our property using aContext and the script object for this window... if (mJSMethodObject) { @@ -111,7 +111,7 @@ nsXBLProtoImplMethod::InstallMember(JSContext* aCx, // First, make the function in the compartment of the scope object. JSAutoCompartment ac(aCx, scopeObject); - JSObject * method = ::JS_CloneFunctionObject(aCx, mJSMethodObject, scopeObject); + JS::Rooted method(aCx, ::JS_CloneFunctionObject(aCx, mJSMethodObject, scopeObject)); if (!method) { return NS_ERROR_OUT_OF_MEMORY; } @@ -119,10 +119,13 @@ nsXBLProtoImplMethod::InstallMember(JSContext* aCx, // Then, enter the content compartment, wrap the method pointer, and define // the wrapped version on the class object. JSAutoCompartment ac2(aCx, aTargetClassObject); - if (!JS_WrapObject(aCx, &method) || - !::JS_DefineUCProperty(aCx, aTargetClassObject, + if (!JS_WrapObject(aCx, method.address())) + return NS_ERROR_OUT_OF_MEMORY; + + JS::Rooted value(aCx, JS::ObjectValue(*method)); + if (!::JS_DefineUCProperty(aCx, aTargetClassObject, static_cast(mName), - name.Length(), OBJECT_TO_JSVAL(method), + name.Length(), value, nullptr, nullptr, JSPROP_ENUMERATE)) { return NS_ERROR_OUT_OF_MEMORY; } diff --git a/content/xbl/src/nsXBLProtoImplMethod.h b/content/xbl/src/nsXBLProtoImplMethod.h index 4211b798912..3cd38db49f2 100644 --- a/content/xbl/src/nsXBLProtoImplMethod.h +++ b/content/xbl/src/nsXBLProtoImplMethod.h @@ -89,7 +89,7 @@ public: void SetLineNumber(uint32_t aLineNumber); virtual nsresult InstallMember(JSContext* aCx, - JSObject* aTargetClassObject); + JS::Handle aTargetClassObject); virtual nsresult CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr, JSObject* aClassObject); @@ -138,7 +138,7 @@ public: // binding instantiations (though they may hang out in mMembers on the // prototype implementation). virtual nsresult InstallMember(JSContext* aCx, - JSObject* aTargetClassObject) { + JS::Handle aTargetClassObject) { return NS_OK; } diff --git a/content/xbl/src/nsXBLProtoImplProperty.cpp b/content/xbl/src/nsXBLProtoImplProperty.cpp index 8a0fc0a81fc..1d89f0d627a 100644 --- a/content/xbl/src/nsXBLProtoImplProperty.cpp +++ b/content/xbl/src/nsXBLProtoImplProperty.cpp @@ -140,47 +140,49 @@ const char* gPropertyArgs[] = { "val" }; nsresult nsXBLProtoImplProperty::InstallMember(JSContext *aCx, - JSObject* aTargetClassObject) + JS::Handle aTargetClassObject) { NS_PRECONDITION(mIsCompiled, "Should not be installing an uncompiled property"); MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx)); - JSObject * globalObject = JS_GetGlobalForObject(aCx, aTargetClassObject); - JSObject * scopeObject = xpc::GetXBLScope(aCx, globalObject); + JS::Rooted globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject)); + JS::Rooted scopeObject(aCx, xpc::GetXBLScope(aCx, globalObject)); // now we want to reevaluate our property using aContext and the script object for this window... if (mJSGetterObject || mJSSetterObject) { - JSObject * getter = nullptr; - // First, enter the compartment of the scope object and clone the functions. JSAutoCompartment ac(aCx, scopeObject); - if (mJSGetterObject) + + JS::Rooted getter(aCx, nullptr); + if (mJSGetterObject) { if (!(getter = ::JS_CloneFunctionObject(aCx, mJSGetterObject, scopeObject))) return NS_ERROR_OUT_OF_MEMORY; + } - JSObject * setter = nullptr; - if (mJSSetterObject) + JS::Rooted setter(aCx, nullptr); + if (mJSSetterObject) { if (!(setter = ::JS_CloneFunctionObject(aCx, mJSSetterObject, scopeObject))) return NS_ERROR_OUT_OF_MEMORY; + } // Now, enter the content compartment, wrap the getter/setter, and define // them on the class object. JSAutoCompartment ac2(aCx, aTargetClassObject); nsDependentString name(mName); - if (!JS_WrapObject(aCx, &getter) || - !JS_WrapObject(aCx, &setter) || + if (!JS_WrapObject(aCx, getter.address()) || + !JS_WrapObject(aCx, setter.address()) || !::JS_DefineUCProperty(aCx, aTargetClassObject, static_cast(mName), name.Length(), JSVAL_VOID, - JS_DATA_TO_FUNC_PTR(JSPropertyOp, getter), - JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, setter), + JS_DATA_TO_FUNC_PTR(JSPropertyOp, getter.get()), + JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, setter.get()), mJSAttributes)) return NS_ERROR_OUT_OF_MEMORY; } return NS_OK; } -nsresult +nsresult nsXBLProtoImplProperty::CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr, JSObject* aClassObject) { diff --git a/content/xbl/src/nsXBLProtoImplProperty.h b/content/xbl/src/nsXBLProtoImplProperty.h index df4cbebfc83..a740f32fff7 100644 --- a/content/xbl/src/nsXBLProtoImplProperty.h +++ b/content/xbl/src/nsXBLProtoImplProperty.h @@ -33,7 +33,7 @@ public: void SetSetterLineNumber(uint32_t aLineNumber); virtual nsresult InstallMember(JSContext* aCx, - JSObject* aTargetClassObject); + JS::Handle aTargetClassObject); virtual nsresult CompileMember(nsIScriptContext* aContext, const nsCString& aClassStr, JSObject* aClassObject);