fix(napi): use create_buffer/arrary_buffer if provided data is empty

This commit is contained in:
LongYinan 2022-04-26 15:14:37 +08:00
parent a41cc07f21
commit 5aa61c2142
6 changed files with 93 additions and 76 deletions
examples
napi-compat-mode/__test__
napi/__test__

View file

@ -34,8 +34,12 @@ 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(), '')
t.throws(() => bindings.createEmptyBorrowedBufferWithFinalize().toString(), {
message: 'Borrowed data should not be null',
})
t.throws(() => bindings.createEmptyBorrowedBufferWithFinalize().toString(), {
message: 'Borrowed data should not be null',
})
})
test('should create empty buffer', (t) => {

View file

@ -377,6 +377,16 @@ test('buffer', (t) => {
t.is(b.toString(), '')
})
test('reset empty buffer', (t) => {
const empty = getEmptyBuffer()
const shared = new ArrayBuffer(0)
const buffer = Buffer.from(shared)
t.notThrows(() => {
buffer.set(empty)
})
})
test('convert typedarray to vec', (t) => {
const input = new Uint32Array([1, 2, 3, 4, 5])
t.deepEqual(convertU32Array(input), Array.from(input))