Bug 1007878 part 2. Add a C++ type to represent MozMap. r=khuey

This commit is contained in:
Boris Zbarsky 2014-05-23 17:32:38 -04:00
parent cfbf67619d
commit 0a59bbbf03
7 changed files with 287 additions and 6 deletions

View File

@ -31,6 +31,7 @@
#include "qsObjectHelper.h"
#include "xpcpublic.h"
#include "nsIVariant.h"
#include "pldhash.h" // For PLDHashOperator
#include "nsWrapperCacheInlines.h"
@ -43,6 +44,7 @@ xpc_qsUnwrapArgImpl(JSContext* cx, JS::Handle<JS::Value> v, const nsIID& iid, vo
namespace mozilla {
namespace dom {
template<typename DataType> class MozMap;
struct SelfRef
{
@ -1977,6 +1979,33 @@ public:
}
};
// XXXbz It's not clear whether it's better to add a pldhash dependency here
// (for PLDHashOperator) or add a BindingUtils.h dependency (for
// SequenceTracer) to MozMap.h...
template<typename T>
static PLDHashOperator
TraceMozMapValue(T* aValue, void* aClosure)
{
JSTracer* trc = static_cast<JSTracer*>(aClosure);
// Act like it's a one-element sequence to leverage all that infrastructure.
SequenceTracer<T>::TraceSequence(trc, aValue, aValue + 1);
return PL_DHASH_NEXT;
}
// sequence<MozMap>
template<typename T>
class SequenceTracer<MozMap<T>, false, false, false>
{
explicit SequenceTracer() MOZ_DELETE; // Should never be instantiated
public:
static void TraceSequence(JSTracer* trc, MozMap<T>* seqp, MozMap<T>* end) {
for (; seqp != end; ++seqp) {
seqp->EnumerateValues(TraceMozMapValue<T>, trc);
}
}
};
template<typename T>
void DoTraceSequence(JSTracer* trc, FallibleTArray<T>& seq)
{
@ -2050,6 +2079,58 @@ public:
SequenceType mSequenceType;
};
// Rooter class for MozMap; this is what we mostly use in the codegen.
template<typename T>
class MOZ_STACK_CLASS MozMapRooter : private JS::CustomAutoRooter
{
public:
MozMapRooter(JSContext *aCx, MozMap<T>* aMozMap
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
mMozMap(aMozMap),
mMozMapType(eMozMap)
{
}
MozMapRooter(JSContext *aCx, Nullable<MozMap<T>>* aMozMap
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: JS::CustomAutoRooter(aCx MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT),
mNullableMozMap(aMozMap),
mMozMapType(eNullableMozMap)
{
}
private:
enum MozMapType {
eMozMap,
eNullableMozMap
};
virtual void trace(JSTracer *trc) MOZ_OVERRIDE
{
MozMap<T>* mozMap;
if (mMozMapType == eMozMap) {
mozMap = mMozMap;
} else {
MOZ_ASSERT(mMozMapType == eNullableMozMap);
if (mNullableMozMap->IsNull()) {
// Nothing to do
return;
}
mozMap = &mNullableMozMap->Value();
}
mozMap->EnumerateValues(TraceMozMapValue<T>, trc);
}
union {
MozMap<T>* mMozMap;
Nullable<MozMap<T>>* mNullableMozMap;
};
MozMapType mMozMapType;
};
template<typename T>
class MOZ_STACK_CLASS RootedUnion : public T,
private JS::CustomAutoRooter

View File

@ -39,7 +39,7 @@ def toBindingNamespace(arg):
def isTypeCopyConstructible(type):
# Nullable and sequence stuff doesn't affect copy/constructibility
# Nullable and sequence stuff doesn't affect copy-constructibility
type = type.unroll()
return (type.isPrimitive() or type.isString() or type.isEnum() or
(type.isUnion() and
@ -1028,7 +1028,7 @@ class CGHeaders(CGWrapper):
headerSet.add(self.getDeclarationFilename(unrolled.inner))
elif unrolled.isCallback():
# Callbacks are both a type and an object
headerSet.add(self.getDeclarationFilename(t.unroll()))
headerSet.add(self.getDeclarationFilename(unrolled))
elif unrolled.isFloat() and not unrolled.isUnrestricted():
# Restricted floats are tested for finiteness
bindingHeaders.add("mozilla/FloatingPoint.h")
@ -1038,6 +1038,14 @@ class CGHeaders(CGWrapper):
declareIncludes.add(filename)
elif unrolled.isPrimitive():
bindingHeaders.add("mozilla/dom/PrimitiveConversions.h")
elif unrolled.isMozMap():
if dictionary or jsImplementedDescriptors:
declareIncludes.add("mozilla/dom/MozMap.h")
else:
bindingHeaders.add("mozilla/dom/MozMap.h")
# Also add headers for the type the MozMap is
# parametrized over, if needed.
addHeadersForType((t.inner, descriptor, dictionary))
map(addHeadersForType,
getAllTypes(descriptors + callbackDescriptors, dictionaries,
@ -1153,6 +1161,8 @@ def UnionTypes(descriptors, dictionaries, callbacks, config):
assert not descriptor or not dictionary
t = t.unroll()
while t.isMozMap():
t = t.inner.unroll()
if not t.isUnion():
continue
name = str(t)
@ -1163,10 +1173,10 @@ def UnionTypes(descriptors, dictionaries, callbacks, config):
owningUnionStructs[name] = CGUnionStruct(t, providers[0],
ownsMembers=True)
for f in t.flatMemberTypes:
f = f.unroll()
def addHeadersForType(f):
if f.nullable():
headers.add("mozilla/dom/Nullable.h")
f = f.unroll()
if f.isInterface():
if f.isSpiderMonkeyInterface():
headers.add("jsfriendapi.h")
@ -1202,6 +1212,14 @@ def UnionTypes(descriptors, dictionaries, callbacks, config):
# the right header to be able to Release() in our inlined
# code.
headers.add(CGHeaders.getDeclarationFilename(f))
elif f.isMozMap():
headers.add("mozilla/dom/MozMap.h")
# And add headers for the type we're parametrized over
addHeadersForType(f.inner)
for f in t.flatMemberTypes:
assert not f.nullable()
addHeadersForType(f)
return (headers, implheaders, declarations,
CGList(SortedDictValues(unionStructs) +
@ -1230,7 +1248,7 @@ def UnionConversions(descriptors, dictionaries, callbacks, config):
if name not in unionConversions:
providers = getRelevantProviders(descriptor, config)
unionConversions[name] = CGUnionConversionStruct(t, providers[0])
for f in t.flatMemberTypes:
def addHeadersForType(f, providers):
f = f.unroll()
if f.isInterface():
if f.isSpiderMonkeyInterface():
@ -1257,6 +1275,13 @@ def UnionConversions(descriptors, dictionaries, callbacks, config):
headers.add(CGHeaders.getDeclarationFilename(f.inner))
elif f.isPrimitive():
headers.add("mozilla/dom/PrimitiveConversions.h")
elif f.isMozMap():
headers.add("mozilla/dom/MozMap.h")
# And the internal type of the MozMap
addHeadersForType(f.inner, providers)
for f in t.flatMemberTypes:
addHeadersForType(f, providers)
return (headers,
CGWrapper(CGList(SortedDictValues(unionConversions), "\n"),
@ -10955,6 +10980,8 @@ class CGForwardDeclarations(CGWrapper):
# since we don't know which one we might want
builder.addInMozillaDom(CGUnionStruct.unionTypeName(t, False))
builder.addInMozillaDom(CGUnionStruct.unionTypeName(t, True))
elif t.isMozMap():
forwardDeclareForType(t.inner, workerness)
# Don't need to do anything for void, primitive, string, any or object.
# There may be some other cases we are missing.
@ -11058,6 +11085,8 @@ class CGBindingRoot(CGThing):
def checkForXPConnectImpls(typeInfo):
type, _, _ = typeInfo
type = type.unroll()
while type.isMozMap():
type = type.inner.unroll()
if not type.isInterface() or not type.isGeckoInterface():
return False
try:

132
dom/bindings/MozMap.h Normal file
View File

@ -0,0 +1,132 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Class for representing MozMap arguments. This is an nsTHashtable
* under the hood, but we don't want to leak that implementation
* detail.
*/
#ifndef mozilla_dom_MozMap_h
#define mozilla_dom_MozMap_h
#include "nsTHashtable.h"
#include "nsHashKeys.h"
#include "nsStringGlue.h"
#include "nsTArray.h"
#include "mozilla/Move.h"
namespace mozilla {
namespace dom {
namespace binding_detail {
template<typename DataType>
class MozMapEntry : public nsStringHashKey
{
public:
MozMapEntry(const nsAString* aKeyTypePointer)
: nsStringHashKey(aKeyTypePointer)
{
}
// Move constructor so we can do MozMaps of MozMaps.
MozMapEntry(MozMapEntry<DataType>&& aOther)
: nsStringHashKey(aOther),
mData(Move(aOther.mData))
{
}
DataType mData;
};
} // namespace binding_detail
template<typename DataType>
class MozMap : protected nsTHashtable<binding_detail::MozMapEntry<DataType>>
{
public:
typedef typename binding_detail::MozMapEntry<DataType> EntryType;
typedef nsTHashtable<EntryType> Base;
typedef MozMap<DataType> SelfType;
MozMap()
{
}
// Move constructor so we can do MozMap of MozMap.
MozMap(SelfType&& aOther) :
Base(Move(aOther))
{
}
// The return value is only safe to use until an AddEntry call.
const DataType& Get(const nsAString& aKey) const
{
const EntryType* ent = this->GetEntry(aKey);
MOZ_ASSERT(ent, "Why are you using a key we didn't claim to have?");
return ent->mData;
}
// The return value is only safe to use until an AddEntry call.
const DataType* GetIfExists(const nsAString& aKey) const
{
const EntryType* ent = this->GetEntry(aKey);
if (!ent) {
return nullptr;
}
return &ent->mData;
}
void GetKeys(nsTArray<nsString>& aKeys) const {
// Sadly, EnumerateEntries is not a const method
const_cast<SelfType*>(this)->EnumerateEntries(KeyEnumerator, &aKeys);
}
// XXXbz we expose this generic enumerator for tracing. Otherwise we'd end up
// with a dependency on BindingUtils.h here for the SequenceTracer bits.
typedef PLDHashOperator (* Enumerator)(DataType* aValue, void* aClosure);
void EnumerateValues(Enumerator aEnumerator, void *aClosure)
{
ValueEnumClosure args = { aEnumerator, aClosure };
this->EnumerateEntries(ValueEnumerator, &args);
}
DataType* AddEntry(const nsAString& aKey) NS_WARN_UNUSED_RESULT
{
// XXXbz can't just use fallible_t() because our superclass has a
// private typedef by that name.
EntryType* ent = this->PutEntry(aKey, mozilla::fallible_t());
if (!ent) {
return nullptr;
}
return &ent->mData;
}
private:
static PLDHashOperator
KeyEnumerator(EntryType* aEntry, void* aClosure)
{
nsTArray<nsString>& keys = *static_cast<nsTArray<nsString>*>(aClosure);
keys.AppendElement(aEntry->GetKey());
return PL_DHASH_NEXT;
}
struct ValueEnumClosure {
Enumerator mEnumerator;
void* mClosure;
};
static PLDHashOperator
ValueEnumerator(EntryType* aEntry, void* aClosure)
{
ValueEnumClosure* enumClosure = static_cast<ValueEnumClosure*>(aClosure);
return enumClosure->mEnumerator(&aEntry->mData, enumClosure->mClosure);
}
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_MozMap_h

View File

@ -9,6 +9,7 @@
#include "mozilla/Assertions.h"
#include "nsTArrayForwardDeclare.h"
#include "mozilla/Move.h"
namespace mozilla {
namespace dom {
@ -33,6 +34,22 @@ public:
, mValue(aValue)
{}
explicit Nullable(Nullable<T>&& aOther)
: mIsNull(aOther.mIsNull)
, mValue(mozilla::Move(aOther.mValue))
{}
Nullable(const Nullable<T>& aOther)
: mIsNull(aOther.mIsNull)
, mValue(aOther.mValue)
{}
void operator=(const Nullable<T>& aOther)
{
mIsNull = aOther.mIsNull;
mValue = aOther.mValue;
}
void SetValue(T aValue) {
mValue = aValue;
mIsNull = false;

View File

@ -12,6 +12,7 @@
#include "js/RootingAPI.h"
#include "js/TracingAPI.h"
#include "mozilla/Attributes.h"
#include "mozilla/Move.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "nsWrapperCache.h"
@ -29,6 +30,12 @@ protected:
{
}
explicit TypedArrayObjectStorage(TypedArrayObjectStorage&& aOther)
: mObj(aOther.mObj)
{
aOther.mObj = nullptr;
}
public:
inline void TraceSelf(JSTracer* trc)
{
@ -62,6 +69,15 @@ struct TypedArray_base : public TypedArrayObjectStorage {
mObj = nullptr;
}
explicit TypedArray_base(TypedArray_base&& aOther)
: TypedArrayObjectStorage(Move(aOther)),
mData(aOther.mData),
mLength(aOther.mLength)
{
aOther.mData = nullptr;
aOther.mLength = 0;
}
private:
T* mData;
uint32_t mLength;
@ -123,6 +139,11 @@ struct TypedArray : public TypedArray_base<T,UnboxArray> {
TypedArray_base<T,UnboxArray>()
{}
explicit TypedArray(TypedArray&& aOther)
: TypedArray_base<T,UnboxArray>(Move(aOther))
{
}
static inline JSObject*
Create(JSContext* cx, nsWrapperCache* creator, uint32_t length,
const T* data = nullptr) {

View File

@ -24,6 +24,7 @@ EXPORTS.mozilla.dom += [
'Errors.msg',
'Exceptions.h',
'JSSlots.h',
'MozMap.h',
'NonRefcountedDOMObject.h',
'Nullable.h',
'OwningNonNull.h',

View File

@ -385,7 +385,7 @@ nsTHashtable<EntryType>::nsTHashtable(
{
// aOther shouldn't touch mTable after this, because we've stolen the table's
// pointers but not overwitten them.
MOZ_MAKE_MEM_UNDEFINED(aOther.mTable, sizeof(aOther.mTable));
MOZ_MAKE_MEM_UNDEFINED(&aOther.mTable, sizeof(aOther.mTable));
// Indicate that aOther is not initialized. This will make its destructor a
// nop, which is what we want.