]>
Commit | Line | Data |
---|---|---|
9dae56ea | 1 | /* |
93a37866 | 2 | * Copyright (C) 2006, 2008, 2012 Apple Inc. All rights reserved. |
9dae56ea A |
3 | * |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Library General Public | |
6 | * License as published by the Free Software Foundation; either | |
7 | * version 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Library General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Library General Public License | |
15 | * along with this library; see the file COPYING.LIB. If not, write to | |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
17 | * Boston, MA 02110-1301, USA. | |
18 | * | |
19 | */ | |
20 | ||
21 | #ifndef PropertyNameArray_h | |
22 | #define PropertyNameArray_h | |
23 | ||
24 | #include "CallFrame.h" | |
25 | #include "Identifier.h" | |
9dae56ea A |
26 | #include <wtf/HashSet.h> |
27 | #include <wtf/Vector.h> | |
28 | ||
29 | namespace JSC { | |
30 | ||
ed1e77d3 A |
31 | // FIXME: Rename to PropertyNameArray. |
32 | class PropertyNameArrayData : public RefCounted<PropertyNameArrayData> { | |
33 | public: | |
34 | typedef Vector<Identifier, 20> PropertyNameVector; | |
35 | ||
36 | static Ref<PropertyNameArrayData> create() { return adoptRef(*new PropertyNameArrayData); } | |
37 | ||
38 | PropertyNameVector& propertyNameVector() { return m_propertyNameVector; } | |
39 | ||
40 | private: | |
41 | PropertyNameArrayData() | |
42 | { | |
43 | } | |
44 | ||
45 | PropertyNameVector m_propertyNameVector; | |
46 | }; | |
47 | ||
48 | // FIXME: Rename to PropertyNameArrayBuilder. | |
49 | class PropertyNameArray { | |
50 | public: | |
51 | PropertyNameArray(VM* vm) | |
52 | : m_data(PropertyNameArrayData::create()) | |
53 | , m_vm(vm) | |
54 | { | |
55 | } | |
56 | ||
57 | PropertyNameArray(ExecState* exec) | |
58 | : m_data(PropertyNameArrayData::create()) | |
59 | , m_vm(&exec->vm()) | |
60 | { | |
61 | } | |
62 | ||
63 | VM* vm() { return m_vm; } | |
64 | ||
65 | void add(uint32_t index) | |
66 | { | |
67 | add(Identifier::from(m_vm, index)); | |
68 | } | |
69 | ||
70 | void add(const Identifier&); | |
71 | void add(UniquedStringImpl*); | |
72 | void addKnownUnique(UniquedStringImpl*); | |
73 | ||
74 | Identifier& operator[](unsigned i) { return m_data->propertyNameVector()[i]; } | |
75 | const Identifier& operator[](unsigned i) const { return m_data->propertyNameVector()[i]; } | |
76 | ||
77 | void setData(PassRefPtr<PropertyNameArrayData> data) { m_data = data; } | |
78 | PropertyNameArrayData* data() { return m_data.get(); } | |
79 | PassRefPtr<PropertyNameArrayData> releaseData() { return m_data.release(); } | |
80 | ||
81 | // FIXME: Remove these functions. | |
82 | bool canAddKnownUniqueForStructure() const { return m_data->propertyNameVector().isEmpty(); } | |
83 | typedef PropertyNameArrayData::PropertyNameVector::const_iterator const_iterator; | |
84 | size_t size() const { return m_data->propertyNameVector().size(); } | |
85 | const_iterator begin() const { return m_data->propertyNameVector().begin(); } | |
86 | const_iterator end() const { return m_data->propertyNameVector().end(); } | |
87 | ||
88 | private: | |
89 | RefPtr<PropertyNameArrayData> m_data; | |
90 | HashSet<UniquedStringImpl*> m_set; | |
91 | VM* m_vm; | |
92 | }; | |
93 | ||
94 | ALWAYS_INLINE void PropertyNameArray::add(const Identifier& identifier) | |
95 | { | |
96 | add(identifier.impl()); | |
97 | } | |
98 | ||
99 | ALWAYS_INLINE void PropertyNameArray::addKnownUnique(UniquedStringImpl* identifier) | |
100 | { | |
101 | m_data->propertyNameVector().append(Identifier::fromUid(m_vm, identifier)); | |
102 | } | |
103 | ||
104 | ALWAYS_INLINE void PropertyNameArray::add(UniquedStringImpl* identifier) | |
105 | { | |
106 | static const unsigned setThreshold = 20; | |
107 | ||
108 | ASSERT(identifier); | |
109 | ||
110 | if (size() < setThreshold) { | |
111 | if (m_data->propertyNameVector().contains(identifier)) | |
112 | return; | |
113 | } else { | |
114 | if (m_set.isEmpty()) { | |
115 | for (Identifier& name : m_data->propertyNameVector()) | |
116 | m_set.add(name.impl()); | |
93a37866 | 117 | } |
ed1e77d3 A |
118 | if (!m_set.add(identifier).isNewEntry) |
119 | return; | |
120 | } | |
9dae56ea | 121 | |
ed1e77d3 A |
122 | addKnownUnique(identifier); |
123 | } | |
9dae56ea A |
124 | |
125 | } // namespace JSC | |
126 | ||
127 | #endif // PropertyNameArray_h |