blob: 1fd3fc02b033bbf25d355464dc59edc510c9f2ed [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
5#ifndef V8_KEY_ACCUMULATOR_H_
6#define V8_KEY_ACCUMULATOR_H_
7
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
39 bool AddKey(uint32_t key);
Ben Murdoch097c5b22016-05-18 11:27:45 +010040 bool AddKey(Object* key, AddKeyConversion convert);
41 bool AddKey(Handle<Object> key, AddKeyConversion convert);
42 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
43 void AddKeys(Handle<JSObject> array, AddKeyConversion convert);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 void AddKeysFromProxy(Handle<JSObject> array);
45 Maybe<bool> AddKeysFromProxy(Handle<JSProxy> proxy, Handle<FixedArray> keys);
46 void AddElementKeysFromInterceptor(Handle<JSObject> array);
47 // Jump to the next level, pushing the current |levelLength_| to
48 // |levelLengths_| and adding a new list to |elements_|.
49 void NextPrototype();
50 // Sort the integer indices in the last list in |elements_|
51 void SortCurrentElementsList();
52 Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS);
53 int length() { return length_; }
54 Isolate* isolate() { return isolate_; }
Ben Murdochda12d292016-06-02 14:46:10 +010055 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056
57 private:
58 bool AddIntegerKey(uint32_t key);
59 bool AddStringKey(Handle<Object> key, AddKeyConversion convert);
60 bool AddSymbolKey(Handle<Object> array);
61 void SortCurrentElementsListRemoveDuplicates();
62
63 Isolate* isolate_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010064 KeyCollectionType type_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 PropertyFilter filter_;
Ben Murdochda12d292016-06-02 14:46:10 +010066 bool filter_proxy_keys_ = true;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 // |elements_| contains the sorted element keys (indices) per level.
68 std::vector<std::vector<uint32_t>*> elements_;
69 // |protoLengths_| contains the total number of keys (elements + properties)
70 // per level. Negative values mark counts for a level with keys from a proxy.
71 std::vector<int> level_lengths_;
72 // |string_properties_| contains the unique String property keys for all
73 // levels in insertion order per level.
74 Handle<OrderedHashSet> string_properties_;
75 // |symbol_properties_| contains the unique Symbol property keys for all
76 // levels in insertion order per level.
77 Handle<OrderedHashSet> symbol_properties_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010078 Handle<FixedArray> ownProxyKeys_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 // |length_| keeps track of the total number of all element and property keys.
80 int length_ = 0;
81 // |levelLength_| keeps track of the number of String keys in the current
82 // level.
83 int level_string_length_ = 0;
84 // |levelSymbolLength_| keeps track of the number of Symbol keys in the
85 // current level.
86 int level_symbol_length_ = 0;
87
88 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
89};
90
Ben Murdochda12d292016-06-02 14:46:10 +010091// The FastKeyAccumulator handles the cases where there are no elements on the
92// prototype chain and forwords the complex/slow cases to the normal
93// KeyAccumulator.
94class FastKeyAccumulator {
95 public:
96 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
97 KeyCollectionType type, PropertyFilter filter)
98 : isolate_(isolate), receiver_(receiver), type_(type), filter_(filter) {
99 Prepare();
100 // TODO(cbruni): pass filter_ directly to the KeyAccumulator.
101 USE(filter_);
102 }
103
104 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
105 bool has_empty_prototype() { return has_empty_prototype_; }
106 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
107
108 MaybeHandle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS);
109
110 private:
111 void Prepare();
112 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
113 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);
114
115 Isolate* isolate_;
116 Handle<JSReceiver> receiver_;
117 KeyCollectionType type_;
118 PropertyFilter filter_;
119 bool is_receiver_simple_enum_ = false;
120 bool has_empty_prototype_ = false;
121 bool filter_proxy_keys_ = true;
122
123 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
124};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000125
126} // namespace internal
127} // namespace v8
128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129#endif // V8_KEY_ACCUMULATOR_H_