nodejs / abi-stable-node

Repository used by the Node-API team to manage work related to Node-API and node-addon-api
239 stars 47 forks source link

Retrieve the finalizer for an object #311

Closed gabrielschulhof closed 2 years ago

gabrielschulhof commented 6 years ago

For objects created using N-API, it would help to have a function to get the finalizer callback for the value, for example:

napi_get_value_finalizer(napi_env env,napi_value val,napi_finalize*result)

This can be used to determine who created an object, by comparing the finalizer function pointer (which will be a null pointer if there is no finalizer callback defined) with the address of the native code's own functions (there is no point calling the returned finalizer callback).

Re: https://github.com/nodejs/node/issues/14256

Such type-checking can also be accomplished by having the wrapped objects be instances of certain JS classes, and then using napi_instanceof() to decide if a given napi_value containing a JavaScript object has the kind of pointer that is expected.

Are there any other use cases where knowing the identity of the finalizer is helpful?

gabrielschulhof commented 6 years ago

In the cases where we might consider adding an API to retrieve the finalizer, if we consider that almost always the native module is fronted by a JS module then that IMO weakens the case for such an API addition, because the native module and the JS module fronting the native module are part of the same package, and therefore under the control of a common entity.

Yet such a mechanism would provide a fail-safe type check.

mhdawson commented 2 years ago

@gabrielschulhof do you think we still need to keep this open? It's been a few years and we've not had any external requests for it.

mhdawson commented 2 years ago

With type tagging this is less necessary and we've not implemented in a number of years so we agreed to close in the Node-api team meeting today. Please let us know if that was not the right thing to do.