keys.h 9.24 KB
Newer Older
1 2 3 4
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_OBJECTS_KEYS_H_
#define V8_OBJECTS_KEYS_H_
7

8
#include "src/objects/hash-table.h"
9
#include "src/objects/js-objects.h"
10
#include "src/objects/objects.h"
11 12 13 14

namespace v8 {
namespace internal {

15
class JSProxy;
16
class FastKeyAccumulator;
17

18
enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX };
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

// This is a helper class for JSReceiver::GetKeys which collects and sorts keys.
// GetKeys needs to sort keys per prototype level, first showing the integer
// indices from elements then the strings from the properties. However, this
// does not apply to proxies which are in full control of how the keys are
// sorted.
//
// For performance reasons the KeyAccumulator internally separates integer keys
// in |elements_| into sorted lists per prototype level. String keys are
// collected in |string_properties_|, a single OrderedHashSet (similar for
// Symbols in |symbol_properties_|. To separate the keys per level later when
// assembling the final list, |levelLengths_| keeps track of the number of
// String and Symbol keys per level.
//
// Only unique keys are kept by the KeyAccumulator, strings are stored in a
// HashSet for inexpensive lookups. Integer keys are kept in sorted lists which
// are more compact and allow for reasonably fast includes check.
36
class KeyAccumulator final {
37
 public:
38
  KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
39
                 PropertyFilter filter)
40
      : isolate_(isolate), mode_(mode), filter_(filter) {}
41
  ~KeyAccumulator() = default;
42 43
  KeyAccumulator(const KeyAccumulator&) = delete;
  KeyAccumulator& operator=(const KeyAccumulator&) = delete;
44

45
  static MaybeHandle<FixedArray> GetKeys(
46 47
      Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter,
      GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers,
48
      bool is_for_in = false, bool skip_indices = false);
49

50 51
  Handle<FixedArray> GetKeys(
      GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
52 53 54
  Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
                          Handle<JSReceiver> object);

55 56
  // Might return directly the object's enum_cache, copy the result before using
  // as an elements backing store for a JSObject.
57 58
  // Does not throw for uninitialized exports in module namespace objects, so
  // this has to be checked separately.
59 60
  static Handle<FixedArray> GetOwnEnumPropertyKeys(Isolate* isolate,
                                                   Handle<JSObject> object);
61

62 63 64 65
  V8_WARN_UNUSED_RESULT ExceptionStatus
  AddKey(Object key, AddKeyConversion convert = DO_NOT_CONVERT);
  V8_WARN_UNUSED_RESULT ExceptionStatus
  AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
66

67 68
  // Jump to the next level, pushing the current |levelLength_| to
  // |levelLengths_| and adding a new list to |elements_|.
69
  Isolate* isolate() { return isolate_; }
70
  // Filter keys based on their property descriptors.
71
  PropertyFilter filter() { return filter_; }
72 73 74
  // The collection mode defines whether we collect the keys from the prototype
  // chain or only look at the receiver.
  KeyCollectionMode mode() { return mode_; }
75
  void set_skip_indices(bool value) { skip_indices_ = value; }
76 77
  // Shadowing keys are used to filter keys. This happens when non-enumerable
  // keys appear again on the prototype chain.
78
  void AddShadowingKey(Object key, AllowGarbageCollection* allow_gc);
79
  void AddShadowingKey(Handle<Object> key);
80 81

 private:
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  enum IndexedOrNamed { kIndexed, kNamed };

  V8_WARN_UNUSED_RESULT ExceptionStatus
  CollectPrivateNames(Handle<JSReceiver> receiver, Handle<JSObject> object);
  Maybe<bool> CollectAccessCheckInterceptorKeys(
      Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver,
      Handle<JSObject> object);

  Maybe<bool> CollectInterceptorKeysInternal(
      Handle<JSReceiver> receiver, Handle<JSObject> object,
      Handle<InterceptorInfo> interceptor, IndexedOrNamed type);
  Maybe<bool> CollectInterceptorKeys(Handle<JSReceiver> receiver,
                                     Handle<JSObject> object,
                                     IndexedOrNamed type);

  Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver,
                                       Handle<JSObject> object);
  Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver,
                                      Handle<JSObject> object);
101 102
  Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
                             Handle<JSObject> object);
103 104 105 106
  Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
                                    Handle<JSProxy> proxy);
  Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy,
                                          Handle<JSReceiver> target);
107 108 109 110 111 112

  V8_WARN_UNUSED_RESULT ExceptionStatus FilterForEnumerableProperties(
      Handle<JSReceiver> receiver, Handle<JSObject> object,
      Handle<InterceptorInfo> interceptor, Handle<JSObject> result,
      IndexedOrNamed type);

113 114
  Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy,
                                 Handle<FixedArray> keys);
