v8 11.3.244 (node 20.3.0)
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 internal::Isolate* isolate, internal::Address* handle,
43
44} // namespace internal
45
47 public:
52 bool IsEmpty() const { return val_ == nullptr; }
53
58 V8_INLINE void Reset();
59
64 if (IsEmpty()) return Local<Value>();
65 return Local<Value>::New(isolate,
66 internal::ValueHelper::SlotAsValue<Value>(val_));
67 }
68
73 bool IsEmptyThreadSafe() const {
74 return this->GetSlotThreadSafe() == nullptr;
75 }
76
80 V8_INLINE void SetWrapperClassId(uint16_t class_id);
81
86 V8_INLINE uint16_t WrapperClassId() const;
87
88 protected:
92 void SetSlotThreadSafe(void* new_val) {
93 reinterpret_cast<std::atomic<void*>*>(&val_)->store(
94 new_val, std::memory_order_relaxed);
95 }
96
100 const void* GetSlotThreadSafe() const {
101 return reinterpret_cast<std::atomic<const void*> const*>(&val_)->load(
102 std::memory_order_relaxed);
103 }
104
105 V8_EXPORT void CheckValue() const;
106
108
109 // val_ points to a GlobalHandles node.
111
114 template <typename F>
115 friend class Local;
116 template <typename U>
117 friend bool operator==(const TracedReferenceBase&, const Local<U>&);
118 friend bool operator==(const TracedReferenceBase&,
119 const TracedReferenceBase&);
120};
121
136template <typename T>
138 public:
142 Local<T> Get(Isolate* isolate) const {
143#ifdef V8_ENABLE_CONSERVATIVE_STACK_SCANNING
144 if (val_ == nullptr) return Local<T>();
145#endif
146 return Local<T>::New(isolate, *this);
147 }
148
149 template <class S>
151 return reinterpret_cast<BasicTracedReference<S>&>(
152 const_cast<BasicTracedReference<T>&>(*this));
153 }
154
155 T* operator->() const {
156#ifdef V8_ENABLE_CHECKS
157 CheckValue();
158#endif // V8_ENABLE_CHECKS
159 return reinterpret_cast<T*>(val_);
160 }
161 T* operator*() const {
162#ifdef V8_ENABLE_CHECKS
163 CheckValue();
164#endif // V8_ENABLE_CHECKS
165 return reinterpret_cast<T*>(val_);
166 }
167
168 private:
172 BasicTracedReference() = default;
173
174 V8_INLINE static internal::Address* New(
175 Isolate* isolate, T* that, void* slot,
177
178 template <typename F>
179 friend class Local;
180 friend class Object;
181 template <typename F>
182 friend class TracedReference;
183 template <typename F>
185 template <typename F>
186 friend class ReturnValue;
187};
188
194template <typename T>
196 public:
198
203
210 template <class S>
212 this->val_ = this->New(isolate, *that, &this->val_,
214 static_assert(std::is_base_of<T, S>::value, "type check");
215 }
216
222 // Forward to operator=.
223 *this = std::move(other);
224 }
225
230 template <typename S>
232 // Forward to operator=.
233 *this = std::move(other);
234 }
235
241 // Forward to operator=;
242 *this = other;
243 }
244
249 template <typename S>
251 // Forward to operator=;
252 *this = other;
253 }
254
259
263 template <class S>
265
270
274 template <class S>
276
281 template <class S>
282 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
283
284 template <class S>
286 return reinterpret_cast<TracedReference<S>&>(
287 const_cast<TracedReference<T>&>(*this));
288 }
289};
290
291// --- Implementation ---
292template <class T>
293internal::Address* BasicTracedReference<T>::New(
294 Isolate* isolate, T* that, void* slot,
296 if (that == internal::ValueHelper::EmptyValue<T>()) return nullptr;
297 internal::Address* p = reinterpret_cast<internal::Address*>(that);
299 reinterpret_cast<internal::Isolate*>(isolate), p,
300 reinterpret_cast<internal::Address*>(slot), store_mode);
301}
302
304 if (IsEmpty()) return;
306 SetSlotThreadSafe(nullptr);
307}
308
310 const TracedReferenceBase& rhs) {
312}
313
314template <typename U>
316 const v8::Local<U>& rhs) {
318}
319
320template <typename U>
322 const TracedReferenceBase& rhs) {
323 return rhs == lhs;
324}
325
327 const TracedReferenceBase& rhs) {
328 return !(lhs == rhs);
329}
330
331template <typename U>
333 const v8::Local<U>& rhs) {
334 return !(lhs == rhs);
335}
336
337template <typename U>
339 const TracedReferenceBase& rhs) {
340 return !(rhs == lhs);
341}
342
343template <class T>
344template <class S>
345void TracedReference<T>::Reset(Isolate* isolate, const Local<S>& other) {
346 static_assert(std::is_base_of<T, S>::value, "type check");
347 this->Reset();
348 if (other.IsEmpty()) return;
349 this->SetSlotThreadSafe(
350 this->New(isolate, *other, &this->val_,
352}
353
354template <class T>
355template <class S>
357 TracedReference<S>&& rhs) noexcept {
358 static_assert(std::is_base_of<T, S>::value, "type check");
359 *this = std::move(rhs.template As<T>());
360 return *this;
361}
362
363template <class T>
364template <class S>
366 const TracedReference<S>& rhs) {
367 static_assert(std::is_base_of<T, S>::value, "type check");
368 *this = rhs.template As<T>();
369 return *this;
370}
371
372template <class T>
374 TracedReference&& rhs) noexcept {
375 if (this != &rhs) {
377 reinterpret_cast<internal::Address**>(&rhs.val_),
378 reinterpret_cast<internal::Address**>(&this->val_));
379 }
380 return *this;
381}
382
383template <class T>
385 if (this != &rhs) {
386 this->Reset();
387 if (rhs.val_ != nullptr) {
389 reinterpret_cast<const internal::Address* const*>(&rhs.val_),
390 reinterpret_cast<internal::Address**>(&this->val_));
391 }
392 }
393 return *this;
394}
395
397 using I = internal::Internals;
398 if (IsEmpty()) return;
399 internal::Address* obj = reinterpret_cast<internal::Address*>(val_);
400 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kTracedNodeClassIdOffset;
401 *reinterpret_cast<uint16_t*>(addr) = class_id;
402}
403
405 using I = internal::Internals;
406 if (IsEmpty()) return 0;
407 internal::Address* obj = reinterpret_cast<internal::Address*>(val_);
408 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kTracedNodeClassIdOffset;
409 return *reinterpret_cast<uint16_t*>(addr);
410}
411
412} // namespace v8
413
414#endif // INCLUDE_V8_TRACED_HANDLE_H_
V8_INLINE BasicTracedReference< S > & As() const
Local< T > Get(Isolate *isolate) const
V8_INLINE bool IsEmpty() const
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
V8_INLINE uint16_t WrapperClassId() const
V8_INLINE v8::Local< v8::Value > Get(v8::Isolate *isolate) const
const void * GetSlotThreadSafe() const
V8_EXPORT void CheckValue() const
friend class internal::BasicTracedReferenceExtractor
friend bool operator==(const TracedReferenceBase &, const Local< U > &)
internal::Address * val_
V8_INLINE internal::Address address() const
V8_INLINE void SetWrapperClassId(uint16_t class_id)
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)
V8_INLINE TracedReference(const TracedReference &other)
V8_INLINE TracedReference< S > & As() const
TracedReference(Isolate *isolate, Local< S > that)
V8_INLINE TracedReference(TracedReference< S > &&other) noexcept
V8_INLINE TracedReference(const TracedReference< S > &other)
V8_INLINE TracedReference & operator=(TracedReference &&rhs) noexcept
static V8_INLINE bool EqualHandles(const T1 &lhs, const T2 &rhs)
V8_EXPORT void CopyTracedReference(const internal::Address *const *from, internal::Address **to)
uintptr_t Address
Definition v8-internal.h:29
V8_EXPORT void MoveTracedReference(internal::Address **from, internal::Address **to)
V8_EXPORT void DisposeTracedReference(internal::Address *global_handle)
V8_EXPORT internal::Address * GlobalizeTracedReference(internal::Isolate *isolate, internal::Address *handle, internal::Address *slot, GlobalHandleStoreMode store_mode)
V8_INLINE bool operator==(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
V8_INLINE bool operator!=(const TracedReferenceBase &lhs, const TracedReferenceBase &rhs)
#define V8_EXPORT
Definition v8config.h:719
#define V8_INLINE
Definition v8config.h:460