1 /*********************************************************************
2 * NAN - Native Abstractions for Node.js
4 * Copyright (c) 2017 NAN contributors
6 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7 ********************************************************************/
9 #ifndef NAN_PERSISTENT_PRE_12_INL_H_
10 #define NAN_PERSISTENT_PRE_12_INL_H_
13 class PersistentBase {
14 v8::Persistent<T> persistent;
16 friend v8::Local<U> New(const PersistentBase<U> &p);
17 template<typename U, typename M>
18 friend v8::Local<U> New(const Persistent<U, M> &p);
20 friend v8::Local<U> New(const Global<U> &p);
21 template<typename S> friend class ReturnValue;
24 inline PersistentBase() :
33 inline void Reset(const v8::Local<S> &other) {
36 if (!persistent.IsEmpty()) {
40 if (other.IsEmpty()) {
43 persistent = v8::Persistent<T>::New(other);
48 inline void Reset(const PersistentBase<S> &other) {
51 if (!persistent.IsEmpty()) {
55 if (other.IsEmpty()) {
58 persistent = v8::Persistent<T>::New(other.persistent);
62 inline bool IsEmpty() const { return persistent.IsEmpty(); }
64 inline void Empty() { persistent.Clear(); }
67 inline bool operator==(const PersistentBase<S> &that) const {
68 return this->persistent == that.persistent;
72 inline bool operator==(const v8::Local<S> &that) const {
73 return this->persistent == that;
77 inline bool operator!=(const PersistentBase<S> &that) const {
78 return !operator==(that);
82 inline bool operator!=(const v8::Local<S> &that) const {
83 return !operator==(that);
89 , typename WeakCallbackInfo<P>::Callback callback
90 , WeakCallbackType type);
92 inline void ClearWeak() { persistent.ClearWeak(); }
94 inline void MarkIndependent() { persistent.MarkIndependent(); }
96 inline bool IsIndependent() const { return persistent.IsIndependent(); }
98 inline bool IsNearDeath() const { return persistent.IsNearDeath(); }
100 inline bool IsWeak() const { return persistent.IsWeak(); }
103 inline explicit PersistentBase(v8::Persistent<T> that) :
105 inline explicit PersistentBase(T *val) : persistent(val) {}
106 template<typename S, typename M> friend class Persistent;
107 template<typename S> friend class Global;
108 friend class ObjectWrap;
112 class NonCopyablePersistentTraits {
114 typedef Persistent<T, NonCopyablePersistentTraits<T> >
115 NonCopyablePersistent;
116 static const bool kResetInDestructor = false;
117 template<typename S, typename M>
118 inline static void Copy(const Persistent<S, M> &source,
119 NonCopyablePersistent *dest) {
120 Uncompilable<v8::Object>();
123 template<typename O> inline static void Uncompilable() {
124 TYPE_CHECK(O, v8::Primitive);
129 struct CopyablePersistentTraits {
130 typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
131 static const bool kResetInDestructor = true;
132 template<typename S, typename M>
133 static inline void Copy(const Persistent<S, M> &source,
134 CopyablePersistent *dest) {}
137 template<typename T, typename M> class Persistent :
138 public PersistentBase<T> {
140 inline Persistent() {}
142 template<typename S> inline Persistent(v8::Handle<S> that)
143 : PersistentBase<T>(v8::Persistent<T>::New(that)) {
147 inline Persistent(const Persistent &that) : PersistentBase<T>() {
151 template<typename S, typename M2>
152 inline Persistent(const Persistent<S, M2> &that) :
153 PersistentBase<T>() {
157 inline Persistent &operator=(const Persistent &that) {
162 template <class S, class M2>
163 inline Persistent &operator=(const Persistent<S, M2> &that) {
168 inline ~Persistent() {
169 if (M::kResetInDestructor) this->Reset();
173 inline T *operator*() const { return *PersistentBase<T>::persistent; }
175 template<typename S, typename M2>
176 inline void Copy(const Persistent<S, M2> &that) {
181 if (!that.IsEmpty()) {
182 this->persistent = v8::Persistent<T>::New(that.persistent);
189 class Global : public PersistentBase<T> {
191 inline explicit RValue(Global* obj) : object(obj) {}
196 inline Global() : PersistentBase<T>(0) { }
198 template <typename S>
199 inline Global(v8::Local<S> that) // NOLINT(runtime/explicit)
200 : PersistentBase<T>(v8::Persistent<T>::New(that)) {
204 template <typename S>
205 inline Global(const PersistentBase<S> &that) // NOLINT(runtime/explicit)
206 : PersistentBase<T>(that) {
212 inline Global(RValue rvalue) // NOLINT(runtime/explicit)
213 : PersistentBase<T>(rvalue.object->persistent) {
214 rvalue.object->Reset();
216 inline ~Global() { this->Reset(); }
218 * Move via assignment.
221 inline Global &operator=(Global<S> rhs) {
223 this->Reset(rhs.persistent);
228 * Cast operator for moves.
230 inline operator RValue() { return RValue(this); }
232 * Pass allows returning uniques from functions, etc.
234 Global Pass() { return Global(RValue(this)); }
238 void operator=(Global &);
239 template<typename S> friend class ReturnValue;
242 #endif // NAN_PERSISTENT_PRE_12_INL_H_