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 Isolate* isolate, Address value, Address* slot,
42 TracedReferenceStoreMode store_mode,
43 TracedReferenceHandling reference_handling);
45V8_EXPORT void CopyTracedReference(const Address* const* from, Address** to);
47
48} // namespace internal
49
55 public:
56 static_assert(sizeof(std::atomic<internal::Address*>) ==
57 sizeof(internal::Address*));
58
63 V8_INLINE void Reset();
64
68 V8_INLINE Local<Data> Get(Isolate* isolate) const {
69 if (IsEmpty()) return Local<Data>();
70 return Local<Data>::New(isolate, this->value<Data>());
71 }
72
77 bool IsEmptyThreadSafe() const { return GetSlotThreadSafe() == nullptr; }
78
79 protected:
81
86 reinterpret_cast<std::atomic<internal::Address*>*>(&slot())->store(
87 new_val, std::memory_order_relaxed);
88 }
89
94 return reinterpret_cast<const std::atomic<internal::Address*>*>(&slot())
95 ->load(std::memory_order_relaxed);
96 }
97
98 V8_EXPORT void CheckValue() const;
99
101 template <typename F>
102 friend class Local;
103 template <typename U>
104 friend bool operator==(const TracedReferenceBase&, const Local<U>&);
105 friend bool operator==(const TracedReferenceBase&,
106 const TracedReferenceBase&);
107};
108
123template <typename T>
125 public:
129 Local<T> Get(Isolate* isolate) const { return Local<T>::New(isolate, *this); }
130
131 template <class S>
133 return reinterpret_cast<BasicTracedReference<S>&>(
134 const_cast<BasicTracedReference<T>&>(*this));
135 }
136
137 private:
141 BasicTracedReference() = default;
142
143 V8_INLINE static internal::Address* NewFromNonEmptyValue(
144 Isolate* isolate, T* that, internal::Address** slot,
146 internal::TracedReferenceHandling reference_handling);
147
148 template <typename F>
149 friend class Local;
150 friend class Object;
151 template <typename F>
152 friend class TracedReference;
153 template <typename F>
155 template <typename F>
156 friend class ReturnValue;
157};
158
164template <typename T>
166 public:
167 struct IsDroppable {};
168
170
175
182 template <class S>
184 static_assert(std::is_base_of<T, S>::value, "type check");
185 if (V8_UNLIKELY(that.IsEmpty())) {
186 return;
187 }
188 this->slot() = this->NewFromNonEmptyValue(
189 isolate, *that, &this->slot(),
192 }
193
202 template <class S>
204 : BasicTracedReference<T>() {
205 static_assert(std::is_base_of<T, S>::value, "type check");
206 if (V8_UNLIKELY(that.IsEmpty())) {
207 return;
208 }
209 this->slot() = this->NewFromNonEmptyValue(
210 isolate, *that, &this->slot(),
213 }
214
220 // Forward to operator=.
221 *this = std::move(other);
222 }
223
228 template <typename S>
230 // Forward to operator=.
231 *this = std::move(other);
232 }
233
239 // Forward to operator=;
240 *this = other;
241 }
242
247 template <typename S>
249 // Forward to operator=;
250 *this = other;
251 }
252
257
261 template <class S>
263
268
272 template <class S>
274
279 template <class S>
280 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
281
286 template <class S>
287 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other, IsDroppable);
288
289 template <class S>
291 return reinterpret_cast<TracedReference<S>&>(
292 const_cast<TracedReference<T>&>(*this));
293 }
294};
295
296// --- Implementation ---
297template <class T>
298internal::Address* BasicTracedReference<T>::NewFromNonEmptyValue(
299 Isolate* isolate, T* that, internal::Address** slot,
301 internal::TracedReferenceHandling reference_handling) {
303 reinterpret_cast<internal::Isolate*>(isolate),
305 reinterpret_cast<internal::Address*>(slot), store_mode,
306 reference_handling);
307}
308
310 if (V8_UNLIKELY(IsEmpty())) {
311 return;
312 }
314 SetSlotThreadSafe(nullptr);
315}
316
318 const TracedReferenceBase& rhs) {
320}
321
322template <typename U>
324 const v8::Local<U>& rhs) {
326}
327
328template <typename U>
330 const TracedReferenceBase& rhs) {
331 return rhs == lhs;
332}
333
335 const TracedReferenceBase& rhs) {
336 return !(lhs == rhs);
337}
338
339template <typename U>
341 const v8::Local<U>& rhs) {
342 return !(lhs == rhs);
343}
344
345template <typename U>
347 const TracedReferenceBase& rhs) {
348 return !(rhs == lhs);
349}
350
351template <class T>
352template <class S>
353void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other) {
354 static_assert(std::is_base_of<T, S>::value, "type check");
355 this->Reset();
356 if (V8_UNLIKELY(other.IsEmpty())) {
357 return;
358 }
359 this->SetSlotThreadSafe(this->NewFromNonEmptyValue(
360 isolate, *other, &this->slot(),
363}
364
365template <class T>
366template <class S>
367void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other,
368 IsDroppable) {
369 static_assert(std::is_base_of<T, S>::value, "type check");
370 this->Reset();
371 if (V8_UNLIKELY(other.IsEmpty())) {
372 return;
373 }
374 this->SetSlotThreadSafe(this->NewFromNonEmptyValue(
375 isolate, *other, &this->slot(),
378}
379
380template <class T>
381template <class S>
383 TracedReference<S>&& rhs) noexcept {
384 static_assert(std::is_base_of<T, S>::value, "type check");
385 *this = std::move(rhs.template As<T>());
386 return *this;
387}
388
389template <class T>
390template <class S>
392 const TracedReference<S>& rhs) {
393 static_assert(std::is_base_of<T, S>::value, "type check");
394 *this = rhs.template As<T>();
395 return *this;
396}
397
398template <class T>
400 TracedReference&& rhs) noexcept {
401 if (this != &rhs) {
402 internal::MoveTracedReference(&rhs.slot(), &this->slot());
403 }
404 return *this;
405}
406
407template <class T>
409 if (this != &rhs) {
410 this->Reset();
411 if (!rhs.IsEmpty()) {
412 internal::CopyTracedReference(&rhs.slot(), &this->slot());
413 }
414 }
415 return *this;
416}
417
418} // namespace v8
419
420#endif // INCLUDE_V8_TRACED_HANDLE_H_
Definition: v8-traced-handle.h:124
BasicTracedReference< S > & As() const
Definition: v8-traced-handle.h:132
Local< T > Get(Isolate *isolate) const
Definition: v8-traced-handle.h:129
friend class BasicTracedReference
Definition: v8-traced-handle.h:154
Definition: v8-isolate.h:261
Definition: v8-local-handle.h:266
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:346
Definition: v8-object.h:233
Definition: v8-function-callback.h:41
Definition: v8-traced-handle.h:54
Local< Data > Get(Isolate *isolate) const
Definition: v8-traced-handle.h:68
void Reset()
Definition: v8-traced-handle.h:309
const internal::Address * GetSlotThreadSafe() const
Definition: v8-traced-handle.h:93
friend class internal::BasicTracedReferenceExtractor
Definition: v8-traced-handle.h:100
friend bool operator==(const TracedReferenceBase &, const Local< U > &)
Definition: v8-traced-handle.h:323
void SetSlotThreadSafe(internal::Address *new_val)
Definition: v8-traced-handle.h:85
bool IsEmptyThreadSafe() const
Definition: v8-traced-handle.h:77
Definition: v8-traced-handle.h:165
TracedReference(TracedReference &&other) noexcept
Definition: v8-traced-handle.h:219
TracedReference(Isolate *isolate, Local< S > that)
Definition: v8-traced-handle.h:183
TracedReference(const TracedReference &other)
Definition: v8-traced-handle.h:238
TracedReference< S > & As() const
Definition: v8-traced-handle.h:290
TracedReference(TracedReference< S > &&other) noexcept
Definition: v8-traced-handle.h:229
TracedReference()=default
TracedReference & operator=(const TracedReference< S > &rhs)
TracedReference(const TracedReference< S > &other)
Definition: v8-traced-handle.h:248
TracedReference & operator=(TracedReference< S > &&rhs) noexcept
TracedReference(Isolate *isolate, Local< S > that, IsDroppable)
Definition: v8-traced-handle.h:203
TracedReference & operator=(TracedReference &&rhs) noexcept
Definition: v8-traced-handle.h:399
Definition: v8-handle-base.h:57
internal::Address *const & slot() const
Definition: v8-handle-base.h:83
bool IsEmpty() const
Definition: v8-handle-base.h:60
static bool EqualHandles(const T1 &lhs, const T2 &rhs)
Definition: v8-internal.h:1664
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1625
Address * GlobalizeTracedReference(Isolate *isolate, Address value, Address *slot, TracedReferenceStoreMode store_mode, TracedReferenceHandling reference_handling)
void MoveTracedReference(Address **from, Address **to)
TracedReferenceHandling
Definition: v8-traced-handle.h:35
TracedReferenceStoreMode
Definition: v8-traced-handle.h:30
void CopyTracedReference(const Address *const *from, Address **to)
void DisposeTracedReference(Address *global_handle)
uintptr_t Address
Definition: v8-internal.h:52
Definition: libplatform.h:15
bool operator!=(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
Definition: v8-traced-handle.h:334
bool operator==(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
Definition: v8-traced-handle.h:317
Definition: v8-traced-handle.h:167
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_UNLIKELY(condition)
Definition: v8config.h:649