nan_object_wrap.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_OBJECT_WRAP_H_
  9. #define NAN_OBJECT_WRAP_H_
  10. class ObjectWrap {
  11. public:
  12. ObjectWrap() {
  13. refs_ = 0;
  14. }
  15. virtual ~ObjectWrap() {
  16. if (persistent().IsEmpty()) {
  17. return;
  18. }
  19. assert(persistent().IsNearDeath());
  20. persistent().ClearWeak();
  21. persistent().Reset();
  22. }
  23. template <class T>
  24. static inline T* Unwrap(v8::Local<v8::Object> object) {
  25. assert(!object.IsEmpty());
  26. assert(object->InternalFieldCount() > 0);
  27. // Cast to ObjectWrap before casting to T. A direct cast from void
  28. // to T won't work right when T has more than one base class.
  29. void* ptr = GetInternalFieldPointer(object, 0);
  30. ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr);
  31. return static_cast<T*>(wrap);
  32. }
  33. inline v8::Local<v8::Object> handle() const {
  34. return New(handle_);
  35. }
  36. inline Persistent<v8::Object>& persistent() {
  37. return handle_;
  38. }
  39. protected:
  40. inline void Wrap(v8::Local<v8::Object> object) {
  41. assert(persistent().IsEmpty());
  42. assert(object->InternalFieldCount() > 0);
  43. SetInternalFieldPointer(object, 0, this);
  44. persistent().Reset(object);
  45. MakeWeak();
  46. }
  47. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  48. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  49. inline void MakeWeak() {
  50. persistent().v8::PersistentBase<v8::Object>::SetWeak(
  51. this, WeakCallback, v8::WeakCallbackType::kParameter);
  52. persistent().MarkIndependent();
  53. }
  54. #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  55. inline void MakeWeak() {
  56. persistent().v8::PersistentBase<v8::Object>::SetWeak(this, WeakCallback);
  57. persistent().MarkIndependent();
  58. }
  59. #else
  60. inline void MakeWeak() {
  61. persistent().persistent.MakeWeak(this, WeakCallback);
  62. persistent().MarkIndependent();
  63. }
  64. #endif
  65. /* Ref() marks the object as being attached to an event loop.
  66. * Refed objects will not be garbage collected, even if
  67. * all references are lost.
  68. */
  69. virtual void Ref() {
  70. assert(!persistent().IsEmpty());
  71. persistent().ClearWeak();
  72. refs_++;
  73. }
  74. /* Unref() marks an object as detached from the event loop. This is its
  75. * default state. When an object with a "weak" reference changes from
  76. * attached to detached state it will be freed. Be careful not to access
  77. * the object after making this call as it might be gone!
  78. * (A "weak reference" means an object that only has a
  79. * persistant handle.)
  80. *
  81. * DO NOT CALL THIS FROM DESTRUCTOR
  82. */
  83. virtual void Unref() {
  84. assert(!persistent().IsEmpty());
  85. assert(!persistent().IsWeak());
  86. assert(refs_ > 0);
  87. if (--refs_ == 0)
  88. MakeWeak();
  89. }
  90. int refs_; // ro
  91. private:
  92. NAN_DISALLOW_ASSIGN_COPY_MOVE(ObjectWrap)
  93. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  94. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  95. static void
  96. WeakCallback(v8::WeakCallbackInfo<ObjectWrap> const& info) {
  97. ObjectWrap* wrap = info.GetParameter();
  98. assert(wrap->refs_ == 0);
  99. assert(wrap->handle_.IsNearDeath());
  100. wrap->handle_.Reset();
  101. delete wrap;
  102. }
  103. #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  104. static void
  105. WeakCallback(v8::WeakCallbackData<v8::Object, ObjectWrap> const& data) {
  106. ObjectWrap* wrap = data.GetParameter();
  107. assert(wrap->refs_ == 0);
  108. assert(wrap->handle_.IsNearDeath());
  109. wrap->handle_.Reset();
  110. delete wrap;
  111. }
  112. #else
  113. static void WeakCallback(v8::Persistent<v8::Value> value, void *data) {
  114. ObjectWrap *wrap = static_cast<ObjectWrap*>(data);
  115. assert(wrap->refs_ == 0);
  116. assert(wrap->handle_.IsNearDeath());
  117. wrap->handle_.Reset();
  118. delete wrap;
  119. }
  120. #endif
  121. Persistent<v8::Object> handle_;
  122. };
  123. #endif // NAN_OBJECT_WRAP_H_