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