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 static MaybeHandle<FixedArray> GetKeys( |
40 KeyCollectionType type, | 40 Handle<JSReceiver> object, KeyCollectionType type, PropertyFilter filter, |
41 PropertyFilter filter, | 41 GetKeysConversion keys_conversion = KEEP_NUMBERS, |
42 GetKeysConversion keys_conversion, | 42 bool filter_proxy_keys = true, bool is_for_in = false); |
43 bool filter_proxy_keys); | |
44 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | 43 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); |
45 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, | 44 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver, |
46 Handle<JSReceiver> object); | 45 Handle<JSReceiver> object); |
47 void CollectOwnElementIndices(Handle<JSObject> object); | 46 Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver, |
48 void CollectOwnPropertyNames(Handle<JSObject> object); | 47 Handle<JSObject> object); |
| 48 Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver, |
| 49 Handle<JSObject> object); |
49 | 50 |
50 static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate, | 51 static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate, |
51 Handle<JSObject> object); | 52 Handle<JSObject> object); |
52 | 53 |
53 bool AddKey(uint32_t key); | 54 void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT); |
54 bool AddKey(Object* key, AddKeyConversion convert); | 55 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); | 56 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert); |
57 void AddKeys(Handle<JSObject> array, AddKeyConversion convert); | 57 void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert); |
58 void AddElementKeysFromInterceptor(Handle<JSObject> array); | |
59 | 58 |
60 // Jump to the next level, pushing the current |levelLength_| to | 59 // Jump to the next level, pushing the current |levelLength_| to |
61 // |levelLengths_| and adding a new list to |elements_|. | 60 // |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_; } | 61 Isolate* isolate() { return isolate_; } |
67 PropertyFilter filter() { return filter_; } | 62 PropertyFilter filter() { return filter_; } |
68 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } | 63 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } |
| 64 void set_is_for_in(bool value) { is_for_in_ = value; } |
| 65 void set_skip_indices(bool value) { skip_indices_ = value; } |
69 | 66 |
70 private: | 67 private: |
71 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, | 68 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver, |
72 Handle<JSObject> object); | 69 Handle<JSObject> object); |
73 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, | 70 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver, |
74 Handle<JSProxy> proxy); | 71 Handle<JSProxy> proxy); |
75 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, | 72 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy, |
76 Handle<JSReceiver> target); | 73 Handle<JSReceiver> target); |
77 | 74 |
78 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, | 75 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy, |
79 Handle<FixedArray> keys); | 76 Handle<FixedArray> keys); |
80 | 77 |
81 bool AddIntegerKey(uint32_t key); | 78 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 | 79 |
86 Isolate* isolate_; | 80 Isolate* isolate_; |
| 81 // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy |
| 82 // keys a Handle<FixedArray>. |
| 83 Handle<FixedArray> keys_; |
87 KeyCollectionType type_; | 84 KeyCollectionType type_; |
88 PropertyFilter filter_; | 85 PropertyFilter filter_; |
89 bool filter_proxy_keys_ = true; | 86 bool filter_proxy_keys_ = true; |
90 // |elements_| contains the sorted element keys (indices) per level. | 87 bool is_for_in_ = false; |
91 std::vector<std::vector<uint32_t>*> elements_; | 88 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 | 89 |
111 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); | 90 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator); |
112 }; | 91 }; |
113 | 92 |
114 // The FastKeyAccumulator handles the cases where there are no elements on the | 93 // The FastKeyAccumulator handles the cases where there are no elements on the |
115 // prototype chain and forwords the complex/slow cases to the normal | 94 // prototype chain and forwords the complex/slow cases to the normal |
116 // KeyAccumulator. | 95 // KeyAccumulator. |
117 class FastKeyAccumulator { | 96 class FastKeyAccumulator { |
118 public: | 97 public: |
119 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, | 98 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver, |
120 KeyCollectionType type, PropertyFilter filter) | 99 KeyCollectionType type, PropertyFilter filter) |
121 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) { | 100 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) { |
122 Prepare(); | 101 Prepare(); |
123 } | 102 } |
124 | 103 |
125 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } | 104 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; } |
126 bool has_empty_prototype() { return has_empty_prototype_; } | 105 bool has_empty_prototype() { return has_empty_prototype_; } |
127 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } | 106 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; } |
| 107 void set_is_for_in(bool value) { is_for_in_ = value; } |
128 | 108 |
129 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); | 109 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS); |
130 | 110 |
131 private: | 111 private: |
132 void Prepare(); | 112 void Prepare(); |
133 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); | 113 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert); |
134 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); | 114 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert); |
135 | 115 |
136 Isolate* isolate_; | 116 Isolate* isolate_; |
137 Handle<JSReceiver> receiver_; | 117 Handle<JSReceiver> receiver_; |
138 KeyCollectionType type_; | 118 KeyCollectionType type_; |
139 PropertyFilter filter_; | 119 PropertyFilter filter_; |
140 bool filter_proxy_keys_ = true; | 120 bool filter_proxy_keys_ = true; |
| 121 bool is_for_in_ = false; |
141 bool is_receiver_simple_enum_ = false; | 122 bool is_receiver_simple_enum_ = false; |
142 bool has_empty_prototype_ = false; | 123 bool has_empty_prototype_ = false; |
143 | 124 |
144 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); | 125 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator); |
145 }; | 126 }; |
146 | 127 |
147 } // namespace internal | 128 } // namespace internal |
148 } // namespace v8 | 129 } // namespace v8 |
149 | 130 |
150 #endif // V8_KEYS_H_ | 131 #endif // V8_KEYS_H_ |
OLD | NEW |