2 * Copyright (C) 2011, 2012 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef SparseArrayValueMap_h
27 #define SparseArrayValueMap_h
30 #include "JSTypeInfo.h"
31 #include "PropertyDescriptor.h"
32 #include "PutDirectIndexMode.h"
33 #include "WriteBarrier.h"
34 #include <wtf/HashMap.h>
35 #include <wtf/Platform.h>
39 class SparseArrayValueMap
;
41 struct SparseArrayEntry
: public WriteBarrier
<Unknown
> {
42 typedef WriteBarrier
<Unknown
> Base
;
44 SparseArrayEntry() : attributes(0) { }
46 JSValue
get(ExecState
*, JSObject
*) const;
47 void get(PropertySlot
&) const;
48 void get(PropertyDescriptor
&) const;
49 void put(ExecState
*, JSValue thisValue
, SparseArrayValueMap
*, JSValue
, bool shouldThrow
);
50 JSValue
getNonSparseMode() const;
55 class SparseArrayValueMap
: public JSCell
{
60 typedef HashMap
<uint64_t, SparseArrayEntry
, WTF::IntHash
<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits
<uint64_t> > Map
;
68 SparseArrayValueMap(VM
&);
69 ~SparseArrayValueMap();
71 void finishCreation(VM
&);
73 static const unsigned StructureFlags
= OverridesVisitChildren
| JSCell::StructureFlags
;
76 static JS_EXPORTDATA
const ClassInfo s_info
;
78 typedef Map::iterator iterator
;
79 typedef Map::const_iterator const_iterator
;
80 typedef Map::AddResult AddResult
;
82 static SparseArrayValueMap
* create(VM
&);
84 static const bool needsDestruction
= true;
85 static const bool hasImmortalStructure
= true;
86 static void destroy(JSCell
*);
88 static Structure
* createStructure(VM
&, JSGlobalObject
*, JSValue prototype
);
90 static void visitChildren(JSCell
*, SlotVisitor
&);
94 return m_flags
& SparseMode
;
99 m_flags
= static_cast<Flags
>(m_flags
| SparseMode
);
102 bool lengthIsReadOnly()
104 return m_flags
& LengthIsReadOnly
;
107 void setLengthIsReadOnly()
109 m_flags
= static_cast<Flags
>(m_flags
| LengthIsReadOnly
);
112 // These methods may mutate the contents of the map
113 void putEntry(ExecState
*, JSObject
*, unsigned, JSValue
, bool shouldThrow
);
114 bool putDirect(ExecState
*, JSObject
*, unsigned, JSValue
, unsigned attributes
, PutDirectIndexMode
);
115 AddResult
add(JSObject
*, unsigned);
116 iterator
find(unsigned i
) { return m_map
.find(i
); }
117 // This should ASSERT the remove is valid (check the result of the find).
118 void remove(iterator it
) { m_map
.remove(it
); }
119 void remove(unsigned i
) { m_map
.remove(i
); }
121 // These methods do not mutate the contents of the map.
122 iterator
notFound() { return m_map
.end(); }
123 bool isEmpty() const { return m_map
.isEmpty(); }
124 bool contains(unsigned i
) const { return m_map
.contains(i
); }
125 size_t size() const { return m_map
.size(); }
126 // Only allow const begin/end iteration.
127 const_iterator
begin() const { return m_map
.begin(); }
128 const_iterator
end() const { return m_map
.end(); }
133 size_t m_reportedCapacity
;
138 #endif // SparseArrayValueMap_h