blob: 502c834527e65caf9e3cab6f5bb68e442f940a09 [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
Ben Murdoch61f157c2016-09-16 13:49:30 +010014enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015
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 Murdoch61f157c2016-09-16 13:49:30 +010034 KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
Ben Murdoch097c5b22016-05-18 11:27:45 +010035 PropertyFilter filter)
Ben Murdoch61f157c2016-09-16 13:49:30 +010036 : isolate_(isolate), mode_(mode), filter_(filter) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 ~KeyAccumulator();
38
Ben Murdoch61f157c2016-09-16 13:49:30 +010039 static MaybeHandle<FixedArray> GetKeys(
40 Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter,
41 GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers,
42 bool filter_proxy_keys = true, bool is_for_in = false);
43
44 Handle<FixedArray> GetKeys(
45 GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
Ben Murdochc5610432016-08-08 18:44:38 +010046 Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
47 Handle<JSReceiver> object);
Ben Murdoch61f157c2016-09-16 13:49:30 +010048 Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver,
49 Handle<JSObject> object);
50 Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver,
51 Handle<JSObject> object);
52 Maybe<bool> CollectAccessCheckInterceptorKeys(
53 Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver,
54 Handle<JSObject> object);
Ben Murdochc5610432016-08-08 18:44:38 +010055
56 static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate,
57 Handle<JSObject> object);
58
Ben Murdoch61f157c2016-09-16 13:49:30 +010059 void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT);
60 void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
Ben Murdoch097c5b22016-05-18 11:27:45 +010061 void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
Ben Murdoch61f157c2016-09-16 13:49:30 +010062 void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert);
Ben Murdochc5610432016-08-08 18:44:38 +010063
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 // Jump to the next level, pushing the current |levelLength_| to
65 // |levelLengths_| and adding a new list to |elements_|.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 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 Murdoch61f157c2016-09-16 13:49:30 +010069 void set_is_for_in(bool value) { is_for_in_ = value; }
70 void set_skip_indices(bool value) { skip_indices_ = value; }
71 void set_last_non_empty_prototype(Handle<JSReceiver> object) {
72 last_non_empty_prototype_ = object;
73 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074
75 private:
Ben Murdochc5610432016-08-08 18:44:38 +010076 Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
77 Handle<JSObject> object);
78 Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
79 Handle<JSProxy> proxy);
80 Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy,
81 Handle<JSReceiver> target);
82
83 Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy,
84 Handle<FixedArray> keys);
85
Ben Murdoch61f157c2016-09-16 13:49:30 +010086 Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087
88 Isolate* isolate_;
Ben Murdoch61f157c2016-09-16 13:49:30 +010089 // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy
90 // keys a Handle<FixedArray>.
91 Handle<FixedArray> keys_;
92 Handle<JSReceiver> last_non_empty_prototype_;
93 KeyCollectionMode mode_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 PropertyFilter filter_;
Ben Murdochda12d292016-06-02 14:46:10 +010095 bool filter_proxy_keys_ = true;
Ben Murdoch61f157c2016-09-16 13:49:30 +010096 bool is_for_in_ = false;
97 bool skip_indices_ = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098
99 DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
100};
101
Ben Murdochda12d292016-06-02 14:46:10 +0100102// The FastKeyAccumulator handles the cases where there are no elements on the
103// prototype chain and forwords the complex/slow cases to the normal
104// KeyAccumulator.
105class FastKeyAccumulator {
106 public:
107 FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
Ben Murdoch61f157c2016-09-16 13:49:30 +0100108 KeyCollectionMode mode, PropertyFilter filter)
109 : isolate_(isolate), receiver_(receiver), mode_(mode), filter_(filter) {
Ben Murdochda12d292016-06-02 14:46:10 +0100110 Prepare();
Ben Murdochda12d292016-06-02 14:46:10 +0100111 }
112
113 bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
114 bool has_empty_prototype() { return has_empty_prototype_; }
115 void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100116 void set_is_for_in(bool value) { is_for_in_ = value; }
Ben Murdochda12d292016-06-02 14:46:10 +0100117
Ben Murdoch61f157c2016-09-16 13:49:30 +0100118 MaybeHandle<FixedArray> GetKeys(
119 GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
Ben Murdochda12d292016-06-02 14:46:10 +0100120
121 private:
122 void Prepare();
123 MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
124 MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);
125
126 Isolate* isolate_;
127 Handle<JSReceiver> receiver_;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100128 Handle<JSReceiver> last_non_empty_prototype_;
129 KeyCollectionMode mode_;
Ben Murdochda12d292016-06-02 14:46:10 +0100130 PropertyFilter filter_;
Ben Murdochc5610432016-08-08 18:44:38 +0100131 bool filter_proxy_keys_ = true;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100132 bool is_for_in_ = false;
Ben Murdochda12d292016-06-02 14:46:10 +0100133 bool is_receiver_simple_enum_ = false;
134 bool has_empty_prototype_ = false;
Ben Murdochda12d292016-06-02 14:46:10 +0100135
136 DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
137};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138
139} // namespace internal
140} // namespace v8
141
Ben Murdochc5610432016-08-08 18:44:38 +0100142#endif // V8_KEYS_H_