1/*
2 * Copyright (C) 2006, 2008, 2012 Apple Inc. All rights reserved.
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#pragma once
22
23#include "CallFrame.h"
24#include "Identifier.h"
25#include <wtf/HashSet.h>
26#include <wtf/Vector.h>
27
28namespace JSC {
29
30// FIXME: Rename to PropertyNameArray.
31class PropertyNameArrayData : public RefCounted<PropertyNameArrayData> {
32public:
33 typedef Vector<Identifier, 20> PropertyNameVector;
34
35 static Ref<PropertyNameArrayData> create() { return adoptRef(*new PropertyNameArrayData); }
36
37 PropertyNameVector& propertyNameVector() { return m_propertyNameVector; }
38
39private:
40 PropertyNameArrayData()
41 {
42 }
43
44 PropertyNameVector m_propertyNameVector;
45};
46
47// FIXME: Rename to PropertyNameArrayBuilder.
48class PropertyNameArray {
49public:
50 PropertyNameArray(VM* vm, PropertyNameMode propertyNameMode, PrivateSymbolMode privateSymbolMode)
51 : m_data(PropertyNameArrayData::create())
52 , m_vm(vm)
53 , m_propertyNameMode(propertyNameMode)
54 , m_privateSymbolMode(privateSymbolMode)
55 {
56 }
57
58 void reset()
59 {
60 m_set.clear();
61 m_data = PropertyNameArrayData::create();
62 }
63
64 VM* vm() { return m_vm; }
65
66 void add(uint32_t index)
67 {
68 add(Identifier::from(m_vm, index));
69 }
70
71 void add(const Identifier&);
72 void add(UniquedStringImpl*);
73 void addUnchecked(UniquedStringImpl*);
74
75 Identifier& operator[](unsigned i) { return m_data->propertyNameVector()[i]; }
76 const Identifier& operator[](unsigned i) const { return m_data->propertyNameVector()[i]; }
77
78 PropertyNameArrayData* data() { return m_data.get(); }
79 RefPtr<PropertyNameArrayData> releaseData() { return WTFMove(m_data); }
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 bool includeSymbolProperties() const;
89 bool includeStringProperties() const;
90
91 PropertyNameMode propertyNameMode() const { return m_propertyNameMode; }
92 PrivateSymbolMode privateSymbolMode() const { return m_privateSymbolMode; }
93
94private:
95 void addUncheckedInternal(UniquedStringImpl*);
96 bool isUidMatchedToTypeMode(UniquedStringImpl* identifier);
97
98 RefPtr<PropertyNameArrayData> m_data;
99 HashSet<UniquedStringImpl*> m_set;
100 VM* m_vm;
101 PropertyNameMode m_propertyNameMode;
102 PrivateSymbolMode m_privateSymbolMode;
103};
104
105ALWAYS_INLINE void PropertyNameArray::add(const Identifier& identifier)
106{
107 add(identifier.impl());
108}
109
110ALWAYS_INLINE void PropertyNameArray::addUncheckedInternal(UniquedStringImpl* identifier)
111{
112 m_data->propertyNameVector().append(Identifier::fromUid(m_vm, identifier));
113}
114
115ALWAYS_INLINE void PropertyNameArray::addUnchecked(UniquedStringImpl* identifier)
116{
117 if (!isUidMatchedToTypeMode(identifier))
118 return;
119 addUncheckedInternal(identifier);
120}
121
122ALWAYS_INLINE void PropertyNameArray::add(UniquedStringImpl* identifier)
123{
124 static const unsigned setThreshold = 20;
125
126 ASSERT(identifier);
127
128 if (!isUidMatchedToTypeMode(identifier))
129 return;
130
131 if (size() < setThreshold) {
132 if (m_data->propertyNameVector().contains(identifier))
133 return;
134 } else {
135 if (m_set.isEmpty()) {
136 for (Identifier& name : m_data->propertyNameVector())
137 m_set.add(name.impl());
138 }
139 if (!m_set.add(identifier).isNewEntry)
140 return;
141 }
142
143 addUncheckedInternal(identifier);
144}
145
146ALWAYS_INLINE bool PropertyNameArray::isUidMatchedToTypeMode(UniquedStringImpl* identifier)
147{
148 if (identifier->isSymbol()) {
149 if (!includeSymbolProperties())
150 return false;
151 if (UNLIKELY(m_privateSymbolMode == PrivateSymbolMode::Include))
152 return true;
153 return !static_cast<SymbolImpl*>(identifier)->isPrivate();
154 }
155 return includeStringProperties();
156}
157
158ALWAYS_INLINE bool PropertyNameArray::includeSymbolProperties() const
159{
160 return static_cast<std::underlying_type<PropertyNameMode>::type>(m_propertyNameMode) & static_cast<std::underlying_type<PropertyNameMode>::type>(PropertyNameMode::Symbols);
161}
162
163ALWAYS_INLINE bool PropertyNameArray::includeStringProperties() const
164{
165 return static_cast<std::underlying_type<PropertyNameMode>::type>(m_propertyNameMode) & static_cast<std::underlying_type<PropertyNameMode>::type>(PropertyNameMode::Strings);
166}
167
168} // namespace JSC
169