blob: c73f10911df3c81f54ea875fa12bd1bfbfcf3b68 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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 Murdochc5610432016-08-08 18:44:38 +01005#ifndef V8_KEYS_H_
6#define V8_KEYS_H_
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007
8#include "src/isolate.h"
9#include "src/objects.h"
10
11namespace v8 {
12namespace internal {
13
14enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX, PROXY_MAGIC };
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.
32class KeyAccumulator final BASE_EMBEDDED {
33 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +010034 KeyAccumulator(Isolate* isolate, KeyCollectionType type,
35 PropertyFilter filter)
36 : isolate_(isolate), type_(type), filter_(filter) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 ~KeyAccumulator();
38
Ben Murdochc5610432016-08-08 18:44:38 +010039 static MaybeHandle<FixedArray> GetKeys(Handle<JSReceiver> object,
40 KeyCollectionType type,
41 PropertyFilter filter,
42 GetKeysConversion keys_conversion,
43 bool filter_proxy_keys);
44 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS);
45 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
46 Handle<JSReceiver> object);
47 void CollectOwnElementIndices(Handle<JSObject> object);
48 void CollectOwnPropertyNames(Handle<JSObject> object);
49
50 static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate,
51 Handle<JSObject> object);
52
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 bool AddKey(uint32_t key);
Ben Murdoch097c5b22016-05-18 11:27:45 +010054 bool AddKey(Object* key, AddKeyConversion convert);
55 bool AddKey(Handle<Object> key, AddKeyConversion convert);
56 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
57 void AddKeys(Handle<JSObject> array, AddKeyConversion convert);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 void AddElementKeysFromInterceptor(Handle<JSObject> array);
Ben Murdochc5610432016-08-08 18:44:38 +010059
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 // Jump to the next level, pushing the current |levelLength_| to
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();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 int length() { return length_; }
66 Isolate* isolate() { return isolate_; }
Ben Murdochc5610432016-08-08 18:44:38 +010067 PropertyFilter filter() { return filter_; }
Ben Murdochda12d292016-06-02 14:46:10 +010068 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069
70 private:
Ben Murdochc5610432016-08-08 18:44:38 +010071 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
72 Handle<JSObject> object);
73 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
74 Handle<JSProxy> proxy);
75 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy,
76 Handle<JSReceiver> target);
77
78 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy,
79 Handle<FixedArray> keys);
80
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 bool AddIntegerKey(uint32_t key);
82 bool AddStringKey(Handle<Object> key, AddKeyConversion convert);
83 bool AddSymbolKey(Handle<Object> array);
84 void SortCurrentElementsListRemoveDuplicates();
85
86 Isolate* isolate_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010087 KeyCollectionType type_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 PropertyFilter filter_;
Ben Murdochda12d292016-06-02 14:46:10 +010089 bool filter_proxy_keys_ = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 // |elements_| contains the sorted element keys (indices) per level.
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_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100101 Handle<FixedArray> ownProxyKeys_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102 // |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
111 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
112};
113
Ben Murdochda12d292016-06-02 14:46:10 +0100114// The FastKeyAccumulator handles the cases where there are no elements on the
115// prototype chain and forwords the complex/slow cases to the normal
116// KeyAccumulator.
117class FastKeyAccumulator {
118 public:
119 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
120 KeyCollectionType type, PropertyFilter filter)
121 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) {
122 Prepare();
Ben Murdochda12d292016-06-02 14:46:10 +0100123 }
124
125 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
126 bool has_empty_prototype() { return has_empty_prototype_; }
127 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
128
129 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS);
130
131 private:
132 void Prepare();
133 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
134 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);
135
136 Isolate* isolate_;
137 Handle<JSReceiver> receiver_;
138 KeyCollectionType type_;
139 PropertyFilter filter_;
Ben Murdochc5610432016-08-08 18:44:38 +0100140 bool filter_proxy_keys_ = true;
Ben Murdochda12d292016-06-02 14:46:10 +0100141 bool is_receiver_simple_enum_ = false;
142 bool has_empty_prototype_ = false;
Ben Murdochda12d292016-06-02 14:46:10 +0100143
144 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
145};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146
147} // namespace internal
148} // namespace v8
149
Ben Murdochc5610432016-08-08 18:44:38 +0100150#endif // V8_KEYS_H_