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 explicit KeyAccumulator(Isolate* isolate, |
| 35 KeyFilter filter = KeyFilter::SKIP_SYMBOLS) |
| 36 : isolate_(isolate), filter_(filter) {} |
| 37 ~KeyAccumulator(); |
| 38 |
| 39 bool AddKey(uint32_t key); |
| 40 bool AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT); |
| 41 bool AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT); |
| 42 void AddKeys(Handle<FixedArray> array, |
| 43 AddKeyConversion convert = DO_NOT_CONVERT); |
| 44 void AddKeys(Handle<JSObject> array, |
| 45 AddKeyConversion convert = DO_NOT_CONVERT); |
| 46 void AddKeysFromProxy(Handle<JSObject> array); |
| 47 void AddElementKeysFromInterceptor(Handle<JSObject> array); |
| 48 // Jump to the next level, pushing the current |levelLength_| to |
| 49 // |levelLengths_| and adding a new list to |elements_|. |
| 50 void NextPrototype(); |
| 51 // Sort the integer indices in the last list in |elements_| |
| 52 void SortCurrentElementsList(); |
| 53 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); |
| 54 int length() { return length_; } |
| 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 KeyFilter filter_; |
| 64 // |elements_| contains the sorted element keys (indices) per level. |
| 65 std::vector<std::vector<uint32_t>*> elements_; |
| 66 // |protoLengths_| contains the total number of keys (elements + properties) |
| 67 // per level. Negative values mark counts for a level with keys from a proxy. |
| 68 std::vector<int> level_lengths_; |
| 69 // |string_properties_| contains the unique String property keys for all |
| 70 // levels in insertion order per level. |
| 71 Handle<OrderedHashSet> string_properties_; |
| 72 // |symbol_properties_| contains the unique Symbol property keys for all |
| 73 // levels in insertion order per level. |
| 74 Handle<OrderedHashSet> symbol_properties_; |
| 75 // |length_| keeps track of the total number of all element and property keys. |
| 76 int length_ = 0; |
| 77 // |levelLength_| keeps track of the number of String keys in the current |
| 78 // level. |
| 79 int level_string_length_ = 0; |
| 80 // |levelSymbolLength_| keeps track of the number of Symbol keys in the |
| 81 // current level. |
| 82 int level_symbol_length_ = 0; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); |
| 85 }; |
| 86 |
| 87 |
| 88 } // namespace internal |
| 89 } // namespace v8 |
| 90 |
| 91 |
| 92 #endif // V8_KEY_ACCUMULATOR_H_ |
OLD | NEW |