nan_persistent_12_inl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2016 NAN contributors
  5. *
  6. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  7. ********************************************************************/
  8. #ifndef NAN_PERSISTENT_12_INL_H_
  9. #define NAN_PERSISTENT_12_INL_H_
  10. template<typename T, typename M> class Persistent :
  11. public v8::Persistent<T, M> {
  12. public:
  13. inline Persistent() : v8::Persistent<T, M>() {}
  14. template<typename S> inline Persistent(v8::Local<S> that) :
  15. v8::Persistent<T, M>(v8::Isolate::GetCurrent(), that) {}
  16. template<typename S, typename M2>
  17. inline Persistent(const v8::Persistent<S, M2> &that) :
  18. v8::Persistent<T, M2>(v8::Isolate::GetCurrent(), that) {}
  19. inline void Reset() { v8::PersistentBase<T>::Reset(); }
  20. template <typename S>
  21. inline void Reset(const v8::Local<S> &other) {
  22. v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
  23. }
  24. template <typename S>
  25. inline void Reset(const v8::PersistentBase<S> &other) {
  26. v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
  27. }
  28. template<typename P>
  29. inline void SetWeak(
  30. P *parameter
  31. , typename WeakCallbackInfo<P>::Callback callback
  32. , WeakCallbackType type);
  33. private:
  34. inline T *operator*() const { return *PersistentBase<T>::persistent; }
  35. template<typename S, typename M2>
  36. inline void Copy(const Persistent<S, M2> &that) {
  37. TYPE_CHECK(T, S);
  38. this->Reset();
  39. if (!that.IsEmpty()) {
  40. this->Reset(that);
  41. M::Copy(that, this);
  42. }
  43. }
  44. };
  45. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  46. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  47. template<typename T>
  48. class Global : public v8::Global<T> {
  49. public:
  50. inline Global() : v8::Global<T>() {}
  51. template<typename S> inline Global(v8::Local<S> that) :
  52. v8::Global<T>(v8::Isolate::GetCurrent(), that) {}
  53. template<typename S>
  54. inline Global(const v8::PersistentBase<S> &that) :
  55. v8::Global<S>(v8::Isolate::GetCurrent(), that) {}
  56. inline void Reset() { v8::PersistentBase<T>::Reset(); }
  57. template <typename S>
  58. inline void Reset(const v8::Local<S> &other) {
  59. v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
  60. }
  61. template <typename S>
  62. inline void Reset(const v8::PersistentBase<S> &other) {
  63. v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
  64. }
  65. template<typename P>
  66. inline void SetWeak(
  67. P *parameter
  68. , typename WeakCallbackInfo<P>::Callback callback
  69. , WeakCallbackType type) {
  70. reinterpret_cast<Persistent<T>*>(this)->SetWeak(
  71. parameter, callback, type);
  72. }
  73. };
  74. #else
  75. template<typename T>
  76. class Global : public v8::UniquePersistent<T> {
  77. public:
  78. inline Global() : v8::UniquePersistent<T>() {}
  79. template<typename S> inline Global(v8::Local<S> that) :
  80. v8::UniquePersistent<T>(v8::Isolate::GetCurrent(), that) {}
  81. template<typename S>
  82. inline Global(const v8::PersistentBase<S> &that) :
  83. v8::UniquePersistent<S>(v8::Isolate::GetCurrent(), that) {}
  84. inline void Reset() { v8::PersistentBase<T>::Reset(); }
  85. template <typename S>
  86. inline void Reset(const v8::Local<S> &other) {
  87. v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
  88. }
  89. template <typename S>
  90. inline void Reset(const v8::PersistentBase<S> &other) {
  91. v8::PersistentBase<T>::Reset(v8::Isolate::GetCurrent(), other);
  92. }
  93. template<typename P>
  94. inline void SetWeak(
  95. P *parameter
  96. , typename WeakCallbackInfo<P>::Callback callback
  97. , WeakCallbackType type) {
  98. reinterpret_cast<Persistent<T>*>(this)->SetWeak(
  99. parameter, callback, type);
  100. }
  101. };
  102. #endif
  103. #endif // NAN_PERSISTENT_12_INL_H_