From 530359421cea307d85d429383e9d57cd275b47d6 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 24 Dec 2020 11:53:23 +0800 Subject: [PATCH] refactor(napi): create_buffer_with_borrowed_data now accepted raw ptr --- napi/src/env.rs | 18 ++++++----- test_module/__test__/buffer.spec.ts | 14 +++++++++ test_module/src/buffer.rs | 47 ++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/napi/src/env.rs b/napi/src/env.rs index 2a272b57..2588a838 100644 --- a/napi/src/env.rs +++ b/napi/src/env.rs @@ -320,20 +320,19 @@ impl Env { /// Provided `finalize_callback` will be called when `Buffer` got dropped. pub unsafe fn create_buffer_with_borrowed_data( &self, - data: &[u8], + data: *const u8, + length: usize, hint: Hint, finalize_callback: Option, ) -> Result where - Finalize: FnOnce(Env, Hint), + Finalize: FnOnce(Hint, Env), { - let length = data.len(); let mut raw_value = ptr::null_mut(); - let data_ptr = data.as_ptr(); check_status!(sys::napi_create_external_buffer( self.0, length, - data_ptr as *mut c_void, + data as *mut c_void, Some( raw_finalize_with_custom_callback:: as unsafe extern "C" fn( @@ -351,7 +350,7 @@ impl Env { value: raw_value, value_type: ValueType::Object, }), - mem::ManuallyDrop::new(Vec::from_raw_parts(data_ptr as *mut u8, length, length)), + mem::ManuallyDrop::new(Vec::from_raw_parts(data as *mut u8, length, length)), )) } @@ -1065,6 +1064,9 @@ impl Env { } } +/// This function could be used for `create_buffer_with_borrowed_data` and want do noting when Buffer finalized. +pub fn noop_finalize(_hint: Hint, _env: Env) {} + unsafe extern "C" fn drop_buffer( _env: sys::napi_env, finalize_data: *mut c_void, @@ -1125,10 +1127,10 @@ unsafe extern "C" fn raw_finalize_with_custom_callback( _finalize_data: *mut c_void, finalize_hint: *mut c_void, ) where - Finalize: FnOnce(Env, Hint), + Finalize: FnOnce(Hint, Env), { let (hint, maybe_callback) = *Box::from_raw(finalize_hint as *mut (Hint, Option)); if let Some(callback) = maybe_callback { - callback(Env::from_raw(env), hint); + callback(hint, Env::from_raw(env)); }; } diff --git a/test_module/__test__/buffer.spec.ts b/test_module/__test__/buffer.spec.ts index 21ebe469..5f5d4865 100644 --- a/test_module/__test__/buffer.spec.ts +++ b/test_module/__test__/buffer.spec.ts @@ -18,3 +18,17 @@ test('should copy', (t) => { t.deepEqual(copyBuffer, fixture) t.not(fixture, copyBuffer) }) + +test('should create borrowed buffer with noop finalize', (t) => { + t.deepEqual( + bindings.createBorrowedBufferWithNoopFinalize(), + Buffer.from([1, 2, 3]), + ) +}) + +test('should create borrowed buffer with finalize', (t) => { + t.deepEqual( + bindings.createBorrowedBufferWithFinalize(), + Buffer.from([1, 2, 3]), + ) +}) diff --git a/test_module/src/buffer.rs b/test_module/src/buffer.rs index d55214e6..0b7dd157 100644 --- a/test_module/src/buffer.rs +++ b/test_module/src/buffer.rs @@ -1,6 +1,10 @@ +use std::mem::ManuallyDrop; use std::str; -use napi::{CallContext, Error, JsBuffer, JsNumber, JsObject, JsString, Result, Status}; +use napi::{ + noop_finalize, CallContext, ContextlessResult, Env, Error, JsBuffer, JsNumber, JsObject, + JsString, Result, Status, +}; #[js_function(1)] pub fn get_buffer_length(ctx: CallContext) -> Result { @@ -22,9 +26,50 @@ pub fn copy_buffer(ctx: CallContext) -> Result { ctx.env.create_buffer_copy(buffer).map(|b| b.into_raw()) } +#[contextless_function] +pub fn create_borrowed_buffer_with_noop_finalize(env: Env) -> ContextlessResult { + let data = vec![1, 2, 3]; + let data_ptr = data.as_ptr(); + let length = data.len(); + let manually_drop = ManuallyDrop::new(data); + + unsafe { + env.create_buffer_with_borrowed_data(data_ptr, length, manually_drop, Some(noop_finalize)) + } + .map(|b| Some(b.into_raw())) +} + +#[contextless_function] +pub fn create_borrowed_buffer_with_finalize(env: Env) -> ContextlessResult { + let data = vec![1, 2, 3]; + let data_ptr = data.as_ptr(); + let length = data.len(); + let manually_drop = ManuallyDrop::new(data); + + unsafe { + env.create_buffer_with_borrowed_data( + data_ptr, + length, + manually_drop, + Some(|mut hint: ManuallyDrop>, _| { + ManuallyDrop::drop(&mut hint); + }), + ) + } + .map(|b| Some(b.into_raw())) +} + pub fn register_js(exports: &mut JsObject) -> Result<()> { exports.create_named_method("getBufferLength", get_buffer_length)?; exports.create_named_method("bufferToString", buffer_to_string)?; exports.create_named_method("copyBuffer", copy_buffer)?; + exports.create_named_method( + "createBorrowedBufferWithNoopFinalize", + create_borrowed_buffer_with_noop_finalize, + )?; + exports.create_named_method( + "createBorrowedBufferWithFinalize", + create_borrowed_buffer_with_finalize, + )?; Ok(()) }