2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
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.
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.
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
28 typedef HashMap<unsigned, JSValuePtr> SparseArrayValueMap;
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 JSValuePtr m_vector[1];
39 class JSArray : public JSObject {
43 explicit JSArray(PassRefPtr<Structure>);
44 JSArray(PassRefPtr<Structure>, unsigned initialLength);
45 JSArray(ExecState*, PassRefPtr<Structure>, const ArgList& initialValues);
48 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
49 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
50 virtual void put(ExecState*, unsigned propertyName, JSValuePtr); // FIXME: Make protected and add setItem.
52 static const ClassInfo info;
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.
57 void sort(ExecState*);
58 void sort(ExecState*, JSValuePtr compareFunction, CallType, const CallData&);
59 void sortNumeric(ExecState*, JSValuePtr compareFunction, CallType, const CallData&);
61 void push(ExecState*, JSValuePtr);
64 bool canGetIndex(unsigned i) { return i < m_fastAccessCutoff; }
65 JSValuePtr getIndex(unsigned i)
67 ASSERT(canGetIndex(i));
68 return m_storage->m_vector[i];
71 bool canSetIndex(unsigned i) { return i < m_fastAccessCutoff; }
72 JSValuePtr setIndex(unsigned i, JSValuePtr v)
74 ASSERT(canSetIndex(i));
75 return m_storage->m_vector[i] = v;
78 void fillArgList(ExecState*, ArgList&);
80 static PassRefPtr<Structure> createStructure(JSValuePtr prototype)
82 return Structure::create(prototype, TypeInfo(ObjectType));
86 virtual void put(ExecState*, const Identifier& propertyName, JSValuePtr, PutPropertySlot&);
87 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
88 virtual bool deleteProperty(ExecState*, unsigned propertyName);
89 virtual void getPropertyNames(ExecState*, PropertyNameArray&);
92 void* lazyCreationData();
93 void setLazyCreationData(void*);
96 virtual const ClassInfo* classInfo() const { return &info; }
98 bool getOwnPropertySlotSlowCase(ExecState*, unsigned propertyName, PropertySlot&);
99 void putSlowCase(ExecState*, unsigned propertyName, JSValuePtr);
101 bool increaseVectorLength(unsigned newLength);
103 unsigned compactForSorting();
105 enum ConsistencyCheckType { NormalConsistencyCheck, DestructorConsistencyCheck, SortConsistencyCheck };
106 void checkConsistency(ConsistencyCheckType = NormalConsistencyCheck);
108 unsigned m_fastAccessCutoff;
109 ArrayStorage* m_storage;
112 JSArray* asArray(JSValuePtr);
114 JSArray* constructEmptyArray(ExecState*);
115 JSArray* constructEmptyArray(ExecState*, unsigned initialLength);
116 JSArray* constructArray(ExecState*, JSValuePtr singleItemValue);
117 JSArray* constructArray(ExecState*, const ArgList& values);
119 inline JSArray* asArray(JSValuePtr value)
121 ASSERT(asObject(value)->inherits(&JSArray::info));
122 return static_cast<JSArray*>(asObject(value));