1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
4 | * Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef JSArray_h
|
---|
23 | #define JSArray_h
|
---|
24 |
|
---|
25 | #include "JSObject.h"
|
---|
26 |
|
---|
27 | namespace KJS {
|
---|
28 |
|
---|
29 | typedef HashMap<unsigned, JSValue*> SparseArrayValueMap;
|
---|
30 |
|
---|
31 | struct ArrayStorage {
|
---|
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 | public:
|
---|
41 | JSArray(JSValue* prototype, unsigned initialLength);
|
---|
42 | JSArray(JSObject* prototype, const ArgList& initialValues);
|
---|
43 | virtual ~JSArray();
|
---|
44 |
|
---|
45 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
46 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
47 | virtual void put(ExecState*, unsigned propertyName, JSValue*); // FIXME: Make protected and add setItem.
|
---|
48 |
|
---|
49 | static const ClassInfo info;
|
---|
50 |
|
---|
51 | unsigned getLength() const { return m_length; }
|
---|
52 | void setLength(unsigned); // OK to use on new arrays, but not if it might be a RegExpMatchArray.
|
---|
53 |
|
---|
54 | void sort(ExecState*);
|
---|
55 | void sort(ExecState*, JSValue* compareFunction, CallType, const CallData&);
|
---|
56 |
|
---|
57 | bool canGetIndex(unsigned i) { return i < m_fastAccessCutoff; }
|
---|
58 | JSValue* getIndex(unsigned i)
|
---|
59 | {
|
---|
60 | ASSERT(canGetIndex(i));
|
---|
61 | return m_storage->m_vector[i];
|
---|
62 | }
|
---|
63 |
|
---|
64 | bool canSetIndex(unsigned i) { return i < m_fastAccessCutoff; }
|
---|
65 | JSValue* setIndex(unsigned i, JSValue* v)
|
---|
66 | {
|
---|
67 | ASSERT(canSetIndex(i));
|
---|
68 | return m_storage->m_vector[i] = v;
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected:
|
---|
72 | virtual void put(ExecState*, const Identifier& propertyName, JSValue*);
|
---|
73 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
74 | virtual bool deleteProperty(ExecState*, unsigned propertyName);
|
---|
75 | virtual void getPropertyNames(ExecState*, PropertyNameArray&);
|
---|
76 | virtual void mark();
|
---|
77 |
|
---|
78 | void* lazyCreationData();
|
---|
79 | void setLazyCreationData(void*);
|
---|
80 |
|
---|
81 | private:
|
---|
82 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
83 |
|
---|
84 | static JSValue* lengthGetter(ExecState*, const Identifier&, const PropertySlot&);
|
---|
85 |
|
---|
86 | bool getOwnPropertySlotSlowCase(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
87 | void putSlowCase(ExecState*, unsigned propertyName, JSValue*);
|
---|
88 |
|
---|
89 | bool increaseVectorLength(unsigned newLength);
|
---|
90 |
|
---|
91 | unsigned compactForSorting();
|
---|
92 |
|
---|
93 | enum ConsistencyCheckType { NormalConsistencyCheck, DestructorConsistencyCheck, SortConsistencyCheck };
|
---|
94 | void checkConsistency(ConsistencyCheckType = NormalConsistencyCheck);
|
---|
95 |
|
---|
96 | unsigned m_length;
|
---|
97 | unsigned m_fastAccessCutoff;
|
---|
98 | ArrayStorage* m_storage;
|
---|
99 | };
|
---|
100 |
|
---|
101 | JSArray* constructEmptyArray(ExecState*);
|
---|
102 | JSArray* constructEmptyArray(ExecState*, unsigned initialLength);
|
---|
103 | JSArray* constructArray(ExecState*, JSValue* singleItemValue);
|
---|
104 | JSArray* constructArray(ExecState*, const ArgList& values);
|
---|
105 |
|
---|
106 | } // namespace KJS
|
---|
107 |
|
---|
108 | #endif
|
---|