prototype.h 2.68 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_OBJECTS_PROTOTYPE_H_
#define V8_OBJECTS_PROTOTYPE_H_
7

8
#include "src/execution/isolate.h"
9
#include "src/objects/objects.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

namespace v8 {
namespace internal {

/**
 * A class to uniformly access the prototype of any Object and walk its
 * prototype chain.
 *
 * The PrototypeIterator can either start at the prototype (default), or
 * include the receiver itself. If a PrototypeIterator is constructed for a
 * Map, it will always start at the prototype.
 *
 * The PrototypeIterator can either run to the null_value(), the first
 * non-hidden prototype, or a given object.
 */
25

26 27 28 29
class PrototypeIterator {
 public:
  enum WhereToEnd { END_AT_NULL, END_AT_NON_HIDDEN };

30 31 32
  inline PrototypeIterator(Isolate* isolate, Handle<JSReceiver> receiver,
                           WhereToStart where_to_start = kStartAtPrototype,
                           WhereToEnd where_to_end = END_AT_NULL);
33

34
  inline PrototypeIterator(Isolate* isolate, JSReceiver receiver,
35 36
                           WhereToStart where_to_start = kStartAtPrototype,
                           WhereToEnd where_to_end = END_AT_NULL);
37

38
  inline explicit PrototypeIterator(Isolate* isolate, Map receiver_map,
39
                                    WhereToEnd where_to_end = END_AT_NULL);
40

41 42
  inline explicit PrototypeIterator(Isolate* isolate, Handle<Map> receiver_map,
                                    WhereToEnd where_to_end = END_AT_NULL);
43

44
  ~PrototypeIterator() = default;
45 46
  PrototypeIterator(const PrototypeIterator&) = delete;
  PrototypeIterator& operator=(const PrototypeIterator&) = delete;
47

48
  inline bool HasAccess() const;
49

50
  template <typename T = HeapObject>
51
  T GetCurrent() const {
52
    DCHECK(handle_.is_null());
53
    return T::cast(object_);
54
  }
55

56
  template <typename T = HeapObject>
57
  static Handle<T> GetCurrent(const PrototypeIterator& iterator) {
58
    DCHECK(!iterator.handle_.is_null());
59
    DCHECK_EQ(iterator.object_, Object());
60
    return Handle<T>::cast(iterator.handle_);
61
  }
62

63
  inline void Advance();
64

65
  inline void AdvanceIgnoringProxies();
66

67
  // Returns false iff a call to JSProxy::GetPrototype throws.
68
  V8_WARN_UNUSED_RESULT inline bool AdvanceFollowingProxies();
69

70 71
  V8_WARN_UNUSED_RESULT inline bool
  AdvanceFollowingProxiesIgnoringAccessChecks();
72

73
  bool IsAtEnd() const { return is_at_end_; }
74
  Isolate* isolate() const { return isolate_; }
75 76

 private:
77
  Isolate* isolate_;
78
  Object object_;
79
  Handle<HeapObject> handle_;
80 81
  WhereToEnd where_to_end_;
  bool is_at_end_;
82
  int seen_proxies_;
83 84 85 86 87 88
};

}  // namespace internal

}  // namespace v8

89
#endif  // V8_OBJECTS_PROTOTYPE_H_