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 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 14 | enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 34 | KeyAccumulator(Isolate* isolate, KeyCollectionMode mode, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 35 | PropertyFilter filter) |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 36 | : isolate_(isolate), mode_(mode), filter_(filter) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | ~KeyAccumulator(); |
| 38 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 39 | static MaybeHandle<FixedArray> GetKeys( |
| 40 | Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter, |
| 41 | GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers, |
| 42 | bool filter_proxy_keys = true, bool is_for_in = false); |
| 43 | |
| 44 | Handle<FixedArray> GetKeys( |
| 45 | GetKeysConversion convert = GetKeysConversion::kKeepNumbers); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 46 | Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, |
| 47 | Handle<JSReceiver> object); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 48 | Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver, |
| 49 | Handle<JSObject> object); |
| 50 | Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver, |
| 51 | Handle<JSObject> object); |
| 52 | Maybe<bool> CollectAccessCheckInterceptorKeys( |
| 53 | Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver, |
| 54 | Handle<JSObject> object); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 55 | |
| 56 | static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate, |
| 57 | Handle<JSObject> object); |
| 58 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 59 | void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT); |
| 60 | void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 61 | void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 62 | void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 63 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 64 | // Jump to the next level, pushing the current |levelLength_| to |
| 65 | // |levelLengths_| and adding a new list to |elements_|. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 69 | void set_is_for_in(bool value) { is_for_in_ = value; } |
| 70 | void set_skip_indices(bool value) { skip_indices_ = value; } |
| 71 | void set_last_non_empty_prototype(Handle<JSReceiver> object) { |
| 72 | last_non_empty_prototype_ = object; |
| 73 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 76 | Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, |
| 77 | Handle<JSObject> object); |
| 78 | Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, |
| 79 | Handle<JSProxy> proxy); |
| 80 | Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, |
| 81 | Handle<JSReceiver> target); |
| 82 | |
| 83 | Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, |
| 84 | Handle<FixedArray> keys); |
| 85 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 86 | Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 87 | |
| 88 | Isolate* isolate_; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 89 | // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy |
| 90 | // keys a Handle<FixedArray>. |
| 91 | Handle<FixedArray> keys_; |
| 92 | Handle<JSReceiver> last_non_empty_prototype_; |
| 93 | KeyCollectionMode mode_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 94 | PropertyFilter filter_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 95 | bool filter_proxy_keys_ = true; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 96 | bool is_for_in_ = false; |
| 97 | bool skip_indices_ = false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); |
| 100 | }; |
| 101 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 102 | // The FastKeyAccumulator handles the cases where there are no elements on the |
| 103 | // prototype chain and forwords the complex/slow cases to the normal |
| 104 | // KeyAccumulator. |
| 105 | class FastKeyAccumulator { |
| 106 | public: |
| 107 | FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 108 | KeyCollectionMode mode, PropertyFilter filter) |
| 109 | : isolate_(isolate), receiver_(receiver), mode_(mode), filter_(filter) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 110 | Prepare(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } |
| 114 | bool has_empty_prototype() { return has_empty_prototype_; } |
| 115 | void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 116 | void set_is_for_in(bool value) { is_for_in_ = value; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 117 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 118 | MaybeHandle<FixedArray> GetKeys( |
| 119 | GetKeysConversion convert = GetKeysConversion::kKeepNumbers); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | void Prepare(); |
| 123 | MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); |
| 124 | MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); |
| 125 | |
| 126 | Isolate* isolate_; |
| 127 | Handle<JSReceiver> receiver_; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 128 | Handle<JSReceiver> last_non_empty_prototype_; |
| 129 | KeyCollectionMode mode_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 130 | PropertyFilter filter_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 131 | bool filter_proxy_keys_ = true; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 132 | bool is_for_in_ = false; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 133 | bool is_receiver_simple_enum_ = false; |
| 134 | bool has_empty_prototype_ = false; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); |
| 137 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | |
| 139 | } // namespace internal |
| 140 | } // namespace v8 |
| 141 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 142 | #endif // V8_KEYS_H_ |