Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(napi): remove previous reference if value_ref existed #1290

Merged
merged 1 commit into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/backend/src/codegen/struct.rs
Expand Up @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/napi/src/bindgen_runtime/callback_info.rs
Expand Up @@ -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),
);
Expand Down Expand Up @@ -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),
);
Expand Down
7 changes: 5 additions & 2 deletions crates/napi/src/bindgen_runtime/js_values/value_ref.rs
Expand Up @@ -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) };
}
});
}

Expand Down