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
49 template <class S>
50 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) {
51 Set(isolate, handle);
52 }
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>::FromSlot(slot());
59 }
60
61 template <class S>
62 void Set(Isolate* isolate, Local<S> handle) {
63 static_assert(std::is_base_of<T, S>::value, "type check");
64 slot() =
65 api_internal::Eternalize(isolate, *handle.template UnsafeAs<Value>());
66 }
67};
68
69namespace api_internal {
70V8_EXPORT void MakeWeak(internal::Address* location, void* data,
72 WeakCallbackType type);
73} // namespace api_internal
74
88template <class T>
90 public:
96
101 template <class S>
102 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
103
108 template <class S>
109 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
110
111 V8_INLINE Local<T> Get(Isolate* isolate) const {
112 return Local<T>::New(isolate, *this);
113 }
114
115 template <class S>
116 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
117 return internal::HandleHelper::EqualHandles(*this, that);
118 }
119
120 template <class S>
121 V8_INLINE bool operator==(const Local<S>& that) const {
122 return internal::HandleHelper::EqualHandles(*this, that);
123 }
124
125 template <class S>
126 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
127 return !operator==(that);
128 }
129
130 template <class S>
131 V8_INLINE bool operator!=(const Local<S>& that) const {
132 return !operator==(that);
133 }
134
147 template <typename P>
148 V8_INLINE void SetWeak(P* parameter,
149 typename WeakCallbackInfo<P>::Callback callback,
150 WeakCallbackType type);
151
158
159 template <typename P>
161
162 // TODO(dcarney): remove this.
163 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
164
171 V8_INLINE void AnnotateStrongRetainer(const char* label);
172
174 V8_INLINE bool IsWeak() const;
175
179 V8_INLINE void SetWrapperClassId(uint16_t class_id);
180
185 V8_INLINE uint16_t WrapperClassId() const;
186
187 PersistentBase(const PersistentBase& other) = delete;
188 void operator=(const PersistentBase&) = delete;
189
190 private:
191 friend class Isolate;
192 friend class Utils;
193 template <class F>
194 friend class Local;
195 template <class F1, class F2>
196 friend class Persistent;
197 template <class F>
198 friend class Global;
199 template <class F>
200 friend class PersistentBase;
201 template <class F>
202 friend class ReturnValue;
203 template <class F1, class F2, class F3>
205 friend class Object;
207
208 V8_INLINE PersistentBase() = default;
209
210 V8_INLINE explicit PersistentBase(internal::Address* location)
211 : IndirectHandleBase(location) {}
212
213 V8_INLINE static internal::Address* New(Isolate* isolate, T* that);
214};
215
222template <class T>
224 public:
226 static const bool kResetInDestructor = false;
227 template <class S, class M>
228 V8_INLINE static void Copy(const Persistent<S, M>& source,
229 NonCopyablePersistent* dest) {
230 static_assert(sizeof(S) < 0,
231 "NonCopyablePersistentTraits::Copy is not instantiable");
232 }
233};
234
245template <class T, class M>
246class Persistent : public PersistentBase<T> {
247 public:
252
258 template <class S>
260 : PersistentBase<T>(
261 PersistentBase<T>::New(isolate, that.template value<S>())) {
262 static_assert(std::is_base_of<T, S>::value, "type check");
263 }
264
270 template <class S, class M2>
272 : PersistentBase<T>(
273 PersistentBase<T>::New(isolate, that.template value<S>())) {
274 static_assert(std::is_base_of<T, S>::value, "type check");
275 }
276
284 Copy(that);
285 }
286 template <class S, class M2>
288 Copy(that);
289 }
291 Copy(that);
292 return *this;
293 }
294 template <class S, class M2>
296 Copy(that);
297 return *this;
298 }
299
306 if (M::kResetInDestructor) this->Reset();
307 }
308
309 // TODO(dcarney): this is pretty useless, fix or remove
310 template <class S, class M2>
312#ifdef V8_ENABLE_CHECKS
313 // If we're going to perform the type check then we have to check
314 // that the handle isn't empty before doing the checked cast.
315 if (!that.IsEmpty()) T::Cast(that.template value<S>());
316#endif
317 return reinterpret_cast<Persistent<T, M>&>(
318 const_cast<Persistent<S, M2>&>(that));
319 }
320
321 // TODO(dcarney): this is pretty useless, fix or remove
322 template <class S, class M2>
324 return Persistent<S, M2>::Cast(*this);
325 }
326
327 private:
328 friend class Isolate;
329 friend class Utils;
330 template <class F>
331 friend class Local;
332 template <class F1, class F2>
333 friend class Persistent;
334 template <class F>
335 friend class ReturnValue;
336
337 template <class S, class M2>
338 V8_INLINE void Copy(const Persistent<S, M2>& that);
339};
340
346template <class T>
347class Global : public PersistentBase<T> {
348 public:
352 V8_INLINE Global() = default;
353
359 template <class S>
361 : PersistentBase<T>(
362 PersistentBase<T>::New(isolate, that.template value<S>())) {
363 static_assert(std::is_base_of<T, S>::value, "type check");
364 }
365
371 template <class S>
373 : PersistentBase<T>(
374 PersistentBase<T>::New(isolate, that.template value<S>())) {
375 static_assert(std::is_base_of<T, S>::value, "type check");
376 }
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:55
void Set(Isolate *isolate, Local< S > handle)
Definition: v8-persistent-handle.h:62
Eternal()=default
Eternal(Isolate *isolate, Local< S > handle)
Definition: v8-persistent-handle.h:50
Definition: v8-persistent-handle.h:347
void MoveOnlyTypeForCPP03
Definition: v8-persistent-handle.h:399
Global()=default
Global(Isolate *isolate, const PersistentBase< S > &that)
Definition: v8-persistent-handle.h:372
Global & operator=(Global< S > &&rhs)
void operator=(const Global &)=delete
~Global()
Definition: v8-persistent-handle.h:383
Global Pass()
Definition: v8-persistent-handle.h:394
Global(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:360
Global(Global &&other)
Definition: v8-persistent-handle.h:532
Global(const Global &)=delete
Definition: v8-isolate.h:212
Definition: v8-local-handle.h:266
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:346
Definition: v8-persistent-handle.h:223
static const bool kResetInDestructor
Definition: v8-persistent-handle.h:226
static void Copy(const Persistent< S, M > &source, NonCopyablePersistent *dest)
Definition: v8-persistent-handle.h:228
Definition: v8-object.h:233
Definition: v8-persistent-handle.h:89
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:163
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:111
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:121
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:126
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:116
friend class PersistentBase
Definition: v8-persistent-handle.h:200
friend class Utils
Definition: v8-persistent-handle.h:192
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:131
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:246
Persistent(Isolate *isolate, Local< S > that)
Definition: v8-persistent-handle.h:259
~Persistent()
Definition: v8-persistent-handle.h:305
Persistent(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:287
Persistent()=default
Persistent< S, M2 > & As() const
Definition: v8-persistent-handle.h:323
Persistent & operator=(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:295
Persistent & operator=(const Persistent &that)
Definition: v8-persistent-handle.h:290
friend class Utils
Definition: v8-persistent-handle.h:329
static Persistent< T, M > & Cast(const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:311
Persistent(Isolate *isolate, const Persistent< S, M2 > &that)
Definition: v8-persistent-handle.h:271
Persistent(const Persistent &that)
Definition: v8-persistent-handle.h:283
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: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:1726
Definition: v8-internal.h:855
Definition: v8-internal.h:1627
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1687
static bool IsEmpty(T *value)
Definition: v8-internal.h:1646
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:51
Definition: libplatform.h:15
WeakCallbackType
Definition: v8-weak-callback-info.h:57
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499