From 0bdb72209714f72e5061adfe6689f3088604bb5e Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 23 Apr 2022 10:43:36 +0200 Subject: [PATCH] test(napi): add test for multiple empty buffers --- .../napi-compat-mode/__test__/buffer.spec.ts | 10 ++++++ examples/napi-compat-mode/src/buffer.rs | 34 ++++++++++++++++++ examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2919 -> 2926 bytes examples/napi/__test__/values.spec.ts | 6 ++++ examples/napi/index.d.ts | 1 + examples/napi/src/typed_array.rs | 5 +++ 7 files changed, 57 insertions(+) diff --git a/examples/napi-compat-mode/__test__/buffer.spec.ts b/examples/napi-compat-mode/__test__/buffer.spec.ts index b41faee6..506ba4c8 100644 --- a/examples/napi-compat-mode/__test__/buffer.spec.ts +++ b/examples/napi-compat-mode/__test__/buffer.spec.ts @@ -33,6 +33,16 @@ test('should create borrowed buffer with finalize', (t) => { ) }) +test('should create empty borrowed buffer with finalize', (t) => { + t.is(bindings.createEmptyBorrowedBufferWithFinalize().toString(), '') + t.is(bindings.createEmptyBorrowedBufferWithFinalize().toString(), '') +}) + +test('should create empty buffer', (t) => { + t.is(bindings.createEmptyBuffer().toString(), '') + t.is(bindings.createEmptyBuffer().toString(), '') +}) + test('should be able to mutate buffer', (t) => { const fixture = Buffer.from([0, 1]) bindings.mutateBuffer(fixture) diff --git a/examples/napi-compat-mode/src/buffer.rs b/examples/napi-compat-mode/src/buffer.rs index 24be7ded..9c5fa9d5 100644 --- a/examples/napi-compat-mode/src/buffer.rs +++ b/examples/napi-compat-mode/src/buffer.rs @@ -57,6 +57,35 @@ pub fn create_borrowed_buffer_with_finalize(env: Env) -> ContextlessResult ContextlessResult { + let data = vec![]; + 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, + |mut hint: ManuallyDrop>, _| { + ManuallyDrop::drop(&mut hint); + }, + ) + } + .map(|b| Some(b.into_raw())) +} + +#[contextless_function] +pub fn create_empty_buffer(env: Env) -> ContextlessResult { + let data = vec![]; + + env + .create_buffer_with_data(data) + .map(|b| Some(b.into_raw())) +} + #[js_function(1)] fn mutate_buffer(ctx: CallContext) -> Result { let buffer = &mut ctx.get::(0)?.into_value()?; @@ -76,6 +105,11 @@ pub fn register_js(exports: &mut JsObject) -> Result<()> { "createBorrowedBufferWithFinalize", create_borrowed_buffer_with_finalize, )?; + exports.create_named_method( + "createEmptyBorrowedBufferWithFinalize", + create_empty_borrowed_buffer_with_finalize, + )?; + exports.create_named_method("createEmptyBuffer", create_empty_buffer)?; exports.create_named_method("mutateBuffer", mutate_buffer)?; Ok(()) } diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index beea5282..5f236e26 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -168,6 +168,7 @@ Generated by [AVA](https://avajs.dev). export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊ export function getBuffer(): Buffer␊ export function appendBuffer(buf: Buffer): Buffer␊ + export function getEmptyBuffer(): Buffer␊ export function convertU32Array(input: Uint32Array): Array␊ export function createExternalTypedArray(): Uint32Array␊ export function mutateTypedArray(input: Float32Array): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 0274c678eef07149edbcd10a2f0d7335694e72a1..b96a5c9202dd00c2daebc908aa7e1105a1363e8f 100644 GIT binary patch literal 2926 zcmV-!3z76eRzVk!8Sz^DpeAH1VJh!He}h(*@7rLT7flD*D}%&hH-m z;Sv7(&F>%m@wb0{^5Q8`GUSo!1YFW#$jH+dE3nD*<}+s#cWJ7`Gka!0hBMOd9`8Rp zIn@B{CKibzen_PxyElp@5e<)qpMkG8gH3X~LP)}BAfj+ao;>O3jk&Ef;$Kom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0{aoZaFT;Hq zV{?CVGuhCfr9o8p!~VPe;ckE6#G0CM?Z%Ivj5Z R;TZgG<$v@o6%t%0001?hs`mf@ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index 3cda09af..98c5aa51 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -33,6 +33,7 @@ import { readPackageJson, getPackageJsonName, getBuffer, + getEmptyBuffer, readFileAsync, eitherStringOrNumber, returnEither, @@ -369,6 +370,11 @@ test('buffer', (t) => { t.is(buf.toString('utf-8'), 'Hello world') buf = appendBuffer(buf) t.is(buf.toString('utf-8'), 'Hello world!') + + const a = getEmptyBuffer() + const b = getEmptyBuffer() + t.is(a.toString(), '') + t.is(b.toString(), '') }) test('convert typedarray to vec', (t) => { diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index ceab0ab7..50da5905 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -158,6 +158,7 @@ export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void export function getBuffer(): Buffer export function appendBuffer(buf: Buffer): Buffer +export function getEmptyBuffer(): Buffer export function convertU32Array(input: Uint32Array): Array export function createExternalTypedArray(): Uint32Array export function mutateTypedArray(input: Float32Array): void diff --git a/examples/napi/src/typed_array.rs b/examples/napi/src/typed_array.rs index e2180925..8f24e494 100644 --- a/examples/napi/src/typed_array.rs +++ b/examples/napi/src/typed_array.rs @@ -12,6 +12,11 @@ fn append_buffer(buf: Buffer) -> Buffer { buf.into() } +#[napi] +fn get_empty_buffer() -> Buffer { + vec![].into() +} + #[napi] fn convert_u32_array(input: Uint32Array) -> Vec { input.to_vec()