Bug 1290614, part 2 - Split XPCNativeSetKey into three constructors. r=mrbkap draft
authorAndrew McCreight <continuation@gmail.com>
Sun, 31 Jul 2016 13:25:05 -0700
changeset 405481 b8acdca031f427b61e4344fd9f5affc934cb54c3
parent 405480 da56c1da08c5006086ff8ca0c6955b1e4eede681
child 405482 98baf421124a650adffce94e6278924c672a57fc
push id27502
push userbmo:continuation@gmail.com
push dateThu, 25 Aug 2016 14:52:41 +0000
reviewersmrbkap
bugs1290614
milestone51.0a1
Bug 1290614, part 2 - Split XPCNativeSetKey into three constructors. r=mrbkap This explicitly represents the three types of keys that are used: 1. A key for an existing set. 2. A key for a new set with one interface added. 3. A key for an existing set with one new interface added at a specified position. MozReview-Commit-ID: Ctw41EymHbd
js/xpconnect/src/XPCInlines.h
js/xpconnect/src/XPCMaps.h
js/xpconnect/src/XPCWrappedNative.cpp
js/xpconnect/src/XPCWrappedNativeInfo.cpp
js/xpconnect/src/xpcprivate.h
--- a/js/xpconnect/src/XPCInlines.h
+++ b/js/xpconnect/src/XPCInlines.h
@@ -274,16 +274,30 @@ XPCNativeInterface::HasAncestor(const ns
 inline size_t
 XPCNativeInterface::OffsetOfMembers()
 {
     return offsetof(XPCNativeInterface, mMembers);
 }
 
 /***************************************************************************/
 
+inline XPCNativeSetKey::XPCNativeSetKey(XPCNativeSet* baseSet,
+                                        XPCNativeInterface* addition,
+                                        uint16_t position)
+    : mBaseSet(baseSet)
+    , mAddition(addition)
+    , mPosition(position)
+{
+    MOZ_ASSERT(mBaseSet);
+    MOZ_ASSERT(mAddition);
+    MOZ_ASSERT(!mBaseSet->HasInterface(mAddition));
+}
+
+/***************************************************************************/
+
 inline bool
 XPCNativeSet::FindMember(jsid name, XPCNativeMember** pMember,
                          uint16_t* pInterfaceIndex) const
 {
     XPCNativeInterface* const * iface;
     int count = (int) mInterfaceCount;
     int i;
 
--- a/js/xpconnect/src/XPCMaps.h
+++ b/js/xpconnect/src/XPCMaps.h
@@ -412,17 +412,17 @@ public:
         entry->key_value = set;
         return set;
     }
 
     inline void Remove(XPCNativeSet* set)
     {
         MOZ_ASSERT(set, "bad param");
 
-        XPCNativeSetKey key(set, nullptr, 0);
+        XPCNativeSetKey key(set);
         mTable.Remove(&key);
     }
 
     inline uint32_t Count() { return mTable.EntryCount(); }
 
     PLDHashTable::Iterator Iter() { return mTable.Iter(); }
 
     size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
--- a/js/xpconnect/src/XPCWrappedNative.cpp
+++ b/js/xpconnect/src/XPCWrappedNative.cpp
@@ -427,17 +427,17 @@ XPCWrappedNative::GetNewOrUsed(xpcObject
 
         wrapper = new XPCWrappedNative(helper.forgetCanonical(), proto);
     } else {
         RefPtr<XPCNativeInterface> iface = Interface;
         if (!iface)
             iface = XPCNativeInterface::GetISupports();
 
         AutoMarkingNativeSetPtr set(cx);
-        XPCNativeSetKey key(nullptr, iface, 0);
+        XPCNativeSetKey key(iface);
         set = XPCNativeSet::GetNewOrUsed(&key);
 
         if (!set)
             return NS_ERROR_FAILURE;
 
         wrapper =
             new XPCWrappedNative(helper.forgetCanonical(), Scope, set);
     }
--- a/js/xpconnect/src/XPCWrappedNativeInfo.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeInfo.cpp
@@ -469,17 +469,17 @@ XPCNativeSet::GetNewOrUsed(const nsIID* 
     AutoJSContext cx;
     AutoMarkingNativeSetPtr set(cx);
 
     RefPtr<XPCNativeInterface> iface =
         XPCNativeInterface::GetNewOrUsed(iid);
     if (!iface)
         return nullptr;
 
-    XPCNativeSetKey key(nullptr, iface, 0);
+    XPCNativeSetKey key(iface);
 
     XPCJSRuntime* rt = XPCJSRuntime::Get();
     NativeSetMap* map = rt->GetNativeSetMap();
     if (!map)
         return nullptr;
 
     set = map->Find(&key);
 
@@ -561,17 +561,17 @@ XPCNativeSet::GetNewOrUsed(nsIClassInfo*
 
         if (interfaceArray.Length() > 0) {
             set = NewInstance(Move(interfaceArray));
             if (set) {
                 NativeSetMap* map2 = rt->GetNativeSetMap();
                 if (!map2)
                     goto out;
 
-                XPCNativeSetKey key(set, nullptr, 0);
+                XPCNativeSetKey key(set);
 
                 XPCNativeSet* set2 = map2->Add(&key, set);
                 if (!set2) {
                     NS_ERROR("failed to add our set!");
                     DestroyInstance(set);
                     set = nullptr;
                     goto out;
                 }
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -1264,24 +1264,43 @@ private:
     nsCOMPtr<nsIInterfaceInfo> mInfo;
     jsid                       mName;
     uint16_t                   mMemberCount;
     XPCNativeMember            mMembers[1]; // always last - object sized for array
 };
 
 /***************************************************************************/
 // XPCNativeSetKey is used to key a XPCNativeSet in a NativeSetMap.
+// It represents a new XPCNativeSet we are considering constructing, without
+// requiring that the set actually be built.
 
 class XPCNativeSetKey final
 {
 public:
-    explicit XPCNativeSetKey(XPCNativeSet* baseSet = nullptr,
-                             XPCNativeInterface* addition = nullptr,
-                             uint16_t position = 0)
-        : mBaseSet(baseSet), mAddition(addition), mPosition(position) {}
+    // This represents an existing set |baseSet|.
+    explicit XPCNativeSetKey(XPCNativeSet* baseSet)
+        : mBaseSet(baseSet), mAddition(nullptr), mPosition(0)
+    {
+        MOZ_ASSERT(baseSet);
+    }
+
+    // This represents a new set containing only nsISupports and
+    // |addition|.
+    explicit XPCNativeSetKey(XPCNativeInterface* addition)
+        : mBaseSet(nullptr), mAddition(addition), mPosition(0)
+    {
+        MOZ_ASSERT(addition);
+    }
+
+    // This represents the existing set |baseSet| with the interface
+    // |addition| inserted at position |position|. |addition| must
+    // not already be present in |baseSet|.
+    explicit XPCNativeSetKey(XPCNativeSet* baseSet,
+                             XPCNativeInterface* addition,
+                             uint16_t position);
     ~XPCNativeSetKey() {}
 
     XPCNativeSet* GetBaseSet() const {return mBaseSet;}
     XPCNativeInterface* GetAddition() const {return mAddition;}
     uint16_t GetPosition() const {return mPosition;}
 
     PLDHashNumber Hash() const;