Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 34 | KeyAccumulator(Isolate* isolate, KeyCollectionType type, |
| 35 | PropertyFilter filter) |
| 36 | : isolate_(isolate), type_(type), filter_(filter) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | ~KeyAccumulator(); |
| 38 | |
| 39 | bool AddKey(uint32_t key); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 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); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 63 | KeyCollectionType type_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 76 | Handle<FixedArray> ownProxyKeys_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
| 90 | } // namespace internal |
| 91 | } // namespace v8 |
| 92 | |
| 93 | |
| 94 | #endif // V8_KEY_ACCUMULATOR_H_ |