1 /*********************************************************************
2 * NAN - Native Abstractions for Node.js
4 * Copyright (c) 2017 NAN contributors
6 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7 ********************************************************************/
9 #ifndef NAN_OBJECT_WRAP_H_
10 #define NAN_OBJECT_WRAP_H_
19 virtual ~ObjectWrap() {
20 if (persistent().IsEmpty()) {
24 assert(persistent().IsNearDeath());
25 persistent().ClearWeak();
31 static inline T* Unwrap(v8::Local<v8::Object> object) {
32 assert(!object.IsEmpty());
33 assert(object->InternalFieldCount() > 0);
34 // Cast to ObjectWrap before casting to T. A direct cast from void
35 // to T won't work right when T has more than one base class.
36 void* ptr = GetInternalFieldPointer(object, 0);
37 ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr);
38 return static_cast<T*>(wrap);
42 inline v8::Local<v8::Object> handle() const {
47 inline Persistent<v8::Object>& persistent() {
53 inline void Wrap(v8::Local<v8::Object> object) {
54 assert(persistent().IsEmpty());
55 assert(object->InternalFieldCount() > 0);
56 SetInternalFieldPointer(object, 0, this);
57 persistent().Reset(object);
61 #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
62 (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
64 inline void MakeWeak() {
65 persistent().v8::PersistentBase<v8::Object>::SetWeak(
66 this, WeakCallback, v8::WeakCallbackType::kParameter);
67 persistent().MarkIndependent();
70 #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
72 inline void MakeWeak() {
73 persistent().v8::PersistentBase<v8::Object>::SetWeak(this, WeakCallback);
74 persistent().MarkIndependent();
79 inline void MakeWeak() {
80 persistent().persistent.MakeWeak(this, WeakCallback);
81 persistent().MarkIndependent();
86 /* Ref() marks the object as being attached to an event loop.
87 * Refed objects will not be garbage collected, even if
88 * all references are lost.
91 assert(!persistent().IsEmpty());
92 persistent().ClearWeak();
96 /* Unref() marks an object as detached from the event loop. This is its
97 * default state. When an object with a "weak" reference changes from
98 * attached to detached state it will be freed. Be careful not to access
99 * the object after making this call as it might be gone!
100 * (A "weak reference" means an object that only has a
101 * persistant handle.)
103 * DO NOT CALL THIS FROM DESTRUCTOR
105 virtual void Unref() {
106 assert(!persistent().IsEmpty());
107 assert(!persistent().IsWeak());
116 NAN_DISALLOW_ASSIGN_COPY_MOVE(ObjectWrap)
117 #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
118 (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
121 WeakCallback(v8::WeakCallbackInfo<ObjectWrap> const& info) {
122 ObjectWrap* wrap = info.GetParameter();
123 assert(wrap->refs_ == 0);
124 assert(wrap->handle_.IsNearDeath());
125 wrap->handle_.Reset();
129 #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
132 WeakCallback(v8::WeakCallbackData<v8::Object, ObjectWrap> const& data) {
133 ObjectWrap* wrap = data.GetParameter();
134 assert(wrap->refs_ == 0);
135 assert(wrap->handle_.IsNearDeath());
136 wrap->handle_.Reset();
142 static void WeakCallback(v8::Persistent<v8::Value> value, void *data) {
143 ObjectWrap *wrap = static_cast<ObjectWrap*>(data);
144 assert(wrap->refs_ == 0);
145 assert(wrap->handle_.IsNearDeath());
146 wrap->handle_.Reset();
151 Persistent<v8::Object> handle_;
155 #endif // NAN_OBJECT_WRAP_H_