115 116 117 118 119
  V8_WARN_UNUSED_RESULT ExceptionStatus AddKeys(Handle<FixedArray> array,
                                                AddKeyConversion convert);
  V8_WARN_UNUSED_RESULT ExceptionStatus AddKeys(Handle<JSObject> array_like,
                                                AddKeyConversion convert);

120
  bool IsShadowed(Handle<Object> key);
121
  bool HasShadowingKeys();
122
  Handle<OrderedHashSet> keys();
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  // In case of for-in loops we have to treat JSProxy keys differently and
  // deduplicate them. Additionally we convert JSProxy keys back to array
  // indices.
  void set_is_for_in(bool value) { is_for_in_ = value; }
  void set_first_prototype_map(Handle<Map> value) {
    first_prototype_map_ = value;
  }
  void set_try_prototype_info_cache(bool value) {
    try_prototype_info_cache_ = value;
  }
  void set_receiver(Handle<JSReceiver> object) { receiver_ = object; }
  // The last_non_empty_prototype is used to limit the prototypes for which
  // we have to keep track of non-enumerable keys that can shadow keys
  // repeated on the prototype chain.
  void set_last_non_empty_prototype(Handle<JSReceiver> object) {
    last_non_empty_prototype_ = object;
  }
  void set_may_have_elements(bool value) { may_have_elements_ = value; }

143
  Isolate* isolate_;
144
  // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy
145 146
  // keys a Handle<FixedArray>. The OrderedHashSet is in-place converted to the
  // result list, a FixedArray containing all collected keys.
147
  Handle<FixedArray> keys_;
148 149
  Handle<Map> first_prototype_map_;
  Handle<JSReceiver> receiver_;
150
  Handle<JSReceiver> last_non_empty_prototype_;
151
  Handle<ObjectHashSet> shadowing_keys_;
152
  KeyCollectionMode mode_;
153
  PropertyFilter filter_;
154 155
  bool is_for_in_ = false;
  bool skip_indices_ = false;
156 157 158
  // For all the keys on the first receiver adding a shadowing key we can skip
  // the shadow check.
  bool skip_shadow_check_ = true;
159
  bool may_have_elements_ = true;
160
  bool try_prototype_info_cache_ = false;
161 162

  friend FastKeyAccumulator;
163 164
};

165 166
// The FastKeyAccumulator handles the cases where there are no elements on the
// prototype chain and forwords the complex/slow cases to the normal
167 168
// KeyAccumulator. This significantly speeds up the cases where the OWN_ONLY
// case where we do not have to walk the prototype chain.
169 170 171
class FastKeyAccumulator {
 public:
  FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
172 173 174 175 176 177 178 179
                     KeyCollectionMode mode, PropertyFilter filter,
                     bool is_for_in = false, bool skip_indices = false)
      : isolate_(isolate),
        receiver_(receiver),
        mode_(mode),
        filter_(filter),
        is_for_in_(is_for_in),
        skip_indices_(skip_indices) {
180 181
    Prepare();
  }
182 183
  FastKeyAccumulator(const FastKeyAccumulator&) = delete;
  FastKeyAccumulator& operator=(const FastKeyAccumulator&) = delete;
184 185 186

  bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
  bool has_empty_prototype() { return has_empty_prototype_; }
187
  bool may_have_elements() { return may_have_elements_; }
188

189 190
  MaybeHandle<FixedArray> GetKeys(
      GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
191 192 193 194 195

 private:
  void Prepare();
  MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
  MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);
196 197
  MaybeHandle<FixedArray> GetKeysWithPrototypeInfoCache(
      GetKeysConversion convert);
198

199 200
  MaybeHandle<FixedArray> GetOwnKeysWithUninitializedEnumCache();

201
  bool MayHaveElements(JSReceiver receiver);
202
  bool TryPrototypeInfoCache(Handle<JSReceiver> receiver);
203

204 205
  Isolate* isolate_;
  Handle<JSReceiver> receiver_;
206 207
  Handle<Map> first_prototype_map_;
  Handle<JSReceiver> first_prototype_;
208
  Handle<JSReceiver> last_non_empty_prototype_;
209
  KeyCollectionMode mode_;
210
  PropertyFilter filter_;
211
  bool is_for_in_ = false;
212
  bool skip_indices_ = false;
213 214
  bool is_receiver_simple_enum_ = false;
  bool has_empty_prototype_ = false;
215
  bool may_have_elements_ = true;
216 217
  bool has_prototype_info_cache_ = false;
  bool try_prototype_info_cache_ = false;
218
  bool only_own_has_simple_elements_ = false;
219
};
220 221 222 223

}  // namespace internal
}  // namespace v8

224
#endif  // V8_OBJECTS_KEYS_H_