/********************************************************************* * NAN - Native Abstractions for Node.js * * Copyright (c) 2015 NAN contributors * * MIT License ********************************************************************/ #ifndef NAN_PERSISTENT_PRE_12_INL_H_ #define NAN_PERSISTENT_PRE_12_INL_H_ template class PersistentBase { v8::Persistent persistent; template friend v8::Local New(const Persistent &p); template friend class ReturnValue; public: NAN_INLINE PersistentBase() : persistent() {} NAN_INLINE void Reset() { persistent.Dispose(); persistent.Clear(); } template NAN_INLINE void Reset(const v8::Local &other) { TYPE_CHECK(T, S); if (!persistent.IsEmpty()) { persistent.Dispose(); } if (other.IsEmpty()) { persistent.Clear(); } else { persistent = v8::Persistent::New(other); } } template NAN_INLINE void Reset(const PersistentBase &other) { TYPE_CHECK(T, S); if (!persistent.IsEmpty()) { persistent.Dispose(); } if (other.IsEmpty()) { persistent.Clear(); } else { persistent = v8::Persistent::New(other.persistent); } } NAN_INLINE bool IsEmpty() const { return persistent.IsEmpty(); } NAN_INLINE void Empty() { persistent.Clear(); } template NAN_INLINE bool operator==(const PersistentBase &that) { return this->persistent == that.persistent; } template NAN_INLINE bool operator==(const v8::Local &that) { return this->persistent == that; } template NAN_INLINE bool operator!=(const PersistentBase &that) { return !operator==(that); } template NAN_INLINE bool operator!=(const v8::Local &that) { return !operator==(that); } template NAN_INLINE void SetWeak( P *parameter , typename WeakCallbackInfo

::Callback callback , WeakCallbackType type); NAN_INLINE void ClearWeak() { persistent.ClearWeak(); } NAN_INLINE void MarkIndependent() { persistent.MarkIndependent(); } NAN_INLINE bool IsIndependent() const { return persistent.IsIndependent(); } NAN_INLINE bool IsNearDeath() const { return persistent.IsNearDeath(); } NAN_INLINE bool IsWeak() const { return persistent.IsWeak(); } private: NAN_INLINE explicit PersistentBase(v8::Persistent that) : persistent(that) { } NAN_INLINE explicit PersistentBase(T *val) : persistent(val) {} template friend class Persistent; template friend class Global; friend class ObjectWrap; }; template class NonCopyablePersistentTraits { public: typedef Persistent > NonCopyablePersistent; static const bool kResetInDestructor = false; template NAN_INLINE static void Copy(const Persistent &source, NonCopyablePersistent *dest) { Uncompilable(); } template NAN_INLINE static void Uncompilable() { TYPE_CHECK(O, v8::Primitive); } }; template struct CopyablePersistentTraits { typedef Persistent > CopyablePersistent; static const bool kResetInDestructor = true; template static NAN_INLINE void Copy(const Persistent &source, CopyablePersistent *dest) {} }; template class Persistent : public PersistentBase { public: NAN_INLINE Persistent() {} template NAN_INLINE Persistent(v8::Handle that) : PersistentBase(v8::Persistent::New(that)) { TYPE_CHECK(T, S); } NAN_INLINE Persistent(const Persistent &that) : PersistentBase() { Copy(that); } template NAN_INLINE Persistent(const Persistent &that) : PersistentBase() { Copy(that); } NAN_INLINE Persistent &operator=(const Persistent &that) { Copy(that); return *this; } template NAN_INLINE Persistent &operator=(const Persistent &that) { Copy(that); return *this; } NAN_INLINE ~Persistent() { if (M::kResetInDestructor) this->Reset(); } private: NAN_INLINE T *operator*() const { return *PersistentBase::persistent; } template NAN_INLINE void Copy(const Persistent &that) { TYPE_CHECK(T, S); this->Reset(); if (!that.IsEmpty()) { this->persistent = v8::Persistent::New(that.persistent); M::Copy(that, this); } } }; template class Global : public PersistentBase { struct RValue { NAN_INLINE explicit RValue(Global* obj) : object(obj) {} Global* object; }; public: NAN_INLINE Global() : PersistentBase(0) { } template NAN_INLINE Global(v8::Local that) : PersistentBase(v8::Persistent::New(that)) { TYPE_CHECK(T, S); } template NAN_INLINE Global(const PersistentBase &that) : PersistentBase(that) { TYPE_CHECK(T, S); } /** * Move constructor. */ NAN_INLINE Global(RValue rvalue) : PersistentBase(rvalue.object.persistent) { rvalue.object->Reset(); } NAN_INLINE ~Global() { this->Reset(); } /** * Move via assignment. */ template NAN_INLINE Global &operator=(Global rhs) { TYPE_CHECK(T, S); this->Reset(rhs.persistent); rhs.Reset(); return *this; } /** * Cast operator for moves. */ NAN_INLINE operator RValue() { return RValue(this); } /** * Pass allows returning uniques from functions, etc. */ Global Pass() { return Global(RValue(this)); } private: Global(Global &); void operator=(Global &); template friend class ReturnValue; }; #endif // NAN_PERSISTENT_PRE_12_INL_H_