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 0274c678..b96a5c92 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ 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()