fix(napi): remove previous reference if value_ref existed

Usually happens while using ZST
This commit is contained in:
LongYinan 2022-08-25 21:51:06 +08:00
parent 767c040d94
commit 525d881590
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
3 changed files with 8 additions and 3 deletions

View file

@ -375,7 +375,7 @@ impl NapiStruct {
"Failed to wrap native object of class `{}`",
#js_name_raw
)?;
napi::bindgen_prelude::Reference::<#name>::add_ref(wrapped_value, (wrapped_value, object_ref, finalize_callbacks_ptr));
napi::bindgen_prelude::Reference::<#name>::add_ref(env, wrapped_value, (wrapped_value, object_ref, finalize_callbacks_ptr));
Ok(result)
}
}

View file

@ -94,6 +94,7 @@ 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),
);
@ -176,6 +177,7 @@ 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),
);

View file

@ -59,9 +59,12 @@ impl<T> Drop for Reference<T> {
impl<T: 'static> Reference<T> {
#[doc(hidden)]
pub fn add_ref(t: *mut c_void, value: RefInformation) {
pub fn add_ref(env: crate::sys::napi_env, t: *mut c_void, value: RefInformation) {
REFERENCE_MAP.borrow_mut(|map| {
map.insert(t, value);
if let Some((_, previous_ref, previous_rc)) = map.insert(t, value) {
unsafe { Rc::from_raw(previous_rc) };
unsafe { crate::sys::napi_delete_reference(env, previous_ref) };
}
});
}