fix(napi-derive): JsArrayBuffer generated type

This commit is contained in:
LongYinan 2024-01-26 14:00:52 +08:00
parent b9ba7c9d68
commit f69771e2d4
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
12 changed files with 29 additions and 13 deletions

View file

@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn lint-staged && cargo fmt --all

View file

@ -148,6 +148,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, (&'static str, bool, bool)>> = La
("Map", ("Record<string, any>", false, false)),
("HashMap", ("Record<{}, {}>", false, false)),
("ArrayBuffer", ("ArrayBuffer", false, false)),
("JsArrayBuffer", ("ArrayBuffer", false, false)),
("Int8Array", ("Int8Array", false, false)),
("Uint8Array", ("Uint8Array", false, false)),
("Uint8ClampedArray", ("Uint8ClampedArray", false, false)),

View file

@ -147,7 +147,7 @@ impl JsArrayBuffer {
let mut data = ptr::null_mut();
let mut len: usize = 0;
check_status!(unsafe {
sys::napi_get_arraybuffer_info(self.0.env, self.0.value, &mut data, &mut len as *mut usize)
sys::napi_get_arraybuffer_info(self.0.env, self.0.value, &mut data, &mut len)
})?;
Ok(JsArrayBufferValue {
data,

View file

@ -252,6 +252,8 @@ Generated by [AVA](https://avajs.dev).
export function arrayBufferPassThrough(buf: Uint8Array): Promise<Uint8Array>
export function asyncBufferToArray(buf: ArrayBuffer): Array<number>
export function asyncMultiTwo(arg: number): Promise<number>
export function asyncPlus100(p: Promise<number>): Promise<number>

View file

@ -54,6 +54,7 @@ const {
getPackageJsonName,
getBuffer,
getEmptyBuffer,
asyncBufferToArray,
readFileAsync,
eitherStringOrNumber,
returnEither,
@ -649,6 +650,9 @@ test('buffer', (t) => {
const b = getEmptyBuffer()
t.is(a.toString(), '')
t.is(b.toString(), '')
// @ts-expect-error
t.true(Array.isArray(asyncBufferToArray(Buffer.from([1, 2, 3]).buffer)))
})
test('reset empty buffer', (t) => {

View file

@ -242,6 +242,8 @@ export function apply1(ctx: Animal, callback: (...args: any[]) => any, name: str
export function arrayBufferPassThrough(buf: Uint8Array): Promise<Uint8Array>
export function asyncBufferToArray(buf: ArrayBuffer): Array<number>
export function asyncMultiTwo(arg: number): Promise<number>
export function asyncPlus100(p: Promise<number>): Promise<number>

View file

@ -346,6 +346,7 @@ function __napi_rs_initialize_modules(__napiInstance) {
__napiInstance.exports['__napi_register__array_buffer_pass_through_295']?.()
__napiInstance.exports['__napi_register__AsyncBuffer_impl_296']?.()
__napiInstance.exports['__napi_register__async_reduce_buffer_297']?.()
__napiInstance.exports['__napi_register__async_buffer_to_array_298']?.()
}
export const Animal = __napiModule.exports.Animal
export const AnimalWithDefaultConstructor =
@ -399,6 +400,7 @@ export const apply0 = __napiModule.exports.apply0
export const apply1 = __napiModule.exports.apply1
export const arrayBufferPassThrough =
__napiModule.exports.arrayBufferPassThrough
export const asyncBufferToArray = __napiModule.exports.asyncBufferToArray
export const asyncMultiTwo = __napiModule.exports.asyncMultiTwo
export const asyncPlus100 = __napiModule.exports.asyncPlus100
export const asyncReduceBuffer = __napiModule.exports.asyncReduceBuffer

View file

@ -311,6 +311,7 @@ function __napi_rs_initialize_modules(__napiInstance) {
__napiInstance.exports['__napi_register__array_buffer_pass_through_295']?.()
__napiInstance.exports['__napi_register__AsyncBuffer_impl_296']?.()
__napiInstance.exports['__napi_register__async_reduce_buffer_297']?.()
__napiInstance.exports['__napi_register__async_buffer_to_array_298']?.()
}
module.exports.Animal = __napiModule.exports.Animal
module.exports.AnimalWithDefaultConstructor = __napiModule.exports.AnimalWithDefaultConstructor
@ -358,6 +359,7 @@ module.exports.appendBuffer = __napiModule.exports.appendBuffer
module.exports.apply0 = __napiModule.exports.apply0
module.exports.apply1 = __napiModule.exports.apply1
module.exports.arrayBufferPassThrough = __napiModule.exports.arrayBufferPassThrough
module.exports.asyncBufferToArray = __napiModule.exports.asyncBufferToArray
module.exports.asyncMultiTwo = __napiModule.exports.asyncMultiTwo
module.exports.asyncPlus100 = __napiModule.exports.asyncPlus100
module.exports.asyncReduceBuffer = __napiModule.exports.asyncReduceBuffer

View file

@ -1,4 +1,4 @@
use napi::bindgen_prelude::*;
use napi::{bindgen_prelude::*, JsArrayBuffer};
#[napi]
fn get_buffer() -> Buffer {
@ -71,3 +71,8 @@ impl Task for AsyncBuffer {
fn async_reduce_buffer(buf: Buffer) -> Result<AsyncTask<AsyncBuffer>> {
Ok(AsyncTask::new(AsyncBuffer { buf }))
}
#[napi]
fn async_buffer_to_array(buf: JsArrayBuffer) -> Result<Vec<u8>> {
Ok(buf.into_value()?.as_ref().to_vec())
}

View file

@ -35,7 +35,8 @@
"test:macro": "cargo test -p napi-examples",
"test:memory": "node memory-testing/index.mjs",
"postinstall": "husky install",
"prepublish": "yarn build"
"prepublish": "yarn build",
"prepare": "husky"
},
"bugs": {
"url": "https://github.com/napi-rs/napi-rs/issues"
@ -80,7 +81,7 @@
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.0",
"husky": "^9.0.0",
"husky": "^9.0.6",
"lerna": "^8.0.0",
"lint-staged": "^15.2.0",
"npm-run-all": "^4.1.5",

View file

@ -6668,12 +6668,12 @@ __metadata:
languageName: node
linkType: hard
"husky@npm:^9.0.0":
version: 9.0.1
resolution: "husky@npm:9.0.1"
"husky@npm:^9.0.6":
version: 9.0.6
resolution: "husky@npm:9.0.6"
bin:
husky: bin.js
checksum: d7f3d158fffc8cc91211172637f7069b2173dbf5e732f46596a2a512e5f5f17ddebd895202cc67b0f159b747b698e2cc5601282b198d9cb5d97a5671fcb2afb7
checksum: 1ef034899dcdc35e5bf5436e1e02f8a7177dbb8a10dd91b77456d7fe110c204d8d4a7d53180e0651b87e35f39d7e0e95a53b4b7c334f68aefbc0d93bce4b54f8
languageName: node
linkType: hard
@ -8705,7 +8705,7 @@ __metadata:
eslint: "npm:^8.55.0"
eslint-config-prettier: "npm:^9.1.0"
eslint-plugin-import: "npm:^2.29.0"
husky: "npm:^9.0.0"
husky: "npm:^9.0.6"
lerna: "npm:^8.0.0"
lint-staged: "npm:^15.2.0"
npm-run-all: "npm:^4.1.5"