ObjectWrap.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef SRC_NODE_OBJECT_WRAP_H_
  22. #define SRC_NODE_OBJECT_WRAP_H_
  23. #include "../config.hpp"
  24. #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
  25. #include "Base.h"
  26. namespace se {
  27. class ObjectWrap {
  28. public:
  29. ObjectWrap();
  30. ~ObjectWrap();
  31. bool init(v8::Local<v8::Object> handle);
  32. void setFinalizeCallback(V8FinalizeFunc finalizeCb);
  33. v8::Local<v8::Object> handle();
  34. v8::Local<v8::Object> handle(v8::Isolate *isolate);
  35. v8::Persistent<v8::Object> &persistent();
  36. void wrap(void *nativeObj);
  37. static void* unwrap(v8::Local<v8::Object> handle);
  38. /* Ref() marks the object as being attached to an event loop.
  39. * Refed objects will not be garbage collected, even if
  40. * all references are lost.
  41. */
  42. void ref();
  43. /* Unref() marks an object as detached from the event loop. This is its
  44. * default state. When an object with a "weak" reference changes from
  45. * attached to detached state it will be freed. Be careful not to access
  46. * the object after making this call as it might be gone!
  47. * (A "weak reference" means an object that only has a
  48. * persistent handle.)
  49. *
  50. * DO NOT CALL THIS FROM DESTRUCTOR
  51. */
  52. void unref();
  53. private:
  54. static void weakCallback(const v8::WeakCallbackInfo<ObjectWrap> &data);
  55. void makeWeak();
  56. int refs_; // ro
  57. v8::Persistent<v8::Object> handle_;
  58. void *_nativeObj;
  59. V8FinalizeFunc _finalizeCb;
  60. };
  61. } // namespace se
  62. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
  63. #endif // SRC_NODE_OBJECT_WRAP_H_