fix(napi-derive): increase initial ref count in async fn (#1577)

This commit is contained in:
LongYinan 2023-04-26 15:18:00 +08:00 committed by GitHub
parent 9a220bd1c2
commit d184d503d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View file

@ -49,6 +49,11 @@ impl TryToTokens for NapiFn {
impl NapiRefContainer {
fn drop(self, env: napi::sys::napi_env) {
for r in self.0.into_iter() {
assert_eq!(
unsafe { napi::sys::napi_reference_unref(env, r, &mut 0) },
napi::sys::Status::napi_ok,
"failed to delete napi ref"
);
assert_eq!(
unsafe { napi::sys::napi_delete_reference(env, r) },
napi::sys::Status::napi_ok,
@ -62,7 +67,7 @@ impl TryToTokens for NapiFn {
let _make_ref = |a: ::std::ptr::NonNull<napi::bindgen_prelude::sys::napi_value__>| {
let mut node_ref = ::std::mem::MaybeUninit::uninit();
napi::bindgen_prelude::check_status!(unsafe {
napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 0, node_ref.as_mut_ptr())
napi::bindgen_prelude::sys::napi_create_reference(env, a.as_ptr(), 1, node_ref.as_mut_ptr())
},
"failed to create napi ref"
)?;

View file

@ -82,7 +82,7 @@ impl<const N: usize> CallbackInfo<N> {
sys::napi_wrap(
self.env,
this,
value_ref as *mut c_void,
value_ref.cast(),
Some(raw_finalize_unchecked::<T>),
ptr::null_mut(),
&mut object_ref
@ -94,8 +94,8 @@ impl<const N: usize> CallbackInfo<N> {
Reference::<T>::add_ref(
self.env,
value_ref as *mut c_void,
(value_ref as *mut c_void, object_ref, finalize_callbacks_ptr),
value_ref.cast(),
(value_ref.cast(), object_ref, finalize_callbacks_ptr),
);
Ok((this, value_ref))
}
@ -176,8 +176,8 @@ impl<const N: usize> CallbackInfo<N> {
Reference::<T>::add_ref(
self.env,
value_ref as *mut c_void,
(value_ref as *mut c_void, object_ref, finalize_callbacks_ptr),
value_ref.cast(),
(value_ref.cast(), object_ref, finalize_callbacks_ptr),
);
Ok((instance, value_ref))
}
@ -212,7 +212,7 @@ impl<const N: usize> CallbackInfo<N> {
T::type_name(),
)?;
Ok(wrapped_val as *mut T)
Ok(wrapped_val.cast())
}
}
}