Skip to content

Commit

Permalink
Reimplement weak callback in ObjectWrap
Browse files Browse the repository at this point in the history
Fixes: #389
PR-URL: #407
  • Loading branch information
kkoopa committed Aug 10, 2015
1 parent f63c230 commit 98d38c1
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions nan_object_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class ObjectWrap {


virtual ~ObjectWrap() {
if (persistent().IsEmpty()) {
return;
}

assert(persistent().IsNearDeath());
persistent().ClearWeak();
persistent().Reset();
}


Expand Down Expand Up @@ -51,12 +58,31 @@ class ObjectWrap {
MakeWeak();
}

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))

inline void MakeWeak() {
persistent().v8::PersistentBase<v8::Object>::SetWeak(
this, WeakCallback, v8::WeakCallbackType::kParameter);
persistent().MarkIndependent();
}

#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION

inline void MakeWeak() {
persistent().v8::PersistentBase<v8::Object>::SetWeak(this, WeakCallback);
persistent().MarkIndependent();
}

#else

inline void MakeWeak() {
persistent().SetWeak(this, WeakCallback, WeakCallbackType::kParameter);
persistent().persistent.MakeWeak(this, WeakCallback);
persistent().MarkIndependent();
}

#endif

/* Ref() marks the object as being attached to an event loop.
* Refed objects will not be garbage collected, even if
* all references are lost.
Expand Down Expand Up @@ -87,14 +113,40 @@ class ObjectWrap {
int refs_; // ro

private:
static void WeakCallback(
const WeakCallbackInfo<ObjectWrap>& data) {
HandleScope scope;
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
(V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))

static void
WeakCallback(v8::WeakCallbackInfo<ObjectWrap> const& info) {
ObjectWrap* wrap = info.GetParameter();
assert(wrap->refs_ == 0);
assert(wrap->handle_.IsNearDeath());
wrap->handle_.Reset();
delete wrap;
}

#elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION

static void
WeakCallback(v8::WeakCallbackData<v8::Object, ObjectWrap> const& data) {
ObjectWrap* wrap = data.GetParameter();
assert(wrap->refs_ == 0);
assert(wrap->handle_.IsNearDeath());
wrap->handle_.Reset();
delete wrap;
}

#else

static void WeakCallback(v8::Persistent<v8::Value> value, void *data) {
ObjectWrap *wrap = static_cast<ObjectWrap*>(data);
assert(wrap->refs_ == 0);
assert(wrap->handle_.IsNearDeath());
wrap->handle_.Reset();
delete wrap;
}

#endif
Persistent<v8::Object> handle_;
};

Expand Down

0 comments on commit 98d38c1

Please sign in to comment.