• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef V8_KEYS_H_
6 #define V8_KEYS_H_
7 
8 #include "src/isolate.h"
9 #include "src/objects.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX };
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.
32 class KeyAccumulator final BASE_EMBEDDED {
33  public:
KeyAccumulator(Isolate * isolate,KeyCollectionMode mode,PropertyFilter filter)34   KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
35                  PropertyFilter filter)
36       : isolate_(isolate), mode_(mode), filter_(filter) {}
37   ~KeyAccumulator();
38 
39   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);
46   Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
47                           Handle<JSReceiver> object);
48   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);
55 
56   static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate,
57                                                 Handle<JSObject> object);
58 
59   void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT);
60   void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
61   void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
62   void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert);
63 
64   // Jump to the next level, pushing the current |levelLength_| to
65   // |levelLengths_| and adding a new list to |elements_|.
isolate()66   Isolate* isolate() { return isolate_; }
filter()67   PropertyFilter filter() { return filter_; }
set_filter_proxy_keys(bool filter)68   void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
set_is_for_in(bool value)69   void set_is_for_in(bool value) { is_for_in_ = value; }
set_skip_indices(bool value)70   void set_skip_indices(bool value) { skip_indices_ = value; }
set_last_non_empty_prototype(Handle<JSReceiver> object)71   void set_last_non_empty_prototype(Handle<JSReceiver> object) {
72     last_non_empty_prototype_ = object;
73   }
74 
75  private:
76   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 
keys()86   Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); }
87 
88   Isolate* isolate_;
89   // 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_;
94   PropertyFilter filter_;
95   bool filter_proxy_keys_ = true;
96   bool is_for_in_ = false;
97   bool skip_indices_ = false;
98 
99   DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
100 };
101 
102 // 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.
105 class FastKeyAccumulator {
106  public:
FastKeyAccumulator(Isolate * isolate,Handle<JSReceiver> receiver,KeyCollectionMode mode,PropertyFilter filter)107   FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
108                      KeyCollectionMode mode, PropertyFilter filter)
109       : isolate_(isolate), receiver_(receiver), mode_(mode), filter_(filter) {
110     Prepare();
111   }
112 
is_receiver_simple_enum()113   bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
has_empty_prototype()114   bool has_empty_prototype() { return has_empty_prototype_; }
set_filter_proxy_keys(bool filter)115   void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
set_is_for_in(bool value)116   void set_is_for_in(bool value) { is_for_in_ = value; }
117 
118   MaybeHandle<FixedArray> GetKeys(
119       GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
120 
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_;
128   Handle<JSReceiver> last_non_empty_prototype_;
129   KeyCollectionMode mode_;
130   PropertyFilter filter_;
131   bool filter_proxy_keys_ = true;
132   bool is_for_in_ = false;
133   bool is_receiver_simple_enum_ = false;
134   bool has_empty_prototype_ = false;
135 
136   DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
137 };
138 
139 }  // namespace internal
140 }  // namespace v8
141 
142 #endif  // V8_KEYS_H_
143