lookup.h 9.75 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 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.

#ifndef V8_LOOKUP_H_
#define V8_LOOKUP_H_

8
#include "src/globals.h"
9
#include "src/heap/factory.h"
10 11
#include "src/isolate.h"
#include "src/objects.h"
12
#include "src/objects/descriptor-array.h"
13
#include "src/objects/map.h"
14 15 16 17

namespace v8 {
namespace internal {

18
class V8_EXPORT_PRIVATE LookupIterator final {
19
 public:
20
  enum Configuration {
21
    // Configuration bits.
22 23
    kInterceptor = 1 << 0,
    kPrototypeChain = 1 << 1,
24

25
    // Convenience combinations of bits.
26 27
    OWN_SKIP_INTERCEPTOR = 0,
    OWN = kInterceptor,
28 29
    PROTOTYPE_CHAIN_SKIP_INTERCEPTOR = kPrototypeChain,
    PROTOTYPE_CHAIN = kPrototypeChain | kInterceptor,
30
    DEFAULT = PROTOTYPE_CHAIN
31 32 33
  };

  enum State {
34
    ACCESS_CHECK,
35
    INTEGER_INDEXED_EXOTIC,
36 37
    INTERCEPTOR,
    JSPROXY,
38
    NOT_FOUND,
39 40
    ACCESSOR,
    DATA,
41
    TRANSITION,
42 43 44
    // Set state_ to BEFORE_PROPERTY to ensure that the next lookup will be a
    // PROPERTY lookup.
    BEFORE_PROPERTY = INTERCEPTOR
45 46
  };

47 48 49
  inline LookupIterator(Isolate* isolate, Handle<Object> receiver,
                        Handle<Name> name,
                        Configuration configuration = DEFAULT);
50

51 52 53
  inline LookupIterator(Handle<Object> receiver, Handle<Name> name,
                        Handle<JSReceiver> holder,
                        Configuration configuration = DEFAULT);
54

55 56 57
  inline LookupIterator(Isolate* isolate, Handle<Object> receiver,
                        Handle<Name> name, Handle<JSReceiver> holder,
                        Configuration configuration = DEFAULT);
58

59 60
  inline LookupIterator(Isolate* isolate, Handle<Object> receiver,
                        uint32_t index, Configuration configuration = DEFAULT);
61 62 63

  LookupIterator(Isolate* isolate, Handle<Object> receiver, uint32_t index,
                 Handle<JSReceiver> holder,
64
                 Configuration configuration = DEFAULT)
65 66 67 68 69
      : configuration_(configuration),
        interceptor_state_(InterceptorState::kUninitialized),
        property_details_(PropertyDetails::Empty()),
        isolate_(isolate),
        receiver_(receiver),
70 71
        initial_holder_(holder),
        index_(index),
72
        number_(static_cast<uint32_t>(DescriptorArray::kNotFound)) {
73 74
    // kMaxUInt32 isn't a valid index.
    DCHECK_NE(kMaxUInt32, index_);
75
    Start<true>();
76 77
  }

78
  static inline LookupIterator PropertyOrElement(
79
      Isolate* isolate, Handle<Object> receiver, Handle<Name> name,
80
      Configuration configuration = DEFAULT);
81

82
  static inline LookupIterator PropertyOrElement(
83
      Isolate* isolate, Handle<Object> receiver, Handle<Name> name,
84
      Handle<JSReceiver> holder, Configuration configuration = DEFAULT);
85

86 87 88 89 90
  static LookupIterator PropertyOrElement(
      Isolate* isolate, Handle<Object> receiver, Handle<Object> key,
      bool* success, Handle<JSReceiver> holder,
      Configuration configuration = DEFAULT);

91 92 93 94
  static LookupIterator PropertyOrElement(
      Isolate* isolate, Handle<Object> receiver, Handle<Object> key,
      bool* success, Configuration configuration = DEFAULT);

95 96 97
  static LookupIterator ForTransitionHandler(
      Isolate* isolate, Handle<Object> receiver, Handle<Name> name,
      Handle<Object> value, MaybeHandle<Map> maybe_transition_map);
98

99 100 101 102
  void Restart() {
    InterceptorState state = InterceptorState::kUninitialized;
    IsElement() ? RestartInternal<true>(state) : RestartInternal<false>(state);
  }
103

104 105
  Isolate* isolate() const { return isolate_; }
  State state() const { return state_; }
106

107 108 109 110
  Handle<Name> name() const {
    DCHECK(!IsElement());
    return name_;
  }
111
  inline Handle<Name> GetName();
112 113 114
  uint32_t index() const { return index_; }

