From 2763a8e7b293dc95f58b17c93e9e09f2456a0296 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 7 Feb 2022 12:24:09 +0800 Subject: [PATCH] fix(napi): deref from Uint8ClampedArray --- crates/napi/src/js_values/arraybuffer.rs | 3 ++- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 2606 -> 2634 bytes examples/napi/__test__/values.spec.ts | 8 ++++++++ examples/napi/index.d.ts | 1 + examples/napi/src/typed_array.rs | 5 +++++ 6 files changed, 17 insertions(+), 1 deletion(-) 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 25a061f2ae08ef337057e6905d70fef41f7825bd..c8cab00c5dfe14031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^*0vrG{+>cx zx%L?NB-i_q;z)>geOq-HVN{r#tNaG5x&D|4HJEs?*#+1v*e%4bNL9y!-7eTU5n%{X z+E8>CUEm)6nO7@d)}jGh@q514#$07;3Fix_-88?C*anwEEK~+V{Q! zy#w&=@K^-wE0g#m2hM`+3`Y0Xa0= zT6mdR9xJeJ1%_6uM5m$kx8O702ce)A^ZXWPiM%8qb+dsrUL+s08zNl^KAPQ+kQqOx z94~div!JJBAq=ES5D$N{dg_JTZzCCj1M@BTD#3<_l{=Yl)^LBtYk7;cS&s1zc{#*+ z*%#w643}mfnPUxSWATZT|NZwrzvz7w?{*~Un3vevIvMF9TuRD-w0mNx)oIx_HQc^q z3DZ{|`qJJX(FDwb_x5FEm+gx*+y-ZRoH|4pY=;c-;nV@4mwmV|9_}UG3YpZ z=3>wCoVwFB;hmjb^I^A3fEC~A@#qunr-=$#Q}^7}xt~n%0xd#tqBQ(-i0^;@UJBQU z-Ja2Sgi2b3jk_IFdEFJE34?+GPR`|QR6gV*f7y`|zstQL=3&NY=H-3O!2o542UdTS zk(%3m+dOlYH5{85>d&yJRE_Gw%HZb%obCV9FH2cz3#O+hrUPH|mj*kP+a%40q@2Xu zFFUiG=hH-4bFynsqC3@S{->iR_WEPd(~L43_IZLn$cku<-&;}LtJFoeLc)~LI!FcO ze{EKP*=zk8Zz?d5oQ!eKy}KK2Xvor?Oy~XK+urehuWzM-nj6oJpSCuet~*VX1JK&~ zw^Jk?)-{v>Xg;A2D5p`>reStI2?v{2Yd*{$FW6tXt3S7 hZ97|`b-b%saFJPW(+>-8k{_1-5BM<`1ry650089m>zn`p 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 +}