mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 01:59:06 -05:00
fix(dts/ffi): non-exact types break FFI inference (#14968)
This commit is contained in:
parent
1bd024f546
commit
cbe9d79f56
2 changed files with 143 additions and 30 deletions
71
cli/dts/lib.deno.unstable.d.ts
vendored
71
cli/dts/lib.deno.unstable.d.ts
vendored
|
@ -362,48 +362,59 @@ declare namespace Deno {
|
|||
|
||||
export type NativeResultType = NativeType | NativeVoidType;
|
||||
|
||||
type ToNativeTypeMap =
|
||||
& Record<NativeNumberType, number>
|
||||
& Record<NativeBigIntType, bigint | number>
|
||||
& Record<NativePointerType, TypedArray | bigint | null>
|
||||
& Record<NativeFunctionType, bigint | null>;
|
||||
|
||||
/** Type conversion for foreign symbol parameters and unsafe callback return types */
|
||||
type ToNativeType<T extends NativeType = NativeType> = T extends
|
||||
NativeNumberType ? number
|
||||
: T extends NativeBigIntType ? bigint | number
|
||||
: T extends NativePointerType ? TypedArray | bigint | null
|
||||
: T extends NativeFunctionType ? bigint | null
|
||||
: never;
|
||||
type ToNativeType<T extends NativeType = NativeType> = ToNativeTypeMap[T];
|
||||
|
||||
type ToNativeResultTypeMap = ToNativeTypeMap & Record<NativeVoidType, void>;
|
||||
|
||||
/** Type conversion for unsafe callback return types */
|
||||
type ToNativeResultType<T extends NativeResultType = NativeResultType> =
|
||||
T extends NativeType ? ToNativeType<T>
|
||||
: T extends NativeVoidType ? void
|
||||
ToNativeResultTypeMap[T];
|
||||
|
||||
type ToNativeParameterTypes<T extends readonly NativeType[]> =
|
||||
//
|
||||
[(T[number])[]] extends [T] ? ToNativeType<T[number]>[]
|
||||
: [readonly (T[number])[]] extends [T]
|
||||
? readonly ToNativeType<T[number]>[]
|
||||
: T extends readonly [...NativeType[]] ? {
|
||||
[K in keyof T]: ToNativeType<T[K]>;
|
||||
}
|
||||
: never;
|
||||
|
||||
type ToNativeParameterTypes<T extends readonly NativeType[]> = T extends
|
||||
readonly [] ? []
|
||||
: T extends readonly [
|
||||
infer U extends NativeType,
|
||||
...(infer V extends NativeType[]),
|
||||
] ? [ToNativeType<U>, ...ToNativeParameterTypes<V>]
|
||||
: never;
|
||||
type FromNativeTypeMap =
|
||||
& Record<NativeNumberType, number>
|
||||
& Record<NativeBigIntType, bigint>
|
||||
& Record<NativePointerType, bigint>
|
||||
& Record<NativeFunctionType, bigint>;
|
||||
|
||||
/** Type conversion for foreign symbol return types and unsafe callback parameters */
|
||||
type FromNativeType<T extends NativeType = NativeType> = T extends
|
||||
NativeNumberType ? number
|
||||
: T extends NativeBigIntType | NativePointerType | NativeFunctionType
|
||||
? bigint
|
||||
: never;
|
||||
type FromNativeType<T extends NativeType = NativeType> = FromNativeTypeMap[T];
|
||||
|
||||
type FromNativeResultTypeMap =
|
||||
& FromNativeTypeMap
|
||||
& Record<NativeVoidType, void>;
|
||||
|
||||
/** Type conversion for foregin symbol return types */
|
||||
type FromNativeResultType<T extends NativeResultType = NativeResultType> =
|
||||
T extends NativeType ? FromNativeType<T>
|
||||
: T extends NativeVoidType ? void
|
||||
: never;
|
||||
FromNativeResultTypeMap[T];
|
||||
|
||||
type FromNativeParameterTypes<T extends readonly NativeType[]> = T extends
|
||||
readonly [] ? []
|
||||
: T extends readonly [
|
||||
infer U extends NativeType,
|
||||
...(infer V extends NativeType[]),
|
||||
] ? [FromNativeType<U>, ...FromNativeParameterTypes<V>]
|
||||
: never;
|
||||
type FromNativeParameterTypes<
|
||||
T extends readonly NativeType[],
|
||||
> =
|
||||
//
|
||||
[(T[number])[]] extends [T] ? FromNativeType<T[number]>[]
|
||||
: [readonly (T[number])[]] extends [T]
|
||||
? readonly FromNativeType<T[number]>[]
|
||||
: T extends readonly [...NativeType[]] ? {
|
||||
[K in keyof T]: FromNativeType<T[K]>;
|
||||
}
|
||||
: never;
|
||||
|
||||
/** A foreign function as defined by its parameter and result types */
|
||||
export interface ForeignFunction<
|
||||
|
|
|
@ -285,3 +285,105 @@ const static13_right: number = remote.symbols.static13;
|
|||
// @ts-expect-error: Invalid member type
|
||||
const static14_wrong: null = remote.symbols.static14;
|
||||
const static14_right: number = remote.symbols.static14;
|
||||
|
||||
// Adapted from https://stackoverflow.com/a/53808212/10873797
|
||||
type Equal<T, U> = (<G>() => G extends T ? 1 : 2) extends
|
||||
(<G>() => G extends U ? 1 : 2) ? true
|
||||
: false;
|
||||
|
||||
type AssertEqual<
|
||||
Expected extends $,
|
||||
Got extends $$,
|
||||
$ = [Equal<Got, Expected>] extends [true] ? Expected
|
||||
: ([Expected] extends [Got] ? never : Got),
|
||||
$$ = [Equal<Expected, Got>] extends [true] ? Got
|
||||
: ([Got] extends [Expected] ? never : Got),
|
||||
> = never;
|
||||
|
||||
type AssertNotEqual<
|
||||
Expected extends $,
|
||||
Got,
|
||||
$ = [Equal<Expected, Got>] extends [true] ? never : Expected,
|
||||
> = never;
|
||||
|
||||
type TypedArray =
|
||||
| Int8Array
|
||||
| Uint8Array
|
||||
| Int16Array
|
||||
| Uint16Array
|
||||
| Int32Array
|
||||
| Uint32Array
|
||||
| Uint8ClampedArray
|
||||
| Float32Array
|
||||
| Float64Array
|
||||
| BigInt64Array
|
||||
| BigUint64Array;
|
||||
|
||||
type __Tests__ = [
|
||||
empty: AssertEqual<
|
||||
{ symbols: Record<never, never>; close(): void },
|
||||
Deno.DynamicLibrary<Record<never, never>>
|
||||
>,
|
||||
basic: AssertEqual<
|
||||
{ symbols: { add: (n1: number, n2: number) => number }; close(): void },
|
||||
Deno.DynamicLibrary<{ add: { parameters: ["i32", "u8"]; result: "i32" } }>
|
||||
>,
|
||||
higher_order_params: AssertEqual<
|
||||
{
|
||||
symbols: {
|
||||
pushBuf: (ptr: bigint | TypedArray | null, func: bigint | null) => void;
|
||||
};
|
||||
close(): void;
|
||||
},
|
||||
Deno.DynamicLibrary<
|
||||
{ pushBuf: { parameters: ["pointer", "function"]; result: "void" } }
|
||||
>
|
||||
>,
|
||||
higher_order_returns: AssertEqual<
|
||||
{
|
||||
symbols: {
|
||||
pushBuf: (
|
||||
ptr: bigint | TypedArray | null,
|
||||
func: bigint | null,
|
||||
) => bigint;
|
||||
};
|
||||
close(): void;
|
||||
},
|
||||
Deno.DynamicLibrary<
|
||||
{ pushBuf: { parameters: ["pointer", "function"]; result: "pointer" } }
|
||||
>
|
||||
>,
|
||||
non_exact_params: AssertEqual<
|
||||
{
|
||||
symbols: {
|
||||
foo: (...args: (number | bigint | TypedArray | null)[]) => bigint;
|
||||
};
|
||||
close(): void;
|
||||
},
|
||||
Deno.DynamicLibrary<
|
||||
{ foo: { parameters: ("i32" | "pointer")[]; result: "u64" } }
|
||||
>
|
||||
>,
|
||||
non_exact_params_empty: AssertEqual<
|
||||
{
|
||||
symbols: {
|
||||
foo: () => number;
|
||||
};
|
||||
close(): void;
|
||||
},
|
||||
Deno.DynamicLibrary<
|
||||
{ foo: { parameters: []; result: "i32" } }
|
||||
>
|
||||
>,
|
||||
non_exact_params_empty: AssertNotEqual<
|
||||
{
|
||||
symbols: {
|
||||
foo: (a: number) => number;
|
||||
};
|
||||
close(): void;
|
||||
},
|
||||
Deno.DynamicLibrary<
|
||||
{ foo: { parameters: []; result: "i32" } }
|
||||
>
|
||||
>,
|
||||
];
|
||||
|
|
Loading…
Reference in a new issue