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

Created on 3 Oct 2020  路  3Comments  路  Source: neon-bindings/neon

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 馃槉

needs RFC

Most helpful comment

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.

All 3 comments

@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.

@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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phrohdoh picture phrohdoh  路  6Comments

crazyfrozenpenguin picture crazyfrozenpenguin  路  3Comments

chpio picture chpio  路  6Comments

manuel2258 picture manuel2258  路  5Comments

usagi picture usagi  路  3Comments