Bug 753518. Add an explicit way for GetParentObject to return an (nsISupports*,nsWrapperCache*) pair. r=peterv
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 10 May 2012 15:24:59 -0400
changeset 93761 893524af2d1af72e20706f856961baa80fa1826f
parent 93760 4da3ac9ee729dea58fa9cdbc53ff5d7b180939f8
child 93762 11816ca37b52229668b039fe25b675ecab99e311
push id22667
push userjdrew@mozilla.com
push dateFri, 11 May 2012 01:26:16 +0000
treeherdermozilla-central@e6a9572b48f7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspeterv
bugs753518
milestone15.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 753518. Add an explicit way for GetParentObject to return an (nsISupports*,nsWrapperCache*) pair. r=peterv
dom/bindings/BindingUtils.h
--- a/dom/bindings/BindingUtils.h
+++ b/dom/bindings/BindingUtils.h
@@ -413,16 +413,57 @@ inline nsWrapperCache*
 GetWrapperCache(nsGlobalWindow* not_allowed);
 
 inline nsWrapperCache*
 GetWrapperCache(void* p)
 {
   return NULL;
 }
 
+struct ParentObject {
+  template<class T>
+  ParentObject(T* aObject) :
+    mObject(aObject),
+    mWrapperCache(GetWrapperCache(aObject))
+  {}
+
+  template<class T, template<class> class SmartPtr>
+  ParentObject(const SmartPtr<T>& aObject) :
+    mObject(aObject.get()),
+    mWrapperCache(GetWrapperCache(aObject.get()))
+  {}
+
+  ParentObject(nsISupports* aObject, nsWrapperCache* aCache) :
+    mObject(aObject),
+    mWrapperCache(aCache)
+  {}
+
+  nsISupports* const mObject;
+  nsWrapperCache* const mWrapperCache;
+};
+
+inline nsWrapperCache*
+GetWrapperCache(const ParentObject& aParentObject)
+{
+  return aParentObject.mWrapperCache;
+}
+
+template<class T>
+inline nsISupports*
+GetParentPointer(T* aObject)
+{
+  return aObject;
+}
+
+inline nsISupports*
+GetParentPointer(const ParentObject& aObject)
+{
+  return aObject.mObject;
+}
+
 // Only set allowNativeWrapper to false if you really know you need it, if in
 // doubt use true. Setting it to false disables security wrappers.
 bool
 XPCOMObjectToJsval(JSContext* cx, JSObject* scope, xpcObjectHelper &helper,
                    const nsIID* iid, bool allowNativeWrapper, JS::Value* rval);
 
 template<class T>
 inline bool
@@ -483,38 +524,38 @@ WrapObject(JSContext* cx, JSObject* scop
 template<>
 inline bool
 WrapObject<JSObject>(JSContext* cx, JSObject* scope, JSObject* p, JS::Value* vp)
 {
   vp->setObjectOrNull(p);
   return true;
 }
 
-template<class T>
+template<typename T>
 static inline JSObject*
-WrapNativeParent(JSContext* cx, JSObject* scope, T* p)
+WrapNativeParent(JSContext* cx, JSObject* scope, const T& p)
 {
-  if (!p)
+  if (!GetParentPointer(p))
     return scope;
 
   nsWrapperCache* cache = GetWrapperCache(p);
   JSObject* obj;
   if (cache && (obj = cache->GetWrapper())) {
 #ifdef DEBUG
-    qsObjectHelper helper(p, cache);
+    qsObjectHelper helper(GetParentPointer(p), cache);
     JS::Value debugVal;
 
     bool ok = XPCOMObjectToJsval(cx, scope, helper, NULL, false, &debugVal);
     NS_ASSERTION(ok && JSVAL_TO_OBJECT(debugVal) == obj,
                  "Unexpected object in nsWrapperCache");
 #endif
     return obj;
   }
 
-  qsObjectHelper helper(p, cache);
+  qsObjectHelper helper(GetParentPointer(p), cache);
   JS::Value v;
   return XPCOMObjectToJsval(cx, scope, helper, NULL, false, &v) ?
          JSVAL_TO_OBJECT(v) :
          NULL;
 }
 
 // Spec needs a name property
 template <typename Spec>