refactor(napi): create_buffer_with_manually_drop_data => create_buffer_with_borrowed_data

This commit is contained in:
LongYinan 2020-12-23 15:34:10 +08:00
parent 62ee58fab7
commit 8fcf42eeb6
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881

View file

@ -315,8 +315,18 @@ impl Env {
#[inline] #[inline]
/// # Safety /// # Safety
/// Mostly the same with `create_buffer_with_data`, but you must ensure data will be dropped **after** the `Buffer` has been GC. /// Mostly the same with `create_buffer_with_data`
pub unsafe fn create_buffer_with_manually_drop_data(&self, data: &[u8]) -> Result<JsBufferValue> { ///
/// Provided `finalize_callback` will be called when `Buffer` got dropped.
pub unsafe fn create_buffer_with_borrowed_data<Hint, Finalize>(
&self,
data: &[u8],
hint: Hint,
finalize_callback: Option<Finalize>,
) -> Result<JsBufferValue>
where
Finalize: FnOnce(Env, Hint),
{
let length = data.len(); let length = data.len();
let mut raw_value = ptr::null_mut(); let mut raw_value = ptr::null_mut();
let data_ptr = data.as_ptr(); let data_ptr = data.as_ptr();
@ -324,8 +334,15 @@ impl Env {
self.0, self.0,
length, length,
data_ptr as *mut c_void, data_ptr as *mut c_void,
None, Some(
ptr::null_mut(), raw_finalize_with_custom_callback::<Hint, Finalize>
as unsafe extern "C" fn(
env: sys::napi_env,
finalize_data: *mut c_void,
finalize_hint: *mut c_void,
)
),
Box::into_raw(Box::new((hint, finalize_callback))) as *mut c_void,
&mut raw_value, &mut raw_value,
))?; ))?;
Ok(JsBufferValue::new( Ok(JsBufferValue::new(
@ -1102,3 +1119,16 @@ unsafe extern "C" fn cleanup_env<T: 'static>(hook_data: *mut c_void) {
let cleanup_env_hook = Box::from_raw(hook_data as *mut CleanupEnvHookData<T>); let cleanup_env_hook = Box::from_raw(hook_data as *mut CleanupEnvHookData<T>);
(cleanup_env_hook.hook)(cleanup_env_hook.data); (cleanup_env_hook.hook)(cleanup_env_hook.data);
} }
unsafe extern "C" fn raw_finalize_with_custom_callback<Hint, Finalize>(
env: sys::napi_env,
_finalize_data: *mut c_void,
finalize_hint: *mut c_void,
) where
Finalize: FnOnce(Env, Hint),
{
let (hint, maybe_callback) = *Box::from_raw(finalize_hint as *mut (Hint, Option<Finalize>));
if let Some(callback) = maybe_callback {
callback(Env::from_raw(env), hint);
};
}