v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
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
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 private:
90 internal::Address* location_ = nullptr;
91};
92
93#ifdef V8_ENABLE_DIRECT_LOCAL
94
99class DirectHandleBase {
100 public:
101 // Returns true if the handle is empty.
102 V8_INLINE bool IsEmpty() const {
103 return ptr_ == internal::ValueHelper::kEmpty;
104 }
105
106 // Sets the handle to be empty. IsEmpty() will then return true.
107 V8_INLINE void Clear() { ptr_ = internal::ValueHelper::kEmpty; }
108
109 protected:
110 friend class internal::ValueHelper;
111 friend class internal::HandleHelper;
112
113 V8_INLINE DirectHandleBase() = default;
114 V8_INLINE DirectHandleBase(const DirectHandleBase& other) = default;
115 V8_INLINE DirectHandleBase& operator=(const DirectHandleBase& that) = default;
116
117 V8_INLINE explicit DirectHandleBase(internal::Address ptr) : ptr_(ptr) {}
118
119 // Returns the address of the referenced object.
120 V8_INLINE internal::Address ptr() const { return ptr_; }
121
122 // Returns the handler's "value" (direct pointer, as direct local support
123 // is guaranteed to be enabled here).
124 template <typename T, bool check_null = false>
125 V8_INLINE T* value() const {
126 return reinterpret_cast<T*>(ptr_);
127 }
128
129 private:
130 internal::Address ptr_ = internal::ValueHelper::kEmpty;
131};
132
133#endif // V8_ENABLE_DIRECT_LOCAL
134
135} // namespace v8::api_internal
136
137#endif // INCLUDE_V8_HANDLE_BASE_H_
V8_INLINE internal::Address ptr() const
V8_INLINE IndirectHandleBase()=default
V8_INLINE internal::Address *const & slot() const
V8_INLINE IndirectHandleBase(const IndirectHandleBase &other)=default
V8_INLINE IndirectHandleBase & operator=(const IndirectHandleBase &that)=default
V8_INLINE IndirectHandleBase(internal::Address *location)
V8_INLINE internal::Address *& slot()
V8_INLINE StackAllocated(const StackAllocated &other, no_checking_tag tag)
V8_INLINE StackAllocated(no_checking_tag tag)
V8_INLINE void VerifyOnStack() const
static constexpr no_checking_tag do_not_check
V8_INLINE StackAllocated()=default
V8_INLINE StackAllocated(no_checking_tag)
V8_INLINE StackAllocated(const StackAllocated &other, no_checking_tag)
uintptr_t Address
Definition v8-internal.h:31
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477
#define V8_TRIVIAL_ABI
Definition v8config.h:703