From a38a1f91cf4e0b298bf166ae0ef63166fe7c67fe Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Tue, 21 Jun 2022 05:50:33 +0300 Subject: chore(ext/ffi): simplify FFI types (#14920) This commit simplifies the TypeScript types used for interacting with Deno FFI. The basis is that types are now first grouped into logical wholes, NativeNumberType, NativeBigIntType etc. These wholes are combined into the NativeType and NativeResultType general types. Additionally, this PR removes the { function: { parameters: [], result: "void" } } type declaration from parameters (and result types. Now functions are merely passed and returned as "function". --- cli/dts/lib.deno.unstable.d.ts | 202 ++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 106 deletions(-) (limited to 'cli/dts/lib.deno.unstable.d.ts') diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 389d08f9f..643386ced 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -329,32 +329,86 @@ declare namespace Deno { */ export function getGid(): number | null; - /** All possible types for interfacing with foreign functions */ - export type NativeType = - | "void" + /** All plain number types for interfacing with foreign functions */ + type NativeNumberType = | "u8" | "i8" | "u16" | "i16" | "u32" | "i32" + | "f32" + | "f64"; + + /** All BigInt number type sfor interfacing with foreign functions */ + type NativeBigIntType = | "u64" | "i64" | "usize" - | "isize" - | "f32" - | "f64" - | "pointer"; + | "isize"; + + type NativePointerType = "pointer"; - type NativeParameterType = - | NativeType - | NativeCallbackType; + type NativeFunctionType = "function"; + + type NativeVoidType = "void"; + + /** All possible types for interfacing with foreign functions */ + export type NativeType = + | NativeNumberType + | NativeBigIntType + | NativePointerType + | NativeFunctionType; + + export type NativeResultType = NativeType | NativeVoidType; + + /** Type conversion for foreign symbol parameters and unsafe callback return types */ + type ToNativeType = T extends + NativeNumberType ? number + : T extends NativeBigIntType ? bigint | number + : T extends NativePointerType ? TypedArray | bigint | null + : T extends NativeFunctionType ? bigint | null + : never; + + /** Type conversion for unsafe callback return types */ + type ToNativeResultType = + T extends NativeType ? ToNativeType + : T extends NativeVoidType ? void + : never; + + type ToNativeParameterTypes = T extends + readonly [] ? [] + : T extends readonly [ + infer U extends NativeType, + ...(infer V extends NativeType[]), + ] ? [ToNativeType, ...ToNativeParameterTypes] + : never; + + /** Type conversion for foreign symbol return types and unsafe callback parameters */ + type FromNativeType = T extends + NativeNumberType ? number + : T extends NativeBigIntType | NativePointerType | NativeFunctionType + ? bigint + : never; + + /** Type conversion for foregin symbol return types */ + type FromNativeResultType = + T extends NativeType ? FromNativeType + : T extends NativeVoidType ? void + : never; + + type FromNativeParameterTypes = T extends + readonly [] ? [] + : T extends readonly [ + infer U extends NativeType, + ...(infer V extends NativeType[]), + ] ? [FromNativeType, ...FromNativeParameterTypes] + : never; /** A foreign function as defined by its parameter and result types */ export interface ForeignFunction< - Parameters extends readonly NativeParameterType[] = - readonly NativeParameterType[], - Result extends NativeType = NativeType, + Parameters extends readonly NativeType[] = readonly NativeType[], + Result extends NativeResultType = NativeResultType, NonBlocking extends boolean = boolean, > { /** Name of the symbol, defaults to the key name in symbols object. */ @@ -368,7 +422,7 @@ declare namespace Deno { export interface ForeignStatic { /** Name of the symbol, defaults to the key name in symbols object. */ name?: string; - type: Exclude; + type: Type; } /** A foreign library interface descriptor */ @@ -376,50 +430,21 @@ declare namespace Deno { [name: string]: ForeignFunction | ForeignStatic; } - /** All possible number types interfacing with foreign functions */ - type StaticNativeNumberType = Exclude< - NativeType, - "void" | "pointer" | StaticNativeBigIntType - >; - - /** All possible bigint types interfacing with foreign functions */ - type StaticNativeBigIntType = "u64" | "i64" | "usize" | "isize"; - - /** Infers a foreign function return type */ - type StaticForeignFunctionResult = T extends "void" - ? void - : T extends StaticNativeBigIntType ? bigint - : T extends StaticNativeNumberType ? number - : T extends "pointer" ? bigint - : never; - - type StaticForeignFunctionParameter = T extends "void" ? void - : T extends StaticNativeNumberType | StaticNativeBigIntType - ? number | bigint - : T extends "pointer" ? bigint | TypedArray | null - : T extends NativeCallbackType ? bigint | null - : unknown; - - /** Infers a foreign function parameter list. */ - type StaticForeignFunctionParameters< - T extends readonly NativeParameterType[], - > = [ - ...{ - [K in keyof T]: StaticForeignFunctionParameter; - }, - ]; - /** Infers a foreign symbol */ type StaticForeignSymbol = - T extends ForeignFunction ? ( - ...args: StaticForeignFunctionParameters - ) => ConditionalAsync< - T["nonblocking"], - StaticForeignFunctionResult - > - : T extends ForeignStatic ? StaticForeignFunctionResult + T extends ForeignFunction ? FromForeignFunction + : T extends ForeignStatic ? FromNativeType : never; + type FromForeignFunction = T["parameters"] extends + readonly [] ? () => StaticForeignSymbolReturnType + : ( + ...args: ToNativeParameterTypes + ) => StaticForeignSymbolReturnType; + + type StaticForeignSymbolReturnType = + ConditionalAsync>; + type ConditionalAsync = IsAsync extends true ? Promise : T; @@ -504,63 +529,23 @@ declare namespace Deno { constructor(pointer: bigint, definition: Fn); - call( - ...args: StaticForeignFunctionParameters - ): ConditionalAsync< - Fn["nonblocking"], - StaticForeignFunctionResult - >; + call: FromForeignFunction; } export interface UnsafeCallbackDefinition< Parameters extends readonly NativeType[] = readonly NativeType[], - Result extends NativeParameterType = NativeParameterType, + Result extends NativeResultType = NativeResultType, > { parameters: Parameters; result: Result; } - interface NativeCallbackType< - Parameters extends readonly NativeType[] = readonly NativeType[], - Result extends NativeParameterType = NativeParameterType, - > { - readonly function: UnsafeCallbackDefinition; - } - - type UnsafeCallbackParameters = T extends [] - ? [] - : T extends - readonly [infer U extends NativeType, ...(infer V extends NativeType[])] - ? [ - UnsafeCallbackParameter, - ...UnsafeCallbackParameters, - ] - : never; - - type UnsafeCallbackParameter = T extends - StaticNativeBigIntType ? bigint - : T extends StaticNativeNumberType ? number - : T extends "pointer" ? bigint - : never; - - type UnsafeCallbackResult = T extends "void" - ? void - : T extends StaticNativeBigIntType ? number | bigint - : T extends StaticNativeNumberType ? number - : T extends "pointer" ? bigint | TypedArray | null - : T extends NativeCallbackType ? bigint | null - : never; - type UnsafeCallbackFunction< Parameters extends readonly NativeType[] = readonly NativeType[], - Result extends NativeParameterType = NativeParameterType, - > = Result extends NativeParameterType - ? Parameters extends readonly [] ? () => UnsafeCallbackResult - : Parameters extends readonly NativeType[] ? ( - ...args: UnsafeCallbackParameters - ) => UnsafeCallbackResult - : never - : never; + Result extends NativeResultType = NativeResultType, + > = Parameters extends readonly [] ? () => ToNativeResultType : ( + ...args: FromNativeParameterTypes + ) => ToNativeResultType; /** * **UNSTABLE**: Unsafe and new API, beware! @@ -571,17 +556,22 @@ declare namespace Deno { * The function pointer remains valid until the `close()` method is called. */ export class UnsafeCallback< - Parameters extends readonly NativeType[] = readonly NativeType[], - Result extends NativeParameterType = NativeParameterType, + Definition extends UnsafeCallbackDefinition = UnsafeCallbackDefinition, > { constructor( - definition: UnsafeCallbackDefinition, - callback: UnsafeCallbackFunction, + definition: Definition, + callback: UnsafeCallbackFunction< + Definition["parameters"], + Definition["result"] + >, ); pointer: bigint; - definition: UnsafeCallbackDefinition; - callback: UnsafeCallbackFunction; + definition: Definition; + callback: UnsafeCallbackFunction< + Definition["parameters"], + Definition["result"] + >; close(): void; } -- cgit v1.2.3