2022-03-21 17:36:06 +09:00
|
|
|
use std::ffi::c_void;
|
|
|
|
use std::mem;
|
2022-04-02 16:19:53 +09:00
|
|
|
use std::rc::Rc;
|
2021-09-23 02:29:09 +09:00
|
|
|
|
|
|
|
pub use callback_info::*;
|
|
|
|
pub use ctor::ctor;
|
|
|
|
pub use env::*;
|
|
|
|
pub use js_values::*;
|
|
|
|
pub use module_register::*;
|
|
|
|
|
|
|
|
use super::sys;
|
2022-04-02 16:19:53 +09:00
|
|
|
use crate::Status;
|
2022-03-21 17:36:06 +09:00
|
|
|
|
|
|
|
mod callback_info;
|
|
|
|
mod env;
|
|
|
|
mod error;
|
|
|
|
mod js_values;
|
|
|
|
mod module_register;
|
2021-09-23 02:29:09 +09:00
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// called when node wrapper objects destroyed
|
|
|
|
pub unsafe extern "C" fn raw_finalize_unchecked<T>(
|
2022-04-02 16:19:53 +09:00
|
|
|
env: sys::napi_env,
|
2021-09-23 02:29:09 +09:00
|
|
|
finalize_data: *mut c_void,
|
2022-03-21 17:36:06 +09:00
|
|
|
_finalize_hint: *mut c_void,
|
2021-09-23 02:29:09 +09:00
|
|
|
) {
|
2022-03-21 17:36:06 +09:00
|
|
|
unsafe { Box::from_raw(finalize_data as *mut T) };
|
2022-04-02 16:19:53 +09:00
|
|
|
if let Some((_, ref_val, finalize_callbacks_ptr)) =
|
|
|
|
REFERENCE_MAP.with(|reference_map| reference_map.borrow_mut().remove(&finalize_data))
|
|
|
|
{
|
|
|
|
let finalize_callbacks_rc = unsafe { Rc::from_raw(finalize_callbacks_ptr) };
|
2022-04-07 00:00:21 +09:00
|
|
|
debug_assert!(Rc::strong_count(&finalize_callbacks_rc) == 1);
|
|
|
|
debug_assert!(Rc::weak_count(&finalize_callbacks_rc) == 0);
|
2022-04-02 16:19:53 +09:00
|
|
|
let finalize = unsafe { Box::from_raw(finalize_callbacks_rc.get()) };
|
|
|
|
finalize();
|
|
|
|
let delete_reference_status = unsafe { sys::napi_delete_reference(env, ref_val) };
|
|
|
|
debug_assert!(
|
|
|
|
delete_reference_status == sys::Status::napi_ok,
|
|
|
|
"Delete reference in finalize callback failed {}",
|
|
|
|
Status::from(delete_reference_status)
|
|
|
|
);
|
|
|
|
}
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// called when node buffer is ready for gc
|
|
|
|
pub unsafe extern "C" fn drop_buffer(
|
|
|
|
_env: sys::napi_env,
|
|
|
|
finalize_data: *mut c_void,
|
|
|
|
finalize_hint: *mut c_void,
|
|
|
|
) {
|
|
|
|
let length_ptr = finalize_hint as *mut (usize, usize);
|
2022-01-13 12:15:02 +09:00
|
|
|
unsafe {
|
|
|
|
let (length, cap) = *Box::from_raw(length_ptr);
|
|
|
|
mem::drop(Vec::from_raw_parts(finalize_data as *mut u8, length, cap));
|
|
|
|
}
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|