選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

nan_object_wrap.h 4.0KB

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