Bug 1146333. Get rid of WrapCallThisValue and just use ToJSValue, now that we have it. r=peterv

This commit is contained in:
Boris Zbarsky 2015-03-30 23:43:45 -04:00
parent c411223b65
commit 2f96938540
4 changed files with 27 additions and 108 deletions

View File

@ -1621,83 +1621,6 @@ struct GetParentObject<T, false>
}
};
MOZ_ALWAYS_INLINE
JSObject* GetJSObjectFromCallback(CallbackObject* callback)
{
return callback->Callback();
}
MOZ_ALWAYS_INLINE
JSObject* GetJSObjectFromCallback(void* noncallback)
{
return nullptr;
}
template<typename T>
static inline bool
WrapCallThisValue(JSContext* cx, const T& p, JS::MutableHandle<JS::Value> rval)
{
// Callbacks are nsISupports, so WrapNativeParent will just happily wrap them
// up as an nsISupports XPCWrappedNative... which is not at all what we want.
// So we need to special-case them.
JS::Rooted<JSObject*> obj(cx, GetJSObjectFromCallback(p));
if (!obj) {
// WrapNativeParent is a bit of a Swiss army knife that will
// wrap anything for us.
obj = WrapNativeParent(cx, p);
if (!obj) {
return false;
}
}
// But all that won't necessarily put things in the compartment of cx.
if (!JS_WrapObject(cx, &obj)) {
return false;
}
rval.setObject(*obj);
return true;
}
/*
* This specialized function simply wraps a JS::Rooted<> since
* WrapNativeParent() is not applicable for JS objects.
*/
template<>
inline bool
WrapCallThisValue<JS::Rooted<JSObject*>>(JSContext* cx,
const JS::Rooted<JSObject*>& p,
JS::MutableHandle<JS::Value> rval)
{
JS::Rooted<JSObject*> obj(cx, p);
if (!JS_WrapObject(cx, &obj)) {
return false;
}
rval.setObject(*obj);
return true;
}
/*
* This specialization is for wrapping any JS value.
*/
template<>
inline bool
WrapCallThisValue<JS::Rooted<JS::Value>>(JSContext* cx,
const JS::Rooted<JS::Value>& v,
JS::MutableHandle<JS::Value> rval)
{
JS::Rooted<JS::Value> val(cx, v);
if (!JS_WrapValue(cx, &val)) {
return false;
}
rval.set(val);
return true;
}
// Helper for calling GetOrCreateDOMReflector with smart pointers
// (nsAutoPtr/nsRefPtr/nsCOMPtr) or references.
template <class T, bool isSmartPtr=IsSmartPtr<T>::value>

View File

@ -1146,14 +1146,14 @@ class CGHeaders(CGWrapper):
if len(callbacks) != 0:
# We need CallbackFunction to serve as our parent class
declareIncludes.add("mozilla/dom/CallbackFunction.h")
# And we need BindingUtils.h so we can wrap "this" objects
declareIncludes.add("mozilla/dom/BindingUtils.h")
# And we need ToJSValue.h so we can wrap "this" objects
declareIncludes.add("mozilla/dom/ToJSValue.h")
if len(callbackDescriptors) != 0 or len(jsImplementedDescriptors) != 0:
# We need CallbackInterface to serve as our parent class
declareIncludes.add("mozilla/dom/CallbackInterface.h")
# And we need BindingUtils.h so we can wrap "this" objects
declareIncludes.add("mozilla/dom/BindingUtils.h")
# And we need ToJSValue.h so we can wrap "this" objects
declareIncludes.add("mozilla/dom/ToJSValue.h")
# Also need to include the headers for ancestors of
# JS-implemented interfaces.
@ -13825,7 +13825,7 @@ class CGCallback(CGClass):
"""
$*{setupCall}
JS::Rooted<JS::Value> thisValJS(s.GetContext());
if (!WrapCallThisValue(s.GetContext(), thisVal, &thisValJS)) {
if (!ToJSValue(s.GetContext(), thisVal, &thisValJS)) {
aRv.Throw(NS_ERROR_FAILURE);
return${errorReturn};
}

View File

@ -38,19 +38,6 @@ ToJSValue(JSContext* aCx, const nsAString& aArgument,
}
namespace tojsvalue_detail {
bool
ISupportsToJSValue(JSContext* aCx,
nsISupports* aArgument,
JS::MutableHandle<JS::Value> aValue)
{
nsresult rv = nsContentUtils::WrapNative(aCx, aArgument, aValue);
return NS_SUCCEEDED(rv);
}
} // namespace tojsvalue_detail
bool
ToJSValue(JSContext* aCx,
nsresult aArgument,

View File

@ -28,10 +28,16 @@ ToJSValue(JSContext* aCx,
const nsAString& aArgument,
JS::MutableHandle<JS::Value> aValue);
// Accept booleans.
MOZ_WARN_UNUSED_RESULT inline bool
// Accept booleans. But be careful here: if we just have a function that takes
// a boolean argument, then any pointer that doesn't match one of our other
// signatures/templates will get treated as a boolean, which is clearly not
// desirable. So make this a template that only gets used if the argument type
// is actually boolean
template<typename T>
MOZ_WARN_UNUSED_RESULT
typename EnableIf<IsSame<T, bool>::value, bool>::Type
ToJSValue(JSContext* aCx,
bool aArgument,
T aArgument,
JS::MutableHandle<JS::Value> aValue)
{
// Make sure we're called in a compartment
@ -174,15 +180,6 @@ ToJSValue(JSContext* aCx,
return true;
}
// We don't want to include nsContentUtils here, so use a helper
// function for the nsISupports case.
namespace tojsvalue_detail {
bool
ISupportsToJSValue(JSContext* aCx,
nsISupports* aArgument,
JS::MutableHandle<JS::Value> aValue);
} // namespace tojsvalue_detail
// Accept objects that inherit from nsISupports but not nsWrapperCache (e.g.
// nsIDOMFile).
template <class T>
@ -197,7 +194,9 @@ ToJSValue(JSContext* aCx,
// Make sure we're called in a compartment
MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
return tojsvalue_detail::ISupportsToJSValue(aCx, &aArgument, aValue);
qsObjectHelper helper(ToSupports(&aArgument), nullptr);
JS::Rooted<JSObject*> scope(aCx, JS::CurrentGlobalOrNull(aCx));
return XPCOMObjectToJsval(aCx, scope, helper, nullptr, true, aValue);
}
// Accept nsRefPtr/nsCOMPtr
@ -257,6 +256,16 @@ ToJSValue(JSContext* aCx, const JS::Rooted<JS::Value>& aArgument,
return MaybeWrapValue(aCx, aValue);
}
// Accept existing rooted JS objects (which may not be same-compartment with
// us).
MOZ_WARN_UNUSED_RESULT inline bool
ToJSValue(JSContext* aCx, const JS::Rooted<JSObject*>& aArgument,
JS::MutableHandle<JS::Value> aValue)
{
aValue.setObjectOrNull(aArgument);
return MaybeWrapObjectOrNullValue(aCx, aValue);
}
// Accept nsresult, for use in rejections, and create an XPCOM
// exception object representing that nsresult.
MOZ_WARN_UNUSED_RESULT bool