1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2003, 2007, 2008, 2009 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef JSArray_h
|
---|
22 | #define JSArray_h
|
---|
23 |
|
---|
24 | #include "JSObject.h"
|
---|
25 |
|
---|
26 | namespace JSC {
|
---|
27 |
|
---|
28 | typedef HashMap<unsigned, JSValue> SparseArrayValueMap;
|
---|
29 |
|
---|
30 | struct ArrayStorage {
|
---|
31 | unsigned m_length;
|
---|
32 | unsigned m_vectorLength;
|
---|
33 | unsigned m_numValuesInVector;
|
---|
34 | SparseArrayValueMap* m_sparseValueMap;
|
---|
35 | void* lazyCreationData; // A JSArray subclass can use this to fill the vector lazily.
|
---|
36 | JSValue m_vector[1];
|
---|
37 | };
|
---|
38 |
|
---|
39 | class JSArray : public JSObject {
|
---|
40 | friend class JIT;
|
---|
41 | friend class Walker;
|
---|
42 |
|
---|
43 | public:
|
---|
44 | explicit JSArray(PassRefPtr<Structure>);
|
---|
45 | JSArray(PassRefPtr<Structure>, unsigned initialLength);
|
---|
46 | JSArray(PassRefPtr<Structure>, const ArgList& initialValues);
|
---|
47 | virtual ~JSArray();
|
---|
48 |
|
---|
49 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
50 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
51 | virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
|
---|
52 | virtual void put(ExecState*, unsigned propertyName, JSValue); // FIXME: Make protected and add setItem.
|
---|
53 |
|
---|
54 | static JS_EXPORTDATA const ClassInfo info;
|
---|
55 |
|
---|
56 | unsigned length() const { return m_storage->m_length; }
|
---|
57 | void setLength(unsigned); // OK to use on new arrays, but not if it might be a RegExpMatchArray.
|
---|
58 |
|
---|
59 | void sort(ExecState*);
|
---|
60 | void sort(ExecState*, JSValue compareFunction, CallType, const CallData&);
|
---|
61 | void sortNumeric(ExecState*, JSValue compareFunction, CallType, const CallData&);
|
---|
62 |
|
---|
63 | void push(ExecState*, JSValue);
|
---|
64 | JSValue pop();
|
---|
65 |
|
---|
66 | bool canGetIndex(unsigned i) { return i < m_fastAccessCutoff; }
|
---|
67 | JSValue getIndex(unsigned i)
|
---|
68 | {
|
---|
69 | ASSERT(canGetIndex(i));
|
---|
70 | return m_storage->m_vector[i];
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool canSetIndex(unsigned i) { return i < m_fastAccessCutoff; }
|
---|
74 | JSValue setIndex(unsigned i, JSValue v)
|
---|
75 | {
|
---|
76 | ASSERT(canSetIndex(i));
|
---|
77 | return m_storage->m_vector[i] = v;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void fillArgList(ExecState*, MarkedArgumentBuffer&);
|
---|
81 | void copyToRegisters(ExecState*, Register*, uint32_t);
|
---|
82 |
|
---|
83 | static PassRefPtr<Structure> createStructure(JSValue prototype)
|
---|
84 | {
|
---|
85 | return Structure::create(prototype, TypeInfo(ObjectType));
|
---|
86 | }
|
---|
87 |
|
---|
88 | inline void markChildrenDirect(MarkStack& markStack);
|
---|
89 |
|
---|
90 | protected:
|
---|
91 | virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
|
---|
92 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
93 | virtual bool deleteProperty(ExecState*, unsigned propertyName);
|
---|
94 | virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&);
|
---|
95 | virtual void markChildren(MarkStack&);
|
---|
96 |
|
---|
97 | void* lazyCreationData();
|
---|
98 | void setLazyCreationData(void*);
|
---|
99 |
|
---|
100 | private:
|
---|
101 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
102 |
|
---|
103 | bool getOwnPropertySlotSlowCase(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
104 | void putSlowCase(ExecState*, unsigned propertyName, JSValue);
|
---|
105 |
|
---|
106 | bool increaseVectorLength(unsigned newLength);
|
---|
107 |
|
---|
108 | unsigned compactForSorting();
|
---|
109 |
|
---|
110 | enum ConsistencyCheckType { NormalConsistencyCheck, DestructorConsistencyCheck, SortConsistencyCheck };
|
---|
111 | void checkConsistency(ConsistencyCheckType = NormalConsistencyCheck);
|
---|
112 |
|
---|
113 | unsigned m_fastAccessCutoff;
|
---|
114 | ArrayStorage* m_storage;
|
---|
115 | };
|
---|
116 |
|
---|
117 | JSArray* asArray(JSValue);
|
---|
118 |
|
---|
119 | inline JSArray* asArray(JSCell* cell)
|
---|
120 | {
|
---|
121 | ASSERT(cell->inherits(&JSArray::info));
|
---|
122 | return static_cast<JSArray*>(cell);
|
---|
123 | }
|
---|
124 |
|
---|
125 | inline JSArray* asArray(JSValue value)
|
---|
126 | {
|
---|
127 | return asArray(value.asCell());
|
---|
128 | }
|
---|
129 |
|
---|
130 | inline bool isJSArray(JSGlobalData* globalData, JSValue v)
|
---|
131 | {
|
---|
132 | return v.isCell() && v.asCell()->vptr() == globalData->jsArrayVPtr;
|
---|
133 | }
|
---|
134 | inline bool isJSArray(JSGlobalData* globalData, JSCell* cell) { return cell->vptr() == globalData->jsArrayVPtr; }
|
---|
135 |
|
---|
136 | inline void JSArray::markChildrenDirect(MarkStack& markStack)
|
---|
137 | {
|
---|
138 | JSObject::markChildrenDirect(markStack);
|
---|
139 |
|
---|
140 | ArrayStorage* storage = m_storage;
|
---|
141 |
|
---|
142 | unsigned usedVectorLength = std::min(storage->m_length, storage->m_vectorLength);
|
---|
143 | markStack.appendValues(storage->m_vector, usedVectorLength, MayContainNullValues);
|
---|
144 |
|
---|
145 | if (SparseArrayValueMap* map = storage->m_sparseValueMap) {
|
---|
146 | SparseArrayValueMap::iterator end = map->end();
|
---|
147 | for (SparseArrayValueMap::iterator it = map->begin(); it != end; ++it)
|
---|
148 | markStack.append(it->second);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | inline void MarkStack::markChildren(JSCell* cell)
|
---|
153 | {
|
---|
154 | ASSERT(Heap::isCellMarked(cell));
|
---|
155 | if (cell->structure()->typeInfo().hasDefaultMark()) {
|
---|
156 | #ifdef NDEBUG
|
---|
157 | asObject(cell)->markChildrenDirect(*this);
|
---|
158 | #else
|
---|
159 | ASSERT(!m_isCheckingForDefaultMarkViolation);
|
---|
160 | m_isCheckingForDefaultMarkViolation = true;
|
---|
161 | cell->markChildren(*this);
|
---|
162 | ASSERT(m_isCheckingForDefaultMarkViolation);
|
---|
163 | m_isCheckingForDefaultMarkViolation = false;
|
---|
164 | #endif
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | if (cell->vptr() == m_jsArrayVPtr) {
|
---|
168 | asArray(cell)->markChildrenDirect(*this);
|
---|
169 | return;
|
---|
170 | }
|
---|
171 | cell->markChildren(*this);
|
---|
172 | }
|
---|
173 |
|
---|
174 | inline void MarkStack::drain()
|
---|
175 | {
|
---|
176 | while (!m_markSets.isEmpty() || !m_values.isEmpty()) {
|
---|
177 | while (!m_markSets.isEmpty() && m_values.size() < 50) {
|
---|
178 | ASSERT(!m_markSets.isEmpty());
|
---|
179 | MarkSet& current = m_markSets.last();
|
---|
180 | ASSERT(current.m_values);
|
---|
181 | JSValue* end = current.m_end;
|
---|
182 | ASSERT(current.m_values);
|
---|
183 | ASSERT(current.m_values != end);
|
---|
184 | findNextUnmarkedNullValue:
|
---|
185 | ASSERT(current.m_values != end);
|
---|
186 | JSValue value = *current.m_values;
|
---|
187 | current.m_values++;
|
---|
188 |
|
---|
189 | JSCell* cell;
|
---|
190 | if (!value || !value.isCell() || Heap::isCellMarked(cell = value.asCell())) {
|
---|
191 | if (current.m_values == end) {
|
---|
192 | m_markSets.removeLast();
|
---|
193 | continue;
|
---|
194 | }
|
---|
195 | goto findNextUnmarkedNullValue;
|
---|
196 | }
|
---|
197 |
|
---|
198 | Heap::markCell(cell);
|
---|
199 | if (cell->structure()->typeInfo().type() < CompoundType) {
|
---|
200 | if (current.m_values == end) {
|
---|
201 | m_markSets.removeLast();
|
---|
202 | continue;
|
---|
203 | }
|
---|
204 | goto findNextUnmarkedNullValue;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (current.m_values == end)
|
---|
208 | m_markSets.removeLast();
|
---|
209 |
|
---|
210 | markChildren(cell);
|
---|
211 | }
|
---|
212 | while (!m_values.isEmpty())
|
---|
213 | markChildren(m_values.removeLast());
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | } // namespace JSC
|
---|
218 |
|
---|
219 | #endif // JSArray_h
|
---|