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