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 // TODO(dcarney): remove this.
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 // TODO(dcarney): this is pretty useless, fix or remove
312 template <class S, class M2>
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 // TODO(dcarney): this is pretty useless, fix or remove
324 template <class S, class M2>
326 return Persistent<S, M2>::Cast(*this);
327 }
328
329 private:
330 friend class Isolate;
331 friend class Utils;
332 template <class F>
333 friend class Local;
334 template <class F1, class F2>
335 friend class Persistent;
336 template <class F>
337 friend class ReturnValue;
338
339 template <class S, class M2>
340 V8_INLINE void Copy(const Persistent<S, M2>& that);
341};
342
348template <class T>
349class Global : public PersistentBase<T> {
350 public:
354 V8_INLINE Global() = default;
355
361 template <class S>
362 requires(std::is_base_of_v<T, S>)
365 PersistentBase<T>::New(isolate, that.template value<S>())) {}
366
372 template <class S>
373 requires(std::is_base_of_v<T, S>)
376 PersistentBase<T>::New(isolate, that.template value<S>())) {}
377
382
383 V8_INLINE ~Global() { this->Reset(); }
384
388 template <class S>
390
394 Global Pass() { return static_cast<Global&&>(*this); }
395
396 /*
397 * For compatibility with Chromium's base::Bind (base::Passed).
398 */
400
401 Global(const Global&) = delete;
402 void operator=(const Global&) = delete;
403
404 private:
405 template <class F>
406 friend class ReturnValue;
407};
408
409// UniquePersistent is an alias for Global for historical reason.
410template <class T>
412
417 public:
418 virtual ~PersistentHandleVisitor() = default;
420 uint16_t class_id) {}
421};
422
423template <class T>
424internal::Address* PersistentBase<T>::New(Isolate* isolate, T* that) {
425 if (internal::ValueHelper::IsEmpty(that)) return nullptr;
427 reinterpret_cast<internal::Isolate*>(isolate),
429}
430
431template <class T, class M>
432template <class S, class M2>
433void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
434 static_assert(std::is_base_of<T, S>::value, "type check");
435 this->Reset();
436 if (that.IsEmpty()) return;
437 this->slot() = api_internal::CopyGlobalReference(that.slot());
438 M::Copy(that, this);
439}
440
441template <class T>
443 using I = internal::Internals;
444 if (this->IsEmpty()) return false;
445 return I::GetNodeState(this->slot()) == I::kNodeStateIsWeakValue;
446}
447
448template <class T>
450 if (this->IsEmpty()) return;
451 api_internal::DisposeGlobal(this->slot());
452 this->Clear();
453}
454
459template <class T>
460template <class S>
461void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
462 static_assert(std::is_base_of<T, S>::value, "type check");
463 Reset();
464 if (other.IsEmpty()) return;
465 this->slot() = New(isolate, *other);
466}
467
472template <class T>
473template <class S>
475 const PersistentBase<S>& other) {
476 static_assert(std::is_base_of<T, S>::value, "type check");
477 Reset();
478 if (other.IsEmpty()) return;
479 this->slot() = New(isolate, other.template value<S>());
480}
481
482template <class T>
483template <typename P>
485 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
486 WeakCallbackType type) {
487 using Callback = WeakCallbackInfo<void>::Callback;
488#if (__GNUC__ >= 8) && !defined(__clang__)
489#pragma GCC diagnostic push
490#pragma GCC diagnostic ignored "-Wcast-function-type"
491#endif
492 api_internal::MakeWeak(this->slot(), parameter,
493 reinterpret_cast<Callback>(callback), type);
494#if (__GNUC__ >= 8) && !defined(__clang__)
495#pragma GCC diagnostic pop
496#endif
497}
498
499template <class T>
501 api_internal::MakeWeak(&this->slot());
502}
503
504template <class T>
505template <typename P>
507 return reinterpret_cast<P*>(api_internal::ClearWeak(this->slot()));
508}
509
510template <class T>
512 api_internal::AnnotateStrongRetainer(this->slot(), label);
513}
514
515template <class T>
517 using I = internal::Internals;
518 if (this->IsEmpty()) return;
519 uint8_t* addr = reinterpret_cast<uint8_t*>(slot()) + I::kNodeClassIdOffset;
520 *reinterpret_cast<uint16_t*>(addr) = class_id;
521}
522
523template <class T>
525 using I = internal::Internals;
526 if (this->IsEmpty()) return 0;
527 uint8_t* addr = reinterpret_cast<uint8_t*>(slot()) + I::kNodeClassIdOffset;
528 return *reinterpret_cast<uint16_t*>(addr);
529}
530
531template <class T>
532Global<T>::Global(Global&& other) : PersistentBase<T>(other.slot()) {
533 if (!other.IsEmpty()) {
534 api_internal::MoveGlobalReference(&other.slot(), &this->slot());
535 other.Clear();
536 }
537}
538
539template <class T>
540template <class S>
542 static_assert(std::is_base_of<T, S>::value, "type check");
543 if (this != &rhs) {
544 this->Reset();
545 if (!rhs.IsEmpty()) {
546 this->slot() = rhs.slot();
547 api_internal::MoveGlobalReference(&rhs.slot(), &this->slot());
548 rhs.Clear();
549 }
550 }
551 return *this;
552}
553
554} // namespace v8
555
556#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:349
void MoveOnlyTypeForCPP03
Definition: v8-persistent-handle.h:399
Global()=default
Global & operator=(Global< S > &&rhs)
void operator=(const Global &)=delete
~Global()
Definition: v8-persistent-handle.h:383
Global(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:363
Global Pass()
Definition: v8-persistent-handle.h:394
Global(Global &&other)
Definition: v8-persistent-handle.h:532
Global(Isolate *isolate, const PersistentBase< S > &that)
Definition: v8-persistent-handle.h:374
Global(const Global &)=delete
Definition: v8-isolate.h:274
Definition: v8-local-handle.h:267
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:349
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:233
Definition: v8-persistent-handle.h:93
void SetWeak(P *parameter, typename WeakCallbackInfo< P >::Callback callback, WeakCallbackType type)
Definition: v8-persistent-handle.h:484
void SetWeak()
Definition: v8-persistent-handle.h:500
void Reset()
Definition: v8-persistent-handle.h:449
void AnnotateStrongRetainer(const char *label)
Definition: v8-persistent-handle.h:511
void ClearWeak()
Definition: v8-persistent-handle.h:167
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:115
bool IsWeak() const
Definition: v8-persistent-handle.h:442
void Reset(Isolate *isolate, const PersistentBase< S > &other)
Definition: v8-persistent-handle.h:474
bool operator==(const Local< S > &that) const
Definition: v8-persistent-handle.h:125
P * ClearWeak()
Definition: v8-persistent-handle.h:506
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:461
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:516
uint16_t WrapperClassId() const
Definition: v8-persistent-handle.h:524
bool operator!=(const Local< S > &that) const
Definition: v8-persistent-handle.h:135
void operator=(const PersistentBase &)=delete
Definition: v8-persistent-handle.h:416
virtual void VisitPersistentHandle(Persistent< Value > *value, uint16_t class_id)
Definition: v8-persistent-handle.h:419
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:331
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:1714
Definition: v8-internal.h:829
Definition: v8-internal.h:1615
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1675
static bool IsEmpty(T *value)
Definition: v8-internal.h:1634
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:52
Definition: libplatform.h:15
WeakCallbackType
Definition: v8-weak-callback-info.h:65
#define V8_EXPORT
Definition: v8config.h:800
#define V8_INLINE
Definition: v8config.h:500