keys.h 7.08 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_KEYS_H_
#define V8_KEYS_H_
7 8

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

namespace v8 {
namespace internal {

15 16
class JSProxy;

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

// 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.
35
class KeyAccumulator final {
36
 public:
37
  KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
38
                 PropertyFilter filter)
39
      : isolate_(isolate), mode_(mode), filter_(filter) {}
40
  ~KeyAccumulator() = default;
41

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

47 48
  Handle<FixedArray> GetKeys(
      GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
49 50
  Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
                          Handle<JSReceiver> object);
51 52 53 54
  Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver,
                                       Handle<JSObject> object);
  Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver,
                                      Handle<JSObject> object);
55 56 57
  Maybe<bool> CollectAccessCheckInterceptorKeys(
      Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver,
      Handle<JSObject> object);
58

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

66
  void AddKey(Object key, AddKeyConversion convert = DO_NOT_CONVERT);
67
  void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
68
  void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
69
  void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert);
70

71 72
  // Jump to the next level, pushing the current |levelLength_| to
  // |levelLengths_| and adding a new list to |elements_|.
73
  Isolate* isolate() { return isolate_; }
74
  // Filter keys based on their property descriptors.
75
  PropertyFilter filter() { return filter_; }
76 77 78 79 80 81
  // The collection mode defines whether we collect the keys from the prototype
  // chain or only look at the receiver.
  KeyCollectionMode mode() { return mode_; }
  // 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.
82 83
  void set_is_for_in(bool value) { is_for_in_ = value; }
  void set_skip_indices(bool value) { skip_indices_ = value; }
84 85 86
  // 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.
87 88 89
  void set_last_non_empty_prototype(Handle<JSReceiver> object) {
    last_non_empty_prototype_ = object;
  }
90 91
  // Shadowing keys are used to filter keys. This happens when non-enumerable
  // keys appear again on the prototype chain.
92
  void AddShadowingKey(Object key);
93
  void AddShadowingKey(Handle<Object> key);
94 95

 private:
96 97
  Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
                             Handle<JSObject> object);
98 99 100 101 102 103
  Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
                                    Handle<JSProxy> proxy);
  Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy,
                                          Handle<JSReceiver> target);
  Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy,
                                 Handle<FixedArray> keys);
104
  bool IsShadowed(Handle<Object> key);
105
  bool HasShadowingKeys();
106
  Handle<OrderedHashSet> keys();
107 108

  Isolate* isolate_;
109
  // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy
110 111
  // keys a Handle<FixedArray>. The OrderedHashSet is in-place converted to the
  // result list, a FixedArray containing all collected keys.
112
  Handle<FixedArray> keys_;
113
  Handle<JSReceiver> last_non_empty_prototype_;
114
  Handle<ObjectHashSet> shadowing_keys_;
115
  KeyCollectionMode mode_;
116
  PropertyFilter filter_;
117 118
  bool is_for_in_ = false;
  bool skip_indices_ = false;
119 120 121
  // For all the keys on the first receiver adding a shadowing key we can skip
  // the shadow check.
  bool skip_shadow_check_ = true;
122 123 124 125

  DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
};

126 127
// The FastKeyAccumulator handles the cases where there are no elements on the
// prototype chain and forwords the complex/slow cases to the normal
128 129
// KeyAccumulator. This significantly speeds up the cases where the OWN_ONLY
// case where we do not have to walk the prototype chain.
130 131 132
class FastKeyAccumulator {
 public:
  FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
133 134 135 136 137 138 139 140
                     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) {
141 142 143 144 145 146
    Prepare();
  }

  bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
  bool has_empty_prototype() { return has_empty_prototype_; }

147 148
  MaybeHandle<FixedArray> GetKeys(
      GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
149 150 151 152 153 154

 private:
  void Prepare();
  MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
  MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);

155 156
  MaybeHandle<FixedArray> GetOwnKeysWithUninitializedEnumCache();

157 158
  Isolate* isolate_;
  Handle<JSReceiver> receiver_;
159
  Handle<JSReceiver> last_non_empty_prototype_;
160
  KeyCollectionMode mode_;
161
  PropertyFilter filter_;
162
  bool is_for_in_ = false;
163
  bool skip_indices_ = false;
164 165 166 167 168
  bool is_receiver_simple_enum_ = false;
  bool has_empty_prototype_ = false;

  DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
};
169 170 171 172

}  // namespace internal
}  // namespace v8

173
#endif  // V8_KEYS_H_