From 454062c586f72d4be339a207643ba1b5dee9ef3f Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 6 Feb 2013 09:42:17 +0000 Subject: [PATCH] Bug 830992 part 1. Make it possible to pass CallbackObject& as the thisObj for callback functions. r=peterv --- dom/bindings/BindingUtils.h | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/dom/bindings/BindingUtils.h b/dom/bindings/BindingUtils.h index d8b64fd5e38f..481a9e9355d6 100644 --- a/dom/bindings/BindingUtils.h +++ b/dom/bindings/BindingUtils.h @@ -23,6 +23,7 @@ #include "nsWrapperCacheInlines.h" #include "mozilla/Likely.h" #include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/CallbackObject.h" // nsGlobalWindow implements nsWrapperCache, but doesn't always use it. Don't // try to use it without fixing that first. @@ -1120,18 +1121,36 @@ struct GetParentObject } }; +MOZ_ALWAYS_INLINE +JSObject* GetJSObjectFromCallback(CallbackObject* callback) +{ + return callback->Callback(); +} + +MOZ_ALWAYS_INLINE +JSObject* GetJSObjectFromCallback(void* noncallback) +{ + return nullptr; +} + template static inline JSObject* WrapCallThisObject(JSContext* cx, JSObject* scope, const T& p) { - // WrapNativeParent is a bit of a Swiss army knife that will - // wrap anything for us. - JSObject* obj = WrapNativeParent(cx, scope, p); + // 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. + JSObject* obj = GetJSObjectFromCallback(p); if (!obj) { - return nullptr; + // WrapNativeParent is a bit of a Swiss army knife that will + // wrap anything for us. + obj = WrapNativeParent(cx, scope, p); + if (!obj) { + return nullptr; + } } - // But it won't necessarily put things in the compartment of cx. + // But all that won't necessarily put things in the compartment of cx. if (!JS_WrapObject(cx, &obj)) { return nullptr; }