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 std::memory_order order = std::memory_order_relaxed) {
87 reinterpret_cast<std::atomic<internal::Address*>*>(&slot())->store(new_val,
88 order);
89 }
90
95 return reinterpret_cast<const std::atomic<internal::Address*>*>(&slot())
96 ->load(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 private:
142 BasicTracedReference() = default;
143
144 V8_INLINE static internal::Address* NewFromNonEmptyValue(
145 Isolate* isolate, T* that, internal::Address** slot,
147 internal::TracedReferenceHandling reference_handling);
148
149 template <typename F>
150 friend class Local;
151 friend class Object;
152 template <typename F>
153 friend class TracedReference;
154 template <typename F>
156 template <typename F>
157 friend class ReturnValue;
158};
159
165template <typename T>
167 public:
168 struct IsDroppable {};
169
171
176
183 template <class S>
185 static_assert(std::is_base_of_v<T, S>, "type check");
186 if (V8_UNLIKELY(that.IsEmpty())) {
187 return;
188 }
189 this->SetSlotThreadSafe(
190 this->NewFromNonEmptyValue(
191 isolate, *that, &this->slot(),
194 std::memory_order_release);
195 }
196
205 template <class S>
207 : BasicTracedReference<T>() {
208 static_assert(std::is_base_of_v<T, S>, "type check");
209 if (V8_UNLIKELY(that.IsEmpty())) {
210 return;
211 }
212 this->SetSlotThreadSafe(
213 this->NewFromNonEmptyValue(
214 isolate, *that, &this->slot(),
217 std::memory_order_release);
218 }
219
225 // Forward to operator=.
226 *this = std::move(other);
227 }
228
233 template <typename S>
235 // Forward to operator=.
236 *this = std::move(other);
237 }
238
244 // Forward to operator=;
245 *this = other;
246 }
247
252 template <typename S>
254 // Forward to operator=;
255 *this = other;
256 }
257
262
266 template <class S>
268
273
277 template <class S>
279
284 template <class S>
285 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
286
291 template <class S>
292 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other, IsDroppable);
293
294 template <class S>
296 return reinterpret_cast<TracedReference<S>&>(
297 const_cast<TracedReference<T>&>(*this));
298 }
299};
300
301// --- Implementation ---
302template <class T>
303internal::Address* BasicTracedReference<T>::NewFromNonEmptyValue(
304 Isolate* isolate, T* that, internal::Address** slot,
306 internal::TracedReferenceHandling reference_handling) {
308 reinterpret_cast<internal::Isolate*>(isolate),
310 reinterpret_cast<internal::Address*>(slot), store_mode,
311 reference_handling);
312}
313
315 if (V8_UNLIKELY(IsEmpty())) {
316 return;
317 }
319 SetSlotThreadSafe(nullptr);
320}
321
323 const TracedReferenceBase& rhs) {
325}
326
327template <typename U>
329 const v8::Local<U>& rhs) {
331}
332
333template <typename U>
335 const TracedReferenceBase& rhs) {
336 return rhs == lhs;
337}
338
340 const TracedReferenceBase& rhs) {
341 return !(lhs == rhs);
342}
343
344template <typename U>
346 const v8::Local<U>& rhs) {
347 return !(lhs == rhs);
348}
349
350template <typename U>
352 const TracedReferenceBase& rhs) {
353 return !(rhs == lhs);
354}
355
356template <class T>
357template <class S>
358void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other) {
359 static_assert(std::is_base_of_v<T, S>, "type check");
360 this->Reset();
361 if (V8_UNLIKELY(other.IsEmpty())) {
362 return;
363 }
364 this->SetSlotThreadSafe(
365 this->NewFromNonEmptyValue(
366 isolate, *other, &this->slot(),
369 std::memory_order_release);
370}
371
372template <class T>
373template <class S>
374void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other,
375 IsDroppable) {
376 static_assert(std::is_base_of_v<T, S>, "type check");
377 this->Reset();
378 if (V8_UNLIKELY(other.IsEmpty())) {
379 return;
380 }
381 this->SetSlotThreadSafe(
382 this->NewFromNonEmptyValue(
383 isolate, *other, &this->slot(),
386 std::memory_order_release);
387}
388
389template <class T>
390template <class S>
392 TracedReference<S>&& rhs) noexcept {
393 static_assert(std::is_base_of_v<T, S>, "type check");
394 *this = std::move(rhs.template As<T>());
395 return *this;
396}
397
398template <class T>
399template <class S>
401 const TracedReference<S>& rhs) {
402 static_assert(std::is_base_of_v<T, S>, "type check");
403 *this = rhs.template As<T>();
404 return *this;
405}
406
407template <class T>
409 TracedReference&& rhs) noexcept {
410 if (this != &rhs) {
411 internal::MoveTracedReference(&rhs.slot(), &this->slot());
412 }
413 return *this;
414}
415
416template <class T>
418 if (this != &rhs) {
419 this->Reset();
420 if (!rhs.IsEmpty()) {
421 internal::CopyTracedReference(&rhs.slot(), &this->slot());
422 }
423 }
424 return *this;
425}
426
427} // namespace v8
428
429#endif // INCLUDE_V8_TRACED_HANDLE_H_
Definition: v8-traced-handle.h:125
BasicTracedReference< S > & As() const
Definition: v8-traced-handle.h:133
Local< T > Get(Isolate *isolate) const
Definition: v8-traced-handle.h:130
friend class BasicTracedReference
Definition: v8-traced-handle.h:155
Definition: v8-isolate.h:298
Definition: v8-local-handle.h:366
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:448
Definition: v8-object.h:267
Definition: v8-function-callback.h:43
Definition: v8-traced-handle.h:54
void SetSlotThreadSafe(internal::Address *new_val, std::memory_order order=std::memory_order_relaxed)
Definition: v8-traced-handle.h:85
Local< Data > Get(Isolate *isolate) const
Definition: v8-traced-handle.h:68
void Reset()
Definition: v8-traced-handle.h:314
const internal::Address * GetSlotThreadSafe() const
Definition: v8-traced-handle.h:94
friend class internal::BasicTracedReferenceExtractor
Definition: v8-traced-handle.h:101
friend bool operator==(const TracedReferenceBase &, const Local< U > &)
Definition: v8-traced-handle.h:328
bool IsEmptyThreadSafe() const
Definition: v8-traced-handle.h:77
Definition: v8-traced-handle.h:166
TracedReference(TracedReference &&other) noexcept
Definition: v8-traced-handle.h:224
TracedReference(Isolate *isolate, Local< S > that)
Definition: v8-traced-handle.h:184
TracedReference(const TracedReference &other)
Definition: v8-traced-handle.h:243
TracedReference< S > & As() const
Definition: v8-traced-handle.h:295
TracedReference(TracedReference< S > &&other) noexcept
Definition: v8-traced-handle.h:234
TracedReference()=default
TracedReference & operator=(const TracedReference< S > &rhs)
TracedReference(const TracedReference< S > &other)
Definition: v8-traced-handle.h:253
TracedReference & operator=(TracedReference< S > &&rhs) noexcept
TracedReference(Isolate *isolate, Local< S > that, IsDroppable)
Definition: v8-traced-handle.h:206
TracedReference & operator=(TracedReference &&rhs) noexcept
Definition: v8-traced-handle.h:408
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:1896
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1857
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:38
Definition: libplatform.h:15
bool operator!=(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
Definition: v8-traced-handle.h:339
bool operator==(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
Definition: v8-traced-handle.h:322
Definition: v8-traced-handle.h:168
#define V8_EXPORT
Definition: v8config.h:874
#define V8_INLINE
Definition: v8config.h:518
#define V8_UNLIKELY(condition)
Definition: v8config.h:677