Skip to content

Commit

Permalink
fix(ext/napi): pass user context to napi_threadsafe_fn finalizers (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Oct 14, 2024
1 parent 7c3da2e commit d22195e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli/napi/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl Drop for TsFn {

if let Some(finalizer) = self.thread_finalize_cb {
unsafe {
(finalizer)(self.env as _, self.thread_finalize_data, ptr::null_mut());
(finalizer)(self.env as _, self.thread_finalize_data, self.context);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/napi/src/tsfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn create_custom_gc(env: sys::napi_env) {
"Create async resource string in napi_register_module_v1 napi_register_module_v1"
);
let mut custom_gc_tsfn = ptr::null_mut();
let context = Box::into_raw(Box::new(0)) as *mut c_void;
check_status_or_panic!(
unsafe {
sys::napi_create_threadsafe_function(
Expand All @@ -57,7 +58,7 @@ fn create_custom_gc(env: sys::napi_env) {
1,
ptr::null_mut(),
Some(custom_gc_finalize),
ptr::null_mut(),
context,
Some(custom_gc),
&mut custom_gc_tsfn,
)
Expand All @@ -80,8 +81,9 @@ unsafe extern "C" fn empty(
unsafe extern "C" fn custom_gc_finalize(
_env: sys::napi_env,
_finalize_data: *mut c_void,
_finalize_hint: *mut c_void,
finalize_hint: *mut c_void,
) {
let _ = Box::from_raw(finalize_hint as *mut i32);
}

extern "C" fn custom_gc(
Expand Down

0 comments on commit d22195e

Please sign in to comment.