diff options
Diffstat (limited to 'cli/napi/env.rs')
-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(()) } |