diff --git a/README.md b/README.md index c153bb5..f1ebe98 100644 --- a/README.md +++ b/README.md @@ -125,12 +125,6 @@ Checks to see if `ref` is a dead reference. Returns `true` if the original Objec has already been GC'd, `false` otherwise. -### Boolean weak.isNearDeath(Weakref ref) - -Checks to see if `ref` is "near death". This will be `true` exactly during the -weak reference callback function, and `false` any other time. - - ### Boolean weak.isWeakRef(Object obj) Checks to see if `obj` is "weak reference" instance. Returns `true` if the diff --git a/src/weakref.cc b/src/weakref.cc index 32f0790..319d69b 100644 --- a/src/weakref.cc +++ b/src/weakref.cc @@ -128,20 +128,26 @@ NAN_INDEX_DELETER(WeakIndexedPropertyDeleter) { info.GetReturnValue().Set(!dead && Nan::Delete(obj, index).FromJust()); } +NAN_PROPERTY_ENUMERATOR(WeakNamedPropertyEnumerator) { + UNWRAP +#if NODE_MAJOR_VERSION >= 7 + info.GetReturnValue().Set(dead ? Nan::New(0) : obj->GetPropertyNames(Nan::GetCurrentContext(), KeyCollectionMode::kIncludePrototypes, ONLY_ENUMERABLE, IndexFilter::kSkipIndices).ToLocalChecked()); +#else + info.GetReturnValue().Set(dead ? Nan::New(0) : Nan::GetPropertyNames(obj).ToLocalChecked()); +#endif +} -/** - * Only one "enumerator" function needs to be defined. This function is used for - * both the property and indexed enumerator functions. - */ - -NAN_PROPERTY_ENUMERATOR(WeakPropertyEnumerator) { +NAN_INDEX_ENUMERATOR(WeakIndexedPropertyEnumerator) { UNWRAP +#if NODE_MAJOR_VERSION >= 7 + info.GetReturnValue().Set(dead ? Nan::New(0) : obj->GetPropertyNames(Nan::GetCurrentContext(), KeyCollectionMode::kIncludePrototypes, static_cast (ONLY_ENUMERABLE | SKIP_STRINGS | SKIP_SYMBOLS), IndexFilter::kIncludeIndices).ToLocalChecked()); +#else info.GetReturnValue().Set(dead ? Nan::New(0) : Nan::GetPropertyNames(obj).ToLocalChecked()); +#endif } /** - * Weakref callback function. Invokes the "global" callback function, - * which emits the _CB event on the per-object EventEmitter. + * Weakref callback function. Invokes the "global" callback function. */ static void TargetCallback(const Nan::WeakCallbackInfo &info) { @@ -152,11 +158,7 @@ static void TargetCallback(const Nan::WeakCallbackInfo &info) { Local argv[] = { Nan::New(cont->emitter) }; - // Invoke callback directly, not via Nan::Callback->Call() which uses - // node::MakeCallback() which calls into process._tickCallback() - // too. Those other callbacks are not safe to run from here. - v8::Local globalCallbackDirect = globalCallback->GetFunction(); - globalCallbackDirect->Call(Nan::GetCurrentContext()->Global(), 1, argv); + Nan::Call(*globalCallback, 1, argv); // clean everything up Local proxy = Nan::New(cont->proxy); @@ -177,7 +179,7 @@ NAN_METHOD(Create) { Local _target = info[0].As(); Local _emitter = info[1].As(); - Local proxy = Nan::New(proxyClass)->NewInstance(); + Local proxy = Nan::NewInstance(Nan::New(proxyClass)).ToLocalChecked(); cont->proxy.Reset(proxy); cont->emitter.Reset(_emitter); cont->target.Reset(_target); @@ -224,23 +226,6 @@ NAN_METHOD(Get) { info.GetReturnValue().Set(Unwrap(proxy)); } -/** - * `isNearDeath(weakref)` JS function. - */ - -NAN_METHOD(IsNearDeath) { - WEAKREF_FIRST_ARG - - proxy_container *cont = reinterpret_cast( - Nan::GetInternalFieldPointer(proxy, FIELD_INDEX_CONTAINER) - ); - assert(cont != NULL); - - Local rtn = Nan::New(cont->target.IsNearDeath()); - - info.GetReturnValue().Set(rtn); -} - /** * `isDead(weakref)` JS function. */ @@ -282,18 +267,17 @@ NAN_MODULE_INIT(Initialize) { WeakNamedPropertySetter, WeakNamedPropertyQuery, WeakNamedPropertyDeleter, - WeakPropertyEnumerator); + WeakNamedPropertyEnumerator); Nan::SetIndexedPropertyHandler(p, WeakIndexedPropertyGetter, WeakIndexedPropertySetter, WeakIndexedPropertyQuery, WeakIndexedPropertyDeleter, - WeakPropertyEnumerator); + WeakIndexedPropertyEnumerator); p->SetInternalFieldCount(FIELD_COUNT); Nan::SetMethod(target, "get", Get); Nan::SetMethod(target, "isWeakRef", IsWeakRef); - Nan::SetMethod(target, "isNearDeath", IsNearDeath); Nan::SetMethod(target, "isDead", IsDead); Nan::SetMethod(target, "_create", Create); Nan::SetMethod(target, "_getEmitter", GetEmitter); diff --git a/test/exports.js b/test/exports.js index 348cffd..b96740f 100644 --- a/test/exports.js +++ b/test/exports.js @@ -18,7 +18,6 @@ describe('exports', function () { checkFunction('get') checkFunction('create') checkFunction('isWeakRef') - checkFunction('isNearDeath') checkFunction('isDead') checkFunction('callbacks') checkFunction('addCallback')