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
47};
48
54 public:
55 // Returns true if the handle is empty.
56 V8_INLINE bool IsEmpty() const { return location_ == nullptr; }
57
58 // Sets the handle to be empty. IsEmpty() will then return true.
59 V8_INLINE void Clear() { location_ = nullptr; }
60
61 protected:
64
68 default;
69
71 : location_(location) {}
72
73 // Returns the address of the actual heap object (tagged).
74 // This method must be called only if the handle is not empty, otherwise it
75 // will crash.
76 V8_INLINE internal::Address ptr() const { return *location_; }
77
78 // Returns a reference to the slot (indirect pointer).
79 V8_INLINE internal::Address* const& slot() const { return location_; }
80 V8_INLINE internal::Address*& slot() { return location_; }
81
82 // Returns the handler's "value" (direct or indirect pointer, depending on
83 // whether direct local support is enabled).
84 template <typename T, bool check_null = false>
85 V8_INLINE T* value() const {
86 return internal::ValueHelper::SlotAsValue<T, check_null>(slot());
87 }
88
89#ifdef V8_ENABLE_DIRECT_HANDLE
91 return location_ ? *location_ : internal::ValueHelper::kEmpty;
92 }
93#else
95 return location_;
96 }
97#endif // V8_ENABLE_DIRECT_HANDLE
98
99 private:
100 internal::Address* location_ = nullptr;
101};
102
103#ifdef V8_ENABLE_DIRECT_HANDLE
104
109class DirectHandleBase {
110 public:
111 // Returns true if the handle is empty.
112 V8_INLINE bool IsEmpty() const {
113 return ptr_ == internal::ValueHelper::kEmpty;
114 }
115
116 // Sets the handle to be empty. IsEmpty() will then return true.
117 V8_INLINE void Clear() { ptr_ = internal::ValueHelper::kEmpty; }
118
119 protected:
120 friend class internal::ValueHelper;
121 friend class internal::HandleHelper;
122
123 V8_INLINE DirectHandleBase() = default;
124 V8_INLINE DirectHandleBase(const DirectHandleBase& other) = default;
125 V8_INLINE DirectHandleBase& operator=(const DirectHandleBase& that) = default;
126
127 V8_INLINE explicit DirectHandleBase(internal::Address ptr) : ptr_(ptr) {}
128
129 // Returns the address of the referenced object.
130 V8_INLINE internal::Address ptr() const { return ptr_; }
131
132 // Returns the handler's "value" (direct pointer, as direct local support
133 // is guaranteed to be enabled here).
134 template <typename T, bool check_null = false>
135 V8_INLINE T* value() const {
136 return reinterpret_cast<T*>(ptr_);
137 }
138
139 V8_INLINE internal::ValueHelper::InternalRepresentationType repr() const {
140 return ptr_;
141 }
142
143 private:
144 internal::Address ptr_ = internal::ValueHelper::kEmpty;
145};
146
147#endif // V8_ENABLE_DIRECT_HANDLE
148
149} // namespace v8::api_internal
150
151#endif // INCLUDE_V8_HANDLE_BASE_H_
Definition: v8-handle-base.h:53
IndirectHandleBase & operator=(const IndirectHandleBase &that)=default
T * value() const
Definition: v8-handle-base.h:85
internal::Address ptr() const
Definition: v8-handle-base.h:76
internal::Address *& slot()
Definition: v8-handle-base.h:80
internal::Address *const & slot() const
Definition: v8-handle-base.h:79
bool IsEmpty() const
Definition: v8-handle-base.h:56
IndirectHandleBase(const IndirectHandleBase &other)=default
IndirectHandleBase(internal::Address *location)
Definition: v8-handle-base.h:70
void Clear()
Definition: v8-handle-base.h:59
internal::ValueHelper::InternalRepresentationType repr() const
Definition: v8-handle-base.h:94
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:1713
Definition: v8-internal.h:1627
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1641
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1642
Definition: v8-function-callback.h:35
uintptr_t Address
Definition: v8-internal.h:51
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_TRIVIAL_ABI
Definition: v8config.h:743