Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef V8_KEYS_H_ | 5 #ifndef V8_KEYS_H_ | 
| 6 #define V8_KEYS_H_ | 6 #define V8_KEYS_H_ | 
| 7 | 7 | 
| 8 #include "src/isolate.h" | 8 #include "src/isolate.h" | 
| 9 #include "src/objects.h" | 9 #include "src/objects.h" | 
| 10 | 10 | 
| 11 namespace v8 { | 11 namespace v8 { | 
| 12 namespace internal { | 12 namespace internal { | 
| 13 | 13 | 
| 14 enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX, PROXY_MAGIC }; | 14 enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX }; | 
| 15 | 15 | 
| 16 // This is a helper class for JSReceiver::GetKeys which collects and sorts keys. | 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 | 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 | 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 | 19 // does not apply to proxies which are in full control of how the keys are | 
| 20 // sorted. | 20 // sorted. | 
| 21 // | 21 // | 
| 22 // For performance reasons the KeyAccumulator internally separates integer keys | 22 // For performance reasons the KeyAccumulator internally separates integer keys | 
| 23 // in |elements_| into sorted lists per prototype level. String keys are | 23 // in |elements_| into sorted lists per prototype level. String keys are | 
| 24 // collected in |string_properties_|, a single OrderedHashSet (similar for | 24 // collected in |string_properties_|, a single OrderedHashSet (similar for | 
| 25 // Symbols in |symbol_properties_|. To separate the keys per level later when | 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 | 26 // assembling the final list, |levelLengths_| keeps track of the number of | 
| 27 // String and Symbol keys per level. | 27 // String and Symbol keys per level. | 
| 28 // | 28 // | 
| 29 // Only unique keys are kept by the KeyAccumulator, strings are stored in a | 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 | 30 // HashSet for inexpensive lookups. Integer keys are kept in sorted lists which | 
| 31 // are more compact and allow for reasonably fast includes check. | 31 // are more compact and allow for reasonably fast includes check. | 
| 32 class KeyAccumulator final BASE_EMBEDDED { | 32 class KeyAccumulator final BASE_EMBEDDED { | 
| 33 public: | 33 public: | 
| 34 KeyAccumulator(Isolate* isolate, KeyCollectionType type, | 34 KeyAccumulator(Isolate* isolate, KeyCollectionType type, | 
| 35 PropertyFilter filter) | 35 PropertyFilter filter) | 
| 36 : isolate_(isolate), type_(type), filter_(filter) {} | 36 : isolate_(isolate), type_(type), filter_(filter) {} | 
| 37 ~KeyAccumulator(); | 37 ~KeyAccumulator(); | 
| 38 | 38 | 
| 39 static MaybeHandle<FixedArray> GetKeys(Handle<JSReceiver> object, | 39 // Computes the enumerable keys for a JSObject. Used for implementing | 
| 
 
Jakob Kummerow
2016/05/23 16:25:21
I think this comment is outdated (and overly restr
 
Camillo Bruni
2016/05/23 19:10:13
removed.
 
 | |
| 40 KeyCollectionType type, | 40 // "for (n in object) { }". | 
| 41 PropertyFilter filter, | 41 static MaybeHandle<FixedArray> GetKeys( | 
| 42 GetKeysConversion keys_conversion, | 42 Handle<JSReceiver> object, KeyCollectionType type, PropertyFilter filter, | 
| 43 bool filter_proxy_keys); | 43 GetKeysConversion keys_conversion = KEEP_NUMBERS, | 
| 44 bool filter_proxy_keys = true, bool is_for_in = false); | |
| 44 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | 45 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | 
| 45 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, | 46 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, | 
| 46 Handle<JSReceiver> object); | 47 Handle<JSReceiver> object); | 
| 47 void CollectOwnElementIndices(Handle<JSObject> object); | 48 Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver, | 
| 48 void CollectOwnPropertyNames(Handle<JSObject> object); | 49 Handle<JSObject> object); | 
| 50 Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver, | |
| 51 Handle<JSObject> object); | |
| 49 | 52 | 
| 50 static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate, | 53 static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate, | 
| 51 Handle<JSObject> object); | 54 Handle<JSObject> object); | 
| 52 | 55 | 
| 53 bool AddKey(uint32_t key); | 56 void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT); | 
| 54 bool AddKey(Object* key, AddKeyConversion convert); | 57 void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT); | 
| 55 bool AddKey(Handle<Object> key, AddKeyConversion convert); | |
| 56 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); | 58 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); | 
| 57 void AddKeys(Handle<JSObject> array, AddKeyConversion convert); | 59 void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert); | 
| 58 void AddElementKeysFromInterceptor(Handle<JSObject> array); | |
| 59 | 60 | 
| 60 // Jump to the next level, pushing the current |levelLength_| to | 61 // Jump to the next level, pushing the current |levelLength_| to | 
| 61 // |levelLengths_| and adding a new list to |elements_|. | 62 // |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(); | |
| 65 int length() { return length_; } | |
| 66 Isolate* isolate() { return isolate_; } | 63 Isolate* isolate() { return isolate_; } | 
| 67 PropertyFilter filter() { return filter_; } | 64 PropertyFilter filter() { return filter_; } | 
| 68 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } | 65 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } | 
| 66 void set_is_for_in(bool value) { is_for_in_ = value; } | |
| 67 void set_skip_indices(bool value) { skip_indices_ = value; } | |
| 69 | 68 | 
| 70 private: | 69 private: | 
| 71 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, | 70 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, | 
| 72 Handle<JSObject> object); | 71 Handle<JSObject> object); | 
| 73 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, | 72 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, | 
| 74 Handle<JSProxy> proxy); | 73 Handle<JSProxy> proxy); | 
| 75 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, | 74 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, | 
| 76 Handle<JSReceiver> target); | 75 Handle<JSReceiver> target); | 
| 77 | 76 | 
| 78 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, | 77 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, | 
| 79 Handle<FixedArray> keys); | 78 Handle<FixedArray> keys); | 
| 80 | 79 | 
| 81 bool AddIntegerKey(uint32_t key); | 80 Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); } | 
| 82 bool AddStringKey(Handle<Object> key, AddKeyConversion convert); | |
| 83 bool AddSymbolKey(Handle<Object> array); | |
| 84 void SortCurrentElementsListRemoveDuplicates(); | |
| 85 | 81 | 
| 86 Isolate* isolate_; | 82 Isolate* isolate_; | 
| 83 // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy | |
| 84 // keys a Handle<FixedArray>. | |
| 85 Handle<FixedArray> keys_; | |
| 87 KeyCollectionType type_; | 86 KeyCollectionType type_; | 
| 88 PropertyFilter filter_; | 87 PropertyFilter filter_; | 
| 89 bool filter_proxy_keys_ = true; | 88 bool filter_proxy_keys_ = true; | 
| 90 // |elements_| contains the sorted element keys (indices) per level. | 89 bool is_for_in_ = false; | 
| 91 std::vector<std::vector<uint32_t>*> elements_; | 90 bool skip_indices_ = false; | 
| 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_; | |
| 101 Handle<FixedArray> ownProxyKeys_; | |
| 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 | 91 | 
| 111 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); | 92 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); | 
| 112 }; | 93 }; | 
| 113 | 94 | 
| 114 // The FastKeyAccumulator handles the cases where there are no elements on the | 95 // The FastKeyAccumulator handles the cases where there are no elements on the | 
| 115 // prototype chain and forwords the complex/slow cases to the normal | 96 // prototype chain and forwords the complex/slow cases to the normal | 
| 116 // KeyAccumulator. | 97 // KeyAccumulator. | 
| 117 class FastKeyAccumulator { | 98 class FastKeyAccumulator { | 
| 118 public: | 99 public: | 
| 119 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, | 100 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, | 
| 120 KeyCollectionType type, PropertyFilter filter) | 101 KeyCollectionType type, PropertyFilter filter) | 
| 121 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) { | 102 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) { | 
| 122 Prepare(); | 103 Prepare(); | 
| 123 } | 104 } | 
| 124 | 105 | 
| 125 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } | 106 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } | 
| 126 bool has_empty_prototype() { return has_empty_prototype_; } | 107 bool has_empty_prototype() { return has_empty_prototype_; } | 
| 127 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } | 108 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } | 
| 109 void set_is_for_in(bool value) { is_for_in_ = value; } | |
| 128 | 110 | 
| 129 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | 111 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | 
| 130 | 112 | 
| 131 private: | 113 private: | 
| 132 void Prepare(); | 114 void Prepare(); | 
| 133 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); | 115 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); | 
| 134 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); | 116 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); | 
| 135 | 117 | 
| 136 Isolate* isolate_; | 118 Isolate* isolate_; | 
| 137 Handle<JSReceiver> receiver_; | 119 Handle<JSReceiver> receiver_; | 
| 138 KeyCollectionType type_; | 120 KeyCollectionType type_; | 
| 139 PropertyFilter filter_; | 121 PropertyFilter filter_; | 
| 140 bool filter_proxy_keys_ = true; | 122 bool filter_proxy_keys_ = true; | 
| 123 bool is_for_in_ = false; | |
| 141 bool is_receiver_simple_enum_ = false; | 124 bool is_receiver_simple_enum_ = false; | 
| 142 bool has_empty_prototype_ = false; | 125 bool has_empty_prototype_ = false; | 
| 143 | 126 | 
| 144 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); | 127 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); | 
| 145 }; | 128 }; | 
| 146 | 129 | 
| 147 } // namespace internal | 130 } // namespace internal | 
| 148 } // namespace v8 | 131 } // namespace v8 | 
| 149 | 132 | 
| 150 #endif // V8_KEYS_H_ | 133 #endif // V8_KEYS_H_ | 
| OLD | NEW |