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
82 // Returns a reference to the slot (indirect pointer).
83 V8_INLINE internal::Address* const& slot() const { return location_; }
84 V8_INLINE internal::Address*& slot() { return location_; }
85
86 // Returns the handler's "value" (direct or indirect pointer, depending on
87 // whether direct local support is enabled).
88 template <typename T, bool check_null = false>
89 V8_INLINE T* value() const {
90 return internal::ValueHelper::SlotAsValue<T, check_null>(slot());
91 }
92
93#ifdef V8_ENABLE_DIRECT_HANDLE
95 return location_ ? *location_ : internal::ValueHelper::kEmpty;
96 }
97#else
99 return location_;
100 }
101#endif // V8_ENABLE_DIRECT_HANDLE
102
103 private:
104 internal::Address* location_ = nullptr;
105};
106
107#ifdef V8_ENABLE_DIRECT_HANDLE
108
113class DirectHandleBase {
114 public:
115 // Returns true if the handle is empty.
116 V8_INLINE bool IsEmpty() const {
117 return ptr_ == internal::ValueHelper::kEmpty;
118 }
119
120 // Sets the handle to be empty. IsEmpty() will then return true.
121 V8_INLINE void Clear() { ptr_ = internal::ValueHelper::kEmpty; }
122
123 protected:
124 friend class internal::ValueHelper;
125 friend class internal::HandleHelper;
126
127 V8_INLINE DirectHandleBase() = default;
128 V8_INLINE DirectHandleBase(const DirectHandleBase& other) = default;
129 V8_INLINE DirectHandleBase& operator=(const DirectHandleBase& that) = default;
130
131 V8_INLINE explicit DirectHandleBase(internal::Address ptr) : ptr_(ptr) {}
132
133 // Returns the address of the referenced object.
134 V8_INLINE internal::Address ptr() const { return ptr_; }
135
136 // Returns the handler's "value" (direct pointer, as direct local support
137 // is guaranteed to be enabled here).
138 template <typename T, bool check_null = false>
139 V8_INLINE T* value() const {
140 return reinterpret_cast<T*>(ptr_);
141 }
142
143 V8_INLINE internal::ValueHelper::InternalRepresentationType repr() const {
144 return ptr_;
145 }
146
147 private:
148 internal::Address ptr_ = internal::ValueHelper::kEmpty;
149};
150
151#endif // V8_ENABLE_DIRECT_HANDLE
152
153} // namespace v8::api_internal
154
155#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:89
internal::Address ptr() const
Definition: v8-handle-base.h:80
internal::Address *& slot()
Definition: v8-handle-base.h:84
internal::Address *const & slot() const
Definition: v8-handle-base.h:83
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:98
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:1651
Definition: v8-internal.h:1565
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1579
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1580
Definition: v8-function-callback.h:35
uintptr_t Address
Definition: v8-internal.h:52
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_TRIVIAL_ABI
Definition: v8config.h:743