Loading...
Searching...
No Matches
v8-persistent-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_PERSISTENT_HANDLE_H_
6#define INCLUDE_V8_PERSISTENT_HANDLE_H_
7
8#include "v8-internal.h" // NOLINT(build/include_directory)
9#include "v8-local-handle.h" // NOLINT(build/include_directory)
10#include "v8-weak-callback-info.h" // NOLINT(build/include_directory)
11#include "v8config.h" // NOLINT(build/include_directory)
12
13namespace v8 {
14
15class Isolate;
16template <class K, class V, class T>
17class PersistentValueMapBase;
18template <class T>
19class Global;
20template <class T>
21class PersistentBase;
22template <class K, class V, class T>
23class PersistentValueMap;
24class Value;
25
26namespace api_internal {
30V8_EXPORT void MakeWeak(internal::Address** location_addr);
33 const char* label);
35 internal::Address value);
38} // namespace api_internal
39
44template <class T>
46 public:
47 V8_INLINE Eternal() = default;
48
52 template <class S>
53 requires(std::is_base_of_v<T, S>)
54 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) {
55 Set(isolate, handle);
56 }
57
58 // Can only be safely called if already set.
59 V8_INLINE Local<T> Get(Isolate* isolate) const {
60 // The eternal handle will never go away, so as with the roots, we don't
61 // even need to open a handle.
62 return Local<T>::FromSlot(slot());
63 }
64
65 template <class S>
66 requires(std::is_base_of_v<T, S>)
67 void Set(Isolate* isolate, Local<S> handle) {
68 slot() =
69 api_internal::Eternalize(isolate, *handle.template UnsafeAs<Value>());
70 }
71};
72
73namespace api_internal {
74V8_EXPORT void MakeWeak(internal::Address* location, void* data,
76 WeakCallbackType type);
77} // namespace api_internal
78
92template <class T>
94 public:
100
105 template <class S>
106 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
107
112 template <class S>
113 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
114
115 V8_INLINE Local<T> Get(Isolate* isolate) const {
116 return Local<T>::New(isolate, *this);
117 }
118
119 template <class S>
120 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
121 return internal::HandleHelper::EqualHandles(*this, that);
122 }
123
124 template <class S>
125 V8_INLINE bool operator==(const Local<S>& that) const {
126 return internal::HandleHelper::EqualHandles(*this, that);
127 }
128
129 template <class S>
130 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
131 return !operator==(that);
132 }
133
134 template <class S>
135 V8_INLINE bool operator!=(const Local<S>& that) const {
136 return !operator==(that);
137 }
138
151 template <typename P>
152 V8_INLINE void SetWeak(P* parameter,
153 typename WeakCallbackInfo<P>::Callback callback,
154 WeakCallbackType type);
155
162
163 template <typename P>
165
166 V8_DEPRECATE_SOON("Use ClearWeak<void>() instead.")
167 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
168
175 V8_INLINE void AnnotateStrongRetainer(const char* label);
176
178 V8_INLINE bool IsWeak() const;
179
183 V8_INLINE void SetWrapperClassId(uint16_t class_id);
184
189 V8_INLINE uint16_t WrapperClassId() const;
190
191 PersistentBase(const PersistentBase& other) = delete;
192 void operator=(const PersistentBase&) = delete;
193
194 private:
195 friend class Isolate;
196 friend class Utils;
197 template <class F>
198 friend class Local;
199 template <class F1, class F2>
200 friend class Persistent;
201 template <class F>
202 friend class Global;
203 template <class F>
204 friend class PersistentBase;
205 template <class F>
206 friend class ReturnValue;
207 template <class F1, class F2, class F3>
209 friend class Object;
211
212 V8_INLINE PersistentBase() = default;
213
214 V8_INLINE explicit PersistentBase(internal::Address* location)
215 : IndirectHandleBase(location) {}
216
217 V8_INLINE static internal::Address* New(Isolate* isolate, T* that);
218};
219
226template <class T>
228 public:
230 static const bool kResetInDestructor = false;
231 template <class S, class M>
232 V8_INLINE static void Copy(const Persistent<S, M>& source,
233 NonCopyablePersistent* dest) {
234 static_assert(sizeof(S) < 0,
235 "NonCopyablePersistentTraits::Copy is not instantiable");
236 }
237};
238
249template <class T, class M>
250class Persistent : public PersistentBase<T> {
251 public:
256
262 template <class S>
263 requires(std::is_base_of_v<T, S>)
266 PersistentBase<T>::New(isolate, that.template value<S>())) {}
267
273 template <class S, class M2>
274 requires(std::is_base_of_v<T, S>)
277 PersistentBase<T>::New(isolate, that.template value<S>())) {}
278
286 Copy(that);
287 }
288 template <class S, class M2>
290 Copy(that);
291 }
293 Copy(that);
294 return *this;
295 }
296 template <class S, class M2>
298 Copy(that);
299 return *this;
300 }
301
308 if (M::kResetInDestructor) this->Reset();
309 }
310
311 template <class S, class M2>
312 V8_DEPRECATE_SOON("Use Local::New(...).As<T>()")
313 V8_INLINE static Persistent<T, M>& Cast(const Persistent<S, M2>& that) {
314#ifdef V8_ENABLE_CHECKS
315 // If we're going to perform the type check then we have to check
316 // that the handle isn't empty before doing the checked cast.
317 if (!that.IsEmpty()) T::Cast(that.template value<S>());
318#endif
319 return reinterpret_cast<Persistent<T, M>&>(
320 const_cast<Persistent<S, M2>&>(that));
321 }
322
323 template <class S, class M2>
324 V8_DEPRECATE_SOON("Use Local::New(...).As<T>()")
325 V8_INLINE Persistent<S, M2>& As() const {
326#ifdef V8_ENABLE_CHECKS
327 if (!this->IsEmpty()) S::Cast(this->template value<T>());
328#endif
329 return reinterpret_cast<Persistent<S, M2>&>(
330 const_cast<Persistent<T, M>&>(*this));
331 }
332
333 private:
334 friend class Isolate;
335 friend class Utils;
336 template <class F>
337 friend class Local;
338 template <class F1, class F2>
339 friend class Persistent;
340 template <class F>
341 friend class ReturnValue;
342
343 template <class S, class M2>
344 V8_INLINE void Copy(const Persistent<S, M2>& that);
345};
346
352template <class T>
353class Global : public PersistentBase<T> {
354 public:
358 V8_INLINE Global() = default;
359
365 template <class S>
366 requires(std::is_base_of_v<T, S>)
369 PersistentBase<T>::New(isolate, that.template value<S>())) {}
370
376 template <class S>
377 requires(std::is_base_of_v<T, S>)
380 PersistentBase<T>::New(isolate, that.template value<S>())) {}
381
386
387 V8_INLINE ~Global() { this->Reset(); }
388
392 template <class S>
394
398 Global Pass() { return static_cast<Global&&>(*this); }
399
400 /*
401 * For compatibility with Chromium's base::Bind (base::Passed).
402 */
404
405 Global(const Global&) = delete;
406 void operator=(const Global&) = delete;
407
408 private:
409 template <class F>
410 friend class ReturnValue;
411};
412
413// UniquePersistent is an alias for Global for historical reason.
414template <class T>
416
421 public:
422 virtual ~PersistentHandleVisitor() = default;
424 uint16_t class_id) {}
425};
426
427template <class T>
428internal::Address* PersistentBase<T>::New(Isolate* isolate, T* that) {
429 if (internal::ValueHelper::IsEmpty(that)) return nullptr;
431 reinterpret_cast<internal::Isolate*>(isolate),
433}
434
435template <class T, class M>
436template <class S, class M2>
437void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
438 static_assert(std::is_base_of_v<T, S>, "type check");
439 this->Reset();
440 if (that.IsEmpty()) return;
441 this->slot() = api_internal::CopyGlobalReference(that.slot());
442 M::Copy(that, this);
443}
444
445template <class T>
447 using I = internal::Internals;
448 if (this->IsEmpty()) return false;
449 return I::GetNodeState(this->slot()) == I::kNodeStateIsWeakValue;
450}
451
452template <class T>
454 if (this->IsEmpty()) return;
455 api_internal::DisposeGlobal(this->slot());
456 this->Clear();
457}
458
463template <class T>
464template <class S>
465void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
466 static_assert(std::is_base_of_v<T, S>, "type check");
467 Reset();
468 if (other.IsEmpty()) return;
469 this->slot() = New(isolate, *other);
470}
471
476template <class T>
477template <class S>
479 const PersistentBase<S>& other) {
480 static_assert(std::is_base_of_v<T, S>, "type check");
481 Reset();
482 if (other.IsEmpty()) return;
483 this->slot() = New(isolate, other.template value<S>());
484}
485
486template <class T>
487template <typename P>
489 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
490 WeakCallbackType type) {
491 using Callback = WeakCallbackInfo<void>::Callback;
492#if (__GNUC__ >= 8) || defined(__clang__)
493#pragma GCC diagnostic push
494#pragma GCC diagnostic ignored "-Wcast-function-type"
495#endif
496 api_internal::MakeWeak(this->slot(), parameter,
497 reinterpret_cast<Callback>(callback), type);
498#if (__GNUC__ >= 8) || defined(__clang__)
499#pragma GCC diagnostic pop
500#endif
501}
502
503template <class T>
505 api_internal::MakeWeak(&this->slot());
506}
507
508template <class T>
509template <typename P>
511 return reinterpret_cast<P*>(api_internal::ClearWeak(this->slot()));
512}
513
514template <class T>
516 api_internal::AnnotateStrongRetainer(this->slot(), label);
517}
518
519template <class T>
521 using I = internal::Internals;
522 if (this->IsEmpty()) return;
523 uint8_t* addr = reinterpret_cast<uint8_t*>(slot()) + I::kNodeClassIdOffset;
524 *reinterpret_cast<uint16_t*>(addr) = class_id;
525}
526
527template <class T>
529 using I = internal::Internals;
530 if (this->IsEmpty()) return 0;
531 uint8_t* addr = reinterpret_cast<uint8_t*>(slot()) + I::kNodeClassIdOffset;
532 return *reinterpret_cast<uint16_t*>(addr);
533}
534
535template <class T>
536Global<T>::Global(Global&& other) : PersistentBase<T>(other.slot()) {
537 if (!other.IsEmpty()) {
538 api_internal::MoveGlobalReference(&other.slot(), &this->slot());
539 other.Clear();
540 }
541}
542
543template <class T>
544template <class S>
546 static_assert(std::is_base_of_v<T, S>, "type check");
547 if (this != &rhs) {
548 this->Reset();
549 if (!rhs.IsEmpty()) {
550 this->slot() = rhs.slot();
551 api_internal::MoveGlobalReference(&rhs.slot(), &this->slot());
552 rhs.Clear();
553 }
554 }
555 return *this;
556}
557
558} // namespace v8
559
560#endif // INCLUDE_V8_PERSISTENT_HANDLE_H_
Definition: v8-persistent-handle.h:45
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:59
Eternal(Isolate *isolate, Local< S > handle)
Definition: v8-persistent-handle.h:54
Eternal()=default
void Set(Isolate *isolate, Local< S > handle)
Definition: v8-persistent-handle.h:67
Definition: v8-persistent-handle.h:353
void MoveOnlyTypeForCPP03
Definition: v8-persistent-handle.h:403
Global()=default
Global & operator=(Global< S > &&rhs)
void operator=(const Global &)=delete
~Global()
Definition: v8-persistent-handle.h:387
Global(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:367
Global Pass()
Definition: v8-persistent-handle.h:398
Global(Global &&other)
Definition: v8-persistent-handle.h:536
Global(Isolate *isolate, const PersistentBase< S > &that)
Definition: v8-persistent-handle.h:378
Global(const Global &)=delete
Definition: v8-isolate.h:291
Definition: v8-local-handle.h:366
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:448
Definition: v8-persistent-handle.h:227
static const bool kResetInDestructor
Definition: v8-persistent-handle.h:230
static void Copy(const Persistent< S, M > &source, NonCopyablePersistent *dest)
Definition: v8-persistent-handle.h:232
Definition: v8-object.h:266
Definition: v8-persistent-handle.h:93
void SetWeak(P *parameter, typename WeakCallbackInfo< P >::Callback callback, WeakCallbackType type)
Definition: v8-persistent-handle.h:488
void SetWeak()
Definition: v8-persistent-handle.h:504
void Reset()
Definition: v8-persistent-handle.h:453
void AnnotateStrongRetainer(const char *label)
Definition: v8-persistent-handle.h:515
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:115
bool IsWeak() const
Definition: v8-persistent-handle.h:446
void Reset(Isolate *isolate, const PersistentBase< S > &other)
Definition: v8-persistent-handle.h:478
bool operator==(const Local< S > &that) const
Definition: v8-persistent-handle.h:125
P * ClearWeak()
Definition: v8-persistent-handle.h:510
PersistentBase(const PersistentBase &other)=delete
bool operator!=(const PersistentBase< S > &that) const
Definition: v8-persistent-handle.h:130
void Reset(Isolate *isolate, const Local< S > &other)
Definition: v8-persistent-handle.h:465
bool operator==(const PersistentBase< S > &that) const
Definition: v8-persistent-handle.h:120
friend class PersistentBase
Definition: v8-persistent-handle.h:204
friend class Utils
Definition: v8-persistent-handle.h:196
void SetWrapperClassId(uint16_t class_id)
Definition: v8-persistent-handle.h:520
uint16_t WrapperClassId() const
Definition: v8-persistent-handle.h:528
bool operator!=(const Local< S > &that) const
Definition: v8-persistent-handle.h:135
void operator=(const PersistentBase &)=delete
Definition: v8-persistent-handle.h:420
virtual void VisitPersistentHandle(Persistent< Value > *value, uint16_t class_id)
Definition: v8-persistent-handle.h:423
virtual ~PersistentHandleVisitor()=default
Definition: v8-util.h:166
Definition: v8-persistent-handle.h:250
~Persistent()
Definition: v8-persistent-handle.h:307
Persistent(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:289
Persistent()=default
Persistent< S, M2 > & As() const
Definition: v8-persistent-handle.h:325
Persistent & operator=(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:297
Persistent & operator=(const Persistent &that)
Definition: v8-persistent-handle.h:292
friend class Utils
Definition: v8-persistent-handle.h:335
Persistent(Isolate *isolate, const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:275
static Persistent< T, M > & Cast(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:313
Persistent(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:264
Persistent(const Persistent &that)
Definition: v8-persistent-handle.h:285
Definition: v8-function-callback.h:41
Definition: v8-container.h:148
Definition: v8-value.h:32
void(*)(const WeakCallbackInfo< T > &data) Callback
Definition: v8-weak-callback-info.h:28
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:1892
Definition: v8-internal.h:986
Definition: v8-internal.h:1793
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1853
static bool IsEmpty(T *value)
Definition: v8-internal.h:1812
void DisposeGlobal(internal::Address *global_handle)
void AnnotateStrongRetainer(internal::Address *location, const char *label)
void MoveGlobalReference(internal::Address **from, internal::Address **to)
internal::Address * GlobalizeReference(internal::Isolate *isolate, internal::Address value)
void * ClearWeak(internal::Address *location)
internal::Address * Eternalize(v8::Isolate *isolate, Value *handle)
void MakeWeak(internal::Address **location_addr)
internal::Address * CopyGlobalReference(internal::Address *from)
uintptr_t Address
Definition: v8-internal.h:38
Definition: libplatform.h:15
WeakCallbackType
Definition: v8-weak-callback-info.h:65
#define V8_EXPORT
Definition: v8config.h:854
#define V8_INLINE
Definition: v8config.h:508
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:621