diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index a2f3d42b..66973867 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -5,6 +5,7 @@ use syn::spanned::Spanned; use crate::{ codegen::{get_intermediate_ident, js_mod_to_token_stream}, BindgenResult, CallbackArg, Diagnostic, FnKind, FnSelf, NapiFn, NapiFnArgKind, TryToTokens, + TYPEDARRAY_SLICE_TYPES, }; impl TryToTokens for NapiFn { @@ -428,10 +429,7 @@ impl NapiFn { if let syn::Type::Slice(slice) = &**elem { if let syn::Type::Path(ele) = &*slice.elem { if let Some(syn::PathSegment { ident, .. }) = ele.path.segments.first() { - static TYPEDARRAY_SLICE_TYPES: &[&str] = &[ - "u8", "i8", "u16", "i16", "u32", "i32", "f32", "f64", "u64", "i64", - ]; - if TYPEDARRAY_SLICE_TYPES.contains(&&*ident.to_string()) { + if TYPEDARRAY_SLICE_TYPES.contains_key(&&*ident.to_string()) { let q = quote! { let #arg_name = { #type_check diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 16881cb6..aff83ba7 100644 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -1,6 +1,9 @@ #[macro_use] extern crate quote; +use std::collections::HashMap; + +use once_cell::sync::Lazy; use proc_macro2::TokenStream; #[macro_use] @@ -105,3 +108,18 @@ pub(crate) static PRIMITIVE_TYPES: &[(&str, (&str, bool, bool))] = &[ ("JsSymbol", ("symbol", false, false)), ("JsFunction", ("(...args: any[]) => any", true, false)), ]; + +pub(crate) static TYPEDARRAY_SLICE_TYPES: Lazy> = Lazy::new(|| { + HashMap::from([ + ("u8", "Uint8Array"), + ("i8", "Int8Array"), + ("u16", "Uint16Array"), + ("i16", "Int16Array"), + ("u32", "Uint32Array"), + ("i32", "Int32Array"), + ("f32", "Float32Array"), + ("f64", "Float64Array"), + ("u64", "BigUint64Array"), + ("i64", "BigInt64Array"), + ]) +}); diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index a05ba110..4e853151 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -6,7 +6,7 @@ pub(crate) mod r#struct; use std::{cell::RefCell, collections::HashMap, env}; use once_cell::sync::Lazy; -use syn::Type; +use syn::{PathSegment, Type, TypePath, TypeSlice}; pub static NAPI_RS_CLI_VERSION: Lazy = Lazy::new(|| { let version = env::var("CARGO_CFG_NAPI_RS_CLI_VERSION").unwrap_or_else(|_| "0.0.0".to_string()); @@ -147,11 +147,14 @@ static KNOWN_TYPES: Lazy> = La ("Value", ("any", false, false)), ("Map", ("Record", false, false)), ("HashMap", ("Record<{}, {}>", false, false)), + ("BTreeMap", ("Record<{}, {}>", false, false)), + ("IndexMap", ("Record<{}, {}>", false, false)), ("ArrayBuffer", ("ArrayBuffer", false, false)), ("JsArrayBuffer", ("ArrayBuffer", false, false)), ("Int8Array", ("Int8Array", false, false)), ("Uint8Array", ("Uint8Array", false, false)), ("Uint8ClampedArray", ("Uint8ClampedArray", false, false)), + ("Uint8ClampedSlice", ("Uint8ClampedArray", false, false)), ("Int16Array", ("Int16Array", false, false)), ("Uint16Array", ("Uint16Array", false, false)), ("Int32Array", ("Int32Array", false, false)), @@ -166,6 +169,7 @@ static KNOWN_TYPES: Lazy> = La ("Date", ("Date", false, false)), ("JsDate", ("Date", false, false)), ("JsBuffer", ("Buffer", false, false)), + ("BufferSlice", ("Buffer", false, false)), ("Buffer", ("Buffer", false, false)), ("Vec", ("Array<{}>", false, false)), ("Result", ("Error | {}", false, true)), @@ -445,6 +449,16 @@ pub fn ty_to_ts_type( ty_to_ts_type(&p.elem, is_return_ty, is_struct_field, false); (element_type, is_optional) } + Type::Slice(TypeSlice { elem, .. }) => { + if let Type::Path(TypePath { path, .. }) = &**elem { + if let Some(PathSegment { ident, .. }) = path.segments.last() { + if let Some(js_type) = crate::TYPEDARRAY_SLICE_TYPES.get(&ident.to_string().as_str()) { + return (js_type.to_string(), false); + } + } + } + ("any[]".to_owned(), false) + } _ => ("any".to_owned(), false), } } diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 93e4c626..d82ccfe6 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -85,7 +85,7 @@ Generated by [AVA](https://avajs.dev). constructor(name: string)␊ getCount(): number␊ getNameAsync(): Promise␊ - acceptSliceMethod(slice: any): number␊ + acceptSliceMethod(slice: Uint8Array): number␊ }␊ ␊ /** Smoking test for type generation */␊ @@ -247,7 +247,7 @@ Generated by [AVA](https://avajs.dev). foo: number␊ }␊ ␊ - export function acceptSlice(fixture: any): bigint␊ + export function acceptSlice(fixture: Uint8Array): bigint␊ ␊ export function acceptThreadsafeFunction(func: (err: Error | null, arg: number) => any): void␊ ␊ @@ -423,15 +423,15 @@ Generated by [AVA](https://avajs.dev). ␊ export function enumToI32(e: CustomNumEnum): number␊ ␊ - export function f32ArrayToArray(input: any): Array␊ + export function f32ArrayToArray(input: Float32Array): Array␊ ␊ - export function f64ArrayToArray(input: any): Array␊ + export function f64ArrayToArray(input: Float64Array): Array␊ ␊ export function fibonacci(n: number): number␊ ␊ export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void␊ ␊ - export function getBtreeMapping(): BTreeMap␊ + export function getBtreeMapping(): Record␊ ␊ export function getBuffer(): Buffer␊ ␊ @@ -443,7 +443,7 @@ Generated by [AVA](https://avajs.dev). ␊ export function getGlobal(): typeof global␊ ␊ - export function getIndexMapping(): IndexMap␊ + export function getIndexMapping(): Record␊ ␊ export function getMapping(): Record␊ ␊ @@ -468,15 +468,15 @@ Generated by [AVA](https://avajs.dev). ␊ export function getWords(): Array␊ ␊ - export function i16ArrayToArray(input: any): Array␊ + export function i16ArrayToArray(input: Int16Array): Array␊ ␊ - export function i32ArrayToArray(input: any): Array␊ + export function i32ArrayToArray(input: Int32Array): Array␊ ␊ - export function i64ArrayToArray(input: any): Array␊ + export function i64ArrayToArray(input: BigInt64Array): Array␊ ␊ - export function i8ArrayToArray(input: any): Array␊ + export function i8ArrayToArray(input: Int8Array): Array␊ ␊ - export function indexmapPassthrough(fixture: IndexMap): IndexMap␊ + export function indexmapPassthrough(fixture: Record): Record␊ ␊ /** default enum values are continuos i32s start from 0 */␊ export const enum Kind {␊ @@ -604,9 +604,9 @@ Generated by [AVA](https://avajs.dev). name: string␊ }␊ ␊ - export function sumBtreeMapping(nums: BTreeMap): number␊ + export function sumBtreeMapping(nums: Record): number␊ ␊ - export function sumIndexMapping(nums: IndexMap): number␊ + export function sumIndexMapping(nums: Record): number␊ ␊ export function sumMapping(nums: Record): number␊ ␊ @@ -649,13 +649,13 @@ Generated by [AVA](https://avajs.dev). typeOverrideOptional?: object␊ }␊ ␊ - export function u16ArrayToArray(input: any): Array␊ + export function u16ArrayToArray(input: Uint16Array): Array␊ ␊ - export function u32ArrayToArray(input: any): Array␊ + export function u32ArrayToArray(input: Uint32Array): Array␊ ␊ - export function u64ArrayToArray(input: any): Array␊ + export function u64ArrayToArray(input: BigUint64Array): Array␊ ␊ - export function u8ArrayToArray(input: any): Array␊ + export function u8ArrayToArray(input: Uint8Array): Array␊ ␊ export interface UseNullableStruct {␊ requiredNumberField: number␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index d8e2ce56..a5c5f0a0 100644 Binary files a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap differ diff --git a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap index f693200f..84b41386 100644 Binary files a/examples/napi/__tests__/__snapshots__/values.spec.ts.snap and b/examples/napi/__tests__/__snapshots__/values.spec.ts.snap differ diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 814003eb..800e9771 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -75,7 +75,7 @@ export class Bird { constructor(name: string) getCount(): number getNameAsync(): Promise - acceptSliceMethod(slice: any): number + acceptSliceMethod(slice: Uint8Array): number } /** Smoking test for type generation */ @@ -237,7 +237,7 @@ export interface A { foo: number } -export function acceptSlice(fixture: any): bigint +export function acceptSlice(fixture: Uint8Array): bigint export function acceptThreadsafeFunction(func: (err: Error | null, arg: number) => any): void @@ -413,15 +413,15 @@ export const enum Empty { export function enumToI32(e: CustomNumEnum): number -export function f32ArrayToArray(input: any): Array +export function f32ArrayToArray(input: Float32Array): Array -export function f64ArrayToArray(input: any): Array +export function f64ArrayToArray(input: Float64Array): Array export function fibonacci(n: number): number export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void -export function getBtreeMapping(): BTreeMap +export function getBtreeMapping(): Record export function getBuffer(): Buffer @@ -433,7 +433,7 @@ export function getExternal(external: ExternalObject): number export function getGlobal(): typeof global -export function getIndexMapping(): IndexMap +export function getIndexMapping(): Record export function getMapping(): Record @@ -458,15 +458,15 @@ export function getUndefined(): void export function getWords(): Array -export function i16ArrayToArray(input: any): Array +export function i16ArrayToArray(input: Int16Array): Array -export function i32ArrayToArray(input: any): Array +export function i32ArrayToArray(input: Int32Array): Array -export function i64ArrayToArray(input: any): Array +export function i64ArrayToArray(input: BigInt64Array): Array -export function i8ArrayToArray(input: any): Array +export function i8ArrayToArray(input: Int8Array): Array -export function indexmapPassthrough(fixture: IndexMap): IndexMap +export function indexmapPassthrough(fixture: Record): Record /** default enum values are continuos i32s start from 0 */ export const enum Kind { @@ -594,9 +594,9 @@ export interface StrictObject { name: string } -export function sumBtreeMapping(nums: BTreeMap): number +export function sumBtreeMapping(nums: Record): number -export function sumIndexMapping(nums: IndexMap): number +export function sumIndexMapping(nums: Record): number export function sumMapping(nums: Record): number @@ -639,13 +639,13 @@ export interface TsTypeChanged { typeOverrideOptional?: object } -export function u16ArrayToArray(input: any): Array +export function u16ArrayToArray(input: Uint16Array): Array -export function u32ArrayToArray(input: any): Array +export function u32ArrayToArray(input: Uint32Array): Array -export function u64ArrayToArray(input: any): Array +export function u64ArrayToArray(input: BigUint64Array): Array -export function u8ArrayToArray(input: any): Array +export function u8ArrayToArray(input: Uint8Array): Array export interface UseNullableStruct { requiredNumberField: number