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