ObjectWrap.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #include "ObjectWrap.h"
  22. #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
  23. namespace se {
  24. ObjectWrap::ObjectWrap() {
  25. refs_ = 0;
  26. _nativeObj = nullptr;
  27. _finalizeCb = nullptr;
  28. }
  29. bool ObjectWrap::init(v8::Local<v8::Object> handle) {
  30. assert(persistent().IsEmpty());
  31. persistent().Reset(v8::Isolate::GetCurrent(), handle);
  32. makeWeak();
  33. return true;
  34. }
  35. void ObjectWrap::setFinalizeCallback(V8FinalizeFunc finalizeCb) {
  36. _finalizeCb = finalizeCb;
  37. }
  38. ObjectWrap::~ObjectWrap() {
  39. if (persistent().IsEmpty())
  40. return;
  41. //cjh assert(persistent().IsNearDeath());
  42. persistent().ClearWeak();
  43. persistent().Reset();
  44. }
  45. /*static*/
  46. void *ObjectWrap::unwrap(v8::Local<v8::Object> handle) {
  47. assert(!handle.IsEmpty());
  48. assert(handle->InternalFieldCount() > 0);
  49. return handle->GetAlignedPointerFromInternalField(0);
  50. }
  51. v8::Local<v8::Object> ObjectWrap::handle() {
  52. return handle(v8::Isolate::GetCurrent());
  53. }
  54. v8::Local<v8::Object> ObjectWrap::handle(v8::Isolate *isolate) {
  55. return v8::Local<v8::Object>::New(isolate, persistent());
  56. }
  57. v8::Persistent<v8::Object> &ObjectWrap::persistent() {
  58. return handle_;
  59. }
  60. void ObjectWrap::wrap(void *nativeObj) {
  61. assert(handle()->InternalFieldCount() > 0);
  62. _nativeObj = nativeObj;
  63. handle()->SetAlignedPointerInInternalField(0, nativeObj);
  64. }
  65. void ObjectWrap::makeWeak() {
  66. persistent().SetWeak(this, weakCallback, v8::WeakCallbackType::kFinalizer);
  67. // persistent().MarkIndependent();
  68. }
  69. void ObjectWrap::ref() {
  70. assert(!persistent().IsEmpty());
  71. persistent().ClearWeak();
  72. refs_++;
  73. }
  74. void ObjectWrap::unref() {
  75. assert(!persistent().IsEmpty());
  76. assert(!persistent().IsWeak());
  77. assert(refs_ > 0);
  78. if (--refs_ == 0)
  79. makeWeak();
  80. }
  81. /*static*/
  82. void ObjectWrap::weakCallback(const v8::WeakCallbackInfo<ObjectWrap> &data) {
  83. ObjectWrap *wrap = data.GetParameter();
  84. // SE_LOGD("weakCallback: %p, nativeObj = %p, finalize: %p\n", wrap, wrap->_nativeObj, wrap->_finalizeCb);
  85. assert(wrap->refs_ == 0);
  86. wrap->handle_.Reset();
  87. if (wrap->_finalizeCb != nullptr)
  88. {
  89. wrap->_finalizeCb(wrap->_nativeObj); // wrap will be destroyed in wrap->_finalizeCb, should not use any wrap object after this line.
  90. }
  91. else
  92. {
  93. assert(false);
  94. }
  95. }
  96. } // namespace se {
  97. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8