diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-01-10 19:15:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-10 19:15:10 +0100 |
commit | 14ada3dce2ede9cffacfe829cca04f4ef262f91b (patch) | |
tree | ff2892510e619f49e6ec94acb904e5f8c7d7cda4 /cli | |
parent | 71ea4ef2746d7d75623a821d4832d3531a8e1654 (diff) |
fix(napi): support for env cleanup hooks (#17324)
This commit adds support for "napi_add_env_cleanup_hook" and
"napi_remove_env_cleanup_hook" function for Node-API.
Diffstat (limited to 'cli')
-rw-r--r-- | cli/napi/env.rs | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/cli/napi/env.rs b/cli/napi/env.rs index 1d59b2f95..6568c20c9 100644 --- a/cli/napi/env.rs +++ b/cli/napi/env.rs @@ -52,24 +52,49 @@ fn napi_fatal_exception(env: *mut Env, value: napi_value) -> Result { ); } -// TODO: properly implement #[napi_sym::napi_sym] fn napi_add_env_cleanup_hook( - _env: *mut Env, - _hook: extern "C" fn(*const c_void), - _data: *const c_void, + env: *mut Env, + hook: extern "C" fn(*const c_void), + data: *const c_void, ) -> Result { - log::info!("napi_add_env_cleanup_hook is currently not supported"); + let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?; + + { + let mut env_cleanup_hooks = env.cleanup_hooks.borrow_mut(); + if env_cleanup_hooks + .iter() + .any(|pair| pair.0 == hook && pair.1 == data) + { + panic!("Cleanup hook with this data already registered"); + } + env_cleanup_hooks.push((hook, data)); + } Ok(()) } #[napi_sym::napi_sym] fn napi_remove_env_cleanup_hook( - _env: *mut Env, - _hook: extern "C" fn(*const c_void), - _data: *const c_void, + env: *mut Env, + hook: extern "C" fn(*const c_void), + data: *const c_void, ) -> Result { - log::info!("napi_remove_env_cleanup_hook is currently not supported"); + let env: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?; + + { + let mut env_cleanup_hooks = env.cleanup_hooks.borrow_mut(); + // Hooks are supposed to be removed in LIFO order + let maybe_index = env_cleanup_hooks + .iter() + .rposition(|&pair| pair.0 == hook && pair.1 == data); + + if let Some(index) = maybe_index { + env_cleanup_hooks.remove(index); + } else { + panic!("Cleanup hook with this data not found"); + } + } + Ok(()) } |