  bool IsElement() const { return index_ != kMaxUInt32; }
115 116 117

  bool IsFound() const { return state_ != NOT_FOUND; }
  void Next();
118 119 120 121
  void NotFound() {
    has_property_ = false;
    state_ = NOT_FOUND;
  }
122

123
  Heap* heap() const { return isolate_->heap(); }
124
  Factory* factory() const { return isolate_->factory(); }
125
  Handle<Object> GetReceiver() const { return receiver_; }
126

127
  template <class T>
128
  inline Handle<T> GetStoreTarget() const;
129
  inline bool is_dictionary_holder() const;
130 131
  inline Handle<Map> transition_map() const;
  inline Handle<PropertyCell> transition_cell() const;
132
  template <class T>
133
  inline Handle<T> GetHolder() const;
134

135
  bool HolderIsReceiver() const;
136
  bool HolderIsReceiverOrHiddenPrototype() const;
137

neis's avatar
neis committed
138 139 140 141
  bool check_prototype_chain() const {
    return (configuration_ & kPrototypeChain) != 0;
  }

142
  /* ACCESS_CHECK */
143
  bool HasAccess() const;
144 145

  /* PROPERTY */
146
  inline bool ExtendingNonExtensible(Handle<JSReceiver> receiver);
147
  void PrepareForDataProperty(Handle<Object> value);
148
  void PrepareTransitionToDataProperty(Handle<JSReceiver> receiver,
149
                                       Handle<Object> value,
150
                                       PropertyAttributes attributes,
151
                                       StoreOrigin store_origin);
152
  inline bool IsCacheableTransition();
153
  void ApplyTransitionToDataProperty(Handle<JSReceiver> receiver);
154 155
  void ReconfigureDataProperty(Handle<Object> value,
                               PropertyAttributes attributes);
156
  void Delete();
157 158
  void TransitionToAccessorProperty(Handle<Object> getter,
                                    Handle<Object> setter,
159
                                    PropertyAttributes attributes);
160 161
  void TransitionToAccessorPair(Handle<Object> pair,
                                PropertyAttributes attributes);
162
  PropertyDetails property_details() const {
163
    DCHECK(has_property_);
164 165
    return property_details_;
  }
166 167 168
  PropertyAttributes property_attributes() const {
    return property_details().attributes();
  }
169
  bool IsConfigurable() const { return property_details().IsConfigurable(); }
170
  bool IsReadOnly() const { return property_details().IsReadOnly(); }
171
  bool IsEnumerable() const { return property_details().IsEnumerable(); }
172 173
  Representation representation() const {
    return property_details().representation();
174
  }
175
  PropertyLocation location() const { return property_details().location(); }
176
  PropertyConstness constness() const { return property_details().constness(); }
177
  Handle<Map> GetFieldOwnerMap() const;
178
  FieldIndex GetFieldIndex() const;
179
  Handle<FieldType> GetFieldType() const;
180
  int GetFieldDescriptorIndex() const;
181
  int GetAccessorIndex() const;
182
  int GetConstantIndex() const;
183
  Handle<PropertyCell> GetPropertyCell() const;
184
  Handle<Object> GetAccessors() const;
185
  inline Handle<InterceptorInfo> GetInterceptor() const;
186
  Handle<InterceptorInfo> GetInterceptorForFailedAccessCheck() const;
187
  Handle<Object> GetDataValue() const;
188
  void WriteDataValue(Handle<Object> value, bool initializing_store);
189
  inline void UpdateProtector();
190

191 192 193 194 195
  // Lookup a 'cached' private property for an accessor.
  // If not found returns false and leaves the LookupIterator unmodified.
  bool TryLookupCachedProperty();
  bool LookupCachedProperty();

196
 private:
197 198 199 200 201
  // For |ForTransitionHandler|.
  LookupIterator(Isolate* isolate, Handle<Object> receiver, Handle<Name> name,
                 Handle<Map> transition_map, PropertyDetails details,
                 bool has_property);

202 203
  void InternalUpdateProtector();

204 205 206 207 208 209
  enum class InterceptorState {
    kUninitialized,
    kSkipNonMasking,
    kProcessNonMasking
  };

210 211
  Handle<Map> GetReceiverMap() const;

212
  V8_WARN_UNUSED_RESULT inline JSReceiver* NextHolder(Map map);
213 214

