| |
| |
| |
|
|
| #ifndef INCLUDE_V8_PERSISTENT_HANDLE_H_ |
| #define INCLUDE_V8_PERSISTENT_HANDLE_H_ |
|
|
| #include "v8-internal.h" |
| #include "v8-local-handle.h" |
| #include "v8-weak-callback-info.h" |
| #include "v8config.h" |
|
|
| namespace v8 { |
|
|
| class Isolate; |
| template <class K, class V, class T> |
| class PersistentValueMapBase; |
| template <class V, class T> |
| class PersistentValueVector; |
| template <class T> |
| class Global; |
| template <class T> |
| class PersistentBase; |
| template <class K, class V, class T> |
| class PersistentValueMap; |
| class Value; |
|
|
| namespace api_internal { |
| V8_EXPORT Value* Eternalize(v8::Isolate* isolate, Value* handle); |
| V8_EXPORT internal::Address* CopyGlobalReference(internal::Address* from); |
| V8_EXPORT void DisposeGlobal(internal::Address* global_handle); |
| V8_EXPORT void MakeWeak(internal::Address** location_addr); |
| V8_EXPORT void* ClearWeak(internal::Address* location); |
| V8_EXPORT void AnnotateStrongRetainer(internal::Address* location, |
| const char* label); |
| V8_EXPORT internal::Address* GlobalizeReference(internal::Isolate* isolate, |
| internal::Address* handle); |
| V8_EXPORT void MoveGlobalReference(internal::Address** from, |
| internal::Address** to); |
| } |
|
|
| |
| |
| |
| |
| template <class T> |
| class Eternal { |
| public: |
| V8_INLINE Eternal() : val_(nullptr) {} |
| template <class S> |
| V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : val_(nullptr) { |
| Set(isolate, handle); |
| } |
| |
| V8_INLINE Local<T> Get(Isolate* isolate) const { |
| |
| |
| return Local<T>(internal::ValueHelper::SlotAsValue<T>(val_)); |
| } |
|
|
| V8_INLINE bool IsEmpty() const { return val_ == nullptr; } |
|
|
| template <class S> |
| void Set(Isolate* isolate, Local<S> handle) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| val_ = reinterpret_cast<T*>( |
| api_internal::Eternalize(isolate, reinterpret_cast<Value*>(*handle))); |
| } |
|
|
| private: |
| V8_INLINE internal::Address address() const { |
| return *reinterpret_cast<internal::Address*>(val_); |
| } |
|
|
| T* val_; |
| }; |
|
|
| namespace api_internal { |
| V8_EXPORT void MakeWeak(internal::Address* location, void* data, |
| WeakCallbackInfo<void>::Callback weak_callback, |
| WeakCallbackType type); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <class T> |
| class PersistentBase { |
| public: |
| |
| |
| |
| |
| V8_INLINE void Reset(); |
|
|
| |
| |
| |
| |
| template <class S> |
| V8_INLINE void Reset(Isolate* isolate, const Local<S>& other); |
|
|
| |
| |
| |
| |
| template <class S> |
| V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other); |
|
|
| V8_INLINE bool IsEmpty() const { return val_ == nullptr; } |
| V8_INLINE void Empty() { val_ = 0; } |
|
|
| V8_INLINE Local<T> Get(Isolate* isolate) const { |
| return Local<T>::New(isolate, *this); |
| } |
|
|
| template <class S> |
| V8_INLINE bool operator==(const PersistentBase<S>& that) const { |
| return internal::HandleHelper::EqualHandles(*this, that); |
| } |
|
|
| template <class S> |
| V8_INLINE bool operator==(const Local<S>& that) const { |
| return internal::HandleHelper::EqualHandles(*this, that); |
| } |
|
|
| template <class S> |
| V8_INLINE bool operator!=(const PersistentBase<S>& that) const { |
| return !operator==(that); |
| } |
|
|
| template <class S> |
| V8_INLINE bool operator!=(const Local<S>& that) const { |
| return !operator==(that); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename P> |
| V8_INLINE void SetWeak(P* parameter, |
| typename WeakCallbackInfo<P>::Callback callback, |
| WeakCallbackType type); |
|
|
| |
| |
| |
| |
| |
| V8_INLINE void SetWeak(); |
|
|
| template <typename P> |
| V8_INLINE P* ClearWeak(); |
|
|
| |
| V8_INLINE void ClearWeak() { ClearWeak<void>(); } |
|
|
| |
| |
| |
| |
| |
| |
| V8_INLINE void AnnotateStrongRetainer(const char* label); |
|
|
| |
| V8_INLINE bool IsWeak() const; |
|
|
| |
| |
| |
| V8_INLINE void SetWrapperClassId(uint16_t class_id); |
|
|
| |
| |
| |
| |
| V8_INLINE uint16_t WrapperClassId() const; |
|
|
| PersistentBase(const PersistentBase& other) = delete; |
| void operator=(const PersistentBase&) = delete; |
|
|
| private: |
| friend class Isolate; |
| friend class Utils; |
| template <class F> |
| friend class Local; |
| template <class F1, class F2> |
| friend class Persistent; |
| template <class F> |
| friend class Global; |
| template <class F> |
| friend class PersistentBase; |
| template <class F> |
| friend class ReturnValue; |
| template <class F1, class F2, class F3> |
| friend class PersistentValueMapBase; |
| template <class F1, class F2> |
| friend class PersistentValueVector; |
| friend class Object; |
| friend class internal::HandleHelper; |
|
|
| explicit V8_INLINE PersistentBase(T* val) : val_(val) {} |
| V8_INLINE T* operator*() const { return this->val_; } |
| V8_INLINE internal::Address address() const { |
| return *reinterpret_cast<internal::Address*>(val_); |
| } |
|
|
| V8_INLINE static T* New(Isolate* isolate, Local<T> that); |
| V8_INLINE static T* New(Isolate* isolate, T* that); |
|
|
| T* val_; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| template <class T> |
| class NonCopyablePersistentTraits { |
| public: |
| using NonCopyablePersistent = Persistent<T, NonCopyablePersistentTraits<T>>; |
| static const bool kResetInDestructor = false; |
| template <class S, class M> |
| V8_INLINE static void Copy(const Persistent<S, M>& source, |
| NonCopyablePersistent* dest) { |
| static_assert(sizeof(S) < 0, |
| "NonCopyablePersistentTraits::Copy is not instantiable"); |
| } |
| }; |
|
|
| |
| |
| |
| |
| template <class T> |
| struct CopyablePersistentTraits { |
| using CopyablePersistent = Persistent<T, CopyablePersistentTraits<T>>; |
| static const bool kResetInDestructor = true; |
| template <class S, class M> |
| static V8_INLINE void Copy(const Persistent<S, M>& source, |
| CopyablePersistent* dest) { |
| |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| template <class T, class M> |
| class Persistent : public PersistentBase<T> { |
| public: |
| |
| |
| |
| V8_INLINE Persistent() : PersistentBase<T>(nullptr) {} |
| |
| |
| |
| |
| |
|
|
| template <class S> |
| V8_INLINE Persistent(Isolate* isolate, Local<S> that) |
| : PersistentBase<T>(PersistentBase<T>::New(isolate, that)) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| } |
|
|
| |
| |
| |
| |
| |
| template <class S, class M2> |
| V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that) |
| : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| } |
| |
| |
| |
| |
| |
| |
| V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(nullptr) { |
| Copy(that); |
| } |
| template <class S, class M2> |
| V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) { |
| Copy(that); |
| } |
| V8_INLINE Persistent& operator=(const Persistent& that) { |
| Copy(that); |
| return *this; |
| } |
| template <class S, class M2> |
| V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { |
| Copy(that); |
| return *this; |
| } |
| |
| |
| |
| |
| |
| V8_INLINE ~Persistent() { |
| if (M::kResetInDestructor) this->Reset(); |
| } |
|
|
| |
| template <class S> |
| V8_INLINE static Persistent<T>& Cast(const Persistent<S>& that) { |
| #ifdef V8_ENABLE_CHECKS |
| |
| |
| if (!that.IsEmpty()) T::Cast(*that); |
| #endif |
| return reinterpret_cast<Persistent<T>&>(const_cast<Persistent<S>&>(that)); |
| } |
|
|
| |
| template <class S> |
| V8_INLINE Persistent<S>& As() const { |
| return Persistent<S>::Cast(*this); |
| } |
|
|
| private: |
| friend class Isolate; |
| friend class Utils; |
| template <class F> |
| friend class Local; |
| template <class F1, class F2> |
| friend class Persistent; |
| template <class F> |
| friend class ReturnValue; |
|
|
| explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {} |
| template <class S, class M2> |
| V8_INLINE void Copy(const Persistent<S, M2>& that); |
| }; |
|
|
| |
| |
| |
| |
| |
| template <class T> |
| class Global : public PersistentBase<T> { |
| public: |
| |
| |
| |
| V8_INLINE Global() : PersistentBase<T>(nullptr) {} |
|
|
| |
| |
| |
| |
| |
| template <class S> |
| V8_INLINE Global(Isolate* isolate, Local<S> that) |
| : PersistentBase<T>(PersistentBase<T>::New(isolate, that)) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| } |
|
|
| |
| |
| |
| |
| |
| template <class S> |
| V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that) |
| : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| } |
|
|
| |
| |
| |
| V8_INLINE Global(Global&& other); |
|
|
| V8_INLINE ~Global() { this->Reset(); } |
|
|
| |
| |
| |
| template <class S> |
| V8_INLINE Global& operator=(Global<S>&& rhs); |
|
|
| |
| |
| |
| Global Pass() { return static_cast<Global&&>(*this); } |
|
|
| |
| |
| |
| using MoveOnlyTypeForCPP03 = void; |
|
|
| Global(const Global&) = delete; |
| void operator=(const Global&) = delete; |
|
|
| private: |
| template <class F> |
| friend class ReturnValue; |
| }; |
|
|
| |
| template <class T> |
| using UniquePersistent = Global<T>; |
|
|
| |
| |
| |
| class V8_EXPORT PersistentHandleVisitor { |
| public: |
| virtual ~PersistentHandleVisitor() = default; |
| virtual void VisitPersistentHandle(Persistent<Value>* value, |
| uint16_t class_id) {} |
| }; |
|
|
| template <class T> |
| T* PersistentBase<T>::New(Isolate* isolate, Local<T> that) { |
| return PersistentBase<T>::New(isolate, |
| internal::ValueHelper::ValueAsSlot(*that)); |
| } |
|
|
| template <class T> |
| T* PersistentBase<T>::New(Isolate* isolate, T* that) { |
| if (that == nullptr) return nullptr; |
| internal::Address* p = reinterpret_cast<internal::Address*>(that); |
| return reinterpret_cast<T*>(api_internal::GlobalizeReference( |
| reinterpret_cast<internal::Isolate*>(isolate), p)); |
| } |
|
|
| template <class T, class M> |
| template <class S, class M2> |
| void Persistent<T, M>::Copy(const Persistent<S, M2>& that) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| this->Reset(); |
| if (that.IsEmpty()) return; |
| internal::Address* p = reinterpret_cast<internal::Address*>(that.val_); |
| this->val_ = reinterpret_cast<T*>(api_internal::CopyGlobalReference(p)); |
| M::Copy(that, this); |
| } |
|
|
| template <class T> |
| bool PersistentBase<T>::IsWeak() const { |
| using I = internal::Internals; |
| if (this->IsEmpty()) return false; |
| return I::GetNodeState(reinterpret_cast<internal::Address*>(this->val_)) == |
| I::kNodeStateIsWeakValue; |
| } |
|
|
| template <class T> |
| void PersistentBase<T>::Reset() { |
| if (this->IsEmpty()) return; |
| api_internal::DisposeGlobal(reinterpret_cast<internal::Address*>(this->val_)); |
| val_ = nullptr; |
| } |
|
|
| |
| |
| |
| |
| template <class T> |
| template <class S> |
| void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| Reset(); |
| if (other.IsEmpty()) return; |
| this->val_ = New(isolate, internal::ValueHelper::ValueAsSlot(*other)); |
| } |
|
|
| |
| |
| |
| |
| template <class T> |
| template <class S> |
| void PersistentBase<T>::Reset(Isolate* isolate, |
| const PersistentBase<S>& other) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| Reset(); |
| if (other.IsEmpty()) return; |
| this->val_ = New(isolate, other.val_); |
| } |
|
|
| template <class T> |
| template <typename P> |
| V8_INLINE void PersistentBase<T>::SetWeak( |
| P* parameter, typename WeakCallbackInfo<P>::Callback callback, |
| WeakCallbackType type) { |
| using Callback = WeakCallbackInfo<void>::Callback; |
| #if (__GNUC__ >= 8) && !defined(__clang__) |
| #pragma GCC diagnostic push |
| #pragma GCC diagnostic ignored "-Wcast-function-type" |
| #endif |
| api_internal::MakeWeak(reinterpret_cast<internal::Address*>(this->val_), |
| parameter, reinterpret_cast<Callback>(callback), type); |
| #if (__GNUC__ >= 8) && !defined(__clang__) |
| #pragma GCC diagnostic pop |
| #endif |
| } |
|
|
| template <class T> |
| void PersistentBase<T>::SetWeak() { |
| api_internal::MakeWeak(reinterpret_cast<internal::Address**>(&this->val_)); |
| } |
|
|
| template <class T> |
| template <typename P> |
| P* PersistentBase<T>::ClearWeak() { |
| return reinterpret_cast<P*>(api_internal::ClearWeak( |
| reinterpret_cast<internal::Address*>(this->val_))); |
| } |
|
|
| template <class T> |
| void PersistentBase<T>::AnnotateStrongRetainer(const char* label) { |
| api_internal::AnnotateStrongRetainer( |
| reinterpret_cast<internal::Address*>(this->val_), label); |
| } |
|
|
| template <class T> |
| void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) { |
| using I = internal::Internals; |
| if (this->IsEmpty()) return; |
| internal::Address* obj = reinterpret_cast<internal::Address*>(this->val_); |
| uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset; |
| *reinterpret_cast<uint16_t*>(addr) = class_id; |
| } |
|
|
| template <class T> |
| uint16_t PersistentBase<T>::WrapperClassId() const { |
| using I = internal::Internals; |
| if (this->IsEmpty()) return 0; |
| internal::Address* obj = reinterpret_cast<internal::Address*>(this->val_); |
| uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset; |
| return *reinterpret_cast<uint16_t*>(addr); |
| } |
|
|
| template <class T> |
| Global<T>::Global(Global&& other) : PersistentBase<T>(other.val_) { |
| if (other.val_ != nullptr) { |
| api_internal::MoveGlobalReference( |
| reinterpret_cast<internal::Address**>(&other.val_), |
| reinterpret_cast<internal::Address**>(&this->val_)); |
| other.val_ = nullptr; |
| } |
| } |
|
|
| template <class T> |
| template <class S> |
| Global<T>& Global<T>::operator=(Global<S>&& rhs) { |
| static_assert(std::is_base_of<T, S>::value, "type check"); |
| if (this != &rhs) { |
| this->Reset(); |
| if (rhs.val_ != nullptr) { |
| this->val_ = rhs.val_; |
| api_internal::MoveGlobalReference( |
| reinterpret_cast<internal::Address**>(&rhs.val_), |
| reinterpret_cast<internal::Address**>(&this->val_)); |
| rhs.val_ = nullptr; |
| } |
| } |
| return *this; |
| } |
|
|
| } |
|
|
| #endif |
|
|