Loading...
Searching...
No Matches
v8-handle-base.h
Go to the documentation of this file.
1// Copyright 2023 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 INCLUDE_V8_HANDLE_BASE_H_
6#define INCLUDE_V8_HANDLE_BASE_H_
7
8#include "v8-internal.h" // NOLINT(build/include_directory)
9
10namespace v8::api_internal {
11
12template <bool check_statically_enabled>
14 public:
16
17 protected:
18 struct no_checking_tag {};
19 static constexpr no_checking_tag do_not_check{};
20
24
25 V8_INLINE void VerifyOnStack() const {}
26};
27
28template <>
29class V8_TRIVIAL_ABI StackAllocated<true> : public StackAllocated<false> {
30 public:
31 V8_INLINE StackAllocated() { VerifyOnStack(); }
32
33#if V8_HAS_ATTRIBUTE_TRIVIAL_ABI
34 // In this case, StackAllocated becomes not trivially copyable.
35 V8_INLINE StackAllocated(const StackAllocated& other) { VerifyOnStack(); }
36 StackAllocated& operator=(const StackAllocated&) = default;
37#endif
38
39 protected:
40 V8_INLINE explicit StackAllocated(no_checking_tag tag)
41 : StackAllocated<false>(tag) {}
43 no_checking_tag tag)
44 : StackAllocated<false>(other, tag) {}
45
46#ifdef ENABLE_SLOW_DCHECKS
47 V8_EXPORT void VerifyOnStack() const;
48#else
50#endif
51};
52
58 public:
59 // Returns true if the handle is empty.
60 V8_INLINE bool IsEmpty() const { return location_ == nullptr; }
61
62 // Sets the handle to be empty. IsEmpty() will then return true.
63 V8_INLINE void Clear() { location_ = nullptr; }
64
65 protected:
68
72 default;
73
75 : location_(location) {}
76
77 // Returns the address of the actual heap object (tagged).
78 // This method must be called only if the handle is not empty, otherwise it
79 // will crash.
80 V8_INLINE internal::Address ptr() const { return *location_; }
81
83 if (const internal::Address* loc =
84 reinterpret_cast<const std::atomic<internal::Address*>*>(&location_)
85 ->load(std::memory_order_relaxed)) {
86 return reinterpret_cast<const std::atomic<internal::Address>*>(loc)->load(
87 std::memory_order_relaxed);
88 }
90 }
91
92 // Returns a reference to the slot (indirect pointer).
93 V8_INLINE internal::Address* const& slot() const { return location_; }
94 V8_INLINE internal::Address*& slot() { return location_; }
95
96 // Returns the handler's "value" (direct or indirect pointer, depending on
97 // whether direct local support is enabled).
98 template <typename T, bool check_null = false>
99 V8_INLINE T* value() const {
100 return internal::ValueHelper::SlotAsValue<T, check_null>(slot());
101 }
102
103#ifdef V8_ENABLE_DIRECT_HANDLE
105 return location_ ? *location_ : internal::ValueHelper::kEmpty;
106 }
107#else
109 return location_;
110 }
111#endif // V8_ENABLE_DIRECT_HANDLE
112
113 private:
114 internal::Address* location_ = nullptr;
115};
116
117#ifdef V8_ENABLE_DIRECT_HANDLE
118
123class DirectHandleBase {
124 public:
125 // Returns true if the handle is empty.
126 V8_INLINE bool IsEmpty() const {
127 return ptr_ == internal::ValueHelper::kEmpty;
128 }
129
130 // Sets the handle to be empty. IsEmpty() will then return true.
131 V8_INLINE void Clear() { ptr_ = internal::ValueHelper::kEmpty; }
132
133 protected:
134 friend class internal::ValueHelper;
135 friend class internal::HandleHelper;
136
137 V8_INLINE DirectHandleBase() = default;
138 V8_INLINE DirectHandleBase(const DirectHandleBase& other) = default;
139 V8_INLINE DirectHandleBase& operator=(const DirectHandleBase& that) = default;
140
141 V8_INLINE explicit DirectHandleBase(internal::Address ptr) : ptr_(ptr) {}
142
143 // Returns the address of the referenced object.
144 V8_INLINE internal::Address ptr() const { return ptr_; }
145
146 V8_INLINE internal::Address ptr_relaxed() const {
147 return reinterpret_cast<const std::atomic<internal::Address>*>(&ptr_)->load(
148 std::memory_order_relaxed);
149 }
150
151 // Returns the handler's "value" (direct pointer, as direct local support
152 // is guaranteed to be enabled here).
153 template <typename T, bool check_null = false>
154 V8_INLINE T* value() const {
155 return reinterpret_cast<T*>(ptr_);
156 }
157
158 V8_INLINE internal::ValueHelper::InternalRepresentationType repr() const {
159 return ptr_;
160 }
161
162 private:
163 internal::Address ptr_ = internal::ValueHelper::kEmpty;
164};
165
166#endif // V8_ENABLE_DIRECT_HANDLE
167
168} // namespace v8::api_internal
169
170#endif // INCLUDE_V8_HANDLE_BASE_H_
Definition: v8-handle-base.h:57
IndirectHandleBase & operator=(const IndirectHandleBase &that)=default
T * value() const
Definition: v8-handle-base.h:99
internal::Address ptr() const
Definition: v8-handle-base.h:80
internal::Address *& slot()
Definition: v8-handle-base.h:94
internal::Address *const & slot() const
Definition: v8-handle-base.h:93
bool IsEmpty() const
Definition: v8-handle-base.h:60
IndirectHandleBase(const IndirectHandleBase &other)=default
IndirectHandleBase(internal::Address *location)
Definition: v8-handle-base.h:74
void Clear()
Definition: v8-handle-base.h:63
internal::ValueHelper::InternalRepresentationType repr() const
Definition: v8-handle-base.h:108
internal::Address ptr_relaxed() const
Definition: v8-handle-base.h:82
void VerifyOnStack() const
Definition: v8-handle-base.h:49
StackAllocated()
Definition: v8-handle-base.h:31
StackAllocated(const StackAllocated &other, no_checking_tag tag)
Definition: v8-handle-base.h:42
StackAllocated(no_checking_tag tag)
Definition: v8-handle-base.h:40
Definition: v8-handle-base.h:13
void VerifyOnStack() const
Definition: v8-handle-base.h:25
StackAllocated(const StackAllocated &other, no_checking_tag)
Definition: v8-handle-base.h:22
StackAllocated(no_checking_tag)
Definition: v8-handle-base.h:21
static constexpr no_checking_tag do_not_check
Definition: v8-handle-base.h:19
Definition: v8-internal.h:1862
Definition: v8-internal.h:1776
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1790
static constexpr Address kTaggedNullAddress
Definition: v8-internal.h:1784
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1791
Definition: v8-function-callback.h:35
uintptr_t Address
Definition: v8-internal.h:38
#define V8_EXPORT
Definition: v8config.h:875
#define V8_INLINE
Definition: v8config.h:519
#define V8_TRIVIAL_ABI
Definition: v8config.h:828