v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
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>
18template <class V, class T>
20template <class T>
21class Global;
22template <class T>
23class PersistentBase;
24template <class K, class V, class T>
26class Value;
27
28namespace api_internal {
32V8_EXPORT void MakeWeak(internal::Address** location_addr);
35 const char* label);
37 internal::Address value);
40} // namespace api_internal
41
46template <class T>
48 public:
49 V8_INLINE Eternal() = default;
50
51 template <class S>
52 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) {
53 Set(isolate, handle);
54 }
55
56 // Can only be safely called if already set.
57 V8_INLINE Local<T> Get(Isolate* isolate) const {
58 // The eternal handle will never go away, so as with the roots, we don't
59 // even need to open a handle.
60 return Local<T>::FromSlot(slot());
61 }
62
63 template <class S>
64 void Set(Isolate* isolate, Local<S> handle) {
65 static_assert(std::is_base_of<T, S>::value, "type check");
66 slot() =
67 api_internal::Eternalize(isolate, *handle.template UnsafeAs<Value>());
68 }
69};
70
71namespace api_internal {
72V8_EXPORT void MakeWeak(internal::Address* location, void* data,
74 WeakCallbackType type);
75} // namespace api_internal
76
90template <class T>
92 public:
98
103 template <class S>
104 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
105
110 template <class S>
111 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
112
113 V8_INLINE Local<T> Get(Isolate* isolate) const {
114 return Local<T>::New(isolate, *this);
115 }
116
117 template <class S>
118 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
119 return internal::HandleHelper::EqualHandles(*this, that);
120 }
121
122 template <class S>
123 V8_INLINE bool operator==(const Local<S>& that) const {
124 return internal::HandleHelper::EqualHandles(*this, that);
125 }
126
127 template <class S>
128 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
129 return !operator==(that);
130 }
131
132 template <class S>
133 V8_INLINE bool operator!=(const Local<S>& that) const {
134 return !operator==(that);
135 }
136
149 template <typename P>
150 V8_INLINE void SetWeak(P* parameter,
151 typename WeakCallbackInfo<P>::Callback callback,
152 WeakCallbackType type);
153
160
161 template <typename P>
163
164 // TODO(dcarney): remove this.
165 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
166
173 V8_INLINE void AnnotateStrongRetainer(const char* label);
174
176 V8_INLINE bool IsWeak() const;
177
181 V8_INLINE void SetWrapperClassId(uint16_t class_id);
182
187 V8_INLINE uint16_t WrapperClassId() const;
188
189 PersistentBase(const PersistentBase& other) = delete;
190 void operator=(const PersistentBase&) = delete;
191
192 private:
193 friend class Isolate;
194 friend class Utils;
195 template <class F>
196 friend class Local;
197 template <class F1, class F2>
198 friend class Persistent;
199 template <class F>
200 friend class Global;
201 template <class F>
202 friend class PersistentBase;
203 template <class F>
204 friend class ReturnValue;
205 template <class F1, class F2, class F3>
207 template <class F1, class F2>
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
243template <class T>
244struct V8_DEPRECATED("Use v8::Global instead") CopyablePersistentTraits {
245 using CopyablePersistent = Persistent<T, CopyablePersistentTraits<T>>;
246 static const bool kResetInDestructor = true;
247 template <class S, class M>
248 static V8_INLINE void Copy(const Persistent<S, M>& source,
249 CopyablePersistent* dest) {
250 // do nothing, just allow copy
251 }
252};
253
264template <class T, class M>
265class Persistent : public PersistentBase<T> {
266 public:
271
277 template <class S>
279 : PersistentBase<T>(
280 PersistentBase<T>::New(isolate, that.template value<S>())) {
281 static_assert(std::is_base_of<T, S>::value, "type check");
282 }
283
289 template <class S, class M2>
291 : PersistentBase<T>(
292 PersistentBase<T>::New(isolate, that.template value<S>())) {
293 static_assert(std::is_base_of<T, S>::value, "type check");
294 }
295
303 Copy(that);
304 }
305 template <class S, class M2>
307 Copy(that);
308 }
310 Copy(that);
311 return *this;
312 }
313 template <class S, class M2>
315 Copy(that);
316 return *this;
317 }
318
325 if (M::kResetInDestructor) this->Reset();
326 }
327
328 // TODO(dcarney): this is pretty useless, fix or remove
329 template <class S, class M2>
331#ifdef V8_ENABLE_CHECKS
332 // If we're going to perform the type check then we have to check
333 // that the handle isn't empty before doing the checked cast.
334 if (!that.IsEmpty()) T::Cast(that.template value<S>());
335#endif
336 return reinterpret_cast<Persistent<T, M>&>(
337 const_cast<Persistent<S, M2>&>(that));
338 }
339
340 // TODO(dcarney): this is pretty useless, fix or remove
341 template <class S, class M2>
343 return Persistent<S, M2>::Cast(*this);
344 }
345
346 private:
347 friend class Isolate;
348 friend class Utils;
349 template <class F>
350 friend class Local;
351 template <class F1, class F2>
352 friend class Persistent;
353 template <class F>
354 friend class ReturnValue;
355
356 template <class S, class M2>
357 V8_INLINE void Copy(const Persistent<S, M2>& that);
358};
359
365template <class T>
366class Global : public PersistentBase<T> {
367 public:
371 V8_INLINE Global() = default;
372
378 template <class S>
380 : PersistentBase<T>(
381 PersistentBase<T>::New(isolate, that.template value<S>())) {
382 static_assert(std::is_base_of<T, S>::value, "type check");
383 }
384
390 template <class S>
392 : PersistentBase<T>(
393 PersistentBase<T>::New(isolate, that.template value<S>())) {
394 static_assert(std::is_base_of<T, S>::value, "type check");
395 }
396
401
402 V8_INLINE ~Global() { this->Reset(); }
403
407 template <class S>
409
413 Global Pass() { return static_cast<Global&&>(*this); }
414
415 /*
416 * For compatibility with Chromium's base::Bind (base::Passed).
417 */
419
420 Global(const Global&) = delete;
421 void operator=(const Global&) = delete;
422
423 private:
424 template <class F>
425 friend class ReturnValue;
426};
427
428// UniquePersistent is an alias for Global for historical reason.
429template <class T>
431
436 public:
437 virtual ~PersistentHandleVisitor() = default;
439 uint16_t class_id) {}
440};
441
442template <class T>
443internal::Address* PersistentBase<T>::New(Isolate* isolate, T* that) {
444 if (internal::ValueHelper::IsEmpty(that)) return nullptr;
446 reinterpret_cast<internal::Isolate*>(isolate),
448}
449
450template <class T, class M>
451template <class S, class M2>
453 static_assert(std::is_base_of<T, S>::value, "type check");
454 this->Reset();
455 if (that.IsEmpty()) return;
456 this->slot() = api_internal::CopyGlobalReference(that.slot());
457 M::Copy(that, this);
458}
459
460template <class T>
462 using I = internal::Internals;
463 if (this->IsEmpty()) return false;
464 return I::GetNodeState(this->slot()) == I::kNodeStateIsWeakValue;
465}
466
467template <class T>
469 if (this->IsEmpty()) return;
470 api_internal::DisposeGlobal(this->slot());
471 this->Clear();
472}
473
478template <class T>
479template <class S>
480void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
481 static_assert(std::is_base_of<T, S>::value, "type check");
482 Reset();
483 if (other.IsEmpty()) return;
484 this->slot() = New(isolate, *other);
485}
486
491template <class T>
492template <class S>
494 const PersistentBase<S>& other) {
495 static_assert(std::is_base_of<T, S>::value, "type check");
496 Reset();
497 if (other.IsEmpty()) return;
498 this->slot() = New(isolate, other.template value<S>());
499}
500
501template <class T>
502template <typename P>
504 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
505 WeakCallbackType type) {
506 using Callback = WeakCallbackInfo<void>::Callback;
507#if (__GNUC__ >= 8) && !defined(__clang__)
508#pragma GCC diagnostic push
509#pragma GCC diagnostic ignored "-Wcast-function-type"
510#endif
511 api_internal::MakeWeak(this->slot(), parameter,
512 reinterpret_cast<Callback>(callback), type);
513#if (__GNUC__ >= 8) && !defined(__clang__)
514#pragma GCC diagnostic pop
515#endif
516}
517
518template <class T>
520 api_internal::MakeWeak(&this->slot());
521}
522
523template <class T>
524template <typename P>
526 return reinterpret_cast<P*>(api_internal::ClearWeak(this->slot()));
527}
528
529template <class T>
531 api_internal::AnnotateStrongRetainer(this->slot(), label);
532}
533
534template <class T>
536 using I = internal::Internals;
537 if (this->IsEmpty()) return;
538 uint8_t* addr = reinterpret_cast<uint8_t*>(slot()) + I::kNodeClassIdOffset;
539 *reinterpret_cast<uint16_t*>(addr) = class_id;
540}
541
542template <class T>
544 using I = internal::Internals;
545 if (this->IsEmpty()) return 0;
546 uint8_t* addr = reinterpret_cast<uint8_t*>(slot()) + I::kNodeClassIdOffset;
547 return *reinterpret_cast<uint16_t*>(addr);
548}
549
550template <class T>
551Global<T>::Global(Global&& other) : PersistentBase<T>(other.slot()) {
552 if (!other.IsEmpty()) {
553 api_internal::MoveGlobalReference(&other.slot(), &this->slot());
554 other.Clear();
555 }
556}
557
558template <class T>
559template <class S>
561 static_assert(std::is_base_of<T, S>::value, "type check");
562 if (this != &rhs) {
563 this->Reset();
564 if (!rhs.IsEmpty()) {
565 this->slot() = rhs.slot();
566 api_internal::MoveGlobalReference(&rhs.slot(), &this->slot());
567 rhs.Clear();
568 }
569 }
570 return *this;
571}
572
573} // namespace v8
574
575#endif // INCLUDE_V8_PERSISTENT_HANDLE_H_
V8_INLINE Eternal()=default
V8_INLINE Eternal(Isolate *isolate, Local< S > handle)
void Set(Isolate *isolate, Local< S > handle)
V8_INLINE Local< T > Get(Isolate *isolate) const
V8_INLINE Global(Isolate *isolate, const PersistentBase< S > &that)
V8_INLINE Global(Isolate *isolate, Local< S > that)
V8_INLINE Global()=default
V8_INLINE Global & operator=(Global< S > &&rhs)
Global(const Global &)=delete
void operator=(const Global &)=delete
V8_INLINE ~Global()
V8_INLINE Global(Global &&other)
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
static V8_INLINE void Copy(const Persistent< S, M > &source, NonCopyablePersistent *dest)
V8_INLINE void Reset(Isolate *isolate, const Local< S > &other)
void operator=(const PersistentBase &)=delete
V8_INLINE uint16_t WrapperClassId() const
V8_INLINE bool IsWeak() const
V8_INLINE bool operator!=(const Local< S > &that) const
V8_INLINE bool operator==(const Local< S > &that) const
V8_INLINE P * ClearWeak()
V8_INLINE bool operator!=(const PersistentBase< S > &that) const
PersistentBase(const PersistentBase &other)=delete
V8_INLINE void AnnotateStrongRetainer(const char *label)
V8_INLINE bool operator==(const PersistentBase< S > &that) const
V8_INLINE void Reset(Isolate *isolate, const PersistentBase< S > &other)
V8_INLINE void SetWeak(P *parameter, typename WeakCallbackInfo< P >::Callback callback, WeakCallbackType type)
V8_INLINE void ClearWeak()
V8_INLINE Local< T > Get(Isolate *isolate) const
V8_INLINE void SetWrapperClassId(uint16_t class_id)
virtual void VisitPersistentHandle(Persistent< Value > *value, uint16_t class_id)
virtual ~PersistentHandleVisitor()=default
V8_INLINE Persistent()=default
V8_INLINE Persistent(Isolate *isolate, Local< S > that)
V8_INLINE Persistent(Isolate *isolate, const Persistent< S, M2 > &that)
static V8_INLINE Persistent< T, M > & Cast(const Persistent< S, M2 > &that)
V8_INLINE Persistent(const Persistent< S, M2 > &that)
V8_INLINE Persistent< S, M2 > & As() const
V8_INLINE Persistent(const Persistent &that)
V8_INLINE Persistent & operator=(const Persistent &that)
V8_INLINE Persistent & operator=(const Persistent< S, M2 > &that)
void(*)(const WeakCallbackInfo< T > &data) Callback
V8_INLINE IndirectHandleBase()=default
V8_INLINE internal::Address *const & slot() const
static V8_INLINE bool EqualHandles(const T1 &lhs, const T2 &rhs)
static V8_INLINE Address ValueAsAddress(const T *value)
static V8_INLINE bool IsEmpty(T *value)
V8_EXPORT void MakeWeak(internal::Address **location_addr)
V8_EXPORT void DisposeGlobal(internal::Address *global_handle)
V8_EXPORT void * ClearWeak(internal::Address *location)
V8_EXPORT internal::Address * CopyGlobalReference(internal::Address *from)
V8_EXPORT void MoveGlobalReference(internal::Address **from, internal::Address **to)
V8_EXPORT internal::Address * Eternalize(v8::Isolate *isolate, Value *handle)
V8_EXPORT internal::Address * GlobalizeReference(internal::Isolate *isolate, internal::Address value)
V8_EXPORT void AnnotateStrongRetainer(internal::Address *location, const char *label)
uintptr_t Address
Definition v8-internal.h:31
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477
#define V8_DEPRECATED(message)
Definition v8config.h:563