node-addon-examples icon indicating copy to clipboard operation
node-addon-examples copied to clipboard

Do we need `wrapper_`?

Open gabrielschulhof opened this issue 5 years ago • 3 comments

In our ObjectWrap N-API examples we store the napi_ref returned from napi_wrap() in the native object instance although we never use it. To napi_delete_reference() in the destructor we need to also store the napi_env on the native instance – a practice we discourage.

Do we do this for illustration purposes? Can we remove this, since wrapper_ is not being used anywhere?

gabrielschulhof avatar Jun 10 '20 16:06 gabrielschulhof

This is the extent to which we use wrapper_ and env_:

https://github.com/nodejs/node-addon-examples/blob/dc86a662c27c5732e069e1c19d3b7a8e74e86d29/6_object_wrap/napi/myobject.cc#L6-L11

and

https://github.com/nodejs/node-addon-examples/blob/dc86a662c27c5732e069e1c19d3b7a8e74e86d29/6_object_wrap/napi/myobject.cc#L72-L78

gabrielschulhof avatar Jun 10 '20 16:06 gabrielschulhof

Compare with the Nan implementation. The use of the persistent reference may stem from there. If so, it may be a good time to diverge, especially since we document that we discourage storing the napi_env.

gabrielschulhof avatar Jun 15 '20 16:06 gabrielschulhof

In fact, none of the Nan examples store a persistent reference to the JS instance. We should remove it.

gabrielschulhof avatar Jun 16 '20 17:06 gabrielschulhof