| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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 #ifndef V8_KEY_ACCUMULATOR_H_ | |
| 6 #define V8_KEY_ACCUMULATOR_H_ | |
| 7 | |
| 8 #include "src/isolate.h" | |
| 9 #include "src/objects.h" | |
| 10 | |
| 11 namespace v8 { | |
| 12 namespace internal { | |
| 13 | |
| 14 enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX, PROXY_MAGIC }; | |
| 15 | |
| 16 // This is a helper class for JSReceiver::GetKeys which collects and sorts keys. | |
| 17 // GetKeys needs to sort keys per prototype level, first showing the integer | |
| 18 // indices from elements then the strings from the properties. However, this | |
| 19 // does not apply to proxies which are in full control of how the keys are | |
| 20 // sorted. | |
| 21 // | |
| 22 // For performance reasons the KeyAccumulator internally separates integer keys | |
| 23 // in |elements_| into sorted lists per prototype level. String keys are | |
| 24 // collected in |string_properties_|, a single OrderedHashSet (similar for | |
| 25 // Symbols in |symbol_properties_|. To separate the keys per level later when | |
| 26 // assembling the final list, |levelLengths_| keeps track of the number of | |
| 27 // String and Symbol keys per level. | |
| 28 // | |
| 29 // Only unique keys are kept by the KeyAccumulator, strings are stored in a | |
| 30 // HashSet for inexpensive lookups. Integer keys are kept in sorted lists which | |
| 31 // are more compact and allow for reasonably fast includes check. | |
| 32 class KeyAccumulator final BASE_EMBEDDED { | |
| 33 public: | |
| 34 KeyAccumulator(Isolate* isolate, KeyCollectionType type, | |
| 35 PropertyFilter filter) | |
| 36 : isolate_(isolate), type_(type), filter_(filter) {} | |
| 37 ~KeyAccumulator(); | |
| 38 | |
| 39 bool AddKey(uint32_t key); | |
| 40 bool AddKey(Object* key, AddKeyConversion convert); | |
| 41 bool AddKey(Handle<Object> key, AddKeyConversion convert); | |
| 42 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); | |
| 43 void AddKeys(Handle<JSObject> array, AddKeyConversion convert); | |
| 44 void AddKeysFromProxy(Handle<JSObject> array); | |
| 45 Maybe<bool> AddKeysFromProxy(Handle<JSProxy> proxy, Handle<FixedArray> keys); | |
| 46 void AddElementKeysFromInterceptor(Handle<JSObject> array); | |
| 47 // Jump to the next level, pushing the current |levelLength_| to | |
| 48 // |levelLengths_| and adding a new list to |elements_|. | |
| 49 void NextPrototype(); | |
| 50 // Sort the integer indices in the last list in |elements_| | |
| 51 void SortCurrentElementsList(); | |
| 52 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | |
| 53 int length() { return length_; } | |
| 54 Isolate* isolate() { return isolate_; } | |
| 55 | |
| 56 private: | |
| 57 bool AddIntegerKey(uint32_t key); | |
| 58 bool AddStringKey(Handle<Object> key, AddKeyConversion convert); | |
| 59 bool AddSymbolKey(Handle<Object> array); | |
| 60 void SortCurrentElementsListRemoveDuplicates(); | |
| 61 | |
| 62 Isolate* isolate_; | |
| 63 KeyCollectionType type_; | |
| 64 PropertyFilter filter_; | |
| 65 // |elements_| contains the sorted element keys (indices) per level. | |
| 66 std::vector<std::vector<uint32_t>*> elements_; | |
| 67 // |protoLengths_| contains the total number of keys (elements + properties) | |
| 68 // per level. Negative values mark counts for a level with keys from a proxy. | |
| 69 std::vector<int> level_lengths_; | |
| 70 // |string_properties_| contains the unique String property keys for all | |
| 71 // levels in insertion order per level. | |
| 72 Handle<OrderedHashSet> string_properties_; | |
| 73 // |symbol_properties_| contains the unique Symbol property keys for all | |
| 74 // levels in insertion order per level. | |
| 75 Handle<OrderedHashSet> symbol_properties_; | |
| 76 Handle<FixedArray> ownProxyKeys_; | |
| 77 // |length_| keeps track of the total number of all element and property keys. | |
| 78 int length_ = 0; | |
| 79 // |levelLength_| keeps track of the number of String keys in the current | |
| 80 // level. | |
| 81 int level_string_length_ = 0; | |
| 82 // |levelSymbolLength_| keeps track of the number of Symbol keys in the | |
| 83 // current level. | |
| 84 int level_symbol_length_ = 0; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); | |
| 87 }; | |
| 88 | |
| 89 // The FastKeyAccumulator handles the cases where there are no elements on the | |
| 90 // prototype chain and forwords the complex/slow cases to the normal | |
| 91 // KeyAccumulator. | |
| 92 class FastKeyAccumulator { | |
| 93 public: | |
| 94 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, | |
| 95 KeyCollectionType type, PropertyFilter filter) | |
| 96 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) { | |
| 97 Prepare(); | |
| 98 // TODO(cbruni): pass filter_ directly to the KeyAccumulator. | |
| 99 USE(filter_); | |
| 100 } | |
| 101 | |
| 102 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } | |
| 103 bool has_empty_prototype() { return has_empty_prototype_; } | |
| 104 | |
| 105 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | |
| 106 | |
| 107 private: | |
| 108 void Prepare(); | |
| 109 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); | |
| 110 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); | |
| 111 | |
| 112 Isolate* isolate_; | |
| 113 Handle<JSReceiver> receiver_; | |
| 114 KeyCollectionType type_; | |
| 115 PropertyFilter filter_; | |
| 116 bool is_receiver_simple_enum_ = false; | |
| 117 bool has_empty_prototype_ = false; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); | |
| 120 }; | |
| 121 | |
| 122 } // namespace internal | |
| 123 } // namespace v8 | |
| 124 | |
| 125 #endif // V8_KEY_ACCUMULATOR_H_ | |
| OLD | NEW |