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 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 5 | #ifndef V8_KEYS_H_ |
| 6 | #define V8_KEYS_H_ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 39 | static MaybeHandle<FixedArray> GetKeys(Handle<JSReceiver> object, |
| 40 | KeyCollectionType type, |
| 41 | PropertyFilter filter, |
| 42 | GetKeysConversion keys_conversion, |
| 43 | bool filter_proxy_keys); |
| 44 | Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); |
| 45 | Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, |
| 46 | Handle<JSReceiver> object); |
| 47 | void CollectOwnElementIndices(Handle<JSObject> object); |
| 48 | void CollectOwnPropertyNames(Handle<JSObject> object); |
| 49 | |
| 50 | static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate, |
| 51 | Handle<JSObject> object); |
| 52 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | bool AddKey(uint32_t key); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 54 | bool AddKey(Object* key, AddKeyConversion convert); |
| 55 | bool AddKey(Handle<Object> key, AddKeyConversion convert); |
| 56 | void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); |
| 57 | void AddKeys(Handle<JSObject> array, AddKeyConversion convert); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | void AddElementKeysFromInterceptor(Handle<JSObject> array); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 59 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 60 | // Jump to the next level, pushing the current |levelLength_| to |
| 61 | // |levelLengths_| and adding a new list to |elements_|. |
| 62 | void NextPrototype(); |
| 63 | // Sort the integer indices in the last list in |elements_| |
| 64 | void SortCurrentElementsList(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | int length() { return length_; } |
| 66 | Isolate* isolate() { return isolate_; } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 67 | PropertyFilter filter() { return filter_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 68 | void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | |
| 70 | private: |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 71 | Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, |
| 72 | Handle<JSObject> object); |
| 73 | Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, |
| 74 | Handle<JSProxy> proxy); |
| 75 | Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, |
| 76 | Handle<JSReceiver> target); |
| 77 | |
| 78 | Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, |
| 79 | Handle<FixedArray> keys); |
| 80 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | bool AddIntegerKey(uint32_t key); |
| 82 | bool AddStringKey(Handle<Object> key, AddKeyConversion convert); |
| 83 | bool AddSymbolKey(Handle<Object> array); |
| 84 | void SortCurrentElementsListRemoveDuplicates(); |
| 85 | |
| 86 | Isolate* isolate_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 87 | KeyCollectionType type_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | PropertyFilter filter_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 89 | bool filter_proxy_keys_ = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | // |elements_| contains the sorted element keys (indices) per level. |
| 91 | std::vector<std::vector<uint32_t>*> elements_; |
| 92 | // |protoLengths_| contains the total number of keys (elements + properties) |
| 93 | // per level. Negative values mark counts for a level with keys from a proxy. |
| 94 | std::vector<int> level_lengths_; |
| 95 | // |string_properties_| contains the unique String property keys for all |
| 96 | // levels in insertion order per level. |
| 97 | Handle<OrderedHashSet> string_properties_; |
| 98 | // |symbol_properties_| contains the unique Symbol property keys for all |
| 99 | // levels in insertion order per level. |
| 100 | Handle<OrderedHashSet> symbol_properties_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 101 | Handle<FixedArray> ownProxyKeys_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | // |length_| keeps track of the total number of all element and property keys. |
| 103 | int length_ = 0; |
| 104 | // |levelLength_| keeps track of the number of String keys in the current |
| 105 | // level. |
| 106 | int level_string_length_ = 0; |
| 107 | // |levelSymbolLength_| keeps track of the number of Symbol keys in the |
| 108 | // current level. |
| 109 | int level_symbol_length_ = 0; |
| 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); |
| 112 | }; |
| 113 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 114 | // The FastKeyAccumulator handles the cases where there are no elements on the |
| 115 | // prototype chain and forwords the complex/slow cases to the normal |
| 116 | // KeyAccumulator. |
| 117 | class FastKeyAccumulator { |
| 118 | public: |
| 119 | FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, |
| 120 | KeyCollectionType type, PropertyFilter filter) |
| 121 | : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) { |
| 122 | Prepare(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } |
| 126 | bool has_empty_prototype() { return has_empty_prototype_; } |
| 127 | void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } |
| 128 | |
| 129 | MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); |
| 130 | |
| 131 | private: |
| 132 | void Prepare(); |
| 133 | MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); |
| 134 | MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); |
| 135 | |
| 136 | Isolate* isolate_; |
| 137 | Handle<JSReceiver> receiver_; |
| 138 | KeyCollectionType type_; |
| 139 | PropertyFilter filter_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 140 | bool filter_proxy_keys_ = true; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 141 | bool is_receiver_simple_enum_ = false; |
| 142 | bool has_empty_prototype_ = false; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 143 | |
| 144 | DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); |
| 145 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 146 | |
| 147 | } // namespace internal |
| 148 | } // namespace v8 |
| 149 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 150 | #endif // V8_KEYS_H_ |