Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: src/key-accumulator.cc

Issue 1425403002: [runtime] Support Symbols in KeyAccumulator (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updating + clarifying Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/key-accumulator.h ('k') | src/objects.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/key-accumulator.h"
6
7 #include "src/elements.h"
8 #include "src/factory.h"
9 #include "src/objects-inl.h"
10
11
12 namespace v8 {
13 namespace internal {
14
15
16 KeyAccumulator::~KeyAccumulator() {
17 for (size_t i = 0; i < elements_.size(); i++) {
18 delete elements_[i];
19 }
20 }
21
22
23 Handle<FixedArray> KeyAccumulator::GetKeys(GetKeysConversion convert) {
24 if (length_ == 0) {
25 return isolate_->factory()->empty_fixed_array();
26 }
27 // Make sure we have all the lengths collected.
28 NextPrototype();
29
30 // Assemble the result array by first adding the element keys and then the
31 // property keys. We use the total number of String + Symbol keys per level in
32 // |level_lengths_| and the available element keys in the corresponding bucket
33 // in |elements_| to deduce the number of keys to take from the
34 // |string_properties_| and |symbol_properties_| set.
35 Handle<FixedArray> result = isolate_->factory()->NewFixedArray(length_);
36 int insertion_index = 0;
37 int string_properties_index = 0;
38 int symbol_properties_index = 0;
39 // String and Symbol lengths always come in pairs:
40 size_t max_level = level_lengths_.size() / 2;
41 for (size_t level = 0; level < max_level; level++) {
42 int num_string_properties = level_lengths_[level * 2];
43 int num_symbol_properties = level_lengths_[level * 2 + 1];
44 if (num_string_properties < 0) {
45 // If the |num_string_properties| is negative, the current level contains
46 // properties from a proxy, hence we skip the integer keys in |elements_|
47 // since proxies define the complete ordering.
48 num_string_properties = -num_string_properties;
49 } else if (level < elements_.size()) {
50 // Add the element indices for this prototype level.
51 std::vector<uint32_t>* elements = elements_[level];
52 int num_elements = static_cast<int>(elements->size());
53 for (int i = 0; i < num_elements; i++) {
54 Handle<Object> key;
55 if (convert == KEEP_NUMBERS) {
56 key = isolate_->factory()->NewNumberFromUint(elements->at(i));
57 } else {
58 key = isolate_->factory()->Uint32ToString(elements->at(i));
59 }
60 result->set(insertion_index, *key);
61 insertion_index++;
62 }
63 }
64 // Add the string property keys for this prototype level.
65 for (int i = 0; i < num_string_properties; i++) {
66 Object* key = string_properties_->KeyAt(string_properties_index);
67 result->set(insertion_index, key);
68 insertion_index++;
69 string_properties_index++;
70 }
71 // Add the symbol property keys for this prototype level.
72 for (int i = 0; i < num_symbol_properties; i++) {
73 Object* key = symbol_properties_->KeyAt(symbol_properties_index);
74 result->set(insertion_index, key);
75 insertion_index++;
76 symbol_properties_index++;
77 }
78 }
79
80 DCHECK_EQ(insertion_index, length_);
81 return result;
82 }
83
84
85 namespace {
86
87 bool AccumulatorHasKey(std::vector<uint32_t>* sub_elements, uint32_t key) {
88 return std::binary_search(sub_elements->begin(), sub_elements->end(), key);
89 }
90
91 } // namespace
92
93 bool KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) {
94 return AddKey(handle(key, isolate_), convert);
95 }
96
97
98 bool KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) {
99 if (key->IsSymbol()) {
100 if (filter_ == SKIP_SYMBOLS) return false;
101 return AddSymbolKey(key);
102 }
103 // Make sure we do not add keys to a proxy-level (see AddKeysFromProxy).
104 DCHECK_LE(0, level_string_length_);
105 // In some cases (e.g. proxies) we might get in String-converted ints which
106 // should be added to the elements list instead of the properties. For
107 // proxies we have to convert as well but also respect the original order.
108 // Therefore we add a converted key to both sides
109 if (convert == CONVERT_TO_ARRAY_INDEX || convert == PROXY_MAGIC) {
110 uint32_t index = 0;
111 int prev_length = length_;
112 int prev_proto = level_string_length_;
113 if ((key->IsString() && Handle<String>::cast(key)->AsArrayIndex(&index)) ||
114 key->ToArrayIndex(&index)) {
115 bool key_was_added = AddIntegerKey(index);
116 if (convert == CONVERT_TO_ARRAY_INDEX) return key_was_added;
117 if (convert == PROXY_MAGIC) {
118 // If we had an array index (number) and it wasn't added, the key
119 // already existed before, hence we cannot add it to the properties
120 // keys as it would lead to duplicate entries.
121 if (!key_was_added) {
122 return false;
123 }
124 length_ = prev_length;
125 level_string_length_ = prev_proto;
126 }
127 }
128 }
129 return AddStringKey(key, convert);
130 }
131
132
133 bool KeyAccumulator::AddKey(uint32_t key) { return AddIntegerKey(key); }
134
135
136 bool KeyAccumulator::AddIntegerKey(uint32_t key) {
137 // Make sure we do not add keys to a proxy-level (see AddKeysFromProxy).
138 // We mark proxy-levels with a negative length
139 DCHECK_LE(0, level_string_length_);
140 // Binary search over all but the last level. The last one might not be
141 // sorted yet.
142 for (size_t i = 1; i < elements_.size(); i++) {
143 if (AccumulatorHasKey(elements_[i - 1], key)) return false;
144 }
145 elements_.back()->push_back(key);
146 length_++;
147 return true;
148 }
149
150
151 bool KeyAccumulator::AddStringKey(Handle<Object> key,
152 AddKeyConversion convert) {
153 if (string_properties_.is_null()) {
154 string_properties_ = OrderedHashSet::Allocate(isolate_, 16);
155 }
156 // TODO(cbruni): remove this conversion once we throw the correct TypeError
157 // for non-string/symbol elements returned by proxies
158 if (convert == PROXY_MAGIC && key->IsNumber()) {
159 key = isolate_->factory()->NumberToString(key);
160 }
161 int prev_size = string_properties_->NumberOfElements();
162 string_properties_ = OrderedHashSet::Add(string_properties_, key);
163 if (prev_size < string_properties_->NumberOfElements()) {
164 length_++;
165 level_string_length_++;
166 return true;
167 } else {
168 return false;
169 }
170 }
171
172
173 bool KeyAccumulator::AddSymbolKey(Handle<Object> key) {
174 if (symbol_properties_.is_null()) {
175 symbol_properties_ = OrderedHashSet::Allocate(isolate_, 16);
176 }
177 int prev_size = symbol_properties_->NumberOfElements();
178 symbol_properties_ = OrderedHashSet::Add(symbol_properties_, key);
179 if (prev_size < symbol_properties_->NumberOfElements()) {
180 length_++;
181 level_symbol_length_++;
182 return true;
183 } else {
184 return false;
185 }
186 }
187
188
189 void KeyAccumulator::AddKeys(Handle<FixedArray> array,
190 AddKeyConversion convert) {
191 int add_length = array->length();
192 if (add_length == 0) return;
193 for (int i = 0; i < add_length; i++) {
194 Handle<Object> current(array->get(i), isolate_);
195 AddKey(current, convert);
196 }
197 }
198
199
200 void KeyAccumulator::AddKeys(Handle<JSObject> array_like,
201 AddKeyConversion convert) {
202 DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements());
203 ElementsAccessor* accessor = array_like->GetElementsAccessor();
204 accessor->AddElementsToKeyAccumulator(array_like, this, convert);
205 }
206
207
208 void KeyAccumulator::AddKeysFromProxy(Handle<JSObject> array_like) {
209 // Proxies define a complete list of keys with no distinction of
210 // elements and properties, which breaks the normal assumption for the
211 // KeyAccumulator.
212 AddKeys(array_like, PROXY_MAGIC);
213 // Invert the current length to indicate a present proxy, so we can ignore
214 // element keys for this level. Otherwise we would not fully respect the order
215 // given by the proxy.
216 level_string_length_ = -level_string_length_;
217 }
218
219
220 void KeyAccumulator::AddElementKeysFromInterceptor(
221 Handle<JSObject> array_like) {
222 AddKeys(array_like, CONVERT_TO_ARRAY_INDEX);
223 // The interceptor might introduce duplicates for the current level, since
224 // these keys get added after the objects's normal element keys.
225 SortCurrentElementsListRemoveDuplicates();
226 }
227
228
229 void KeyAccumulator::SortCurrentElementsListRemoveDuplicates() {
230 // Sort and remove duplicates from the current elements level and adjust.
231 // the lengths accordingly.
232 auto last_level = elements_.back();
233 size_t nof_removed_keys = last_level->size();
234 std::sort(last_level->begin(), last_level->end());
235 last_level->erase(std::unique(last_level->begin(), last_level->end()),
236 last_level->end());
237 // Adjust total length by the number of removed duplicates.
238 nof_removed_keys -= last_level->size();
239 length_ -= static_cast<int>(nof_removed_keys);
240 }
241
242
243 void KeyAccumulator::SortCurrentElementsList() {
244 if (elements_.empty()) return;
245 auto element_keys = elements_.back();
246 std::sort(element_keys->begin(), element_keys->end());
247 }
248
249
250 void KeyAccumulator::NextPrototype() {
251 // Store the protoLength on the first call of this method.
252 if (!elements_.empty()) {
253 level_lengths_.push_back(level_string_length_);
254 level_lengths_.push_back(level_symbol_length_);
255 }
256 elements_.push_back(new std::vector<uint32_t>());
257 level_string_length_ = 0;
258 level_symbol_length_ = 0;
259 }
260
261
262 } // namespace internal
263 } // namespace v8
OLDNEW
« no previous file with comments | « src/key-accumulator.h ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698