initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / JSArray.h
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
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 #define CHECK_ARRAY_CONSISTENCY 0
27
28 namespace JSC {
29
30     typedef HashMap<unsigned, WriteBarrier<Unknown> > SparseArrayValueMap;
31
32     // This struct holds the actual data values of an array.  A JSArray object points to it's contained ArrayStorage
33     // struct by pointing to m_vector.  To access the contained ArrayStorage struct, use the getStorage() and 
34     // setStorage() methods.  It is important to note that there may be space before the ArrayStorage that 
35     // is used to quick unshift / shift operation.  The actual allocated pointer is available by using:
36     //     getStorage() - m_indexBias * sizeof(JSValue)
37     struct ArrayStorage {
38         unsigned m_length; // The "length" property on the array
39         unsigned m_numValuesInVector;
40         SparseArrayValueMap* m_sparseValueMap;
41         void* subclassData; // A JSArray subclass can use this to fill the vector lazily.
42         void* m_allocBase; // Pointer to base address returned by malloc().  Keeping this pointer does eliminate false positives from the leak detector.
43         size_t reportedMapCapacity;
44 #if CHECK_ARRAY_CONSISTENCY
45         bool m_inCompactInitialization;
46 #endif
47         WriteBarrier<Unknown> m_vector[1];
48     };
49
50     // The CreateCompact creation mode is used for fast construction of arrays
51     // whose size and contents are known at time of creation.
52     //
53     // There are two obligations when using this mode:
54     //
55     //   - uncheckedSetIndex() must be used when initializing the array.
56     //   - setLength() must be called after initialization.
57
58     enum ArrayCreationMode { CreateCompact, CreateInitialized };
59
60     class JSArray : public JSNonFinalObject {
61         friend class Walker;
62
63     protected:
64         explicit JSArray(JSGlobalData&, Structure*);
65
66         void finishCreation(JSGlobalData&);
67         void finishCreation(JSGlobalData&, unsigned initialLength, ArrayCreationMode);
68         void finishCreation(JSGlobalData&, const ArgList&);
69     
70     public:
71         typedef JSNonFinalObject Base;
72
73         JSArray(VPtrStealingHackType);
74         virtual ~JSArray();
75
76         static JSArray* create(JSGlobalData& globalData, Structure* structure)
77         {
78             JSArray* array = new (allocateCell<JSArray>(globalData.heap)) JSArray(globalData, structure);
79             array->finishCreation(globalData);
80             return array;
81         }
82
83         static JSArray* create(JSGlobalData& globalData, Structure* structure, unsigned initialLength, ArrayCreationMode createMode)
84         {
85             JSArray* array = new (allocateCell<JSArray>(globalData.heap)) JSArray(globalData, structure);
86             array->finishCreation(globalData, initialLength, createMode);
87             return array;
88         }
89
90         static JSArray* create(JSGlobalData& globalData, Structure* structure, const ArgList& initialValues)
91         {
92             JSArray* array = new (allocateCell<JSArray>(globalData.heap)) JSArray(globalData, structure);
93             array->finishCreation(globalData, initialValues);
94             return array;
95         }
96
97         virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
98         virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
99         virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
100         virtual void put(ExecState*, unsigned propertyName, JSValue); // FIXME: Make protected and add setItem.
101
102         static JS_EXPORTDATA const ClassInfo s_info;
103         
104         unsigned length() const { return m_storage->m_length; }
105         void setLength(unsigned); // OK to use on new arrays, but not if it might be a RegExpMatchArray.
106
107         void sort(ExecState*);
108         void sort(ExecState*, JSValue compareFunction, CallType, const CallData&);
109         void sortNumeric(ExecState*, JSValue compareFunction, CallType, const CallData&);
110
111         void push(ExecState*, JSValue);
112         JSValue pop();
113
114         void shiftCount(ExecState*, int count);
115         void unshiftCount(ExecState*, int count);
116
117         bool canGetIndex(unsigned i) { return i < m_vectorLength && m_storage->m_vector[i]; }
118         JSValue getIndex(unsigned i)
119         {
120             ASSERT(canGetIndex(i));
121             return m_storage->m_vector[i].get();
122         }
123
124         bool canSetIndex(unsigned i) { return i < m_vectorLength; }
125         void setIndex(JSGlobalData& globalData, unsigned i, JSValue v)
126         {
127             ASSERT(canSetIndex(i));
128             
129             WriteBarrier<Unknown>& x = m_storage->m_vector[i];
130             if (!x) {
131                 ArrayStorage *storage = m_storage;
132                 ++storage->m_numValuesInVector;
133                 if (i >= storage->m_length)
134                     storage->m_length = i + 1;
135             }
136             x.set(globalData, this, v);
137         }
138         
139         void uncheckedSetIndex(JSGlobalData& globalData, unsigned i, JSValue v)
140         {
141             ASSERT(canSetIndex(i));
142             ArrayStorage *storage = m_storage;
143 #if CHECK_ARRAY_CONSISTENCY
144             ASSERT(storage->m_inCompactInitialization);
145 #endif
146             storage->m_vector[i].set(globalData, this, v);
147         }
148
149         void fillArgList(ExecState*, MarkedArgumentBuffer&);
150         void copyToRegisters(ExecState*, Register*, uint32_t);
151
152         static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
153         {
154             return Structure::create(globalData, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
155         }
156         
157         inline void visitChildrenDirect(SlotVisitor&);
158
159         static ptrdiff_t storageOffset()
160         {
161             return OBJECT_OFFSETOF(JSArray, m_storage);
162         }
163
164         static ptrdiff_t vectorLengthOffset()
165         {
166             return OBJECT_OFFSETOF(JSArray, m_vectorLength);
167         }
168
169     protected:
170         static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
171         virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
172         virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
173         virtual bool deleteProperty(ExecState*, unsigned propertyName);
174         virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
175         virtual void visitChildren(SlotVisitor&);
176
177         void* subclassData() const;
178         void setSubclassData(void*);
179
180     private:
181         bool getOwnPropertySlotSlowCase(ExecState*, unsigned propertyName, PropertySlot&);
182         void putSlowCase(ExecState*, unsigned propertyName, JSValue);
183
184         unsigned getNewVectorLength(unsigned desiredLength);
185         bool increaseVectorLength(unsigned newLength);
186         bool increaseVectorPrefixLength(unsigned newLength);
187         
188         unsigned compactForSorting();
189
190         enum ConsistencyCheckType { NormalConsistencyCheck, DestructorConsistencyCheck, SortConsistencyCheck };
191         void checkConsistency(ConsistencyCheckType = NormalConsistencyCheck);
192
193         unsigned m_vectorLength; // The valid length of m_vector
194         int m_indexBias; // The number of JSValue sized blocks before ArrayStorage.
195         ArrayStorage *m_storage;
196     };
197
198     JSArray* asArray(JSValue);
199
200     inline JSArray* asArray(JSCell* cell)
201     {
202         ASSERT(cell->inherits(&JSArray::s_info));
203         return static_cast<JSArray*>(cell);
204     }
205
206     inline JSArray* asArray(JSValue value)
207     {
208         return asArray(value.asCell());
209     }
210
211     inline bool isJSArray(JSGlobalData* globalData, JSCell* cell) { return cell->vptr() == globalData->jsArrayVPtr; }
212     inline bool isJSArray(JSGlobalData* globalData, JSValue v) { return v.isCell() && isJSArray(globalData, v.asCell()); }
213
214     inline void JSArray::visitChildrenDirect(SlotVisitor& visitor)
215     {
216         JSObject::visitChildrenDirect(visitor);
217         
218         ArrayStorage* storage = m_storage;
219
220         unsigned usedVectorLength = std::min(storage->m_length, m_vectorLength);
221         visitor.appendValues(storage->m_vector, usedVectorLength);
222
223         if (SparseArrayValueMap* map = storage->m_sparseValueMap) {
224             SparseArrayValueMap::iterator end = map->end();
225             for (SparseArrayValueMap::iterator it = map->begin(); it != end; ++it)
226                 visitor.append(&it->second);
227         }
228     }
229
230     // Rule from ECMA 15.2 about what an array index is.
231     // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
232     inline unsigned Identifier::toArrayIndex(bool& ok) const
233     {
234         unsigned i = toUInt32(ok);
235         if (ok && i >= 0xFFFFFFFFU)
236             ok = false;
237         return i;
238     }
239
240 } // namespace JSC
241
242 #endif // JSArray_h