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 | |
| 7 | #include "src/elements.h" |
| 8 | #include "src/factory.h" |
| 9 | #include "src/isolate-inl.h" |
| 10 | #include "src/objects-inl.h" |
| 11 | #include "src/property-descriptor.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 12 | #include "src/prototype.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | KeyAccumulator::~KeyAccumulator() { |
| 18 | for (size_t i = 0; i < elements_.size(); i++) { |
| 19 | delete elements_[i]; |
| 20 | } |
| 21 | } |
| 22 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 30 | if (type_ == OWN_ONLY && !ownProxyKeys_.is_null()) { |
| 31 | return ownProxyKeys_; |
| 32 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | // Assemble the result array by first adding the element keys and then the |
| 34 | // property keys. We use the total number of String + Symbol keys per level in |
| 35 | // |level_lengths_| and the available element keys in the corresponding bucket |
| 36 | // in |elements_| to deduce the number of keys to take from the |
| 37 | // |string_properties_| and |symbol_properties_| set. |
| 38 | Handle<FixedArray> result = isolate_->factory()->NewFixedArray(length_); |
| 39 | int insertion_index = 0; |
| 40 | int string_properties_index = 0; |
| 41 | int symbol_properties_index = 0; |
| 42 | // String and Symbol lengths always come in pairs: |
| 43 | size_t max_level = level_lengths_.size() / 2; |
| 44 | for (size_t level = 0; level < max_level; level++) { |
| 45 | int num_string_properties = level_lengths_[level * 2]; |
| 46 | int num_symbol_properties = level_lengths_[level * 2 + 1]; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 47 | int num_elements = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 48 | if (num_string_properties < 0) { |
| 49 | // If the |num_string_properties| is negative, the current level contains |
| 50 | // properties from a proxy, hence we skip the integer keys in |elements_| |
| 51 | // since proxies define the complete ordering. |
| 52 | num_string_properties = -num_string_properties; |
| 53 | } else if (level < elements_.size()) { |
| 54 | // Add the element indices for this prototype level. |
| 55 | std::vector<uint32_t>* elements = elements_[level]; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 56 | num_elements = static_cast<int>(elements->size()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 57 | for (int i = 0; i < num_elements; i++) { |
| 58 | Handle<Object> key; |
| 59 | if (convert == KEEP_NUMBERS) { |
| 60 | key = isolate_->factory()->NewNumberFromUint(elements->at(i)); |
| 61 | } else { |
| 62 | key = isolate_->factory()->Uint32ToString(elements->at(i)); |
| 63 | } |
| 64 | result->set(insertion_index, *key); |
| 65 | insertion_index++; |
| 66 | } |
| 67 | } |
| 68 | // Add the string property keys for this prototype level. |
| 69 | for (int i = 0; i < num_string_properties; i++) { |
| 70 | Object* key = string_properties_->KeyAt(string_properties_index); |
| 71 | result->set(insertion_index, key); |
| 72 | insertion_index++; |
| 73 | string_properties_index++; |
| 74 | } |
| 75 | // Add the symbol property keys for this prototype level. |
| 76 | for (int i = 0; i < num_symbol_properties; i++) { |
| 77 | Object* key = symbol_properties_->KeyAt(symbol_properties_index); |
| 78 | result->set(insertion_index, key); |
| 79 | insertion_index++; |
| 80 | symbol_properties_index++; |
| 81 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 82 | if (FLAG_trace_for_in_enumerate) { |
| 83 | PrintF("| strings=%d symbols=%d elements=%i ", num_string_properties, |
| 84 | num_symbol_properties, num_elements); |
| 85 | } |
| 86 | } |
| 87 | if (FLAG_trace_for_in_enumerate) { |
| 88 | PrintF("|| prototypes=%zu ||\n", max_level); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | DCHECK_EQ(insertion_index, length_); |
| 92 | return result; |
| 93 | } |
| 94 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | namespace { |
| 96 | |
| 97 | bool AccumulatorHasKey(std::vector<uint32_t>* sub_elements, uint32_t key) { |
| 98 | return std::binary_search(sub_elements->begin(), sub_elements->end(), key); |
| 99 | } |
| 100 | |
| 101 | } // namespace |
| 102 | |
| 103 | bool KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) { |
| 104 | return AddKey(handle(key, isolate_), convert); |
| 105 | } |
| 106 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 107 | bool KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) { |
| 108 | if (key->IsSymbol()) { |
| 109 | if (filter_ & SKIP_SYMBOLS) return false; |
| 110 | if (Handle<Symbol>::cast(key)->is_private()) return false; |
| 111 | return AddSymbolKey(key); |
| 112 | } |
| 113 | if (filter_ & SKIP_STRINGS) return false; |
| 114 | // Make sure we do not add keys to a proxy-level (see AddKeysFromProxy). |
| 115 | DCHECK_LE(0, level_string_length_); |
| 116 | // In some cases (e.g. proxies) we might get in String-converted ints which |
| 117 | // should be added to the elements list instead of the properties. For |
| 118 | // proxies we have to convert as well but also respect the original order. |
| 119 | // Therefore we add a converted key to both sides |
| 120 | if (convert == CONVERT_TO_ARRAY_INDEX || convert == PROXY_MAGIC) { |
| 121 | uint32_t index = 0; |
| 122 | int prev_length = length_; |
| 123 | int prev_proto = level_string_length_; |
| 124 | if ((key->IsString() && Handle<String>::cast(key)->AsArrayIndex(&index)) || |
| 125 | key->ToArrayIndex(&index)) { |
| 126 | bool key_was_added = AddIntegerKey(index); |
| 127 | if (convert == CONVERT_TO_ARRAY_INDEX) return key_was_added; |
| 128 | if (convert == PROXY_MAGIC) { |
| 129 | // If we had an array index (number) and it wasn't added, the key |
| 130 | // already existed before, hence we cannot add it to the properties |
| 131 | // keys as it would lead to duplicate entries. |
| 132 | if (!key_was_added) { |
| 133 | return false; |
| 134 | } |
| 135 | length_ = prev_length; |
| 136 | level_string_length_ = prev_proto; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | return AddStringKey(key, convert); |
| 141 | } |
| 142 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 143 | bool KeyAccumulator::AddKey(uint32_t key) { return AddIntegerKey(key); } |
| 144 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | bool KeyAccumulator::AddIntegerKey(uint32_t key) { |
| 146 | // Make sure we do not add keys to a proxy-level (see AddKeysFromProxy). |
| 147 | // We mark proxy-levels with a negative length |
| 148 | DCHECK_LE(0, level_string_length_); |
| 149 | // Binary search over all but the last level. The last one might not be |
| 150 | // sorted yet. |
| 151 | for (size_t i = 1; i < elements_.size(); i++) { |
| 152 | if (AccumulatorHasKey(elements_[i - 1], key)) return false; |
| 153 | } |
| 154 | elements_.back()->push_back(key); |
| 155 | length_++; |
| 156 | return true; |
| 157 | } |
| 158 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 159 | bool KeyAccumulator::AddStringKey(Handle<Object> key, |
| 160 | AddKeyConversion convert) { |
| 161 | if (string_properties_.is_null()) { |
| 162 | string_properties_ = OrderedHashSet::Allocate(isolate_, 16); |
| 163 | } |
| 164 | // TODO(cbruni): remove this conversion once we throw the correct TypeError |
| 165 | // for non-string/symbol elements returned by proxies |
| 166 | if (convert == PROXY_MAGIC && key->IsNumber()) { |
| 167 | key = isolate_->factory()->NumberToString(key); |
| 168 | } |
| 169 | int prev_size = string_properties_->NumberOfElements(); |
| 170 | string_properties_ = OrderedHashSet::Add(string_properties_, key); |
| 171 | if (prev_size < string_properties_->NumberOfElements()) { |
| 172 | length_++; |
| 173 | level_string_length_++; |
| 174 | return true; |
| 175 | } else { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 180 | bool KeyAccumulator::AddSymbolKey(Handle<Object> key) { |
| 181 | if (symbol_properties_.is_null()) { |
| 182 | symbol_properties_ = OrderedHashSet::Allocate(isolate_, 16); |
| 183 | } |
| 184 | int prev_size = symbol_properties_->NumberOfElements(); |
| 185 | symbol_properties_ = OrderedHashSet::Add(symbol_properties_, key); |
| 186 | if (prev_size < symbol_properties_->NumberOfElements()) { |
| 187 | length_++; |
| 188 | level_symbol_length_++; |
| 189 | return true; |
| 190 | } else { |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 195 | void KeyAccumulator::AddKeys(Handle<FixedArray> array, |
| 196 | AddKeyConversion convert) { |
| 197 | int add_length = array->length(); |
| 198 | if (add_length == 0) return; |
| 199 | for (int i = 0; i < add_length; i++) { |
| 200 | Handle<Object> current(array->get(i), isolate_); |
| 201 | AddKey(current, convert); |
| 202 | } |
| 203 | } |
| 204 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 205 | void KeyAccumulator::AddKeys(Handle<JSObject> array_like, |
| 206 | AddKeyConversion convert) { |
| 207 | DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements()); |
| 208 | ElementsAccessor* accessor = array_like->GetElementsAccessor(); |
| 209 | accessor->AddElementsToKeyAccumulator(array_like, this, convert); |
| 210 | } |
| 211 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 212 | void KeyAccumulator::AddKeysFromProxy(Handle<JSObject> array_like) { |
| 213 | // Proxies define a complete list of keys with no distinction of |
| 214 | // elements and properties, which breaks the normal assumption for the |
| 215 | // KeyAccumulator. |
| 216 | AddKeys(array_like, PROXY_MAGIC); |
| 217 | // Invert the current length to indicate a present proxy, so we can ignore |
| 218 | // element keys for this level. Otherwise we would not fully respect the order |
| 219 | // given by the proxy. |
| 220 | level_string_length_ = -level_string_length_; |
| 221 | } |
| 222 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 223 | MaybeHandle<FixedArray> FilterProxyKeys(Isolate* isolate, Handle<JSProxy> owner, |
| 224 | Handle<FixedArray> keys, |
| 225 | PropertyFilter filter) { |
| 226 | if (filter == ALL_PROPERTIES) { |
| 227 | // Nothing to do. |
| 228 | return keys; |
| 229 | } |
| 230 | int store_position = 0; |
| 231 | for (int i = 0; i < keys->length(); ++i) { |
| 232 | Handle<Name> key(Name::cast(keys->get(i)), isolate); |
| 233 | if (key->FilterKey(filter)) continue; // Skip this key. |
| 234 | if (filter & ONLY_ENUMERABLE) { |
| 235 | PropertyDescriptor desc; |
| 236 | Maybe<bool> found = |
| 237 | JSProxy::GetOwnPropertyDescriptor(isolate, owner, key, &desc); |
| 238 | MAYBE_RETURN(found, MaybeHandle<FixedArray>()); |
| 239 | if (!found.FromJust() || !desc.enumerable()) continue; // Skip this key. |
| 240 | } |
| 241 | // Keep this key. |
| 242 | if (store_position != i) { |
| 243 | keys->set(store_position, *key); |
| 244 | } |
| 245 | store_position++; |
| 246 | } |
| 247 | if (store_position == 0) return isolate->factory()->empty_fixed_array(); |
| 248 | keys->Shrink(store_position); |
| 249 | return keys; |
| 250 | } |
| 251 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 252 | // Returns "nothing" in case of exception, "true" on success. |
| 253 | Maybe<bool> KeyAccumulator::AddKeysFromProxy(Handle<JSProxy> proxy, |
| 254 | Handle<FixedArray> keys) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 255 | if (filter_proxy_keys_) { |
| 256 | ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
| 257 | isolate_, keys, FilterProxyKeys(isolate_, proxy, keys, filter_), |
| 258 | Nothing<bool>()); |
| 259 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 260 | // Proxies define a complete list of keys with no distinction of |
| 261 | // elements and properties, which breaks the normal assumption for the |
| 262 | // KeyAccumulator. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 263 | if (type_ == OWN_ONLY) { |
| 264 | ownProxyKeys_ = keys; |
| 265 | level_string_length_ = keys->length(); |
| 266 | length_ = level_string_length_; |
| 267 | } else { |
| 268 | AddKeys(keys, PROXY_MAGIC); |
| 269 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 270 | // Invert the current length to indicate a present proxy, so we can ignore |
| 271 | // element keys for this level. Otherwise we would not fully respect the order |
| 272 | // given by the proxy. |
| 273 | level_string_length_ = -level_string_length_; |
| 274 | return Just(true); |
| 275 | } |
| 276 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 277 | void KeyAccumulator::AddElementKeysFromInterceptor( |
| 278 | Handle<JSObject> array_like) { |
| 279 | AddKeys(array_like, CONVERT_TO_ARRAY_INDEX); |
| 280 | // The interceptor might introduce duplicates for the current level, since |
| 281 | // these keys get added after the objects's normal element keys. |
| 282 | SortCurrentElementsListRemoveDuplicates(); |
| 283 | } |
| 284 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 285 | void KeyAccumulator::SortCurrentElementsListRemoveDuplicates() { |
| 286 | // Sort and remove duplicates from the current elements level and adjust. |
| 287 | // the lengths accordingly. |
| 288 | auto last_level = elements_.back(); |
| 289 | size_t nof_removed_keys = last_level->size(); |
| 290 | std::sort(last_level->begin(), last_level->end()); |
| 291 | last_level->erase(std::unique(last_level->begin(), last_level->end()), |
| 292 | last_level->end()); |
| 293 | // Adjust total length by the number of removed duplicates. |
| 294 | nof_removed_keys -= last_level->size(); |
| 295 | length_ -= static_cast<int>(nof_removed_keys); |
| 296 | } |
| 297 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 298 | void KeyAccumulator::SortCurrentElementsList() { |
| 299 | if (elements_.empty()) return; |
| 300 | auto element_keys = elements_.back(); |
| 301 | std::sort(element_keys->begin(), element_keys->end()); |
| 302 | } |
| 303 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 304 | void KeyAccumulator::NextPrototype() { |
| 305 | // Store the protoLength on the first call of this method. |
| 306 | if (!elements_.empty()) { |
| 307 | level_lengths_.push_back(level_string_length_); |
| 308 | level_lengths_.push_back(level_symbol_length_); |
| 309 | } |
| 310 | elements_.push_back(new std::vector<uint32_t>()); |
| 311 | level_string_length_ = 0; |
| 312 | level_symbol_length_ = 0; |
| 313 | } |
| 314 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 315 | namespace { |
| 316 | |
| 317 | void TrySettingEmptyEnumCache(JSReceiver* object) { |
| 318 | Map* map = object->map(); |
| 319 | DCHECK_EQ(kInvalidEnumCacheSentinel, map->EnumLength()); |
| 320 | if (!map->OnlyHasSimpleProperties()) return; |
| 321 | if (map->IsJSProxyMap()) return; |
| 322 | if (map->NumberOfOwnDescriptors() > 0) { |
| 323 | int number_of_enumerable_own_properties = |
| 324 | map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS); |
| 325 | if (number_of_enumerable_own_properties > 0) return; |
| 326 | } |
| 327 | DCHECK(object->IsJSObject()); |
| 328 | map->SetEnumLength(0); |
| 329 | } |
| 330 | |
| 331 | bool CheckAndInitalizeSimpleEnumCache(JSReceiver* object) { |
| 332 | if (object->map()->EnumLength() == kInvalidEnumCacheSentinel) { |
| 333 | TrySettingEmptyEnumCache(object); |
| 334 | } |
| 335 | if (object->map()->EnumLength() != 0) return false; |
| 336 | DCHECK(object->IsJSObject()); |
| 337 | return !JSObject::cast(object)->HasEnumerableElements(); |
| 338 | } |
| 339 | } // namespace |
| 340 | |
| 341 | void FastKeyAccumulator::Prepare() { |
| 342 | DisallowHeapAllocation no_gc; |
| 343 | // Directly go for the fast path for OWN_ONLY keys. |
| 344 | if (type_ == OWN_ONLY) return; |
| 345 | // Fully walk the prototype chain and find the last prototype with keys. |
| 346 | is_receiver_simple_enum_ = false; |
| 347 | has_empty_prototype_ = true; |
| 348 | JSReceiver* first_non_empty_prototype; |
| 349 | for (PrototypeIterator iter(isolate_, *receiver_); !iter.IsAtEnd(); |
| 350 | iter.Advance()) { |
| 351 | JSReceiver* current = iter.GetCurrent<JSReceiver>(); |
| 352 | if (CheckAndInitalizeSimpleEnumCache(current)) continue; |
| 353 | has_empty_prototype_ = false; |
| 354 | first_non_empty_prototype = current; |
| 355 | // TODO(cbruni): use the first non-empty prototype. |
| 356 | USE(first_non_empty_prototype); |
| 357 | return; |
| 358 | } |
| 359 | DCHECK(has_empty_prototype_); |
| 360 | is_receiver_simple_enum_ = |
| 361 | receiver_->map()->EnumLength() != kInvalidEnumCacheSentinel && |
| 362 | !JSObject::cast(*receiver_)->HasEnumerableElements(); |
| 363 | } |
| 364 | |
| 365 | namespace { |
| 366 | |
| 367 | template <bool fast_properties> |
| 368 | Handle<FixedArray> GetOwnKeysWithElements(Isolate* isolate, |
| 369 | Handle<JSObject> object, |
| 370 | GetKeysConversion convert) { |
| 371 | Handle<FixedArray> keys; |
| 372 | ElementsAccessor* accessor = object->GetElementsAccessor(); |
| 373 | if (fast_properties) { |
| 374 | keys = JSObject::GetFastEnumPropertyKeys(isolate, object); |
| 375 | } else { |
| 376 | // TODO(cbruni): preallocate big enough array to also hold elements. |
| 377 | keys = JSObject::GetEnumPropertyKeys(object); |
| 378 | } |
| 379 | Handle<FixedArray> result = |
| 380 | accessor->PrependElementIndices(object, keys, convert, ONLY_ENUMERABLE); |
| 381 | |
| 382 | if (FLAG_trace_for_in_enumerate) { |
| 383 | PrintF("| strings=%d symbols=0 elements=%u || prototypes>=1 ||\n", |
| 384 | keys->length(), result->length() - keys->length()); |
| 385 | } |
| 386 | return result; |
| 387 | } |
| 388 | |
| 389 | MaybeHandle<FixedArray> GetOwnKeysWithUninitializedEnumCache( |
| 390 | Isolate* isolate, Handle<JSObject> object) { |
| 391 | // Uninitalized enum cache |
| 392 | Map* map = object->map(); |
| 393 | if (object->elements() != isolate->heap()->empty_fixed_array() || |
| 394 | object->elements() != isolate->heap()->empty_slow_element_dictionary()) { |
| 395 | // Assume that there are elements. |
| 396 | return MaybeHandle<FixedArray>(); |
| 397 | } |
| 398 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 399 | if (number_of_own_descriptors == 0) { |
| 400 | map->SetEnumLength(0); |
| 401 | return isolate->factory()->empty_fixed_array(); |
| 402 | } |
| 403 | // We have no elements but possibly enumerable property keys, hence we can |
| 404 | // directly initialize the enum cache. |
| 405 | return JSObject::GetFastEnumPropertyKeys(isolate, object); |
| 406 | } |
| 407 | |
| 408 | bool OnlyHasSimpleProperties(Map* map) { |
| 409 | return map->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER; |
| 410 | } |
| 411 | |
| 412 | } // namespace |
| 413 | |
| 414 | MaybeHandle<FixedArray> FastKeyAccumulator::GetKeys(GetKeysConversion convert) { |
| 415 | Handle<FixedArray> keys; |
| 416 | if (GetKeysFast(convert).ToHandle(&keys)) { |
| 417 | return keys; |
| 418 | } |
| 419 | return GetKeysSlow(convert); |
| 420 | } |
| 421 | |
| 422 | MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysFast( |
| 423 | GetKeysConversion convert) { |
| 424 | bool own_only = has_empty_prototype_ || type_ == OWN_ONLY; |
| 425 | Map* map = receiver_->map(); |
| 426 | if (!own_only || !OnlyHasSimpleProperties(map)) { |
| 427 | return MaybeHandle<FixedArray>(); |
| 428 | } |
| 429 | |
| 430 | // From this point on we are certiain to only collect own keys. |
| 431 | DCHECK(receiver_->IsJSObject()); |
| 432 | Handle<JSObject> object = Handle<JSObject>::cast(receiver_); |
| 433 | |
| 434 | // Do not try to use the enum-cache for dict-mode objects. |
| 435 | if (map->is_dictionary_map()) { |
| 436 | return GetOwnKeysWithElements<false>(isolate_, object, convert); |
| 437 | } |
| 438 | int enum_length = receiver_->map()->EnumLength(); |
| 439 | if (enum_length == kInvalidEnumCacheSentinel) { |
| 440 | Handle<FixedArray> keys; |
| 441 | // Try initializing the enum cache and return own properties. |
| 442 | if (GetOwnKeysWithUninitializedEnumCache(isolate_, object) |
| 443 | .ToHandle(&keys)) { |
| 444 | if (FLAG_trace_for_in_enumerate) { |
| 445 | PrintF("| strings=%d symbols=0 elements=0 || prototypes>=1 ||\n", |
| 446 | keys->length()); |
| 447 | } |
| 448 | is_receiver_simple_enum_ = |
| 449 | object->map()->EnumLength() != kInvalidEnumCacheSentinel; |
| 450 | return keys; |
| 451 | } |
| 452 | } |
| 453 | // The properties-only case failed because there were probably elements on the |
| 454 | // receiver. |
| 455 | return GetOwnKeysWithElements<true>(isolate_, object, convert); |
| 456 | } |
| 457 | |
| 458 | MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysSlow( |
| 459 | GetKeysConversion convert) { |
| 460 | return JSReceiver::GetKeys(receiver_, type_, ENUMERABLE_STRINGS, KEEP_NUMBERS, |
| 461 | filter_proxy_keys_); |
| 462 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 463 | |
| 464 | } // namespace internal |
| 465 | } // namespace v8 |