feat(napi): clone reference for TypedArray/Buffer

This commit is contained in:
LongYinan 2022-07-06 19:08:34 +08:00
parent 1ac7fcf2ce
commit 1a7cff167e
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
9 changed files with 267 additions and 32 deletions

View file

@ -187,6 +187,7 @@ Generated by [AVA](https://avajs.dev).
export function mutateTypedArray(input: Float32Array): void␊
export function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number␊
export function bufferPassThrough(buf: Buffer): Promise<Buffer>
export function arrayBufferPassThrough(buf: Uint8Array): Promise<Uint8Array>
export function asyncReduceBuffer(buf: Buffer): Promise<number>
/**␊
* \`constructor\` option for \`struct\` requires all fields to be public,␊
@ -240,8 +241,10 @@ Generated by [AVA](https://avajs.dev).
export class Blake2BKey { }␊
export class Context {␊
maybeNeed?: boolean␊
buffer: Uint8Array␊
constructor()␊
static withData(data: string): Context␊
static withBuffer(buf: Uint8Array): Context␊
method(): string␊
}␊
export class AnimalWithDefaultConstructor {␊

View file

@ -90,6 +90,7 @@ import {
derefUint8Array,
chronoDateAdd1Minute,
bufferPassThrough,
arrayBufferPassThrough,
JsRepo,
CssStyleSheet,
asyncReduceBuffer,
@ -208,6 +209,14 @@ test('class constructor return Result', (t) => {
t.is(c.method(), 'not empty')
})
test('class default field is TypedArray', (t) => {
const c = new Context()
t.deepEqual(c.buffer, new Uint8Array([0, 1, 2, 3]))
const fixture = new Uint8Array([0, 1, 2, 3, 4, 5, 6])
const c2 = Context.withBuffer(fixture)
t.is(c2.buffer, fixture)
})
test('class Factory return Result', (t) => {
const c = Context.withData('not empty')
t.is(c.method(), 'not empty')
@ -479,6 +488,12 @@ test('buffer passthrough', async (t) => {
t.deepEqual(ret, fixture)
})
test('arraybuffer passthrough', async (t) => {
const fixture = new Uint8Array([1, 2, 3, 4, 5])
const ret = await arrayBufferPassThrough(fixture)
t.deepEqual(ret, fixture)
})
test('async reduce buffer', async (t) => {
const input = [1, 2, 3, 4, 5, 6]
const fixture = Buffer.from(input)

View file

@ -177,6 +177,7 @@ export function createExternalTypedArray(): Uint32Array
export function mutateTypedArray(input: Float32Array): void
export function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number
export function bufferPassThrough(buf: Buffer): Promise<Buffer>
export function arrayBufferPassThrough(buf: Uint8Array): Promise<Uint8Array>
export function asyncReduceBuffer(buf: Buffer): Promise<number>
/**
* `constructor` option for `struct` requires all fields to be public,
@ -230,8 +231,10 @@ export type Blake2bKey = Blake2BKey
export class Blake2BKey { }
export class Context {
maybeNeed?: boolean
buffer: Uint8Array
constructor()
static withData(data: string): Context
static withBuffer(buf: Uint8Array): Context
method(): string
}
export class AnimalWithDefaultConstructor {

View file

@ -1,5 +1,5 @@
use napi::{
bindgen_prelude::{Buffer, ClassInstance, This},
bindgen_prelude::{Buffer, ClassInstance, This, Uint8Array},
Env, Result,
};
@ -159,6 +159,7 @@ impl Blake2bKey {
pub struct Context {
data: String,
pub maybe_need: Option<bool>,
pub buffer: Uint8Array,
}
// Test for return `napi::Result` and `Result`
@ -169,6 +170,7 @@ impl Context {
Ok(Self {
data: "not empty".into(),
maybe_need: None,
buffer: Uint8Array::new(vec![0, 1, 2, 3]),
})
}
@ -177,9 +179,19 @@ impl Context {
Ok(Self {
data,
maybe_need: Some(true),
buffer: Uint8Array::new(vec![0, 1, 2, 3]),
})
}
#[napi(factory)]
pub fn with_buffer(buf: Uint8Array) -> Self {
Self {
data: "not empty".into(),
maybe_need: None,
buffer: buf,
}
}
#[napi]
pub fn method(&self) -> String {
self.data.clone()

View file

@ -44,6 +44,11 @@ async fn buffer_pass_through(buf: Buffer) -> Result<Buffer> {
Ok(buf)
}
#[napi]
async fn array_buffer_pass_through(buf: Uint8Array) -> Result<Uint8Array> {
Ok(buf)
}
struct AsyncBuffer {
buf: Buffer,
}