V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
maybe-handles.h
1 // Copyright 2011 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_MAYBE_HANDLES_H_
6 #define V8_MAYBE_HANDLES_H_
7 
8 #include <type_traits>
9 
10 #include "src/handles.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 // ----------------------------------------------------------------------------
16 // A Handle can be converted into a MaybeHandle. Converting a MaybeHandle
17 // into a Handle requires checking that it does not point to nullptr. This
18 // ensures nullptr checks before use.
19 //
20 // Also note that Handles do not provide default equality comparison or hashing
21 // operators on purpose. Such operators would be misleading, because intended
22 // semantics is ambiguous between Handle location and object identity.
23 template <typename T>
24 class MaybeHandle final {
25  public:
26  V8_INLINE MaybeHandle() = default;
27 
28  // Constructor for handling automatic up casting from Handle.
29  // Ex. Handle<JSArray> can be passed when MaybeHandle<Object> is expected.
30  // TODO(3770): Drop std::is_same special cases after the migration.
31  template <typename S, typename = typename std::enable_if<
32  std::is_convertible<S*, T*>::value ||
33  std::is_same<T, Object>::value ||
34  (std::is_same<T, HeapObject>::value &&
35  (std::is_same<S, FixedArray>::value ||
36  std::is_same<S, Map>::value))>::type>
37  V8_INLINE MaybeHandle(Handle<S> handle) : location_(handle.location_) {}
38 
39  // Constructor for handling automatic up casting.
40  // Ex. MaybeHandle<JSArray> can be passed when Handle<Object> is expected.
41  // TODO(3770): Remove std::is_same special cases after the migration.
42  template <typename S, typename = typename std::enable_if<
43  std::is_convertible<S*, T*>::value ||
44  std::is_same<T, Object>::value ||
45  (std::is_same<T, HeapObject>::value &&
46  std::is_same<S, Map>::value)>::type>
47  V8_INLINE MaybeHandle(MaybeHandle<S> maybe_handle)
48  : location_(maybe_handle.location_) {}
49 
50  V8_INLINE MaybeHandle(T* object, Isolate* isolate);
51 
52  V8_INLINE void Assert() const { DCHECK_NOT_NULL(location_); }
53  V8_INLINE void Check() const { CHECK_NOT_NULL(location_); }
54 
55  V8_INLINE Handle<T> ToHandleChecked() const {
56  Check();
57  return Handle<T>(location_);
58  }
59 
60  // Convert to a Handle with a type that can be upcasted to.
61  template <typename S>
62  V8_INLINE bool ToHandle(Handle<S>* out) const {
63  if (location_ == nullptr) {
64  *out = Handle<T>::null();
65  return false;
66  } else {
67  *out = Handle<T>(location_);
68  return true;
69  }
70  }
71 
72  // Returns the raw address where this handle is stored. This should only be
73  // used for hashing handles; do not ever try to dereference it.
74  V8_INLINE Address address() const {
75  return reinterpret_cast<Address>(location_);
76  }
77 
78  bool is_null() const { return location_ == nullptr; }
79 
80  protected:
81  Address* location_ = nullptr;
82 
83  // MaybeHandles of different classes are allowed to access each
84  // other's location_.
85  template <typename>
86  friend class MaybeHandle;
87 };
88 
89 // A handle which contains a potentially weak pointer. Keeps it alive (strongly)
90 // while the MaybeObjectHandle is alive.
92  public:
93  inline MaybeObjectHandle();
94  inline MaybeObjectHandle(MaybeObject object, Isolate* isolate);
95  inline MaybeObjectHandle(Object* object, Isolate* isolate);
96  inline explicit MaybeObjectHandle(Handle<Object> object);
97 
98  static inline MaybeObjectHandle Weak(Object* object, Isolate* isolate);
99  static inline MaybeObjectHandle Weak(Handle<Object> object);
100 
101  inline MaybeObject operator*() const;
102  inline MaybeObject operator->() const;
103  inline Handle<Object> object() const;
104 
105  bool is_identical_to(const MaybeObjectHandle& other) const {
106  Handle<Object> this_handle;
107  Handle<Object> other_handle;
108  return reference_type_ == other.reference_type_ &&
109  handle_.ToHandle(&this_handle) ==
110  other.handle_.ToHandle(&other_handle) &&
111  this_handle.is_identical_to(other_handle);
112  }
113 
114  bool is_null() const { return handle_.is_null(); }
115 
116  private:
117  inline MaybeObjectHandle(Object* object,
118  HeapObjectReferenceType reference_type,
119  Isolate* isolate);
120  inline MaybeObjectHandle(Handle<Object> object,
121  HeapObjectReferenceType reference_type);
122 
123  HeapObjectReferenceType reference_type_;
124  MaybeHandle<Object> handle_;
125 };
126 
127 } // namespace internal
128 } // namespace v8
129 
130 #endif // V8_MAYBE_HANDLES_H_
Definition: libplatform.h:13