diff --git a/crates/napi/src/js_values/arraybuffer.rs b/crates/napi/src/js_values/arraybuffer.rs index da624536..d61b4963 100644 --- a/crates/napi/src/js_values/arraybuffer.rs +++ b/crates/napi/src/js_values/arraybuffer.rs @@ -263,7 +263,8 @@ impl JsTypedArray { impl JsTypedArrayValue { #[inline] fn is_valid_as_ref(&self, dest_type: TypedArrayType) { - if self.typedarray_type == TypedArrayType::Uint8Clamped && dest_type != TypedArrayType::Uint8 { + // deref `Uint8ClampedArray` as `&[u8]` is valid + if self.typedarray_type == TypedArrayType::Uint8Clamped && dest_type == TypedArrayType::Uint8 { return; } if self.typedarray_type != dest_type { diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 1d598448..58ce38af 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -145,6 +145,7 @@ Generated by [AVA](https://avajs.dev). export function convertU32Array(input: Uint32Array): Array␊ export function createExternalTypedArray(): Uint32Array␊ export function mutateTypedArray(input: Float32Array): void␊ + export function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number␊ /**␊ * \`constructor\` option for \`struct\` requires all fields to be public,␊ * otherwise tag impl fn as constructor␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 25a061f2..c8cab00c 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 bdc5d8b4..a5c30f41 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -83,6 +83,7 @@ import { testSerdeRoundtrip, createObjWithProperty, dateToNumber, + derefUint8Array, } from '../' test('export const', (t) => { @@ -358,6 +359,13 @@ test('mutate TypedArray', (t) => { t.deepEqual(input, new Float32Array([2.0, 4.0, 6.0, 8.0, 10.0])) }) +test('deref uint8 array', (t) => { + t.is( + derefUint8Array(new Uint8Array([1, 2]), new Uint8ClampedArray([3, 4])), + 4, + ) +}) + test('async', async (t) => { const bufPromise = readFileAsync(join(__dirname, '../package.json')) await t.notThrowsAsync(bufPromise) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 84c3e695..ef8eb4e3 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -135,6 +135,7 @@ export function appendBuffer(buf: Buffer): Buffer export function convertU32Array(input: Uint32Array): Array export function createExternalTypedArray(): Uint32Array export function mutateTypedArray(input: Float32Array): void +export function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number /** * `constructor` option for `struct` requires all fields to be public, * otherwise tag impl fn as constructor diff --git a/examples/napi/src/typed_array.rs b/examples/napi/src/typed_array.rs index 68a92500..c4a769db 100644 --- a/examples/napi/src/typed_array.rs +++ b/examples/napi/src/typed_array.rs @@ -28,3 +28,8 @@ fn mutate_typed_array(mut input: Float32Array) { *item *= 2.0; } } + +#[napi] +fn deref_uint8_array(a: Uint8Array, b: Uint8ClampedArray) -> u32 { + (a.len() + b.len()) as u32 +}