From d184d503d5b7a870a01a7fd2f3c79e21e9e945de Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 26 Apr 2023 15:18:00 +0800 Subject: [PATCH] fix(napi-derive): increase initial ref count in async fn (#1577) --- crates/backend/src/codegen/fn.rs | 7 ++++++- crates/napi/src/bindgen_runtime/callback_info.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index caf0acf2..2c6eb00d 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -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| { 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" )?; diff --git a/crates/napi/src/bindgen_runtime/callback_info.rs b/crates/napi/src/bindgen_runtime/callback_info.rs index fd3a34ad..b41ce559 100644 --- a/crates/napi/src/bindgen_runtime/callback_info.rs +++ b/crates/napi/src/bindgen_runtime/callback_info.rs @@ -82,7 +82,7 @@ impl CallbackInfo { sys::napi_wrap( self.env, this, - value_ref as *mut c_void, + value_ref.cast(), Some(raw_finalize_unchecked::), ptr::null_mut(), &mut object_ref @@ -94,8 +94,8 @@ impl CallbackInfo { Reference::::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 CallbackInfo { Reference::::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 CallbackInfo { T::type_name(), )?; - Ok(wrapped_val as *mut T) + Ok(wrapped_val.cast()) } } }