summaryrefslogtreecommitdiff
path: root/test_napi/src
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-01-13 22:17:25 +0100
committerGitHub <noreply@github.com>2023-01-13 22:17:25 +0100
commit225114166aa7426d4b93fa13635559029c5ba65d (patch)
treead450bcd2a68b49adbeaf21ec10c4652ca2af056 /test_napi/src
parentd4767a1a876f06b3b4b703b79b16af3571f2f583 (diff)
fix(napi): allow cleanup hook to remove itself (#17402)
This commit fixes "cleanup hooks" in NAPI integration in two ways: - don't hold to RefCell's borrow while iterating over hooks - allow a hook to remove itself when being called
Diffstat (limited to 'test_napi/src')
-rw-r--r--test_napi/src/lib.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/test_napi/src/lib.rs b/test_napi/src/lib.rs
index 2d43b0aea..b54b3886b 100644
--- a/test_napi/src/lib.rs
+++ b/test_napi/src/lib.rs
@@ -66,6 +66,11 @@ extern "C" fn cleanup(arg: *mut c_void) {
println!("cleanup({})", arg as i64);
}
+extern "C" fn remove_this_hook(arg: *mut c_void) {
+ let env = arg as napi_env;
+ unsafe { napi_remove_env_cleanup_hook(env, Some(remove_this_hook), arg) };
+}
+
static SECRET: i64 = 42;
static WRONG_SECRET: i64 = 17;
static THIRD_SECRET: i64 = 18;
@@ -81,6 +86,7 @@ extern "C" fn install_cleanup_hook(
napi_add_env_cleanup_hook(env, Some(cleanup), WRONG_SECRET as *mut c_void);
napi_add_env_cleanup_hook(env, Some(cleanup), SECRET as *mut c_void);
napi_add_env_cleanup_hook(env, Some(cleanup), THIRD_SECRET as *mut c_void);
+ napi_add_env_cleanup_hook(env, Some(remove_this_hook), env as *mut c_void);
napi_remove_env_cleanup_hook(
env,
Some(cleanup),