fix(napi-derive): HashMap/IndexMap and slice generated types (#1977)

This commit is contained in:
LongYinan 2024-02-25 00:56:30 +08:00 committed by GitHub
parent 5af366b042
commit 3e187151a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 69 additions and 39 deletions

View file

@ -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

View file

@ -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<HashMap<&str, &str>> = Lazy::new(|| {
HashMap::from([
("u8", "Uint8Array"),
("i8", "Int8Array"),
("u16", "Uint16Array"),
("i16", "Int16Array"),
("u32", "Uint32Array"),
("i32", "Int32Array"),
("f32", "Float32Array"),
("f64", "Float64Array"),
("u64", "BigUint64Array"),
("i64", "BigInt64Array"),
])
});

View file

@ -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<semver::Version> = 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<HashMap<&'static str, (&'static str, bool, bool)>> = La
("Value", ("any", false, false)),
("Map", ("Record<string, any>", 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<HashMap<&'static str, (&'static str, bool, bool)>> = 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),
}
}

View file

@ -85,7 +85,7 @@ Generated by [AVA](https://avajs.dev).
constructor(name: string)␊
getCount(): number␊
getNameAsync(): Promise<string>
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<number>
export function f32ArrayToArray(input: Float32Array): Array<number>
export function f64ArrayToArray(input: any): Array<number>
export function f64ArrayToArray(input: Float64Array): Array<number>
export function fibonacci(n: number): number␊
export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void␊
export function getBtreeMapping(): BTreeMap
export function getBtreeMapping(): Record<string, number>
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<string, number>
export function getMapping(): Record<string, number>
@ -468,15 +468,15 @@ Generated by [AVA](https://avajs.dev).
export function getWords(): Array<string>
export function i16ArrayToArray(input: any): Array<number>
export function i16ArrayToArray(input: Int16Array): Array<number>
export function i32ArrayToArray(input: any): Array<number>
export function i32ArrayToArray(input: Int32Array): Array<number>
export function i64ArrayToArray(input: any): Array<number>
export function i64ArrayToArray(input: BigInt64Array): Array<number>
export function i8ArrayToArray(input: any): Array<number>
export function i8ArrayToArray(input: Int8Array): Array<number>
export function indexmapPassthrough(fixture: IndexMap): IndexMap
export function indexmapPassthrough(fixture: Record<string, number>): Record<string, number>
/** 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<string, number>): number␊
export function sumIndexMapping(nums: IndexMap): number␊
export function sumIndexMapping(nums: Record<string, number>): number␊
export function sumMapping(nums: Record<string, number>): number␊
@ -649,13 +649,13 @@ Generated by [AVA](https://avajs.dev).
typeOverrideOptional?: object␊
}␊
export function u16ArrayToArray(input: any): Array<number>
export function u16ArrayToArray(input: Uint16Array): Array<number>
export function u32ArrayToArray(input: any): Array<number>
export function u32ArrayToArray(input: Uint32Array): Array<number>
export function u64ArrayToArray(input: any): Array<bigint>
export function u64ArrayToArray(input: BigUint64Array): Array<bigint>
export function u8ArrayToArray(input: any): Array<number>
export function u8ArrayToArray(input: Uint8Array): Array<number>
export interface UseNullableStruct {␊
requiredNumberField: number␊

View file

@ -75,7 +75,7 @@ export class Bird {
constructor(name: string)
getCount(): number
getNameAsync(): Promise<string>
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<number>
export function f32ArrayToArray(input: Float32Array): Array<number>
export function f64ArrayToArray(input: any): Array<number>
export function f64ArrayToArray(input: Float64Array): Array<number>
export function fibonacci(n: number): number
export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void
export function getBtreeMapping(): BTreeMap
export function getBtreeMapping(): Record<string, number>
export function getBuffer(): Buffer
@ -433,7 +433,7 @@ export function getExternal(external: ExternalObject<number>): number
export function getGlobal(): typeof global
export function getIndexMapping(): IndexMap
export function getIndexMapping(): Record<string, number>
export function getMapping(): Record<string, number>
@ -458,15 +458,15 @@ export function getUndefined(): void
export function getWords(): Array<string>
export function i16ArrayToArray(input: any): Array<number>
export function i16ArrayToArray(input: Int16Array): Array<number>
export function i32ArrayToArray(input: any): Array<number>
export function i32ArrayToArray(input: Int32Array): Array<number>
export function i64ArrayToArray(input: any): Array<number>
export function i64ArrayToArray(input: BigInt64Array): Array<number>
export function i8ArrayToArray(input: any): Array<number>
export function i8ArrayToArray(input: Int8Array): Array<number>
export function indexmapPassthrough(fixture: IndexMap): IndexMap
export function indexmapPassthrough(fixture: Record<string, number>): Record<string, number>
/** 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<string, number>): number
export function sumIndexMapping(nums: IndexMap): number
export function sumIndexMapping(nums: Record<string, number>): number
export function sumMapping(nums: Record<string, number>): number
@ -639,13 +639,13 @@ export interface TsTypeChanged {
typeOverrideOptional?: object
}
export function u16ArrayToArray(input: any): Array<number>
export function u16ArrayToArray(input: Uint16Array): Array<number>
export function u32ArrayToArray(input: any): Array<number>
export function u32ArrayToArray(input: Uint32Array): Array<number>
export function u64ArrayToArray(input: any): Array<bigint>
export function u64ArrayToArray(input: BigUint64Array): Array<bigint>
export function u8ArrayToArray(input: any): Array<number>
export function u8ArrayToArray(input: Uint8Array): Array<number>
export interface UseNullableStruct {
requiredNumberField: number