  template <bool is_element>
215
  V8_EXPORT_PRIVATE void Start();
216
  template <bool is_element>
217
  void NextInternal(Map map, JSReceiver* holder);
218
  template <bool is_element>
219
  inline State LookupInHolder(Map map, JSReceiver* holder) {
220
    return map->IsSpecialReceiverMap()
221 222 223 224
               ? LookupInSpecialHolder<is_element>(map, holder)
               : LookupInRegularHolder<is_element>(map, holder);
  }
  template <bool is_element>
225
  State LookupInRegularHolder(Map map, JSReceiver* holder);
226
  template <bool is_element>
227
  State LookupInSpecialHolder(Map map, JSReceiver* holder);
228
  template <bool is_element>
229
  void RestartLookupForNonMaskingInterceptors() {
230
    RestartInternal<is_element>(InterceptorState::kProcessNonMasking);
231
  }
232
  template <bool is_element>
233
  void RestartInternal(InterceptorState interceptor_state);
234
  Handle<Object> FetchValue() const;
235
  bool IsConstFieldValueEqualTo(Object* value) const;
236
  template <bool is_element>
237
  void ReloadPropertyInformation();
238

239 240 241
  template <bool is_element>
  bool SkipInterceptor(JSObject* holder);
  template <bool is_element>
242
  inline InterceptorInfo* GetInterceptor(JSObject* holder) const {
243 244
    return is_element ? holder->GetIndexedInterceptor()
                      : holder->GetNamedInterceptor();
245
  }
246

247
  bool check_interceptor() const {
248
    return (configuration_ & kInterceptor) != 0;
249
  }
250 251
  inline int descriptor_number() const;
  inline int dictionary_entry() const;
252

253 254
  static inline Configuration ComputeConfiguration(
      Configuration configuration, Handle<Name> name);
255

256 257
  static Handle<JSReceiver> GetRootForNonJSReceiver(
      Isolate* isolate, Handle<Object> receiver, uint32_t index = kMaxUInt32);
258
  static inline Handle<JSReceiver> GetRoot(Isolate* isolate,
259
                                           Handle<Object> receiver,
260
                                           uint32_t index = kMaxUInt32);
261

262
  State NotFound(JSReceiver* const holder) const;
263

264 265
  // If configuration_ becomes mutable, update
  // HolderIsReceiverOrHiddenPrototype.
266
  const Configuration configuration_;
267 268
  State state_;
  bool has_property_;
269
  InterceptorState interceptor_state_;
270
  PropertyDetails property_details_;
271
  Isolate* const isolate_;
272
  Handle<Name> name_;
273
  Handle<Object> transition_;
274
  const Handle<Object> receiver_;
275
  Handle<JSReceiver> holder_;
276
  const Handle<JSReceiver> initial_holder_;
277
  const uint32_t index_;
278
  uint32_t number_;
279 280 281
};


282 283
}  // namespace internal
}  // namespace v8
284 285

#endif  // V8_LOOKUP_H_