OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/keys.h" | 5 #include "src/keys.h" |
6 | 6 |
7 #include "src/api-arguments.h" | 7 #include "src/api-arguments.h" |
8 #include "src/elements.h" | 8 #include "src/elements.h" |
9 #include "src/factory.h" | 9 #include "src/factory.h" |
10 #include "src/identity-map.h" | 10 #include "src/identity-map.h" |
11 #include "src/isolate-inl.h" | 11 #include "src/isolate-inl.h" |
12 #include "src/objects-inl.h" | 12 #include "src/objects-inl.h" |
13 #include "src/property-descriptor.h" | 13 #include "src/property-descriptor.h" |
14 #include "src/prototype.h" | 14 #include "src/prototype.h" |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 KeyAccumulator::~KeyAccumulator() { | 19 KeyAccumulator::~KeyAccumulator() { |
20 for (size_t i = 0; i < elements_.size(); i++) { | |
21 delete elements_[i]; | |
22 } | |
23 } | 20 } |
24 | 21 |
25 namespace { | 22 namespace { |
26 | 23 |
27 static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { | 24 static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
28 int len = array->length(); | 25 int len = array->length(); |
29 for (int i = 0; i < len; i++) { | 26 for (int i = 0; i < len; i++) { |
30 Object* e = array->get(i); | 27 Object* e = array->get(i); |
31 if (!(e->IsName() || e->IsNumber())) return false; | 28 if (!(e->IsName() || e->IsNumber())) return false; |
32 } | 29 } |
33 return true; | 30 return true; |
34 } | 31 } |
35 | 32 |
36 } // namespace | 33 } // namespace |
37 | 34 |
38 MaybeHandle<FixedArray> KeyAccumulator::GetKeys( | 35 MaybeHandle<FixedArray> KeyAccumulator::GetKeys( |
39 Handle<JSReceiver> object, KeyCollectionType type, PropertyFilter filter, | 36 Handle<JSReceiver> object, KeyCollectionType type, PropertyFilter filter, |
40 GetKeysConversion keys_conversion, bool filter_proxy_keys) { | 37 GetKeysConversion keys_conversion, bool filter_proxy_keys, bool is_for_in) { |
41 USE(ContainsOnlyValidKeys); | 38 USE(ContainsOnlyValidKeys); |
42 Isolate* isolate = object->GetIsolate(); | 39 Isolate* isolate = object->GetIsolate(); |
43 KeyAccumulator accumulator(isolate, type, filter); | 40 KeyAccumulator accumulator(isolate, type, filter); |
44 accumulator.set_filter_proxy_keys(filter_proxy_keys); | 41 accumulator.set_filter_proxy_keys(filter_proxy_keys); |
42 accumulator.set_is_for_in(is_for_in); | |
45 MAYBE_RETURN(accumulator.CollectKeys(object, object), | 43 MAYBE_RETURN(accumulator.CollectKeys(object, object), |
46 MaybeHandle<FixedArray>()); | 44 MaybeHandle<FixedArray>()); |
47 Handle<FixedArray> keys = accumulator.GetKeys(keys_conversion); | 45 Handle<FixedArray> keys = accumulator.GetKeys(keys_conversion); |
48 DCHECK(ContainsOnlyValidKeys(keys)); | 46 DCHECK(ContainsOnlyValidKeys(keys)); |
49 return keys; | 47 return keys; |
50 } | 48 } |
51 | 49 |
52 Handle<FixedArray> KeyAccumulator::GetKeys(GetKeysConversion convert) { | 50 Handle<FixedArray> KeyAccumulator::GetKeys(GetKeysConversion convert) { |
53 if (length_ == 0) { | 51 if (keys_.is_null()) { |
54 return isolate_->factory()->empty_fixed_array(); | 52 return isolate_->factory()->empty_fixed_array(); |
55 } | 53 } |
56 // Make sure we have all the lengths collected. | 54 if (type_ == OWN_ONLY && |
57 NextPrototype(); | 55 keys_->map() == isolate_->heap()->fixed_array_map()) { |
58 | 56 return Handle<FixedArray>::cast(keys_); |
59 if (type_ == OWN_ONLY && !ownProxyKeys_.is_null()) { | |
60 return ownProxyKeys_; | |
61 } | 57 } |
62 // Assemble the result array by first adding the element keys and then the | 58 return OrderedHashSet::ConvertToKeysArray(keys(), convert); |
63 // property keys. We use the total number of String + Symbol keys per level in | |
64 // |level_lengths_| and the available element keys in the corresponding bucket | |
65 // in |elements_| to deduce the number of keys to take from the | |
66 // |string_properties_| and |symbol_properties_| set. | |
67 Handle<FixedArray> result = isolate_->factory()->NewFixedArray(length_); | |
68 int insertion_index = 0; | |
69 int string_properties_index = 0; | |
70 int symbol_properties_index = 0; | |
71 // String and Symbol lengths always come in pairs: | |
72 size_t max_level = level_lengths_.size() / 2; | |
73 for (size_t level = 0; level < max_level; level++) { | |
74 int num_string_properties = level_lengths_[level * 2]; | |
75 int num_symbol_properties = level_lengths_[level * 2 + 1]; | |
76 int num_elements = 0; | |
77 if (num_string_properties < 0) { | |
78 // If the |num_string_properties| is negative, the current level contains | |
79 // properties from a proxy, hence we skip the integer keys in |elements_| | |
80 // since proxies define the complete ordering. | |
81 num_string_properties = -num_string_properties; | |
82 } else if (level < elements_.size()) { | |
83 // Add the element indices for this prototype level. | |
84 std::vector<uint32_t>* elements = elements_[level]; | |
85 num_elements = static_cast<int>(elements->size()); | |
86 for (int i = 0; i < num_elements; i++) { | |
87 Handle<Object> key; | |
88 if (convert == KEEP_NUMBERS) { | |
89 key = isolate_->factory()->NewNumberFromUint(elements->at(i)); | |
90 } else { | |
91 key = isolate_->factory()->Uint32ToString(elements->at(i)); | |
92 } | |
93 result->set(insertion_index, *key); | |
94 insertion_index++; | |
95 } | |
96 } | |
97 // Add the string property keys for this prototype level. | |
98 for (int i = 0; i < num_string_properties; i++) { | |
99 Object* key = string_properties_->KeyAt(string_properties_index); | |
100 result->set(insertion_index, key); | |
101 insertion_index++; | |
102 string_properties_index++; | |
103 } | |
104 // Add the symbol property keys for this prototype level. | |
105 for (int i = 0; i < num_symbol_properties; i++) { | |
106 Object* key = symbol_properties_->KeyAt(symbol_properties_index); | |
107 result->set(insertion_index, key); | |
108 insertion_index++; | |
109 symbol_properties_index++; | |
110 } | |
111 if (FLAG_trace_for_in_enumerate) { | |
112 PrintF("| strings=%d symbols=%d elements=%i ", num_string_properties, | |
113 num_symbol_properties, num_elements); | |
114 } | |
115 } | |
116 if (FLAG_trace_for_in_enumerate) { | |
117 PrintF("|| prototypes=%zu ||\n", max_level); | |
118 } | |
119 | |
120 DCHECK_EQ(insertion_index, length_); | |
121 return result; | |
122 } | 59 } |
123 | 60 |
124 namespace { | 61 void KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) { |
125 | 62 AddKey(handle(key, isolate_), convert); |
126 bool AccumulatorHasKey(std::vector<uint32_t>* sub_elements, uint32_t key) { | |
127 return std::binary_search(sub_elements->begin(), sub_elements->end(), key); | |
128 } | 63 } |
129 | 64 |
130 } // namespace | 65 void KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) { |
131 | |
132 bool KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) { | |
133 return AddKey(handle(key, isolate_), convert); | |
134 } | |
135 | |
136 bool KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) { | |
137 if (key->IsSymbol()) { | 66 if (key->IsSymbol()) { |
138 if (filter_ & SKIP_SYMBOLS) return false; | 67 if (filter_ & SKIP_SYMBOLS) return; |
139 if (Handle<Symbol>::cast(key)->is_private()) return false; | 68 if (Handle<Symbol>::cast(key)->is_private()) return; |
140 return AddSymbolKey(key); | 69 } else if (filter_ & SKIP_STRINGS) { |
70 return; | |
141 } | 71 } |
142 if (filter_ & SKIP_STRINGS) return false; | 72 if (keys_.is_null()) { |
143 // Make sure we do not add keys to a proxy-level (see AddKeysFromJSProxy). | 73 keys_ = OrderedHashSet::Allocate(isolate_, 16); |
144 DCHECK_LE(0, level_string_length_); | |
145 // In some cases (e.g. proxies) we might get in String-converted ints which | |
146 // should be added to the elements list instead of the properties. For | |
147 // proxies we have to convert as well but also respect the original order. | |
148 // Therefore we add a converted key to both sides | |
149 if (convert == CONVERT_TO_ARRAY_INDEX || convert == PROXY_MAGIC) { | |
150 uint32_t index = 0; | |
151 int prev_length = length_; | |
152 int prev_proto = level_string_length_; | |
153 if ((key->IsString() && Handle<String>::cast(key)->AsArrayIndex(&index)) || | |
154 key->ToArrayIndex(&index)) { | |
155 bool key_was_added = AddIntegerKey(index); | |
156 if (convert == CONVERT_TO_ARRAY_INDEX) return key_was_added; | |
157 if (convert == PROXY_MAGIC) { | |
158 // If we had an array index (number) and it wasn't added, the key | |
159 // already existed before, hence we cannot add it to the properties | |
160 // keys as it would lead to duplicate entries. | |
161 if (!key_was_added) { | |
162 return false; | |
163 } | |
164 length_ = prev_length; | |
165 level_string_length_ = prev_proto; | |
166 } | |
167 } | |
168 } | 74 } |
169 return AddStringKey(key, convert); | 75 uint32_t index; |
170 } | 76 if (convert == CONVERT_TO_ARRAY_INDEX && key->IsString() && |
171 | 77 Handle<String>::cast(key)->AsArrayIndex(&index)) { |
172 bool KeyAccumulator::AddKey(uint32_t key) { return AddIntegerKey(key); } | 78 key = isolate_->factory()->NewHeapNumber(index); |
Jakob Kummerow
2016/05/23 16:25:21
Shouldn't this use NewNumberFromUint so Smis will
Camillo Bruni
2016/05/23 19:10:13
indeed.
| |
173 | |
174 bool KeyAccumulator::AddIntegerKey(uint32_t key) { | |
175 // Make sure we do not add keys to a proxy-level (see AddKeysFromJSProxy). | |
176 // We mark proxy-levels with a negative length | |
177 DCHECK_LE(0, level_string_length_); | |
178 // Binary search over all but the last level. The last one might not be | |
179 // sorted yet. | |
180 for (size_t i = 1; i < elements_.size(); i++) { | |
181 if (AccumulatorHasKey(elements_[i - 1], key)) return false; | |
182 } | 79 } |
183 elements_.back()->push_back(key); | 80 keys_ = OrderedHashSet::Add(keys(), key); |
184 length_++; | |
185 return true; | |
186 } | |
187 | |
188 bool KeyAccumulator::AddStringKey(Handle<Object> key, | |
189 AddKeyConversion convert) { | |
190 if (string_properties_.is_null()) { | |
191 string_properties_ = OrderedHashSet::Allocate(isolate_, 16); | |
192 } | |
193 // TODO(cbruni): remove this conversion once we throw the correct TypeError | |
194 // for non-string/symbol elements returned by proxies | |
195 if (convert == PROXY_MAGIC && key->IsNumber()) { | |
196 key = isolate_->factory()->NumberToString(key); | |
197 } | |
198 int prev_size = string_properties_->NumberOfElements(); | |
199 string_properties_ = OrderedHashSet::Add(string_properties_, key); | |
200 if (prev_size < string_properties_->NumberOfElements()) { | |
201 length_++; | |
202 level_string_length_++; | |
203 return true; | |
204 } else { | |
205 return false; | |
206 } | |
207 } | |
208 | |
209 bool KeyAccumulator::AddSymbolKey(Handle<Object> key) { | |
210 if (symbol_properties_.is_null()) { | |
211 symbol_properties_ = OrderedHashSet::Allocate(isolate_, 16); | |
212 } | |
213 int prev_size = symbol_properties_->NumberOfElements(); | |
214 symbol_properties_ = OrderedHashSet::Add(symbol_properties_, key); | |
215 if (prev_size < symbol_properties_->NumberOfElements()) { | |
216 length_++; | |
217 level_symbol_length_++; | |
218 return true; | |
219 } else { | |
220 return false; | |
221 } | |
222 } | 81 } |
223 | 82 |
224 void KeyAccumulator::AddKeys(Handle<FixedArray> array, | 83 void KeyAccumulator::AddKeys(Handle<FixedArray> array, |
225 AddKeyConversion convert) { | 84 AddKeyConversion convert) { |
226 int add_length = array->length(); | 85 int add_length = array->length(); |
227 if (add_length == 0) return; | |
228 for (int i = 0; i < add_length; i++) { | 86 for (int i = 0; i < add_length; i++) { |
229 Handle<Object> current(array->get(i), isolate_); | 87 Handle<Object> current(array->get(i), isolate_); |
230 AddKey(current, convert); | 88 AddKey(current, convert); |
231 } | 89 } |
232 } | 90 } |
233 | 91 |
234 void KeyAccumulator::AddKeys(Handle<JSObject> array_like, | 92 void KeyAccumulator::AddKeys(Handle<JSObject> array_like, |
235 AddKeyConversion convert) { | 93 AddKeyConversion convert) { |
236 DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements()); | 94 DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements()); |
237 ElementsAccessor* accessor = array_like->GetElementsAccessor(); | 95 ElementsAccessor* accessor = array_like->GetElementsAccessor(); |
(...skipping 26 matching lines...) Expand all Loading... | |
264 } | 122 } |
265 if (store_position == 0) return isolate->factory()->empty_fixed_array(); | 123 if (store_position == 0) return isolate->factory()->empty_fixed_array(); |
266 keys->Shrink(store_position); | 124 keys->Shrink(store_position); |
267 return keys; | 125 return keys; |
268 } | 126 } |
269 | 127 |
270 // Returns "nothing" in case of exception, "true" on success. | 128 // Returns "nothing" in case of exception, "true" on success. |
271 Maybe<bool> KeyAccumulator::AddKeysFromJSProxy(Handle<JSProxy> proxy, | 129 Maybe<bool> KeyAccumulator::AddKeysFromJSProxy(Handle<JSProxy> proxy, |
272 Handle<FixedArray> keys) { | 130 Handle<FixedArray> keys) { |
273 if (filter_proxy_keys_) { | 131 if (filter_proxy_keys_) { |
132 DCHECK(!is_for_in_); | |
274 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | 133 ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
275 isolate_, keys, FilterProxyKeys(isolate_, proxy, keys, filter_), | 134 isolate_, keys, FilterProxyKeys(isolate_, proxy, keys, filter_), |
276 Nothing<bool>()); | 135 Nothing<bool>()); |
277 } | 136 } |
278 // Proxies define a complete list of keys with no distinction of | 137 if (type_ == OWN_ONLY && !is_for_in_) { |
279 // elements and properties, which breaks the normal assumption for the | 138 // If we collect only the keys from a JSProxy do not sort or deduplicate it. |
280 // KeyAccumulator. | 139 keys_ = keys; |
281 if (type_ == OWN_ONLY) { | 140 return Just(true); |
282 ownProxyKeys_ = keys; | |
283 level_string_length_ = keys->length(); | |
284 length_ = level_string_length_; | |
285 } else { | |
286 AddKeys(keys, PROXY_MAGIC); | |
287 } | 141 } |
288 // Invert the current length to indicate a present proxy, so we can ignore | 142 AddKeys(keys, is_for_in_ ? CONVERT_TO_ARRAY_INDEX : DO_NOT_CONVERT); |
289 // element keys for this level. Otherwise we would not fully respect the order | |
290 // given by the proxy. | |
291 level_string_length_ = -level_string_length_; | |
292 return Just(true); | 143 return Just(true); |
293 } | 144 } |
294 | 145 |
295 void KeyAccumulator::AddElementKeysFromInterceptor( | |
296 Handle<JSObject> array_like) { | |
297 AddKeys(array_like, CONVERT_TO_ARRAY_INDEX); | |
298 // The interceptor might introduce duplicates for the current level, since | |
299 // these keys get added after the objects's normal element keys. | |
300 SortCurrentElementsListRemoveDuplicates(); | |
301 } | |
302 | |
303 void KeyAccumulator::SortCurrentElementsListRemoveDuplicates() { | |
304 // Sort and remove duplicates from the current elements level and adjust. | |
305 // the lengths accordingly. | |
306 auto last_level = elements_.back(); | |
307 size_t nof_removed_keys = last_level->size(); | |
308 std::sort(last_level->begin(), last_level->end()); | |
309 last_level->erase(std::unique(last_level->begin(), last_level->end()), | |
310 last_level->end()); | |
311 // Adjust total length by the number of removed duplicates. | |
312 nof_removed_keys -= last_level->size(); | |
313 length_ -= static_cast<int>(nof_removed_keys); | |
314 } | |
315 | |
316 void KeyAccumulator::SortCurrentElementsList() { | |
317 if (elements_.empty()) return; | |
318 auto element_keys = elements_.back(); | |
319 std::sort(element_keys->begin(), element_keys->end()); | |
320 } | |
321 | |
322 void KeyAccumulator::NextPrototype() { | |
323 // Store the protoLength on the first call of this method. | |
324 if (!elements_.empty()) { | |
325 level_lengths_.push_back(level_string_length_); | |
326 level_lengths_.push_back(level_symbol_length_); | |
327 } | |
328 elements_.push_back(new std::vector<uint32_t>()); | |
329 level_string_length_ = 0; | |
330 level_symbol_length_ = 0; | |
331 } | |
332 | |
333 Maybe<bool> KeyAccumulator::CollectKeys(Handle<JSReceiver> receiver, | 146 Maybe<bool> KeyAccumulator::CollectKeys(Handle<JSReceiver> receiver, |
334 Handle<JSReceiver> object) { | 147 Handle<JSReceiver> object) { |
335 // Proxies have no hidden prototype and we should not trigger the | 148 // Proxies have no hidden prototype and we should not trigger the |
336 // [[GetPrototypeOf]] trap on the last iteration when using | 149 // [[GetPrototypeOf]] trap on the last iteration when using |
337 // AdvanceFollowingProxies. | 150 // AdvanceFollowingProxies. |
338 if (type_ == OWN_ONLY && object->IsJSProxy()) { | 151 if (type_ == OWN_ONLY && object->IsJSProxy()) { |
339 MAYBE_RETURN(CollectOwnJSProxyKeys(receiver, Handle<JSProxy>::cast(object)), | 152 MAYBE_RETURN(CollectOwnJSProxyKeys(receiver, Handle<JSProxy>::cast(object)), |
340 Nothing<bool>()); | 153 Nothing<bool>()); |
341 return Just(true); | 154 return Just(true); |
342 } | 155 } |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
588 return keys; | 401 return keys; |
589 } | 402 } |
590 } | 403 } |
591 // The properties-only case failed because there were probably elements on the | 404 // The properties-only case failed because there were probably elements on the |
592 // receiver. | 405 // receiver. |
593 return GetOwnKeysWithElements<true>(isolate_, object, convert); | 406 return GetOwnKeysWithElements<true>(isolate_, object, convert); |
594 } | 407 } |
595 | 408 |
596 MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysSlow( | 409 MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysSlow( |
597 GetKeysConversion convert) { | 410 GetKeysConversion convert) { |
598 return JSReceiver::GetKeys(receiver_, type_, filter_, KEEP_NUMBERS, | 411 return KeyAccumulator::GetKeys(receiver_, type_, filter_, KEEP_NUMBERS, |
599 filter_proxy_keys_); | 412 filter_proxy_keys_, is_for_in_); |
600 } | 413 } |
601 | 414 |
415 namespace { | |
416 | |
602 enum IndexedOrNamed { kIndexed, kNamed }; | 417 enum IndexedOrNamed { kIndexed, kNamed }; |
603 | 418 |
604 // Returns |true| on success, |nothing| on exception. | 419 // Returns |true| on success, |nothing| on exception. |
605 template <class Callback, IndexedOrNamed type> | 420 template <class Callback, IndexedOrNamed type> |
606 static Maybe<bool> GetKeysFromInterceptor(Handle<JSReceiver> receiver, | 421 Maybe<bool> GetKeysFromInterceptor(Handle<JSReceiver> receiver, |
607 Handle<JSObject> object, | 422 Handle<JSObject> object, |
608 KeyAccumulator* accumulator) { | 423 KeyAccumulator* accumulator) { |
609 Isolate* isolate = accumulator->isolate(); | 424 Isolate* isolate = accumulator->isolate(); |
610 if (type == kIndexed) { | 425 if (type == kIndexed) { |
611 if (!object->HasIndexedInterceptor()) return Just(true); | 426 if (!object->HasIndexedInterceptor()) return Just(true); |
612 } else { | 427 } else { |
613 if (!object->HasNamedInterceptor()) return Just(true); | 428 if (!object->HasNamedInterceptor()) return Just(true); |
614 } | 429 } |
615 Handle<InterceptorInfo> interceptor(type == kIndexed | 430 Handle<InterceptorInfo> interceptor(type == kIndexed |
616 ? object->GetIndexedInterceptor() | 431 ? object->GetIndexedInterceptor() |
617 : object->GetNamedInterceptor(), | 432 : object->GetNamedInterceptor(), |
618 isolate); | 433 isolate); |
619 if ((accumulator->filter() & ONLY_ALL_CAN_READ) && | 434 if ((accumulator->filter() & ONLY_ALL_CAN_READ) && |
620 !interceptor->all_can_read()) { | 435 !interceptor->all_can_read()) { |
621 return Just(true); | 436 return Just(true); |
622 } | 437 } |
623 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, | 438 PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
624 *object, Object::DONT_THROW); | 439 *object, Object::DONT_THROW); |
625 Handle<JSObject> result; | 440 Handle<JSObject> result; |
626 if (!interceptor->enumerator()->IsUndefined()) { | 441 if (!interceptor->enumerator()->IsUndefined()) { |
627 Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator()); | 442 Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator()); |
628 const char* log_tag = type == kIndexed ? "interceptor-indexed-enum" | 443 const char* log_tag = type == kIndexed ? "interceptor-indexed-enum" |
629 : "interceptor-named-enum"; | 444 : "interceptor-named-enum"; |
630 LOG(isolate, ApiObjectAccess(log_tag, *object)); | 445 LOG(isolate, ApiObjectAccess(log_tag, *object)); |
631 result = args.Call(enum_fun); | 446 result = args.Call(enum_fun); |
632 } | 447 } |
633 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); | 448 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
634 if (result.is_null()) return Just(true); | 449 if (result.is_null()) return Just(true); |
635 DCHECK(result->IsJSArray() || result->HasSloppyArgumentsElements()); | |
636 // The accumulator takes care of string/symbol filtering. | |
637 if (type == kIndexed) { | 450 if (type == kIndexed) { |
638 accumulator->AddElementKeysFromInterceptor(result); | 451 accumulator->AddKeys(result, CONVERT_TO_ARRAY_INDEX); |
Jakob Kummerow
2016/05/23 16:25:21
nit: could shorten this further now:
accumulator-
Camillo Bruni
2016/05/23 19:10:13
done.
| |
639 } else { | 452 } else { |
640 accumulator->AddKeys(result, DO_NOT_CONVERT); | 453 accumulator->AddKeys(result, DO_NOT_CONVERT); |
641 } | 454 } |
642 return Just(true); | 455 return Just(true); |
643 } | 456 } |
644 | 457 |
645 void KeyAccumulator::CollectOwnElementIndices(Handle<JSObject> object) { | 458 } // namespace |
646 if (filter_ & SKIP_STRINGS) return; | 459 |
460 Maybe<bool> KeyAccumulator::CollectOwnElementIndices( | |
461 Handle<JSReceiver> receiver, Handle<JSObject> object) { | |
462 if (filter_ & SKIP_STRINGS || skip_indices_) return Just(true); | |
463 | |
647 ElementsAccessor* accessor = object->GetElementsAccessor(); | 464 ElementsAccessor* accessor = object->GetElementsAccessor(); |
648 accessor->CollectElementIndices(object, this); | 465 accessor->CollectElementIndices(object, this); |
466 | |
467 return GetKeysFromInterceptor<v8::IndexedPropertyEnumeratorCallback, | |
468 kIndexed>(receiver, object, this); | |
649 } | 469 } |
650 | 470 |
651 void KeyAccumulator::CollectOwnPropertyNames(Handle<JSObject> object) { | 471 namespace { |
652 if (object->HasFastProperties()) { | 472 |
653 int real_size = object->map()->NumberOfOwnDescriptors(); | 473 template <bool skip_symbols> |
654 Handle<DescriptorArray> descs(object->map()->instance_descriptors(), | 474 int CollectOwnStringPropertyNames(Handle<JSObject> object, KeyAccumulator* keys, |
Jakob Kummerow
2016/05/23 16:25:21
nit: this name is confusing, the function can coll
Camillo Bruni
2016/05/23 19:10:13
changed.
| |
655 isolate_); | 475 Handle<DescriptorArray> descs, |
656 for (int i = 0; i < real_size; i++) { | 476 int start_index, int limit) { |
657 PropertyDetails details = descs->GetDetails(i); | 477 int first_skipped = -1; |
658 if ((details.attributes() & filter_) != 0) continue; | 478 for (int i = start_index; i < limit; i++) { |
659 if (filter_ & ONLY_ALL_CAN_READ) { | 479 PropertyDetails details = descs->GetDetails(i); |
660 if (details.kind() != kAccessor) continue; | 480 if ((details.attributes() & keys->filter()) != 0) continue; |
661 Object* accessors = descs->GetValue(i); | 481 if (keys->filter() & ONLY_ALL_CAN_READ) { |
662 if (!accessors->IsAccessorInfo()) continue; | 482 if (details.kind() != kAccessor) continue; |
663 if (!AccessorInfo::cast(accessors)->all_can_read()) continue; | 483 Object* accessors = descs->GetValue(i); |
484 if (!accessors->IsAccessorInfo()) continue; | |
485 if (!AccessorInfo::cast(accessors)->all_can_read()) continue; | |
486 } | |
487 Name* key = descs->GetKey(i); | |
488 if (skip_symbols == key->IsSymbol()) { | |
489 if (first_skipped == -1) first_skipped = i; | |
490 continue; | |
491 } | |
492 if (key->FilterKey(keys->filter())) continue; | |
493 keys->AddKey(key, DO_NOT_CONVERT); | |
494 } | |
495 return first_skipped; | |
496 } | |
497 | |
498 } // namespace | |
499 | |
500 Maybe<bool> KeyAccumulator::CollectOwnPropertyNames(Handle<JSReceiver> receiver, | |
501 Handle<JSObject> object) { | |
502 if (filter_ == ENUMERABLE_STRINGS) { | |
503 Handle<FixedArray> enum_keys = | |
504 KeyAccumulator::GetEnumPropertyKeys(isolate_, object); | |
505 AddKeys(enum_keys, DO_NOT_CONVERT); | |
506 } else { | |
507 if (object->HasFastProperties()) { | |
508 int limit = object->map()->NumberOfOwnDescriptors(); | |
509 Handle<DescriptorArray> descs(object->map()->instance_descriptors(), | |
510 isolate_); | |
511 // First Collect the Strings, | |
Jakob Kummerow
2016/05/23 16:25:21
nit: s/Collect/collect/, s/Strings/strings/
| |
512 int first_symbol = | |
513 CollectOwnStringPropertyNames<true>(object, this, descs, 0, limit); | |
514 // then the Symbols. | |
Jakob Kummerow
2016/05/23 16:25:21
nit: s/Symbols/symbols/
| |
515 if (first_symbol != -1) { | |
516 CollectOwnStringPropertyNames<false>(object, this, descs, first_symbol, | |
517 limit); | |
664 } | 518 } |
665 Name* key = descs->GetKey(i); | 519 } else if (object->IsJSGlobalObject()) { |
666 if (key->FilterKey(filter_)) continue; | 520 GlobalDictionary::CollectKeysTo( |
667 AddKey(key, DO_NOT_CONVERT); | 521 handle(object->global_dictionary(), isolate_), this, filter_); |
522 } else { | |
523 NameDictionary::CollectKeysTo( | |
524 handle(object->property_dictionary(), isolate_), this, filter_); | |
668 } | 525 } |
669 } else if (object->IsJSGlobalObject()) { | |
670 GlobalDictionary::CollectKeysTo( | |
671 handle(object->global_dictionary(), isolate_), this, filter_); | |
672 } else { | |
673 NameDictionary::CollectKeysTo( | |
674 handle(object->property_dictionary(), isolate_), this, filter_); | |
675 } | 526 } |
527 // Add the property keys from the interceptor. | |
528 return GetKeysFromInterceptor<v8::GenericNamedPropertyEnumeratorCallback, | |
529 kNamed>(receiver, object, this); | |
676 } | 530 } |
677 | 531 |
678 // Returns |true| on success, |false| if prototype walking should be stopped, | 532 // Returns |true| on success, |false| if prototype walking should be stopped, |
679 // |nothing| if an exception was thrown. | 533 // |nothing| if an exception was thrown. |
680 Maybe<bool> KeyAccumulator::CollectOwnKeys(Handle<JSReceiver> receiver, | 534 Maybe<bool> KeyAccumulator::CollectOwnKeys(Handle<JSReceiver> receiver, |
681 Handle<JSObject> object) { | 535 Handle<JSObject> object) { |
682 NextPrototype(); | |
683 // Check access rights if required. | 536 // Check access rights if required. |
684 if (object->IsAccessCheckNeeded() && | 537 if (object->IsAccessCheckNeeded() && |
685 !isolate_->MayAccess(handle(isolate_->context()), object)) { | 538 !isolate_->MayAccess(handle(isolate_->context()), object)) { |
686 // The cross-origin spec says that [[Enumerate]] shall return an empty | 539 // The cross-origin spec says that [[Enumerate]] shall return an empty |
687 // iterator when it doesn't have access... | 540 // iterator when it doesn't have access... |
688 if (type_ == INCLUDE_PROTOS) { | 541 if (type_ == INCLUDE_PROTOS) { |
689 return Just(false); | 542 return Just(false); |
690 } | 543 } |
691 // ...whereas [[OwnPropertyKeys]] shall return whitelisted properties. | 544 // ...whereas [[OwnPropertyKeys]] shall return whitelisted properties. |
692 DCHECK_EQ(OWN_ONLY, type_); | 545 DCHECK_EQ(OWN_ONLY, type_); |
693 filter_ = static_cast<PropertyFilter>(filter_ | ONLY_ALL_CAN_READ); | 546 filter_ = static_cast<PropertyFilter>(filter_ | ONLY_ALL_CAN_READ); |
694 } | 547 } |
695 | 548 MAYBE_RETURN(CollectOwnElementIndices(receiver, object), Nothing<bool>()); |
696 CollectOwnElementIndices(object); | 549 MAYBE_RETURN(CollectOwnPropertyNames(receiver, object), Nothing<bool>()); |
697 | |
698 // Add the element keys from the interceptor. | |
699 Maybe<bool> success = | |
700 GetKeysFromInterceptor<v8::IndexedPropertyEnumeratorCallback, kIndexed>( | |
701 receiver, object, this); | |
702 MAYBE_RETURN(success, Nothing<bool>()); | |
703 | |
704 if (filter_ == ENUMERABLE_STRINGS) { | |
705 Handle<FixedArray> enum_keys = | |
706 KeyAccumulator::GetEnumPropertyKeys(isolate_, object); | |
707 AddKeys(enum_keys, DO_NOT_CONVERT); | |
708 } else { | |
709 CollectOwnPropertyNames(object); | |
710 } | |
711 | |
712 // Add the property keys from the interceptor. | |
713 success = GetKeysFromInterceptor<v8::GenericNamedPropertyEnumeratorCallback, | |
714 kNamed>(receiver, object, this); | |
715 MAYBE_RETURN(success, Nothing<bool>()); | |
716 return Just(true); | 550 return Just(true); |
717 } | 551 } |
718 | 552 |
719 // static | 553 // static |
720 Handle<FixedArray> KeyAccumulator::GetEnumPropertyKeys( | 554 Handle<FixedArray> KeyAccumulator::GetEnumPropertyKeys( |
721 Isolate* isolate, Handle<JSObject> object) { | 555 Isolate* isolate, Handle<JSObject> object) { |
722 if (object->HasFastProperties()) { | 556 if (object->HasFastProperties()) { |
723 return GetFastEnumPropertyKeys(isolate, object); | 557 return GetFastEnumPropertyKeys(isolate, object); |
724 } else if (object->IsJSGlobalObject()) { | 558 } else if (object->IsJSGlobalObject()) { |
725 Handle<GlobalDictionary> dictionary(object->global_dictionary(), isolate); | 559 Handle<GlobalDictionary> dictionary(object->global_dictionary(), isolate); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
815 target_keys->get(i)); | 649 target_keys->get(i)); |
816 nonconfigurable_keys_length++; | 650 nonconfigurable_keys_length++; |
817 // The key was moved, null it out in the original list. | 651 // The key was moved, null it out in the original list. |
818 target_keys->set(i, Smi::FromInt(0)); | 652 target_keys->set(i, Smi::FromInt(0)); |
819 } else { | 653 } else { |
820 // 14c. Else, | 654 // 14c. Else, |
821 // 14c i. Append key as an element of targetConfigurableKeys. | 655 // 14c i. Append key as an element of targetConfigurableKeys. |
822 // (No-op, just keep it in |target_keys|.) | 656 // (No-op, just keep it in |target_keys|.) |
823 } | 657 } |
824 } | 658 } |
825 NextPrototype(); // Prepare for accumulating keys. | |
826 // 15. If extensibleTarget is true and targetNonconfigurableKeys is empty, | 659 // 15. If extensibleTarget is true and targetNonconfigurableKeys is empty, |
827 // then: | 660 // then: |
828 if (extensible_target && nonconfigurable_keys_length == 0) { | 661 if (extensible_target && nonconfigurable_keys_length == 0) { |
829 // 15a. Return trapResult. | 662 // 15a. Return trapResult. |
830 return AddKeysFromJSProxy(proxy, trap_result); | 663 return AddKeysFromJSProxy(proxy, trap_result); |
831 } | 664 } |
832 // 16. Let uncheckedResultKeys be a new List which is a copy of trapResult. | 665 // 16. Let uncheckedResultKeys be a new List which is a copy of trapResult. |
833 Zone set_zone(isolate_->allocator()); | 666 Zone set_zone(isolate_->allocator()); |
834 const int kPresent = 1; | 667 const int kPresent = 1; |
835 const int kGone = 0; | 668 const int kGone = 0; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
889 // 21. Return trapResult. | 722 // 21. Return trapResult. |
890 return AddKeysFromJSProxy(proxy, trap_result); | 723 return AddKeysFromJSProxy(proxy, trap_result); |
891 } | 724 } |
892 | 725 |
893 Maybe<bool> KeyAccumulator::CollectOwnJSProxyTargetKeys( | 726 Maybe<bool> KeyAccumulator::CollectOwnJSProxyTargetKeys( |
894 Handle<JSProxy> proxy, Handle<JSReceiver> target) { | 727 Handle<JSProxy> proxy, Handle<JSReceiver> target) { |
895 // TODO(cbruni): avoid creating another KeyAccumulator | 728 // TODO(cbruni): avoid creating another KeyAccumulator |
896 Handle<FixedArray> keys; | 729 Handle<FixedArray> keys; |
897 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | 730 ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
898 isolate_, keys, JSReceiver::OwnPropertyKeys(target), Nothing<bool>()); | 731 isolate_, keys, JSReceiver::OwnPropertyKeys(target), Nothing<bool>()); |
899 NextPrototype(); // Prepare for accumulating keys. | |
900 bool prev_filter_proxy_keys_ = filter_proxy_keys_; | 732 bool prev_filter_proxy_keys_ = filter_proxy_keys_; |
901 filter_proxy_keys_ = false; | 733 filter_proxy_keys_ = false; |
902 Maybe<bool> result = AddKeysFromJSProxy(proxy, keys); | 734 Maybe<bool> result = AddKeysFromJSProxy(proxy, keys); |
903 filter_proxy_keys_ = prev_filter_proxy_keys_; | 735 filter_proxy_keys_ = prev_filter_proxy_keys_; |
904 return result; | 736 return result; |
905 } | 737 } |
906 | 738 |
907 } // namespace internal | 739 } // namespace internal |
908 } // namespace v8 | 740 } // namespace v8 |
OLD | NEW |