v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-traced-handle.h
Go to the documentation of this file.
1// Copyright 2021 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_TRACED_HANDLE_H_
6#define INCLUDE_V8_TRACED_HANDLE_H_
7
8#include <stddef.h>
9#include <stdint.h>
10#include <stdio.h>
11
12#include <atomic>
13#include <memory>
14#include <type_traits>
15#include <utility>
16
17#include "v8-internal.h" // NOLINT(build/include_directory)
18#include "v8-local-handle.h" // NOLINT(build/include_directory)
19#include "v8-weak-callback-info.h" // NOLINT(build/include_directory)
20#include "v8config.h" // NOLINT(build/include_directory)
21
22namespace v8 {
23
24class Value;
25
26namespace internal {
27
28class BasicTracedReferenceExtractor;
29
33};
34
36 kDefault, // See EmbedderRootsHandler::IsRoot().
38};
39
41 internal::Isolate* isolate, internal::Address value,
43 internal::TracedReferenceHandling reference_handling);
49
50} // namespace internal
51
57 public:
62 V8_INLINE void Reset();
63
68 if (IsEmpty()) return Local<Value>();
69 return Local<Value>::New(isolate, this->value<Value>());
70 }
71
76 bool IsEmptyThreadSafe() const {
77 return this->GetSlotThreadSafe() == nullptr;
78 }
79
80 protected:
82
86 void SetSlotThreadSafe(void* new_val) {
87 reinterpret_cast<std::atomic<void*>*>(&slot())->store(
88 new_val, std::memory_order_relaxed);
89 }
90
94 const void* GetSlotThreadSafe() const {
95 return reinterpret_cast<std::atomic<const void*> const*>(&slot())->load(
96 std::memory_order_relaxed);
97 }
98
99 V8_EXPORT void CheckValue() const;
100
102 template <typename F>
103 friend class Local;
104 template <typename U>
105 friend bool operator==(const TracedReferenceBase&, const Local<U>&);
106 friend bool operator==(const TracedReferenceBase&,
107 const TracedReferenceBase&);
108};
109
124template <typename T>
126 public:
130 Local<T> Get(Isolate* isolate) const { return Local<T>::New(isolate, *this); }
131
132 template <class S>
134 return reinterpret_cast<BasicTracedReference<S>&>(
135 const_cast<BasicTracedReference<T>&>(*this));
136 }
137
138 V8_DEPRECATE_SOON("Use Get to convert to Local instead")
139 V8_INLINE T* operator->() const {
140#ifdef V8_ENABLE_CHECKS
141 CheckValue();
142#endif // V8_ENABLE_CHECKS
143 return this->template value<T>();
144 }
145
146 V8_DEPRECATE_SOON("Use Get to convert to Local instead")
147 V8_INLINE T* operator*() const { return this->operator->(); }
148
149 private:
153 BasicTracedReference() = default;
154
155 V8_INLINE static internal::Address* NewFromNonEmptyValue(
156 Isolate* isolate, T* that, internal::Address** slot,
158 internal::TracedReferenceHandling reference_handling);
159
160 template <typename F>
161 friend class Local;
162 friend class Object;
163 template <typename F>
164 friend class TracedReference;
165 template <typename F>
167 template <typename F>
168 friend class ReturnValue;
169};
170
176template <typename T>
178 public:
179 struct IsDroppable {};
180
181 using BasicTracedReference<T>::Reset;
182
187
194 template <class S>
196 static_assert(std::is_base_of<T, S>::value, "type check");
197 if (V8_UNLIKELY(that.IsEmpty())) {
198 return;
199 }
200 this->slot() = this->NewFromNonEmptyValue(
201 isolate, *that, &this->slot(),
204 }
205
214 template <class S>
216 : BasicTracedReference<T>() {
217 static_assert(std::is_base_of<T, S>::value, "type check");
218 if (V8_UNLIKELY(that.IsEmpty())) {
219 return;
220 }
221 this->slot() = this->NewFromNonEmptyValue(
222 isolate, *that, &this->slot(),
225 }
226
232 // Forward to operator=.
233 *this = std::move(other);
234 }
235
240 template <typename S>
242 // Forward to operator=.
243 *this = std::move(other);
244 }
245
251 // Forward to operator=;
252 *this = other;
253 }
254
259 template <typename S>
261 // Forward to operator=;
262 *this = other;
263 }
264
268 V8_INLINE TracedReference& operator=(TracedReference&& rhs) noexcept;
269
273 template <class S>
275
279 V8_INLINE TracedReference& operator=(const TracedReference& rhs);
280
284 template <class S>
286
291 template <class S>
292 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
293
298 template <class S>
299 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other, IsDroppable);
300
301 template <class S>
303 return reinterpret_cast<TracedReference<S>&>(
304 const_cast<TracedReference<T>&>(*this));
305 }
306};
307
308// --- Implementation ---
309template <class T>
310internal::Address* BasicTracedReference<T>::NewFromNonEmptyValue(
311 Isolate* isolate, T* that, internal::Address** slot,
313 internal::TracedReferenceHandling reference_handling) {
315 reinterpret_cast<internal::Isolate*>(isolate),
317 reinterpret_cast<internal::Address*>(slot), store_mode,
318 reference_handling);
319}
320
322 if (V8_UNLIKELY(IsEmpty())) {
323 return;
324 }
326 SetSlotThreadSafe(nullptr);
327}
328
330 const TracedReferenceBase& rhs) {
332}
333
334template <typename U>
336 const v8::Local<U>& rhs) {
338}
339
340template <typename U>
342 const TracedReferenceBase& rhs) {
343 return rhs == lhs;
344}
345
347 const TracedReferenceBase& rhs) {
348 return !(lhs == rhs);
349}
350
351template <typename U>
353 const v8::Local<U>& rhs) {
354 return !(lhs == rhs);
355}
356
357template <typename U>
359 const TracedReferenceBase& rhs) {
360 return !(rhs == lhs);
361}
362
363template <class T>
364template <class S>
365void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other) {
366 static_assert(std::is_base_of<T, S>::value, "type check");
367 this->Reset();
368 if (V8_UNLIKELY(other.IsEmpty())) {
369 return;
370 }
371 this->SetSlotThreadSafe(this->NewFromNonEmptyValue(
372 isolate, *other, &this->slot(),
375}
376
377template <class T>
378template <class S>
379void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other,
380 IsDroppable) {
381 static_assert(std::is_base_of<T, S>::value, "type check");
382 this->Reset();
383 if (V8_UNLIKELY(other.IsEmpty())) {
384 return;
385 }
386 this->SetSlotThreadSafe(this->NewFromNonEmptyValue(
387 isolate, *other, &this->slot(),
390}
391
392template <class T>
393template <class S>
395 TracedReference<S>&& rhs) noexcept {
396 static_assert(std::is_base_of<T, S>::value, "type check");
397 *this = std::move(rhs.template As<T>());
398 return *this;
399}
400
401template <class T>
402template <class S>
404 const TracedReference<S>& rhs) {
405 static_assert(std::is_base_of<T, S>::value, "type check");
406 *this = rhs.template As<T>();
407 return *this;
408}
409
410template <class T>
412 TracedReference&& rhs) noexcept {
413 if (this != &rhs) {
414 internal::MoveTracedReference(&rhs.slot(), &this->slot());
415 }
416 return *this;
417}
418
419template <class T>
421 if (this != &rhs) {
422 this->Reset();
423 if (!rhs.IsEmpty()) {
424 internal::CopyTracedReference(&rhs.slot(), &this->slot());
425 }
426 }
427 return *this;
428}
429
430} // namespace v8
431
432#endif // INCLUDE_V8_TRACED_HANDLE_H_
V8_INLINE T * operator->() const
V8_INLINE BasicTracedReference< S > & As() const
Local< T > Get(Isolate *isolate) const
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
const void * GetSlotThreadSafe() const
V8_EXPORT void CheckValue() const
friend class internal::BasicTracedReferenceExtractor
friend bool operator==(const TracedReferenceBase &, const Local< U > &)
V8_INLINE Local< Value > Get(Isolate *isolate) const
V8_INLINE TracedReferenceBase()=default
void SetSlotThreadSafe(void *new_val)
V8_INLINE void Reset(Isolate *isolate, const Local< S > &other)
V8_INLINE TracedReference(TracedReference &&other) noexcept
V8_INLINE TracedReference & operator=(TracedReference< S > &&rhs) noexcept
V8_INLINE TracedReference & operator=(const TracedReference< S > &rhs)
TracedReference(Isolate *isolate, Local< S > that, IsDroppable)
V8_INLINE TracedReference(const TracedReference &other)
V8_INLINE void Reset(Isolate *isolate, const Local< S > &other, IsDroppable)
V8_INLINE TracedReference< S > & As() const
TracedReference(Isolate *isolate, Local< S > that)
V8_INLINE TracedReference()=default
V8_INLINE TracedReference(TracedReference< S > &&other) noexcept
V8_INLINE TracedReference(const TracedReference< S > &other)
V8_INLINE TracedReference & operator=(TracedReference &&rhs) noexcept
V8_INLINE internal::Address *const & slot() const
static V8_INLINE bool EqualHandles(const T1 &lhs, const T2 &rhs)
static V8_INLINE Address ValueAsAddress(const T *value)
V8_EXPORT internal::Address * GlobalizeTracedReference(internal::Isolate *isolate, internal::Address value, internal::Address *slot, TracedReferenceStoreMode store_mode, internal::TracedReferenceHandling reference_handling)
V8_EXPORT void CopyTracedReference(const internal::Address *const *from, internal::Address **to)
uintptr_t Address
Definition v8-internal.h:31
V8_EXPORT void MoveTracedReference(internal::Address **from, internal::Address **to)
V8_EXPORT void DisposeTracedReference(internal::Address *global_handle)
V8_INLINE bool operator==(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
V8_INLINE bool operator!=(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
@ V8_DEPRECATE_SOON
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477
#define V8_UNLIKELY(condition)
Definition v8config.h:617