]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
3 | * Copyright (C) 2003, 2007, 2008 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 | ||
ba379fdc | 28 | typedef HashMap<unsigned, JSValue> SparseArrayValueMap; |
9dae56ea A |
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. | |
ba379fdc | 36 | JSValue m_vector[1]; |
9dae56ea A |
37 | }; |
38 | ||
39 | class JSArray : public JSObject { | |
40 | friend class JIT; | |
41 | ||
42 | public: | |
43 | explicit JSArray(PassRefPtr<Structure>); | |
44 | JSArray(PassRefPtr<Structure>, unsigned initialLength); | |
ba379fdc | 45 | JSArray(PassRefPtr<Structure>, const ArgList& initialValues); |
9dae56ea A |
46 | virtual ~JSArray(); |
47 | ||
48 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&); | |
49 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&); | |
ba379fdc | 50 | virtual void put(ExecState*, unsigned propertyName, JSValue); // FIXME: Make protected and add setItem. |
9dae56ea | 51 | |
ba379fdc | 52 | static JS_EXPORTDATA const ClassInfo info; |
9dae56ea A |
53 | |
54 | unsigned length() const { return m_storage->m_length; } | |
55 | void setLength(unsigned); // OK to use on new arrays, but not if it might be a RegExpMatchArray. | |
56 | ||
57 | void sort(ExecState*); | |
ba379fdc A |
58 | void sort(ExecState*, JSValue compareFunction, CallType, const CallData&); |
59 | void sortNumeric(ExecState*, JSValue compareFunction, CallType, const CallData&); | |
9dae56ea | 60 | |
ba379fdc A |
61 | void push(ExecState*, JSValue); |
62 | JSValue pop(); | |
9dae56ea A |
63 | |
64 | bool canGetIndex(unsigned i) { return i < m_fastAccessCutoff; } | |
ba379fdc | 65 | JSValue getIndex(unsigned i) |
9dae56ea A |
66 | { |
67 | ASSERT(canGetIndex(i)); | |
68 | return m_storage->m_vector[i]; | |
69 | } | |
70 | ||
71 | bool canSetIndex(unsigned i) { return i < m_fastAccessCutoff; } | |
ba379fdc | 72 | JSValue setIndex(unsigned i, JSValue v) |
9dae56ea A |
73 | { |
74 | ASSERT(canSetIndex(i)); | |
75 | return m_storage->m_vector[i] = v; | |
76 | } | |
77 | ||
ba379fdc A |
78 | void fillArgList(ExecState*, MarkedArgumentBuffer&); |
79 | void copyToRegisters(ExecState*, Register*, uint32_t); | |
9dae56ea | 80 | |
ba379fdc | 81 | static PassRefPtr<Structure> createStructure(JSValue prototype) |
9dae56ea A |
82 | { |
83 | return Structure::create(prototype, TypeInfo(ObjectType)); | |
84 | } | |
85 | ||
86 | protected: | |
ba379fdc | 87 | virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&); |
9dae56ea A |
88 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName); |
89 | virtual bool deleteProperty(ExecState*, unsigned propertyName); | |
90 | virtual void getPropertyNames(ExecState*, PropertyNameArray&); | |
91 | virtual void mark(); | |
92 | ||
93 | void* lazyCreationData(); | |
94 | void setLazyCreationData(void*); | |
95 | ||
96 | private: | |
97 | virtual const ClassInfo* classInfo() const { return &info; } | |
98 | ||
99 | bool getOwnPropertySlotSlowCase(ExecState*, unsigned propertyName, PropertySlot&); | |
ba379fdc | 100 | void putSlowCase(ExecState*, unsigned propertyName, JSValue); |
9dae56ea A |
101 | |
102 | bool increaseVectorLength(unsigned newLength); | |
103 | ||
104 | unsigned compactForSorting(); | |
105 | ||
106 | enum ConsistencyCheckType { NormalConsistencyCheck, DestructorConsistencyCheck, SortConsistencyCheck }; | |
107 | void checkConsistency(ConsistencyCheckType = NormalConsistencyCheck); | |
108 | ||
109 | unsigned m_fastAccessCutoff; | |
110 | ArrayStorage* m_storage; | |
111 | }; | |
112 | ||
ba379fdc | 113 | JSArray* asArray(JSValue); |
9dae56ea A |
114 | |
115 | JSArray* constructEmptyArray(ExecState*); | |
116 | JSArray* constructEmptyArray(ExecState*, unsigned initialLength); | |
ba379fdc | 117 | JSArray* constructArray(ExecState*, JSValue singleItemValue); |
9dae56ea A |
118 | JSArray* constructArray(ExecState*, const ArgList& values); |
119 | ||
ba379fdc | 120 | inline JSArray* asArray(JSValue value) |
9dae56ea A |
121 | { |
122 | ASSERT(asObject(value)->inherits(&JSArray::info)); | |
123 | return static_cast<JSArray*>(asObject(value)); | |
124 | } | |
125 | ||
ba379fdc A |
126 | inline bool isJSArray(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsArrayVPtr; } |
127 | ||
9dae56ea A |
128 | } // namespace JSC |
129 | ||
130 | #endif // JSArray_h |