You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ability to get reference counter value from Napi::Reference object #702
There is no way to get reference count value from Napi::Reference object without side effects. I can use only Ref() or Unref() method, which returns the resulting reference count value.
What is the reason to not export this functionality as public API?
@ikokostya if you want to execute some code when a certain Napi::Object goes out of scope you can use Napi::Object::AddFinalizer to attach a callback that will be called when the Napi::Object is about to be garbage-collected.
auto worker = new DisposeWorker(info.Env(), this);
worker->Queue();
because there may exist another worker threads on the native object. Instead, I need to wait while other worker threads will be executed before start the dispose worker.
Napi::Object::AddFinalizer doesn't help me here, because JavaScript code keeps reference to the foo object and GC will not be called.
I don't believe there is any guarantee as to when the finalizer will be called except that it will be sometime after the object is collected. This means it is reasonable for napi_get_reference_value to return NULL before the finalizer is run.
Do you have a small recreate? The code in Value checks if the
_ref is nullptr so I'm wondering how the call to napi_get_reference_value is being passed an invalid value.
I can try to get a small recreate. But in the meantime I believe the problem to be that napi_get_reference_value in Value() is succeeding but returning NULL in value.
The invalid argument exception then comes from the call to T(_env, value):
In my case T is Buffer<uint8_t> so it ends up calling TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value). This then calls napi_get_typedarray_info which fails and results in an error being called.
Note that you will need to call IsEmpty() on the value after getting it back from weak.Value() to check if the value was returned or not. The weak.IsEmpty() does not check if the underlying object is present or not just if the weak itself wraps a N-API reference.
I solved my original problem without Napi::Reference. Just to be clear, current value of Napi::Reference instance can be got using napi_get_reference_value function. Closing issue.
Activity
mhdawson commentedon Apr 13, 2020
There was not specific reason, other than we did not think of it as requirement. What is your use case for it?
ikokostya commentedon Apr 13, 2020
I want to use ref counter for detecting if there are no active workers for some object.
Consider some class with asynchronous methods:
Each async method increases ref counter for
Fooinstance to prevent GC. In this case native object can be disposed only if ref count is equal to0:gabrielschulhof commentedon Apr 20, 2020
@ikokostya if you want to execute some code when a certain
Napi::Objectgoes out of scope you can useNapi::Object::AddFinalizerto attach a callback that will be called when theNapi::Objectis about to be garbage-collected.gabrielschulhof commentedon Apr 20, 2020
@ikokostya something like this might work:
ikokostya commentedon Apr 20, 2020
@gabrielschulhof I have another use case. The dispose worker calls non-trivial destructor on the corresponding native object:
When dispose will be called from JavaScript
I cannot just queue new worker
because there may exist another worker threads on the native object. Instead, I need to wait while other worker threads will be executed before start the dispose worker.
Napi::Object::AddFinalizerdoesn't help me here, because JavaScript code keeps reference to thefooobject and GC will not be called.davedoesdev commentedon May 8, 2020
Should finalizer always be called before napi_get_reference_value starts returning null for result?
I'm seeing occasions where my finalizer gets called later, meaning Reference#Value() is throwing an Invalid Argument exception.
mhdawson commentedon May 8, 2020
I don't believe there is any guarantee as to when the finalizer will be called except that it will be sometime after the object is collected. This means it is reasonable for napi_get_reference_value to return NULL before the finalizer is run.
Do you have a small recreate? The code in Value checks if the
_ref is nullptr so I'm wondering how the call to napi_get_reference_value is being passed an invalid value.
davedoesdev commentedon May 8, 2020
I can try to get a small recreate. But in the meantime I believe the problem to be that
napi_get_reference_valueinValue()is succeeding but returningNULLinvalue.The invalid argument exception then comes from the call to
T(_env, value):In my case
TisBuffer<uint8_t>so it ends up callingTypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value). This then callsnapi_get_typedarray_infowhich fails and results in an error being called.davedoesdev commentedon May 12, 2020
@mhdawson I got a small repro.
stalereference.ccstalereference.jsmhdawson commentedon May 12, 2020
Thanks, I've created a test we can add to our test suite and am looking at how we should fix.
src: fix use of Reference with typed arrays
mhdawson commentedon May 13, 2020
@davedoesdev can you check out the fix in #726.
Note that you will need to call IsEmpty() on the value after getting it back from weak.Value() to check if the value was returned or not. The weak.IsEmpty() does not check if the underlying object is present or not just if the weak itself wraps a N-API reference.
davedoesdev commentedon May 13, 2020
Yep, works for me - thanks!
ikokostya commentedon May 14, 2020
I solved my original problem without
Napi::Reference. Just to be clear, current value ofNapi::Referenceinstance can be got using napi_get_reference_value function. Closing issue.ikokostya commentedon May 14, 2020
Oops, missed that there is open associated PR
src: fix use of Reference with typed arrays
src: fix use of Reference with typed arrays
src: fix use of Reference with typed arrays
src: fix use of Reference with typed arrays