v8 10.2.154 (node 18.16.0)
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>
17class PersistentValueMapBase;
18template <class V, class T>
19class PersistentValueVector;
20template <class T>
21class Global;
22template <class T>
23class PersistentBase;
24template <class K, class V, class T>
25class PersistentValueMap;
26class Value;
27
28namespace api_internal {
32V8_EXPORT void MakeWeak(internal::Address** location_addr);
35 const char* label);
37 internal::Address* handle);
40} // namespace api_internal
41
46template <class T>
47class Eternal {
48 public:
49 V8_INLINE Eternal() : val_(nullptr) {}
50 template <class S>
51 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : val_(nullptr) {
52 Set(isolate, handle);
53 }
54 // Can only be safely called if already set.
55 V8_INLINE Local<T> Get(Isolate* isolate) const {
56 // The eternal handle will never go away, so as with the roots, we don't
57 // even need to open a handle.
58 return Local<T>(val_);
59 }
60
61 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
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 val_ = reinterpret_cast<T*>(
67 api_internal::Eternalize(isolate, reinterpret_cast<Value*>(*handle)));
68 }
69
70 private:
71 T* val_;
72};
73
74namespace api_internal {
75V8_EXPORT void MakeWeak(internal::Address* location, void* data,
77 WeakCallbackType type);
78} // namespace api_internal
79
93template <class T>
95 public:
101
106 template <class S>
107 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
108
113 template <class S>
114 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
115
116 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
117 V8_INLINE void Empty() { val_ = 0; }
118
119 V8_INLINE Local<T> Get(Isolate* isolate) const {
120 return Local<T>::New(isolate, *this);
121 }
122
123 template <class S>
124 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
125 internal::Address* a = reinterpret_cast<internal::Address*>(this->val_);
126 internal::Address* b = reinterpret_cast<internal::Address*>(that.val_);
127 if (a == nullptr) return b == nullptr;
128 if (b == nullptr) return false;
129 return *a == *b;
130 }
131
132 template <class S>
133 V8_INLINE bool operator==(const Local<S>& that) const {
134 internal::Address* a = reinterpret_cast<internal::Address*>(this->val_);
135 internal::Address* b = reinterpret_cast<internal::Address*>(that.val_);
136 if (a == nullptr) return b == nullptr;
137 if (b == nullptr) return false;
138 return *a == *b;
139 }
140
141 template <class S>
142 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
143 return !operator==(that);
144 }
145
146 template <class S>
147 V8_INLINE bool operator!=(const Local<S>& that) const {
148 return !operator==(that);
149 }
150
163 template <typename P>
164 V8_INLINE void SetWeak(P* parameter,
165 typename WeakCallbackInfo<P>::Callback callback,
166 WeakCallbackType type);
167
176
177 template <typename P>
179
180 // TODO(dcarney): remove this.
181 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
182
189 V8_INLINE void AnnotateStrongRetainer(const char* label);
190
192 V8_INLINE bool IsWeak() const;
193
197 V8_INLINE void SetWrapperClassId(uint16_t class_id);
198
203 V8_INLINE uint16_t WrapperClassId() const;
204
205 PersistentBase(const PersistentBase& other) = delete;
206 void operator=(const PersistentBase&) = delete;
207
208 private:
209 friend class Isolate;
210 friend class Utils;
211 template <class F>
212 friend class Local;
213 template <class F1, class F2>
214 friend class Persistent;
215 template <class F>
216 friend class Global;
217 template <class F>
218 friend class PersistentBase;
219 template <class F>
220 friend class ReturnValue;
221 template <class F1, class F2, class F3>
223 template <class F1, class F2>
225 friend class Object;
226
227 explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
228 V8_INLINE static T* New(Isolate* isolate, T* that);
229
230 T* val_;
231};
232
239template <class T>
241 public:
243 static const bool kResetInDestructor = false;
244 template <class S, class M>
245 V8_INLINE static void Copy(const Persistent<S, M>& source,
246 NonCopyablePersistent* dest) {
247 static_assert(sizeof(S) < 0,
248 "NonCopyablePersistentTraits::Copy is not instantiable");
249 }
250};
251
256template <class T>
259 static const bool kResetInDestructor = true;
260 template <class S, class M>
261 static V8_INLINE void Copy(const Persistent<S, M>& source,
262 CopyablePersistent* dest) {
263 // do nothing, just allow copy
264 }
265};
266
275template <class T, class M>
276class Persistent : public PersistentBase<T> {
277 public:
287 template <class S>
289 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
290 static_assert(std::is_base_of<T, S>::value, "type check");
291 }
297 template <class S, class M2>
299 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
300 static_assert(std::is_base_of<T, S>::value, "type check");
301 }
308 V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(nullptr) {
309 Copy(that);
310 }
311 template <class S, class M2>
313 Copy(that);
314 }
316 Copy(that);
317 return *this;
318 }
319 template <class S, class M2>
321 Copy(that);
322 return *this;
323 }
330 if (M::kResetInDestructor) this->Reset();
331 }
332
333 // TODO(dcarney): this is pretty useless, fix or remove
334 template <class S>
336#ifdef V8_ENABLE_CHECKS
337 // If we're going to perform the type check then we have to check
338 // that the handle isn't empty before doing the checked cast.
339 if (!that.IsEmpty()) T::Cast(*that);
340#endif
341 return reinterpret_cast<Persistent<T>&>(const_cast<Persistent<S>&>(that));
342 }
343
344 // TODO(dcarney): this is pretty useless, fix or remove
345 template <class S>
347 return Persistent<S>::Cast(*this);
348 }
349
350 private:
351 friend class Isolate;
352 friend class Utils;
353 template <class F>
354 friend class Local;
355 template <class F1, class F2>
356 friend class Persistent;
357 template <class F>
358 friend class ReturnValue;
359
360 explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {}
361 V8_INLINE T* operator*() const { return this->val_; }
362 template <class S, class M2>
363 V8_INLINE void Copy(const Persistent<S, M2>& that);
364};
365
371template <class T>
372class Global : public PersistentBase<T> {
373 public:
377 V8_INLINE Global() : PersistentBase<T>(nullptr) {}
378
384 template <class S>
386 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
387 static_assert(std::is_base_of<T, S>::value, "type check");
388 }
389
395 template <class S>
397 : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) {
398 static_assert(std::is_base_of<T, S>::value, "type check");
399 }
400
404 V8_INLINE Global(Global&& other);
405
406 V8_INLINE ~Global() { this->Reset(); }
407
411 template <class S>
413
417 Global Pass() { return static_cast<Global&&>(*this); }
418
419 /*
420 * For compatibility with Chromium's base::Bind (base::Passed).
421 */
423
424 Global(const Global&) = delete;
425 void operator=(const Global&) = delete;
426
427 private:
428 template <class F>
429 friend class ReturnValue;
430 V8_INLINE T* operator*() const { return this->val_; }
431};
432
433// UniquePersistent is an alias for Global for historical reason.
434template <class T>
436
441 public:
442 virtual ~PersistentHandleVisitor() = default;
444 uint16_t class_id) {}
445};
446
447template <class T>
448T* PersistentBase<T>::New(Isolate* isolate, T* that) {
449 if (that == nullptr) return nullptr;
450 internal::Address* p = reinterpret_cast<internal::Address*>(that);
451 return reinterpret_cast<T*>(api_internal::GlobalizeReference(
452 reinterpret_cast<internal::Isolate*>(isolate), p));
453}
454
455template <class T, class M>
456template <class S, class M2>
458 static_assert(std::is_base_of<T, S>::value, "type check");
459 this->Reset();
460 if (that.IsEmpty()) return;
461 internal::Address* p = reinterpret_cast<internal::Address*>(that.val_);
462 this->val_ = reinterpret_cast<T*>(api_internal::CopyGlobalReference(p));
463 M::Copy(that, this);
464}
465
466template <class T>
468 using I = internal::Internals;
469 if (this->IsEmpty()) return false;
470 return I::GetNodeState(reinterpret_cast<internal::Address*>(this->val_)) ==
471 I::kNodeStateIsWeakValue;
472}
473
474template <class T>
476 if (this->IsEmpty()) return;
477 api_internal::DisposeGlobal(reinterpret_cast<internal::Address*>(this->val_));
478 val_ = nullptr;
479}
480
485template <class T>
486template <class S>
487void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
488 static_assert(std::is_base_of<T, S>::value, "type check");
489 Reset();
490 if (other.IsEmpty()) return;
491 this->val_ = New(isolate, other.val_);
492}
493
498template <class T>
499template <class S>
501 const PersistentBase<S>& other) {
502 static_assert(std::is_base_of<T, S>::value, "type check");
503 Reset();
504 if (other.IsEmpty()) return;
505 this->val_ = New(isolate, other.val_);
506}
507
508template <class T>
509template <typename P>
511 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
512 WeakCallbackType type) {
513 using Callback = WeakCallbackInfo<void>::Callback;
514#if (__GNUC__ >= 8) && !defined(__clang__)
515#pragma GCC diagnostic push
516#pragma GCC diagnostic ignored "-Wcast-function-type"
517#endif
518 api_internal::MakeWeak(reinterpret_cast<internal::Address*>(this->val_),
519 parameter, reinterpret_cast<Callback>(callback), type);
520#if (__GNUC__ >= 8) && !defined(__clang__)
521#pragma GCC diagnostic pop
522#endif
523}
524
525template <class T>
527 api_internal::MakeWeak(reinterpret_cast<internal::Address**>(&this->val_));
528}
529
530template <class T>
531template <typename P>
533 return reinterpret_cast<P*>(api_internal::ClearWeak(
534 reinterpret_cast<internal::Address*>(this->val_)));
535}
536
537template <class T>
540 reinterpret_cast<internal::Address*>(this->val_), label);
541}
542
543template <class T>
545 using I = internal::Internals;
546 if (this->IsEmpty()) return;
547 internal::Address* obj = reinterpret_cast<internal::Address*>(this->val_);
548 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
549 *reinterpret_cast<uint16_t*>(addr) = class_id;
550}
551
552template <class T>
554 using I = internal::Internals;
555 if (this->IsEmpty()) return 0;
556 internal::Address* obj = reinterpret_cast<internal::Address*>(this->val_);
557 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
558 return *reinterpret_cast<uint16_t*>(addr);
559}
560
561template <class T>
562Global<T>::Global(Global&& other) : PersistentBase<T>(other.val_) {
563 if (other.val_ != nullptr) {
565 reinterpret_cast<internal::Address**>(&other.val_),
566 reinterpret_cast<internal::Address**>(&this->val_));
567 other.val_ = nullptr;
568 }
569}
570
571template <class T>
572template <class S>
574 static_assert(std::is_base_of<T, S>::value, "type check");
575 if (this != &rhs) {
576 this->Reset();
577 if (rhs.val_ != nullptr) {
578 this->val_ = rhs.val_;
580 reinterpret_cast<internal::Address**>(&rhs.val_),
581 reinterpret_cast<internal::Address**>(&this->val_));
582 rhs.val_ = nullptr;
583 }
584 }
585 return *this;
586}
587
588} // namespace v8
589
590#endif // INCLUDE_V8_PERSISTENT_HANDLE_H_
V8_INLINE Eternal(Isolate *isolate, Local< S > handle)
V8_INLINE bool IsEmpty() const
void Set(Isolate *isolate, Local< S > handle)
V8_INLINE Local< T > Get(Isolate *isolate) const
V8_INLINE Eternal()
V8_INLINE Global(Isolate *isolate, const PersistentBase< S > &that)
V8_INLINE Global(Isolate *isolate, Local< S > that)
V8_INLINE Global & operator=(Global< S > &&rhs)
Global(const Global &)=delete
void operator=(const Global &)=delete
V8_INLINE ~Global()
V8_INLINE bool IsEmpty() const
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 IsEmpty() 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(Isolate *isolate, Local< S > that)
V8_INLINE Persistent< S > & As() const
V8_INLINE Persistent(Isolate *isolate, const Persistent< S, M2 > &that)
V8_INLINE Persistent(const Persistent< S, M2 > &that)
V8_INLINE Persistent(const Persistent &that)
V8_INLINE Persistent & operator=(const Persistent &that)
V8_INLINE Persistent & operator=(const Persistent< S, M2 > &that)
static V8_INLINE Persistent< T > & Cast(const Persistent< S > &that)
void(*)(const WeakCallbackInfo< T > &data) Callback
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 internal::Address * GlobalizeReference(internal::Isolate *isolate, internal::Address *handle)
V8_EXPORT Value * Eternalize(v8::Isolate *isolate, Value *handle)
V8_EXPORT void MoveGlobalReference(internal::Address **from, internal::Address **to)
V8_EXPORT void AnnotateStrongRetainer(internal::Address *location, const char *label)
uintptr_t Address
Definition v8-internal.h:29
static V8_INLINE void Copy(const Persistent< S, M > &source, CopyablePersistent *dest)
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425