Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 5 | #include "src/keys.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 7 | #include "src/api-arguments.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/elements.h" |
| 9 | #include "src/factory.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 10 | #include "src/identity-map.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11 | #include "src/isolate-inl.h" |
| 12 | #include "src/objects-inl.h" |
| 13 | #include "src/property-descriptor.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 14 | #include "src/prototype.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | |
| 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | KeyAccumulator::~KeyAccumulator() { |
| 20 | for (size_t i = 0; i < elements_.size(); i++) { |
| 21 | delete elements_[i]; |
| 22 | } |
| 23 | } |
| 24 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 25 | namespace { |
| 26 | |
| 27 | static bool ContainsOnlyValidKeys(Handle<FixedArray> array) { |
| 28 | int len = array->length(); |
| 29 | for (int i = 0; i < len; i++) { |
| 30 | Object* e = array->get(i); |
| 31 | if (!(e->IsName() || e->IsNumber())) return false; |
| 32 | } |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | MaybeHandle<FixedArray> KeyAccumulator::GetKeys( |
| 39 | Handle<JSReceiver> object, KeyCollectionType type, PropertyFilter filter, |
| 40 | GetKeysConversion keys_conversion, bool filter_proxy_keys) { |
| 41 | USE(ContainsOnlyValidKeys); |
| 42 | Isolate* isolate = object->GetIsolate(); |
| 43 | KeyAccumulator accumulator(isolate, type, filter); |
| 44 | accumulator.set_filter_proxy_keys(filter_proxy_keys); |
| 45 | MAYBE_RETURN(accumulator.CollectKeys(object, object), |
| 46 | MaybeHandle<FixedArray>()); |
| 47 | Handle<FixedArray> keys = accumulator.GetKeys(keys_conversion); |
| 48 | DCHECK(ContainsOnlyValidKeys(keys)); |
| 49 | return keys; |
| 50 | } |
| 51 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 52 | Handle<FixedArray> KeyAccumulator::GetKeys(GetKeysConversion convert) { |
| 53 | if (length_ == 0) { |
| 54 | return isolate_->factory()->empty_fixed_array(); |
| 55 | } |
| 56 | // Make sure we have all the lengths collected. |
| 57 | NextPrototype(); |
| 58 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 59 | if (type_ == OWN_ONLY && !ownProxyKeys_.is_null()) { |
| 60 | return ownProxyKeys_; |
| 61 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 62 | // Assemble the result array by first adding the element keys and then the |
| 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]; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 76 | int num_elements = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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]; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 85 | num_elements = static_cast<int>(elements->size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | DCHECK_EQ(insertion_index, length_); |
| 121 | return result; |
| 122 | } |
| 123 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 124 | namespace { |
| 125 | |
| 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 | } |
| 129 | |
| 130 | } // namespace |
| 131 | |
| 132 | bool KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) { |
| 133 | return AddKey(handle(key, isolate_), convert); |
| 134 | } |
| 135 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 136 | bool KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) { |
| 137 | if (key->IsSymbol()) { |
| 138 | if (filter_ & SKIP_SYMBOLS) return false; |
| 139 | if (Handle<Symbol>::cast(key)->is_private()) return false; |
| 140 | return AddSymbolKey(key); |
| 141 | } |
| 142 | if (filter_ & SKIP_STRINGS) return false; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 143 | // Make sure we do not add keys to a proxy-level (see AddKeysFromJSProxy). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | } |
| 169 | return AddStringKey(key, convert); |
| 170 | } |
| 171 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 172 | bool KeyAccumulator::AddKey(uint32_t key) { return AddIntegerKey(key); } |
| 173 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 174 | bool KeyAccumulator::AddIntegerKey(uint32_t key) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 175 | // Make sure we do not add keys to a proxy-level (see AddKeysFromJSProxy). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | } |
| 183 | elements_.back()->push_back(key); |
| 184 | length_++; |
| 185 | return true; |
| 186 | } |
| 187 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | } |
| 223 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 224 | void KeyAccumulator::AddKeys(Handle<FixedArray> array, |
| 225 | AddKeyConversion convert) { |
| 226 | int add_length = array->length(); |
| 227 | if (add_length == 0) return; |
| 228 | for (int i = 0; i < add_length; i++) { |
| 229 | Handle<Object> current(array->get(i), isolate_); |
| 230 | AddKey(current, convert); |
| 231 | } |
| 232 | } |
| 233 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 234 | void KeyAccumulator::AddKeys(Handle<JSObject> array_like, |
| 235 | AddKeyConversion convert) { |
| 236 | DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements()); |
| 237 | ElementsAccessor* accessor = array_like->GetElementsAccessor(); |
| 238 | accessor->AddElementsToKeyAccumulator(array_like, this, convert); |
| 239 | } |
| 240 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 241 | MaybeHandle<FixedArray> FilterProxyKeys(Isolate* isolate, Handle<JSProxy> owner, |
| 242 | Handle<FixedArray> keys, |
| 243 | PropertyFilter filter) { |
| 244 | if (filter == ALL_PROPERTIES) { |
| 245 | // Nothing to do. |
| 246 | return keys; |
| 247 | } |
| 248 | int store_position = 0; |
| 249 | for (int i = 0; i < keys->length(); ++i) { |
| 250 | Handle<Name> key(Name::cast(keys->get(i)), isolate); |
| 251 | if (key->FilterKey(filter)) continue; // Skip this key. |
| 252 | if (filter & ONLY_ENUMERABLE) { |
| 253 | PropertyDescriptor desc; |
| 254 | Maybe<bool> found = |
| 255 | JSProxy::GetOwnPropertyDescriptor(isolate, owner, key, &desc); |
| 256 | MAYBE_RETURN(found, MaybeHandle<FixedArray>()); |
| 257 | if (!found.FromJust() || !desc.enumerable()) continue; // Skip this key. |
| 258 | } |
| 259 | // Keep this key. |
| 260 | if (store_position != i) { |
| 261 | keys->set(store_position, *key); |
| 262 | } |
| 263 | store_position++; |
| 264 | } |
| 265 | if (store_position == 0) return isolate->factory()->empty_fixed_array(); |
| 266 | keys->Shrink(store_position); |
| 267 | return keys; |
| 268 | } |
| 269 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 270 | // Returns "nothing" in case of exception, "true" on success. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 271 | Maybe<bool> KeyAccumulator::AddKeysFromJSProxy(Handle<JSProxy> proxy, |
| 272 | Handle<FixedArray> keys) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 273 | if (filter_proxy_keys_) { |
| 274 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 275 | isolate_, keys, FilterProxyKeys(isolate_, proxy, keys, filter_), |
| 276 | Nothing<bool>()); |
| 277 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 278 | // Proxies define a complete list of keys with no distinction of |
| 279 | // elements and properties, which breaks the normal assumption for the |
| 280 | // KeyAccumulator. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 281 | if (type_ == OWN_ONLY) { |
| 282 | ownProxyKeys_ = keys; |
| 283 | level_string_length_ = keys->length(); |
| 284 | length_ = level_string_length_; |
| 285 | } else { |
| 286 | AddKeys(keys, PROXY_MAGIC); |
| 287 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 288 | // Invert the current length to indicate a present proxy, so we can ignore |
| 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); |
| 293 | } |
| 294 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 333 | Maybe<bool> KeyAccumulator::CollectKeys(Handle<JSReceiver> receiver, |
| 334 | Handle<JSReceiver> object) { |
| 335 | // Proxies have no hidden prototype and we should not trigger the |
| 336 | // [[GetPrototypeOf]] trap on the last iteration when using |
| 337 | // AdvanceFollowingProxies. |
| 338 | if (type_ == OWN_ONLY && object->IsJSProxy()) { |
| 339 | MAYBE_RETURN(CollectOwnJSProxyKeys(receiver, Handle<JSProxy>::cast(object)), |
| 340 | Nothing<bool>()); |
| 341 | return Just(true); |
| 342 | } |
| 343 | |
| 344 | PrototypeIterator::WhereToEnd end = type_ == OWN_ONLY |
| 345 | ? PrototypeIterator::END_AT_NON_HIDDEN |
| 346 | : PrototypeIterator::END_AT_NULL; |
| 347 | for (PrototypeIterator iter(isolate_, object, |
| 348 | PrototypeIterator::START_AT_RECEIVER, end); |
| 349 | !iter.IsAtEnd();) { |
| 350 | Handle<JSReceiver> current = |
| 351 | PrototypeIterator::GetCurrent<JSReceiver>(iter); |
| 352 | Maybe<bool> result = Just(false); // Dummy initialization. |
| 353 | if (current->IsJSProxy()) { |
| 354 | result = CollectOwnJSProxyKeys(receiver, Handle<JSProxy>::cast(current)); |
| 355 | } else { |
| 356 | DCHECK(current->IsJSObject()); |
| 357 | result = CollectOwnKeys(receiver, Handle<JSObject>::cast(current)); |
| 358 | } |
| 359 | MAYBE_RETURN(result, Nothing<bool>()); |
| 360 | if (!result.FromJust()) break; // |false| means "stop iterating". |
| 361 | // Iterate through proxies but ignore access checks for the ALL_CAN_READ |
| 362 | // case on API objects for OWN_ONLY keys handled in CollectOwnKeys. |
| 363 | if (!iter.AdvanceFollowingProxiesIgnoringAccessChecks()) { |
| 364 | return Nothing<bool>(); |
| 365 | } |
| 366 | } |
| 367 | return Just(true); |
| 368 | } |
| 369 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 370 | namespace { |
| 371 | |
| 372 | void TrySettingEmptyEnumCache(JSReceiver* object) { |
| 373 | Map* map = object->map(); |
| 374 | DCHECK_EQ(kInvalidEnumCacheSentinel, map->EnumLength()); |
| 375 | if (!map->OnlyHasSimpleProperties()) return; |
| 376 | if (map->IsJSProxyMap()) return; |
| 377 | if (map->NumberOfOwnDescriptors() > 0) { |
| 378 | int number_of_enumerable_own_properties = |
| 379 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS); |
| 380 | if (number_of_enumerable_own_properties > 0) return; |
| 381 | } |
| 382 | DCHECK(object->IsJSObject()); |
| 383 | map->SetEnumLength(0); |
| 384 | } |
| 385 | |
| 386 | bool CheckAndInitalizeSimpleEnumCache(JSReceiver* object) { |
| 387 | if (object->map()->EnumLength() == kInvalidEnumCacheSentinel) { |
| 388 | TrySettingEmptyEnumCache(object); |
| 389 | } |
| 390 | if (object->map()->EnumLength() != 0) return false; |
| 391 | DCHECK(object->IsJSObject()); |
| 392 | return !JSObject::cast(object)->HasEnumerableElements(); |
| 393 | } |
| 394 | } // namespace |
| 395 | |
| 396 | void FastKeyAccumulator::Prepare() { |
| 397 | DisallowHeapAllocation no_gc; |
| 398 | // Directly go for the fast path for OWN_ONLY keys. |
| 399 | if (type_ == OWN_ONLY) return; |
| 400 | // Fully walk the prototype chain and find the last prototype with keys. |
| 401 | is_receiver_simple_enum_ = false; |
| 402 | has_empty_prototype_ = true; |
| 403 | JSReceiver* first_non_empty_prototype; |
| 404 | for (PrototypeIterator iter(isolate_, *receiver_); !iter.IsAtEnd(); |
| 405 | iter.Advance()) { |
| 406 | JSReceiver* current = iter.GetCurrent<JSReceiver>(); |
| 407 | if (CheckAndInitalizeSimpleEnumCache(current)) continue; |
| 408 | has_empty_prototype_ = false; |
| 409 | first_non_empty_prototype = current; |
| 410 | // TODO(cbruni): use the first non-empty prototype. |
| 411 | USE(first_non_empty_prototype); |
| 412 | return; |
| 413 | } |
| 414 | DCHECK(has_empty_prototype_); |
| 415 | is_receiver_simple_enum_ = |
| 416 | receiver_->map()->EnumLength() != kInvalidEnumCacheSentinel && |
| 417 | !JSObject::cast(*receiver_)->HasEnumerableElements(); |
| 418 | } |
| 419 | |
| 420 | namespace { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 421 | static Handle<FixedArray> ReduceFixedArrayTo(Isolate* isolate, |
| 422 | Handle<FixedArray> array, |
| 423 | int length) { |
| 424 | DCHECK_LE(length, array->length()); |
| 425 | if (array->length() == length) return array; |
| 426 | return isolate->factory()->CopyFixedArrayUpTo(array, length); |
| 427 | } |
| 428 | |
| 429 | Handle<FixedArray> GetFastEnumPropertyKeys(Isolate* isolate, |
| 430 | Handle<JSObject> object) { |
| 431 | Handle<Map> map(object->map()); |
| 432 | bool cache_enum_length = map->OnlyHasSimpleProperties(); |
| 433 | |
| 434 | Handle<DescriptorArray> descs = |
| 435 | Handle<DescriptorArray>(map->instance_descriptors(), isolate); |
| 436 | int own_property_count = map->EnumLength(); |
| 437 | // If the enum length of the given map is set to kInvalidEnumCache, this |
| 438 | // means that the map itself has never used the present enum cache. The |
| 439 | // first step to using the cache is to set the enum length of the map by |
| 440 | // counting the number of own descriptors that are ENUMERABLE_STRINGS. |
| 441 | if (own_property_count == kInvalidEnumCacheSentinel) { |
| 442 | own_property_count = |
| 443 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS); |
| 444 | } else { |
| 445 | DCHECK( |
| 446 | own_property_count == |
| 447 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS)); |
| 448 | } |
| 449 | |
| 450 | if (descs->HasEnumCache()) { |
| 451 | Handle<FixedArray> keys(descs->GetEnumCache(), isolate); |
| 452 | // In case the number of properties required in the enum are actually |
| 453 | // present, we can reuse the enum cache. Otherwise, this means that the |
| 454 | // enum cache was generated for a previous (smaller) version of the |
| 455 | // Descriptor Array. In that case we regenerate the enum cache. |
| 456 | if (own_property_count <= keys->length()) { |
| 457 | isolate->counters()->enum_cache_hits()->Increment(); |
| 458 | if (cache_enum_length) map->SetEnumLength(own_property_count); |
| 459 | return ReduceFixedArrayTo(isolate, keys, own_property_count); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (descs->IsEmpty()) { |
| 464 | isolate->counters()->enum_cache_hits()->Increment(); |
| 465 | if (cache_enum_length) map->SetEnumLength(0); |
| 466 | return isolate->factory()->empty_fixed_array(); |
| 467 | } |
| 468 | |
| 469 | isolate->counters()->enum_cache_misses()->Increment(); |
| 470 | |
| 471 | Handle<FixedArray> storage = |
| 472 | isolate->factory()->NewFixedArray(own_property_count); |
| 473 | Handle<FixedArray> indices = |
| 474 | isolate->factory()->NewFixedArray(own_property_count); |
| 475 | |
| 476 | int size = map->NumberOfOwnDescriptors(); |
| 477 | int index = 0; |
| 478 | |
| 479 | for (int i = 0; i < size; i++) { |
| 480 | PropertyDetails details = descs->GetDetails(i); |
| 481 | if (details.IsDontEnum()) continue; |
| 482 | Object* key = descs->GetKey(i); |
| 483 | if (key->IsSymbol()) continue; |
| 484 | storage->set(index, key); |
| 485 | if (!indices.is_null()) { |
| 486 | if (details.type() != DATA) { |
| 487 | indices = Handle<FixedArray>(); |
| 488 | } else { |
| 489 | FieldIndex field_index = FieldIndex::ForDescriptor(*map, i); |
| 490 | int load_by_field_index = field_index.GetLoadByFieldIndex(); |
| 491 | indices->set(index, Smi::FromInt(load_by_field_index)); |
| 492 | } |
| 493 | } |
| 494 | index++; |
| 495 | } |
| 496 | DCHECK(index == storage->length()); |
| 497 | |
| 498 | DescriptorArray::SetEnumCache(descs, isolate, storage, indices); |
| 499 | if (cache_enum_length) { |
| 500 | map->SetEnumLength(own_property_count); |
| 501 | } |
| 502 | return storage; |
| 503 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 504 | |
| 505 | template <bool fast_properties> |
| 506 | Handle<FixedArray> GetOwnKeysWithElements(Isolate* isolate, |
| 507 | Handle<JSObject> object, |
| 508 | GetKeysConversion convert) { |
| 509 | Handle<FixedArray> keys; |
| 510 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 511 | if (fast_properties) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 512 | keys = GetFastEnumPropertyKeys(isolate, object); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 513 | } else { |
| 514 | // TODO(cbruni): preallocate big enough array to also hold elements. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 515 | keys = KeyAccumulator::GetEnumPropertyKeys(isolate, object); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 516 | } |
| 517 | Handle<FixedArray> result = |
| 518 | accessor->PrependElementIndices(object, keys, convert, ONLY_ENUMERABLE); |
| 519 | |
| 520 | if (FLAG_trace_for_in_enumerate) { |
| 521 | PrintF("| strings=%d symbols=0 elements=%u || prototypes>=1 ||\n", |
| 522 | keys->length(), result->length() - keys->length()); |
| 523 | } |
| 524 | return result; |
| 525 | } |
| 526 | |
| 527 | MaybeHandle<FixedArray> GetOwnKeysWithUninitializedEnumCache( |
| 528 | Isolate* isolate, Handle<JSObject> object) { |
| 529 | // Uninitalized enum cache |
| 530 | Map* map = object->map(); |
| 531 | if (object->elements() != isolate->heap()->empty_fixed_array() || |
| 532 | object->elements() != isolate->heap()->empty_slow_element_dictionary()) { |
| 533 | // Assume that there are elements. |
| 534 | return MaybeHandle<FixedArray>(); |
| 535 | } |
| 536 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 537 | if (number_of_own_descriptors == 0) { |
| 538 | map->SetEnumLength(0); |
| 539 | return isolate->factory()->empty_fixed_array(); |
| 540 | } |
| 541 | // We have no elements but possibly enumerable property keys, hence we can |
| 542 | // directly initialize the enum cache. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 543 | return GetFastEnumPropertyKeys(isolate, object); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | bool OnlyHasSimpleProperties(Map* map) { |
| 547 | return map->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER; |
| 548 | } |
| 549 | |
| 550 | } // namespace |
| 551 | |
| 552 | MaybeHandle<FixedArray> FastKeyAccumulator::GetKeys(GetKeysConversion convert) { |
| 553 | Handle<FixedArray> keys; |
| 554 | if (GetKeysFast(convert).ToHandle(&keys)) { |
| 555 | return keys; |
| 556 | } |
| 557 | return GetKeysSlow(convert); |
| 558 | } |
| 559 | |
| 560 | MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysFast( |
| 561 | GetKeysConversion convert) { |
| 562 | bool own_only = has_empty_prototype_ || type_ == OWN_ONLY; |
| 563 | Map* map = receiver_->map(); |
| 564 | if (!own_only || !OnlyHasSimpleProperties(map)) { |
| 565 | return MaybeHandle<FixedArray>(); |
| 566 | } |
| 567 | |
| 568 | // From this point on we are certiain to only collect own keys. |
| 569 | DCHECK(receiver_->IsJSObject()); |
| 570 | Handle<JSObject> object = Handle<JSObject>::cast(receiver_); |
| 571 | |
| 572 | // Do not try to use the enum-cache for dict-mode objects. |
| 573 | if (map->is_dictionary_map()) { |
| 574 | return GetOwnKeysWithElements<false>(isolate_, object, convert); |
| 575 | } |
| 576 | int enum_length = receiver_->map()->EnumLength(); |
| 577 | if (enum_length == kInvalidEnumCacheSentinel) { |
| 578 | Handle<FixedArray> keys; |
| 579 | // Try initializing the enum cache and return own properties. |
| 580 | if (GetOwnKeysWithUninitializedEnumCache(isolate_, object) |
| 581 | .ToHandle(&keys)) { |
| 582 | if (FLAG_trace_for_in_enumerate) { |
| 583 | PrintF("| strings=%d symbols=0 elements=0 || prototypes>=1 ||\n", |
| 584 | keys->length()); |
| 585 | } |
| 586 | is_receiver_simple_enum_ = |
| 587 | object->map()->EnumLength() != kInvalidEnumCacheSentinel; |
| 588 | return keys; |
| 589 | } |
| 590 | } |
| 591 | // The properties-only case failed because there were probably elements on the |
| 592 | // receiver. |
| 593 | return GetOwnKeysWithElements<true>(isolate_, object, convert); |
| 594 | } |
| 595 | |
| 596 | MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysSlow( |
| 597 | GetKeysConversion convert) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 598 | return JSReceiver::GetKeys(receiver_, type_, filter_, KEEP_NUMBERS, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 599 | filter_proxy_keys_); |
| 600 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 601 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 602 | enum IndexedOrNamed { kIndexed, kNamed }; |
| 603 | |
| 604 | // Returns |true| on success, |nothing| on exception. |
| 605 | template <class Callback, IndexedOrNamed type> |
| 606 | static Maybe<bool> GetKeysFromInterceptor(Handle<JSReceiver> receiver, |
| 607 | Handle<JSObject> object, |
| 608 | KeyAccumulator* accumulator) { |
| 609 | Isolate* isolate = accumulator->isolate(); |
| 610 | if (type == kIndexed) { |
| 611 | if (!object->HasIndexedInterceptor()) return Just(true); |
| 612 | } else { |
| 613 | if (!object->HasNamedInterceptor()) return Just(true); |
| 614 | } |
| 615 | Handle<InterceptorInfo> interceptor(type == kIndexed |
| 616 | ? object->GetIndexedInterceptor() |
| 617 | : object->GetNamedInterceptor(), |
| 618 | isolate); |
| 619 | if ((accumulator->filter() & ONLY_ALL_CAN_READ) && |
| 620 | !interceptor->all_can_read()) { |
| 621 | return Just(true); |
| 622 | } |
| 623 | PropertyCallbackArguments args(isolate, interceptor->data(), *receiver, |
| 624 | *object, Object::DONT_THROW); |
| 625 | Handle<JSObject> result; |
| 626 | if (!interceptor->enumerator()->IsUndefined()) { |
| 627 | Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator()); |
| 628 | const char* log_tag = type == kIndexed ? "interceptor-indexed-enum" |
| 629 | : "interceptor-named-enum"; |
| 630 | LOG(isolate, ApiObjectAccess(log_tag, *object)); |
| 631 | result = args.Call(enum_fun); |
| 632 | } |
| 633 | RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>()); |
| 634 | 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) { |
| 638 | accumulator->AddElementKeysFromInterceptor(result); |
| 639 | } else { |
| 640 | accumulator->AddKeys(result, DO_NOT_CONVERT); |
| 641 | } |
| 642 | return Just(true); |
| 643 | } |
| 644 | |
| 645 | void KeyAccumulator::CollectOwnElementIndices(Handle<JSObject> object) { |
| 646 | if (filter_ & SKIP_STRINGS) return; |
| 647 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 648 | accessor->CollectElementIndices(object, this); |
| 649 | } |
| 650 | |
| 651 | void KeyAccumulator::CollectOwnPropertyNames(Handle<JSObject> object) { |
| 652 | if (object->HasFastProperties()) { |
| 653 | int real_size = object->map()->NumberOfOwnDescriptors(); |
| 654 | Handle<DescriptorArray> descs(object->map()->instance_descriptors(), |
| 655 | isolate_); |
| 656 | for (int i = 0; i < real_size; i++) { |
| 657 | PropertyDetails details = descs->GetDetails(i); |
| 658 | if ((details.attributes() & filter_) != 0) continue; |
| 659 | if (filter_ & ONLY_ALL_CAN_READ) { |
| 660 | if (details.kind() != kAccessor) continue; |
| 661 | Object* accessors = descs->GetValue(i); |
| 662 | if (!accessors->IsAccessorInfo()) continue; |
| 663 | if (!AccessorInfo::cast(accessors)->all_can_read()) continue; |
| 664 | } |
| 665 | Name* key = descs->GetKey(i); |
| 666 | if (key->FilterKey(filter_)) continue; |
| 667 | AddKey(key, DO_NOT_CONVERT); |
| 668 | } |
| 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 | } |
| 676 | } |
| 677 | |
| 678 | // Returns |true| on success, |false| if prototype walking should be stopped, |
| 679 | // |nothing| if an exception was thrown. |
| 680 | Maybe<bool> KeyAccumulator::CollectOwnKeys(Handle<JSReceiver> receiver, |
| 681 | Handle<JSObject> object) { |
| 682 | NextPrototype(); |
| 683 | // Check access rights if required. |
| 684 | if (object->IsAccessCheckNeeded() && |
| 685 | !isolate_->MayAccess(handle(isolate_->context()), object)) { |
| 686 | // The cross-origin spec says that [[Enumerate]] shall return an empty |
| 687 | // iterator when it doesn't have access... |
| 688 | if (type_ == INCLUDE_PROTOS) { |
| 689 | return Just(false); |
| 690 | } |
| 691 | // ...whereas [[OwnPropertyKeys]] shall return whitelisted properties. |
| 692 | DCHECK_EQ(OWN_ONLY, type_); |
| 693 | filter_ = static_cast<PropertyFilter>(filter_ | ONLY_ALL_CAN_READ); |
| 694 | } |
| 695 | |
| 696 | CollectOwnElementIndices(object); |
| 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); |
| 717 | } |
| 718 | |
| 719 | // static |
| 720 | Handle<FixedArray> KeyAccumulator::GetEnumPropertyKeys( |
| 721 | Isolate* isolate, Handle<JSObject> object) { |
| 722 | if (object->HasFastProperties()) { |
| 723 | return GetFastEnumPropertyKeys(isolate, object); |
| 724 | } else if (object->IsJSGlobalObject()) { |
| 725 | Handle<GlobalDictionary> dictionary(object->global_dictionary(), isolate); |
| 726 | int length = dictionary->NumberOfEnumElements(); |
| 727 | if (length == 0) { |
| 728 | return isolate->factory()->empty_fixed_array(); |
| 729 | } |
| 730 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); |
| 731 | dictionary->CopyEnumKeysTo(*storage); |
| 732 | return storage; |
| 733 | } else { |
| 734 | Handle<NameDictionary> dictionary(object->property_dictionary(), isolate); |
| 735 | int length = dictionary->NumberOfEnumElements(); |
| 736 | if (length == 0) { |
| 737 | return isolate->factory()->empty_fixed_array(); |
| 738 | } |
| 739 | Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length); |
| 740 | dictionary->CopyEnumKeysTo(*storage); |
| 741 | return storage; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // ES6 9.5.12 |
| 746 | // Returns |true| on success, |nothing| in case of exception. |
| 747 | Maybe<bool> KeyAccumulator::CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, |
| 748 | Handle<JSProxy> proxy) { |
| 749 | STACK_CHECK(isolate_, Nothing<bool>()); |
| 750 | // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O. |
| 751 | Handle<Object> handler(proxy->handler(), isolate_); |
| 752 | // 2. If handler is null, throw a TypeError exception. |
| 753 | // 3. Assert: Type(handler) is Object. |
| 754 | if (proxy->IsRevoked()) { |
| 755 | isolate_->Throw(*isolate_->factory()->NewTypeError( |
| 756 | MessageTemplate::kProxyRevoked, isolate_->factory()->ownKeys_string())); |
| 757 | return Nothing<bool>(); |
| 758 | } |
| 759 | // 4. Let target be the value of the [[ProxyTarget]] internal slot of O. |
| 760 | Handle<JSReceiver> target(proxy->target(), isolate_); |
| 761 | // 5. Let trap be ? GetMethod(handler, "ownKeys"). |
| 762 | Handle<Object> trap; |
| 763 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 764 | isolate_, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler), |
| 765 | isolate_->factory()->ownKeys_string()), |
| 766 | Nothing<bool>()); |
| 767 | // 6. If trap is undefined, then |
| 768 | if (trap->IsUndefined()) { |
| 769 | // 6a. Return target.[[OwnPropertyKeys]](). |
| 770 | return CollectOwnJSProxyTargetKeys(proxy, target); |
| 771 | } |
| 772 | // 7. Let trapResultArray be Call(trap, handler, «target»). |
| 773 | Handle<Object> trap_result_array; |
| 774 | Handle<Object> args[] = {target}; |
| 775 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 776 | isolate_, trap_result_array, |
| 777 | Execution::Call(isolate_, trap, handler, arraysize(args), args), |
| 778 | Nothing<bool>()); |
| 779 | // 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, |
| 780 | // «String, Symbol»). |
| 781 | Handle<FixedArray> trap_result; |
| 782 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 783 | isolate_, trap_result, |
| 784 | Object::CreateListFromArrayLike(isolate_, trap_result_array, |
| 785 | ElementTypes::kStringAndSymbol), |
| 786 | Nothing<bool>()); |
| 787 | // 9. Let extensibleTarget be ? IsExtensible(target). |
| 788 | Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target); |
| 789 | MAYBE_RETURN(maybe_extensible, Nothing<bool>()); |
| 790 | bool extensible_target = maybe_extensible.FromJust(); |
| 791 | // 10. Let targetKeys be ? target.[[OwnPropertyKeys]](). |
| 792 | Handle<FixedArray> target_keys; |
| 793 | ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate_, target_keys, |
| 794 | JSReceiver::OwnPropertyKeys(target), |
| 795 | Nothing<bool>()); |
| 796 | // 11. (Assert) |
| 797 | // 12. Let targetConfigurableKeys be an empty List. |
| 798 | // To save memory, we're re-using target_keys and will modify it in-place. |
| 799 | Handle<FixedArray> target_configurable_keys = target_keys; |
| 800 | // 13. Let targetNonconfigurableKeys be an empty List. |
| 801 | Handle<FixedArray> target_nonconfigurable_keys = |
| 802 | isolate_->factory()->NewFixedArray(target_keys->length()); |
| 803 | int nonconfigurable_keys_length = 0; |
| 804 | // 14. Repeat, for each element key of targetKeys: |
| 805 | for (int i = 0; i < target_keys->length(); ++i) { |
| 806 | // 14a. Let desc be ? target.[[GetOwnProperty]](key). |
| 807 | PropertyDescriptor desc; |
| 808 | Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor( |
| 809 | isolate_, target, handle(target_keys->get(i), isolate_), &desc); |
| 810 | MAYBE_RETURN(found, Nothing<bool>()); |
| 811 | // 14b. If desc is not undefined and desc.[[Configurable]] is false, then |
| 812 | if (found.FromJust() && !desc.configurable()) { |
| 813 | // 14b i. Append key as an element of targetNonconfigurableKeys. |
| 814 | target_nonconfigurable_keys->set(nonconfigurable_keys_length, |
| 815 | target_keys->get(i)); |
| 816 | nonconfigurable_keys_length++; |
| 817 | // The key was moved, null it out in the original list. |
| 818 | target_keys->set(i, Smi::FromInt(0)); |
| 819 | } else { |
| 820 | // 14c. Else, |
| 821 | // 14c i. Append key as an element of targetConfigurableKeys. |
| 822 | // (No-op, just keep it in |target_keys|.) |
| 823 | } |
| 824 | } |
| 825 | NextPrototype(); // Prepare for accumulating keys. |
| 826 | // 15. If extensibleTarget is true and targetNonconfigurableKeys is empty, |
| 827 | // then: |
| 828 | if (extensible_target && nonconfigurable_keys_length == 0) { |
| 829 | // 15a. Return trapResult. |
| 830 | return AddKeysFromJSProxy(proxy, trap_result); |
| 831 | } |
| 832 | // 16. Let uncheckedResultKeys be a new List which is a copy of trapResult. |
| 833 | Zone set_zone(isolate_->allocator()); |
| 834 | const int kPresent = 1; |
| 835 | const int kGone = 0; |
| 836 | IdentityMap<int> unchecked_result_keys(isolate_->heap(), &set_zone); |
| 837 | int unchecked_result_keys_size = 0; |
| 838 | for (int i = 0; i < trap_result->length(); ++i) { |
| 839 | DCHECK(trap_result->get(i)->IsUniqueName()); |
| 840 | Object* key = trap_result->get(i); |
| 841 | int* entry = unchecked_result_keys.Get(key); |
| 842 | if (*entry != kPresent) { |
| 843 | *entry = kPresent; |
| 844 | unchecked_result_keys_size++; |
| 845 | } |
| 846 | } |
| 847 | // 17. Repeat, for each key that is an element of targetNonconfigurableKeys: |
| 848 | for (int i = 0; i < nonconfigurable_keys_length; ++i) { |
| 849 | Object* key = target_nonconfigurable_keys->get(i); |
| 850 | // 17a. If key is not an element of uncheckedResultKeys, throw a |
| 851 | // TypeError exception. |
| 852 | int* found = unchecked_result_keys.Find(key); |
| 853 | if (found == nullptr || *found == kGone) { |
| 854 | isolate_->Throw(*isolate_->factory()->NewTypeError( |
| 855 | MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate_))); |
| 856 | return Nothing<bool>(); |
| 857 | } |
| 858 | // 17b. Remove key from uncheckedResultKeys. |
| 859 | *found = kGone; |
| 860 | unchecked_result_keys_size--; |
| 861 | } |
| 862 | // 18. If extensibleTarget is true, return trapResult. |
| 863 | if (extensible_target) { |
| 864 | return AddKeysFromJSProxy(proxy, trap_result); |
| 865 | } |
| 866 | // 19. Repeat, for each key that is an element of targetConfigurableKeys: |
| 867 | for (int i = 0; i < target_configurable_keys->length(); ++i) { |
| 868 | Object* key = target_configurable_keys->get(i); |
| 869 | if (key->IsSmi()) continue; // Zapped entry, was nonconfigurable. |
| 870 | // 19a. If key is not an element of uncheckedResultKeys, throw a |
| 871 | // TypeError exception. |
| 872 | int* found = unchecked_result_keys.Find(key); |
| 873 | if (found == nullptr || *found == kGone) { |
| 874 | isolate_->Throw(*isolate_->factory()->NewTypeError( |
| 875 | MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate_))); |
| 876 | return Nothing<bool>(); |
| 877 | } |
| 878 | // 19b. Remove key from uncheckedResultKeys. |
| 879 | *found = kGone; |
| 880 | unchecked_result_keys_size--; |
| 881 | } |
| 882 | // 20. If uncheckedResultKeys is not empty, throw a TypeError exception. |
| 883 | if (unchecked_result_keys_size != 0) { |
| 884 | DCHECK_GT(unchecked_result_keys_size, 0); |
| 885 | isolate_->Throw(*isolate_->factory()->NewTypeError( |
| 886 | MessageTemplate::kProxyOwnKeysNonExtensible)); |
| 887 | return Nothing<bool>(); |
| 888 | } |
| 889 | // 21. Return trapResult. |
| 890 | return AddKeysFromJSProxy(proxy, trap_result); |
| 891 | } |
| 892 | |
| 893 | Maybe<bool> KeyAccumulator::CollectOwnJSProxyTargetKeys( |
| 894 | Handle<JSProxy> proxy, Handle<JSReceiver> target) { |
| 895 | // TODO(cbruni): avoid creating another KeyAccumulator |
| 896 | Handle<FixedArray> keys; |
| 897 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 898 | isolate_, keys, JSReceiver::OwnPropertyKeys(target), Nothing<bool>()); |
| 899 | NextPrototype(); // Prepare for accumulating keys. |
| 900 | bool prev_filter_proxy_keys_ = filter_proxy_keys_; |
| 901 | filter_proxy_keys_ = false; |
| 902 | Maybe<bool> result = AddKeysFromJSProxy(proxy, keys); |
| 903 | filter_proxy_keys_ = prev_filter_proxy_keys_; |
| 904 | return result; |
| 905 | } |
| 906 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 907 | } // namespace internal |
| 908 | } // namespace v8 |