2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
30 #include "Structure.h"
31 #include <wtf/HashFunctions.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/MathExtras.h>
37 class MapData
: public JSCell
{
41 struct const_iterator
{
42 const_iterator(const MapData
*);
44 const WTF::KeyValuePair
<JSValue
, JSValue
> operator*() const;
45 JSValue
key() const { ASSERT(!atEnd()); return m_mapData
->m_entries
[m_index
].key
.get(); }
46 JSValue
value() const { ASSERT(!atEnd()); return m_mapData
->m_entries
[m_index
].value
.get(); }
47 void operator++() { ASSERT(!atEnd()); internalIncrement(); }
48 static const_iterator
end(const MapData
*);
49 bool operator!=(const const_iterator
& other
);
50 bool operator==(const const_iterator
& other
);
55 // This is a bit gnarly. We use an index of -1 to indicate the
56 // "end()" iterator. By casting to unsigned we can immediately
57 // test if both iterators are at the end of their iteration.
58 // We need this in order to keep the common case (eg. iter != end())
60 bool atEnd() const { return static_cast<size_t>(m_index
) >= static_cast<size_t>(m_mapData
->m_size
); }
61 void internalIncrement();
62 const MapData
* m_mapData
;
67 ALWAYS_INLINE
KeyType() { }
72 static MapData
* create(VM
& vm
)
74 MapData
* mapData
= new (NotNull
, allocateCell
<MapData
>(vm
.heap
)) MapData(vm
);
75 mapData
->finishCreation(vm
);
79 static Structure
* createStructure(VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
81 return Structure::create(vm
, globalObject
, prototype
, TypeInfo(CompoundType
, StructureFlags
), info());
84 static const bool needsDestruction
= true;
85 static const bool hasImmortalStructure
= true;
87 JS_EXPORT_PRIVATE
void set(CallFrame
*, KeyType
, JSValue
);
88 JSValue
get(CallFrame
*, KeyType
);
89 bool remove(CallFrame
*, KeyType
);
90 bool contains(CallFrame
*, KeyType
);
91 size_t size(CallFrame
*) const { return m_size
- m_deletedCount
; }
93 const_iterator
begin() const { return const_iterator(this); }
94 const_iterator
end() const { return const_iterator::end(this); }
99 static const unsigned StructureFlags
= OverridesVisitChildren
| StructureIsImmortal
| Base::StructureFlags
;
102 typedef WTF::UnsignedWithZeroKeyHashTraits
<int32_t> IndexTraits
;
104 // Our marking functions expect Entry to maintain this layout, and have all
105 // fields be WriteBarrier<Unknown>
107 WriteBarrier
<Unknown
> key
;
108 WriteBarrier
<Unknown
> value
;
111 typedef HashMap
<JSCell
*, int32_t, typename
WTF::DefaultHash
<JSCell
*>::Hash
, WTF::HashTraits
<JSCell
*>, IndexTraits
> CellKeyedMap
;
112 typedef HashMap
<EncodedJSValue
, int32_t, EncodedJSValueHash
, EncodedJSValueHashTraits
, IndexTraits
> ValueKeyedMap
;
113 typedef HashMap
<StringImpl
*, int32_t, typename
WTF::DefaultHash
<StringImpl
*>::Hash
, WTF::HashTraits
<StringImpl
*>, IndexTraits
> StringKeyedMap
;
115 size_t capacityInBytes() { return m_capacity
* sizeof(Entry
); }
118 static void destroy(JSCell
*);
119 static void visitChildren(JSCell
*, SlotVisitor
&);
120 static void copyBackingStore(JSCell
*, CopyVisitor
&, CopyToken
);
123 ALWAYS_INLINE Entry
* find(CallFrame
*, KeyType
);
124 ALWAYS_INLINE Entry
* add(CallFrame
*, KeyType
);
125 template <typename Map
, typename Key
> ALWAYS_INLINE Entry
* add(CallFrame
*, Map
&, Key
, KeyType
);
127 ALWAYS_INLINE
bool shouldPack() const { return m_deletedCount
&& !m_iteratorCount
; }
128 CheckedBoolean
ensureSpaceForAppend(CallFrame
*);
130 ALWAYS_INLINE
void replaceAndPackBackingStore(Entry
* destination
, int32_t newSize
);
131 ALWAYS_INLINE
void replaceBackingStore(Entry
* destination
, int32_t newSize
);
133 CellKeyedMap m_cellKeyedTable
;
134 ValueKeyedMap m_valueKeyedTable
;
135 StringKeyedMap m_stringKeyedTable
;
138 int32_t m_deletedCount
;
139 mutable int32_t m_iteratorCount
;
143 ALWAYS_INLINE
void MapData::clear()
145 m_cellKeyedTable
.clear();
146 m_valueKeyedTable
.clear();
147 m_stringKeyedTable
.clear();
154 ALWAYS_INLINE
MapData::KeyType::KeyType(JSValue v
)
160 double d
= v
.asDouble();
161 if (std::isnan(d
) || (std::signbit(d
) && d
== 0.0)) {
166 int i
= static_cast<int>(v
.asDouble());
173 ALWAYS_INLINE
void MapData::const_iterator::internalIncrement()
175 Entry
* entries
= m_mapData
->m_entries
;
176 size_t index
= m_index
+ 1;
177 size_t end
= m_mapData
->m_size
;
178 while (index
< end
&& !entries
[index
].key
)
183 ALWAYS_INLINE
bool MapData::const_iterator::ensureSlot()
185 // When an iterator exists outside of host cost it is possible for
186 // the containing map to be modified
187 Entry
* entries
= m_mapData
->m_entries
;
188 size_t index
= m_index
;
189 size_t end
= m_mapData
->m_size
;
190 if (index
< end
&& entries
[index
].key
)
193 return static_cast<size_t>(m_index
) < end
;
196 ALWAYS_INLINE
MapData::const_iterator::const_iterator(const MapData
* mapData
)
203 ALWAYS_INLINE
MapData::const_iterator::~const_iterator()
205 m_mapData
->m_iteratorCount
--;
208 ALWAYS_INLINE
const WTF::KeyValuePair
<JSValue
, JSValue
> MapData::const_iterator::operator*() const
210 Entry
* entry
= &m_mapData
->m_entries
[m_index
];
211 return WTF::KeyValuePair
<JSValue
, JSValue
>(entry
->key
.get(), entry
->value
.get());
214 ALWAYS_INLINE
MapData::const_iterator
MapData::const_iterator::end(const MapData
* mapData
)
216 const_iterator
result(mapData
);
221 ALWAYS_INLINE
bool MapData::const_iterator::operator!=(const const_iterator
& other
)
223 ASSERT(other
.m_mapData
== m_mapData
);
224 if (atEnd() && other
.atEnd())
226 return m_index
!= other
.m_index
;
229 ALWAYS_INLINE
bool MapData::const_iterator::operator==(const const_iterator
& other
)
231 return !(*this != other
);
236 #endif /* !defined(MapData_h) */