napi/bindgen_runtime/
mod.rs

1use std::ffi::c_void;
2use std::rc::Rc;
3
4pub use callback_info::*;
5pub use ctor::ctor;
6pub use env::*;
7pub use iterator::Generator;
8pub use js_values::*;
9pub use module_register::*;
10
11use super::sys;
12use crate::{JsError, Result, Status};
13
14#[cfg(feature = "tokio_rt")]
15pub mod async_iterator;
16mod callback_info;
17mod env;
18mod error;
19pub mod iterator;
20mod js_values;
21mod module_register;
22
23pub trait ObjectFinalize: Sized {
24  #[allow(unused)]
25  fn finalize(self, env: Env) -> Result<()> {
26    Ok(())
27  }
28}
29
30/// # Safety
31///
32/// called when node wrapper objects destroyed
33#[doc(hidden)]
34pub(crate) unsafe extern "C" fn raw_finalize_unchecked<T: ObjectFinalize>(
35  env: sys::napi_env,
36  finalize_data: *mut c_void,
37  _finalize_hint: *mut c_void,
38) {
39  let data: Box<T> = unsafe { Box::from_raw(finalize_data.cast()) };
40  if let Err(err) = data.finalize(Env::from_raw(env)) {
41    let e: JsError = err.into();
42    unsafe { e.throw_into(env) };
43    return;
44  }
45  if let Some((_, ref_val, finalize_callbacks_ptr)) =
46    REFERENCE_MAP.with(|cell| cell.borrow_mut(|reference_map| reference_map.remove(&finalize_data)))
47  {
48    let finalize_callbacks_rc = unsafe { Rc::from_raw(finalize_callbacks_ptr) };
49
50    #[cfg(all(debug_assertions, not(target_family = "wasm")))]
51    {
52      let rc_strong_count = Rc::strong_count(&finalize_callbacks_rc);
53      // If `Rc` strong count is 2, it means the finalize of referenced `Object` is called before the `fn drop` of the `Reference`
54      // It always happened on exiting process
55      // In general, the `fn drop` would happen first
56      if rc_strong_count != 1 && rc_strong_count != 2 {
57        eprintln!("Rc strong count is: {rc_strong_count}, it should be 1 or 2");
58      }
59    }
60    let finalize = unsafe { Box::from_raw(finalize_callbacks_rc.get()) };
61    finalize();
62    let delete_reference_status = unsafe { sys::napi_delete_reference(env, ref_val) };
63    debug_assert!(
64      delete_reference_status == sys::Status::napi_ok,
65      "Delete reference in finalize callback failed {}",
66      Status::from(delete_reference_status)
67    );
68  }
69}
70
71/// # Safety
72///
73/// called when node buffer is ready for gc
74#[doc(hidden)]
75pub unsafe extern "C" fn drop_buffer(
76  _env: sys::napi_env,
77  #[allow(unused)] finalize_data: *mut c_void,
78  finalize_hint: *mut c_void,
79) {
80  #[cfg(all(debug_assertions, not(windows)))]
81  {
82    js_values::BUFFER_DATA.with(|buffer_data| {
83      let mut buffer = buffer_data.lock().expect("Unlock Buffer data failed");
84      buffer.remove(&(finalize_data as *mut u8));
85    });
86  }
87  unsafe {
88    drop(Box::from_raw(finalize_hint as *mut Buffer));
89  }
90}
91
92/// # Safety
93///
94/// called when node buffer slice is ready for gc
95#[doc(hidden)]
96pub unsafe extern "C" fn drop_buffer_slice(
97  _env: sys::napi_env,
98  finalize_data: *mut c_void,
99  finalize_hint: *mut c_void,
100) {
101  let len = *unsafe { Box::from_raw(finalize_hint.cast()) };
102  #[cfg(all(debug_assertions, not(windows)))]
103  {
104    js_values::BUFFER_DATA.with(|buffer_data| {
105      let mut buffer = buffer_data.lock().expect("Unlock Buffer data failed");
106      buffer.remove(&(finalize_data as *mut u8));
107    });
108  }
109  unsafe {
110    drop(Vec::from_raw_parts(finalize_data, len, len));
111  }
112}