nan_persistent_pre_12_inl.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2017 NAN contributors
  5. *
  6. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  7. ********************************************************************/
  8. #ifndef NAN_PERSISTENT_PRE_12_INL_H_
  9. #define NAN_PERSISTENT_PRE_12_INL_H_
  10. template<typename T>
  11. class PersistentBase {
  12. v8::Persistent<T> persistent;
  13. template<typename U>
  14. friend v8::Local<U> New(const PersistentBase<U> &p);
  15. template<typename U, typename M>
  16. friend v8::Local<U> New(const Persistent<U, M> &p);
  17. template<typename U>
  18. friend v8::Local<U> New(const Global<U> &p);
  19. template<typename S> friend class ReturnValue;
  20. public:
  21. inline PersistentBase() :
  22. persistent() {}
  23. inline void Reset() {
  24. persistent.Dispose();
  25. persistent.Clear();
  26. }
  27. template<typename S>
  28. inline void Reset(const v8::Local<S> &other) {
  29. TYPE_CHECK(T, S);
  30. if (!persistent.IsEmpty()) {
  31. persistent.Dispose();
  32. }
  33. if (other.IsEmpty()) {
  34. persistent.Clear();
  35. } else {
  36. persistent = v8::Persistent<T>::New(other);
  37. }
  38. }
  39. template<typename S>
  40. inline void Reset(const PersistentBase<S> &other) {
  41. TYPE_CHECK(T, S);
  42. if (!persistent.IsEmpty()) {
  43. persistent.Dispose();
  44. }
  45. if (other.IsEmpty()) {
  46. persistent.Clear();
  47. } else {
  48. persistent = v8::Persistent<T>::New(other.persistent);
  49. }
  50. }
  51. inline bool IsEmpty() const { return persistent.IsEmpty(); }
  52. inline void Empty() { persistent.Clear(); }
  53. template<typename S>
  54. inline bool operator==(const PersistentBase<S> &that) const {
  55. return this->persistent == that.persistent;
  56. }
  57. template<typename S>
  58. inline bool operator==(const v8::Local<S> &that) const {
  59. return this->persistent == that;
  60. }
  61. template<typename S>
  62. inline bool operator!=(const PersistentBase<S> &that) const {
  63. return !operator==(that);
  64. }
  65. template<typename S>
  66. inline bool operator!=(const v8::Local<S> &that) const {
  67. return !operator==(that);
  68. }
  69. template<typename P>
  70. inline void SetWeak(
  71. P *parameter
  72. , typename WeakCallbackInfo<P>::Callback callback
  73. , WeakCallbackType type);
  74. inline void ClearWeak() { persistent.ClearWeak(); }
  75. inline void MarkIndependent() { persistent.MarkIndependent(); }
  76. inline bool IsIndependent() const { return persistent.IsIndependent(); }
  77. inline bool IsNearDeath() const { return persistent.IsNearDeath(); }
  78. inline bool IsWeak() const { return persistent.IsWeak(); }
  79. private:
  80. inline explicit PersistentBase(v8::Persistent<T> that) :
  81. persistent(that) { }
  82. inline explicit PersistentBase(T *val) : persistent(val) {}
  83. template<typename S, typename M> friend class Persistent;
  84. template<typename S> friend class Global;
  85. friend class ObjectWrap;
  86. };
  87. template<typename T>
  88. class NonCopyablePersistentTraits {
  89. public:
  90. typedef Persistent<T, NonCopyablePersistentTraits<T> >
  91. NonCopyablePersistent;
  92. static const bool kResetInDestructor = false;
  93. template<typename S, typename M>
  94. inline static void Copy(const Persistent<S, M> &source,
  95. NonCopyablePersistent *dest) {
  96. Uncompilable<v8::Object>();
  97. }
  98. template<typename O> inline static void Uncompilable() {
  99. TYPE_CHECK(O, v8::Primitive);
  100. }
  101. };
  102. template<typename T>
  103. struct CopyablePersistentTraits {
  104. typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
  105. static const bool kResetInDestructor = true;
  106. template<typename S, typename M>
  107. static inline void Copy(const Persistent<S, M> &source,
  108. CopyablePersistent *dest) {}
  109. };
  110. template<typename T, typename M> class Persistent :
  111. public PersistentBase<T> {
  112. public:
  113. inline Persistent() {}
  114. template<typename S> inline Persistent(v8::Handle<S> that)
  115. : PersistentBase<T>(v8::Persistent<T>::New(that)) {
  116. TYPE_CHECK(T, S);
  117. }
  118. inline Persistent(const Persistent &that) : PersistentBase<T>() {
  119. Copy(that);
  120. }
  121. template<typename S, typename M2>
  122. inline Persistent(const Persistent<S, M2> &that) :
  123. PersistentBase<T>() {
  124. Copy(that);
  125. }
  126. inline Persistent &operator=(const Persistent &that) {
  127. Copy(that);
  128. return *this;
  129. }
  130. template <class S, class M2>
  131. inline Persistent &operator=(const Persistent<S, M2> &that) {
  132. Copy(that);
  133. return *this;
  134. }
  135. inline ~Persistent() {
  136. if (M::kResetInDestructor) this->Reset();
  137. }
  138. private:
  139. inline T *operator*() const { return *PersistentBase<T>::persistent; }
  140. template<typename S, typename M2>
  141. inline void Copy(const Persistent<S, M2> &that) {
  142. TYPE_CHECK(T, S);
  143. this->Reset();
  144. if (!that.IsEmpty()) {
  145. this->persistent = v8::Persistent<T>::New(that.persistent);
  146. M::Copy(that, this);
  147. }
  148. }
  149. };
  150. template<typename T>
  151. class Global : public PersistentBase<T> {
  152. struct RValue {
  153. inline explicit RValue(Global* obj) : object(obj) {}
  154. Global* object;
  155. };
  156. public:
  157. inline Global() : PersistentBase<T>(0) { }
  158. template <typename S>
  159. inline Global(v8::Local<S> that) // NOLINT(runtime/explicit)
  160. : PersistentBase<T>(v8::Persistent<T>::New(that)) {
  161. TYPE_CHECK(T, S);
  162. }
  163. template <typename S>
  164. inline Global(const PersistentBase<S> &that) // NOLINT(runtime/explicit)
  165. : PersistentBase<T>(that) {
  166. TYPE_CHECK(T, S);
  167. }
  168. /**
  169. * Move constructor.
  170. */
  171. inline Global(RValue rvalue) // NOLINT(runtime/explicit)
  172. : PersistentBase<T>(rvalue.object->persistent) {
  173. rvalue.object->Reset();
  174. }
  175. inline ~Global() { this->Reset(); }
  176. /**
  177. * Move via assignment.
  178. */
  179. template<typename S>
  180. inline Global &operator=(Global<S> rhs) {
  181. TYPE_CHECK(T, S);
  182. this->Reset(rhs.persistent);
  183. rhs.Reset();
  184. return *this;
  185. }
  186. /**
  187. * Cast operator for moves.
  188. */
  189. inline operator RValue() { return RValue(this); }
  190. /**
  191. * Pass allows returning uniques from functions, etc.
  192. */
  193. Global Pass() { return Global(RValue(this)); }
  194. private:
  195. Global(Global &);
  196. void operator=(Global &);
  197. template<typename S> friend class ReturnValue;
  198. };
  199. #endif // NAN_PERSISTENT_PRE_12_INL_H_