mirror of
https://github.com/napi-rs/napi-rs.git
synced 2025-12-08 19:56:07 +00:00
fix(napi): avoid allocate empty Box if closures is empty (#2712)
Ref: https://github.com/napi-rs/napi-rs/pull/2711
This commit is contained in:
parent
97ed022e46
commit
fcfac5083a
@ -324,21 +324,23 @@ pub trait JsObjectValue<'env>: JsValue<'env> {
|
||||
.map(|p| p.data)
|
||||
.filter(|data| !data.is_null())
|
||||
.collect::<Vec<*mut std::ffi::c_void>>();
|
||||
let len = Box::into_raw(Box::new(closures.len()));
|
||||
check_status!(
|
||||
unsafe {
|
||||
sys::napi_add_finalizer(
|
||||
env,
|
||||
self.value().value,
|
||||
closures.as_mut_ptr().cast(),
|
||||
Some(finalize_closures),
|
||||
len.cast(),
|
||||
ptr::null_mut(),
|
||||
)
|
||||
},
|
||||
"Failed to add finalizer"
|
||||
)?;
|
||||
std::mem::forget(closures);
|
||||
if !closures.is_empty() {
|
||||
let len = Box::into_raw(Box::new(closures.len()));
|
||||
check_status!(
|
||||
unsafe {
|
||||
sys::napi_add_finalizer(
|
||||
env,
|
||||
self.value().value,
|
||||
closures.as_mut_ptr().cast(),
|
||||
Some(finalize_closures),
|
||||
len.cast(),
|
||||
ptr::null_mut(),
|
||||
)
|
||||
},
|
||||
"Failed to add finalizer"
|
||||
)?;
|
||||
std::mem::forget(closures);
|
||||
}
|
||||
}
|
||||
}
|
||||
check_status!(unsafe {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user