neon icon indicating copy to clipboard operation
neon copied to clipboard

Get a raw `napi_env` from `Context` for use in N-API methods not implemented by Neon.

Open vasishath opened this issue 5 years ago • 4 comments
trafficstars

I want to call a napi cleanup routine method napi_add_env_cleanup_hook(). This method gives a hook to perform any cleanup (for eg. in electron before a page is refreshed or is being navigated away).

It is available in the generated bindings but is not implemented yet in the nodejs-sys crate. Since env is not exposed to users, is there a way to call this function directly ? Or will all the methods be wrapped eventually ?

Thanks a lot 😊

vasishath avatar Oct 03 '20 18:10 vasishath

@vasishath, I'm not sure what you mean by "performing any cleanup before a page is refreshed or being navigated away." Is this related to Electron? There isn't really a concept of pages in node.js.

As far as providing this method, we are happy to accept a PR either as an RFC to add this feature or with an implementation to start the discussion. However, a clean-up hook doesn't seem very "Rust-y". My preference would be to implement napi_set_instance_data and napi_get_instance_data. A Drop implementation on the instance data would provide the same functionality as napi_add_env_cleanup_hook in a more idiomatic way.

kjvalencik avatar Oct 05 '20 15:10 kjvalencik

@vasishath, I'm not sure what you mean by "performing any cleanup before a page is refreshed or being navigated away." Is this related to Electron? There isn't really a concept of pages in node.js.

As far as providing this method, we are happy to accept a PR either as an RFC to add this feature or with an implementation to start the discussion. However, a clean-up hook doesn't seem very "Rust-y". My preference would be to implement napi_set_instance_data and napi_get_instance_data. A Drop implementation on the instance data would provide the same functionality as napi_add_env_cleanup_hook in a more idiomatic way.

Yes actually I meant electron. Sorry I forgot to mention that. Yea even I would prefer a rusty way for the tear down phase. I didn't knew this API existed.

vasishath avatar Oct 05 '20 15:10 vasishath

Summary

Currently, neon users may only interact with the JavaScript VM in ways prescribed by neon. Users may want to leverage N-API methods before they are adopted by Neon.

Idea

Add a public method to Context to allow users access to the raw Env.

trait Context<'a> {
    fn raw_env(&self) -> *mut c_void;
}

It's always safe to get a raw pointers. Users would require unsafe to use the pointer.

kjvalencik avatar Oct 13 '20 14:10 kjvalencik

Removed the RFC. This is straight forward.

kjvalencik avatar Sep 15 '21 21:09 kjvalencik