]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/SparseArrayValueMap.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / SparseArrayValueMap.h
1 /*
2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #ifndef SparseArrayValueMap_h
27 #define SparseArrayValueMap_h
28
29 #include "JSCell.h"
30 #include "JSTypeInfo.h"
31 #include "PropertyDescriptor.h"
32 #include "PutDirectIndexMode.h"
33 #include "WriteBarrier.h"
34 #include <wtf/HashMap.h>
35
36 namespace JSC {
37
38 class SparseArrayValueMap;
39
40 struct SparseArrayEntry : public WriteBarrier<Unknown> {
41 typedef WriteBarrier<Unknown> Base;
42
43 SparseArrayEntry() : attributes(0) { }
44
45 JSValue get(ExecState*, JSObject*) const;
46 void get(JSObject*, PropertySlot&) const;
47 void get(PropertyDescriptor&) const;
48 void put(ExecState*, JSValue thisValue, SparseArrayValueMap*, JSValue, bool shouldThrow);
49 JSValue getNonSparseMode() const;
50
51 unsigned attributes;
52 };
53
54 class SparseArrayValueMap final : public JSCell {
55 public:
56 typedef JSCell Base;
57 static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
58
59 private:
60 typedef HashMap<uint64_t, SparseArrayEntry, WTF::IntHash<uint64_t>, WTF::UnsignedWithZeroKeyHashTraits<uint64_t>> Map;
61
62 enum Flags {
63 Normal = 0,
64 SparseMode = 1,
65 LengthIsReadOnly = 2,
66 };
67
68 SparseArrayValueMap(VM&);
69 ~SparseArrayValueMap();
70
71 void finishCreation(VM&);
72
73 public:
74 DECLARE_EXPORT_INFO;
75
76 typedef Map::iterator iterator;
77 typedef Map::const_iterator const_iterator;
78 typedef Map::AddResult AddResult;
79
80 static SparseArrayValueMap* create(VM&);
81
82 static const bool needsDestruction = true;
83 static void destroy(JSCell*);
84
85 static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
86
87 static void visitChildren(JSCell*, SlotVisitor&);
88
89 bool sparseMode()
90 {
91 return m_flags & SparseMode;
92 }
93
94 void setSparseMode()
95 {
96 m_flags = static_cast<Flags>(m_flags | SparseMode);
97 }
98
99 bool lengthIsReadOnly()
100 {
101 return m_flags & LengthIsReadOnly;
102 }
103
104 void setLengthIsReadOnly()
105 {
106 m_flags = static_cast<Flags>(m_flags | LengthIsReadOnly);
107 }
108
109 // These methods may mutate the contents of the map
110 void putEntry(ExecState*, JSObject*, unsigned, JSValue, bool shouldThrow);
111 bool putDirect(ExecState*, JSObject*, unsigned, JSValue, unsigned attributes, PutDirectIndexMode);
112 AddResult add(JSObject*, unsigned);
113 iterator find(unsigned i) { return m_map.find(i); }
114 // This should ASSERT the remove is valid (check the result of the find).
115 void remove(iterator it) { m_map.remove(it); }
116 void remove(unsigned i) { m_map.remove(i); }
117
118 // These methods do not mutate the contents of the map.
119 iterator notFound() { return m_map.end(); }
120 bool isEmpty() const { return m_map.isEmpty(); }
121 bool contains(unsigned i) const { return m_map.contains(i); }
122 size_t size() const { return m_map.size(); }
123 // Only allow const begin/end iteration.
124 const_iterator begin() const { return m_map.begin(); }
125 const_iterator end() const { return m_map.end(); }
126
127 private:
128 Map m_map;
129 Flags m_flags;
130 size_t m_reportedCapacity;
131 };
132
133 } // namespace JSC
134
135 #endif // SparseArrayValueMap_h
136