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 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
249template <class T, class M>
250class Persistent : public PersistentBase<T> {
251 public:
256
262 template <class S>
264 : PersistentBase<T>(
265 PersistentBase<T>::New(isolate, that.template value<S>())) {
266 static_assert(std::is_base_of<T, S>::value, "type check");
267 }
268
274 template <class S, class M2>
276 : PersistentBase<T>(
277 PersistentBase<T>::New(isolate, that.template value<S>())) {
278 static_assert(std::is_base_of<T, S>::value, "type check");
279 }
280
288 Copy(that);
289 }
290 template <class S, class M2>
292 Copy(that);
293 }
295 Copy(that);
296 return *this;
297 }
298 template <class S, class M2>
300 Copy(that);
301 return *this;
302 }
303
310 if (M::kResetInDestructor) this->Reset();
311 }
312
313 // TODO(dcarney): this is pretty useless, fix or remove
314 template <class S, class M2>
316#ifdef V8_ENABLE_CHECKS
317 // If we're going to perform the type check then we have to check
318 // that the handle isn't empty before doing the checked cast.
319 if (!that.IsEmpty()) T::Cast(that.template value<S>());
320#endif
321 return reinterpret_cast<Persistent<T, M>&>(
322 const_cast<Persistent<S, M2>&>(that));
323 }
324
325 // TODO(dcarney): this is pretty useless, fix or remove
326 template <class S, class M2>
328 return Persistent<S, M2>::Cast(*this);
329 }
330
331 private:
332 friend class Isolate;
333 friend class Utils;
334 template <class F>
335 friend class Local;
336 template <class F1, class F2>
337 friend class Persistent;
338 template <class F>
339 friend class ReturnValue;
340
341 template <class S, class M2>
342 V8_INLINE void Copy(const Persistent<S, M2>& that);
343};
344
350template <class T>
351class Global : public PersistentBase<T> {
352 public:
356 V8_INLINE Global() = default;
357
363 template <class S>
365 : PersistentBase<T>(
366 PersistentBase<T>::New(isolate, that.template value<S>())) {
367 static_assert(std::is_base_of<T, S>::value, "type check");
368 }
369
375 template <class S>
377 : PersistentBase<T>(
378 PersistentBase<T>::New(isolate, that.template value<S>())) {
379 static_assert(std::is_base_of<T, S>::value, "type check");
380 }
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<T, S>::value, "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<T, S>::value, "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<T, S>::value, "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<T, S>::value, "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:47
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:57
void Set(Isolate *isolate, Local< S > handle)
Definition: v8-persistent-handle.h:64
Eternal()=default
Eternal(Isolate *isolate, Local< S > handle)
Definition: v8-persistent-handle.h:52
Definition: v8-persistent-handle.h:351
void MoveOnlyTypeForCPP03
Definition: v8-persistent-handle.h:403
Global()=default
Global(Isolate *isolate, const PersistentBase< S > &that)
Definition: v8-persistent-handle.h:376
Global & operator=(Global< S > &&rhs)
void operator=(const Global &)=delete
~Global()
Definition: v8-persistent-handle.h:387
Global Pass()
Definition: v8-persistent-handle.h:398
Global(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:364
Global(Global &&other)
Definition: v8-persistent-handle.h:536
Global(const Global &)=delete
Definition: v8-isolate.h:210
Definition: v8-local-handle.h:258
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:338
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:238
Definition: v8-persistent-handle.h:91
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
void ClearWeak()
Definition: v8-persistent-handle.h:165
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:113
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:123
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:128
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:118
friend class PersistentBase
Definition: v8-persistent-handle.h:202
friend class Utils
Definition: v8-persistent-handle.h:194
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:133
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-util.h:582
Definition: v8-persistent-handle.h:250
Persistent(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:263
~Persistent()
Definition: v8-persistent-handle.h:309
Persistent(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:291
Persistent()=default
Persistent< S, M2 > & As() const
Definition: v8-persistent-handle.h:327
Persistent & operator=(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:299
Persistent & operator=(const Persistent &that)
Definition: v8-persistent-handle.h:294
friend class Utils
Definition: v8-persistent-handle.h:333
static Persistent< T, M > & Cast(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:315
Persistent(Isolate *isolate, const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:275
Persistent(const Persistent &that)
Definition: v8-persistent-handle.h:287
Definition: v8-function-callback.h:35
Definition: v8-container.h:148
Definition: v8-value.h:32
void(*)(const WeakCallbackInfo< T > &data) Callback
Definition: v8-weak-callback-info.h:24
Definition: v8-handle-base.h:53
T * value() const
Definition: v8-handle-base.h:85
internal::Address *const & slot() const
Definition: v8-handle-base.h:79
bool IsEmpty() const
Definition: v8-handle-base.h:56
static bool EqualHandles(const T1 &lhs, const T2 &rhs)
Definition: v8-internal.h:1485
Definition: v8-internal.h:729
Definition: v8-internal.h:1416
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1457
static bool IsEmpty(T *value)
Definition: v8-internal.h:1426
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:31
Definition: libplatform.h:15
WeakCallbackType
Definition: v8-weak-callback-info.h:57
#define V8_EXPORT
Definition: v8config.h:762
#define V8_INLINE
Definition: v8config.h:477