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_KEYS_H_ 6 #define V8_KEYS_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 }; 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: KeyAccumulator(Isolate * isolate,KeyCollectionMode mode,PropertyFilter filter)34 KeyAccumulator(Isolate* isolate, KeyCollectionMode mode, 35 PropertyFilter filter) 36 : isolate_(isolate), mode_(mode), filter_(filter) {} 37 ~KeyAccumulator(); 38 39 static MaybeHandle<FixedArray> GetKeys( 40 Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter, 41 GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers, 42 bool is_for_in = false); 43 44 Handle<FixedArray> GetKeys( 45 GetKeysConversion convert = GetKeysConversion::kKeepNumbers); 46 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, 47 Handle<JSReceiver> object); 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); 55 56 static Handle<FixedArray> GetOwnEnumPropertyKeys(Isolate* isolate, 57 Handle<JSObject> object); 58 59 void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT); 60 void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT); 61 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); 62 void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert); 63 64 // Jump to the next level, pushing the current |levelLength_| to 65 // |levelLengths_| and adding a new list to |elements_|. isolate()66 Isolate* isolate() { return isolate_; } 67 // Filter keys based on their property descriptors. filter()68 PropertyFilter filter() { return filter_; } 69 // The collection mode defines whether we collect the keys from the prototype 70 // chain or only look at the receiver. mode()71 KeyCollectionMode mode() { return mode_; } 72 // In case of for-in loops we have to treat JSProxy keys differently and 73 // deduplicate them. Additionally we convert JSProxy keys back to array 74 // indices. set_is_for_in(bool value)75 void set_is_for_in(bool value) { is_for_in_ = value; } set_skip_indices(bool value)76 void set_skip_indices(bool value) { skip_indices_ = value; } 77 // The last_non_empty_prototype is used to limit the prototypes for which 78 // we have to keep track of non-enumerable keys that can shadow keys 79 // repeated on the prototype chain. set_last_non_empty_prototype(Handle<JSReceiver> object)80 void set_last_non_empty_prototype(Handle<JSReceiver> object) { 81 last_non_empty_prototype_ = object; 82 } 83 // Shadowing keys are used to filter keys. This happens when non-enumerable 84 // keys appear again on the prototype chain. 85 void AddShadowingKey(Object* key); 86 void AddShadowingKey(Handle<Object> key); 87 88 private: 89 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, 90 Handle<JSObject> object); 91 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, 92 Handle<JSProxy> proxy); 93 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, 94 Handle<JSReceiver> target); 95 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, 96 Handle<FixedArray> keys); 97 bool IsShadowed(Handle<Object> key); 98 bool HasShadowingKeys(); keys()99 Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); } 100 101 Isolate* isolate_; 102 // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy 103 // keys a Handle<FixedArray>. The OrderedHashSet is in-place converted to the 104 // result list, a FixedArray containing all collected keys. 105 Handle<FixedArray> keys_; 106 Handle<JSReceiver> last_non_empty_prototype_; 107 Handle<ObjectHashSet> shadowing_keys_; 108 KeyCollectionMode mode_; 109 PropertyFilter filter_; 110 bool is_for_in_ = false; 111 bool skip_indices_ = false; 112 // For all the keys on the first receiver adding a shadowing key we can skip 113 // the shadow check. 114 bool skip_shadow_check_ = true; 115 116 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); 117 }; 118 119 // The FastKeyAccumulator handles the cases where there are no elements on the 120 // prototype chain and forwords the complex/slow cases to the normal 121 // KeyAccumulator. This significantly speeds up the cases where the OWN_ONLY 122 // case where we do not have to walk the prototype chain. 123 class FastKeyAccumulator { 124 public: FastKeyAccumulator(Isolate * isolate,Handle<JSReceiver> receiver,KeyCollectionMode mode,PropertyFilter filter)125 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, 126 KeyCollectionMode mode, PropertyFilter filter) 127 : isolate_(isolate), receiver_(receiver), mode_(mode), filter_(filter) { 128 Prepare(); 129 } 130 is_receiver_simple_enum()131 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } has_empty_prototype()132 bool has_empty_prototype() { return has_empty_prototype_; } set_is_for_in(bool value)133 void set_is_for_in(bool value) { is_for_in_ = value; } 134 135 MaybeHandle<FixedArray> GetKeys( 136 GetKeysConversion convert = GetKeysConversion::kKeepNumbers); 137 138 private: 139 void Prepare(); 140 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); 141 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); 142 143 Isolate* isolate_; 144 Handle<JSReceiver> receiver_; 145 Handle<JSReceiver> last_non_empty_prototype_; 146 KeyCollectionMode mode_; 147 PropertyFilter filter_; 148 bool is_for_in_ = false; 149 bool is_receiver_simple_enum_ = false; 150 bool has_empty_prototype_ = false; 151 152 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); 153 }; 154 155 } // namespace internal 156 } // namespace v8 157 158 #endif // V8_KEYS_H_ 159