1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

BREAKING(ext/ffi): remove deprecated UnsafeFnPointer constructor type with untyped Deno.PointerObject parameter (#25577)

This commit is contained in:
Aapo Alasuutari 2024-09-16 15:08:36 +03:00 committed by GitHub
parent 81c9e0ba25
commit f8547e2617
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 138 additions and 183 deletions

View file

@ -536,10 +536,7 @@ declare namespace Deno {
* set of permissions to the test context. * set of permissions to the test context.
* *
* @category Permissions */ * @category Permissions */
export type PermissionOptions = export type PermissionOptions = "inherit" | "none" | PermissionOptionsObject;
| "inherit"
| "none"
| PermissionOptionsObject;
/** /**
* A set of options which can define the permissions within a test or worker * A set of options which can define the permissions within a test or worker
@ -944,10 +941,7 @@ declare namespace Deno {
* *
* @category Testing * @category Testing
*/ */
( (name: string, fn: (t: TestContext) => void | Promise<void>): void;
name: string,
fn: (t: TestContext) => void | Promise<void>,
): void;
/** Register a test which will be run when `deno test` is used on the command /** Register a test which will be run when `deno test` is used on the command
* line and the containing module looks like a test module. * line and the containing module looks like a test module.
@ -1079,10 +1073,7 @@ declare namespace Deno {
* *
* @category Testing * @category Testing
*/ */
ignore( ignore(name: string, fn: (t: TestContext) => void | Promise<void>): void;
name: string,
fn: (t: TestContext) => void | Promise<void>,
): void;
/** Shorthand property for ignoring a particular test case. /** Shorthand property for ignoring a particular test case.
* *
@ -1128,10 +1119,7 @@ declare namespace Deno {
* *
* @category Testing * @category Testing
*/ */
only( only(name: string, fn: (t: TestContext) => void | Promise<void>): void;
name: string,
fn: (t: TestContext) => void | Promise<void>,
): void;
/** Shorthand property for focusing a particular test case. /** Shorthand property for focusing a particular test case.
* *
@ -3964,10 +3952,7 @@ declare namespace Deno {
* *
* @category Permissions * @category Permissions
*/ */
export type PermissionState = export type PermissionState = "granted" | "denied" | "prompt";
| "granted"
| "denied"
| "prompt";
/** The permission descriptor for the `allow-run` and `deny-run` permissions, which controls /** The permission descriptor for the `allow-run` and `deny-run` permissions, which controls
* access to what sub-processes can be executed by Deno. The option `command` * access to what sub-processes can be executed by Deno. The option `command`
@ -4107,7 +4092,7 @@ declare namespace Deno {
* *
* @category Permissions */ * @category Permissions */
export interface PermissionStatusEventMap { export interface PermissionStatusEventMap {
"change": Event; change: Event;
} }
/** An {@linkcode EventTarget} returned from the {@linkcode Deno.permissions} /** An {@linkcode EventTarget} returned from the {@linkcode Deno.permissions}
@ -5311,9 +5296,7 @@ declare namespace Deno {
* @category HTTP Server * @category HTTP Server
*/ */
export function serve( export function serve(
options: options: ServeTcpOptions | (ServeTcpOptions & TlsCertifiedKeyPem),
| ServeTcpOptions
| (ServeTcpOptions & TlsCertifiedKeyPem),
handler: ServeHandler<Deno.NetAddr>, handler: ServeHandler<Deno.NetAddr>,
): HttpServer<Deno.NetAddr>; ): HttpServer<Deno.NetAddr>;
/** Serves HTTP requests with the given option bag. /** Serves HTTP requests with the given option bag.
@ -5392,11 +5375,7 @@ declare namespace Deno {
* *
* @category FFI * @category FFI
*/ */
export type NativeBigIntType = export type NativeBigIntType = "u64" | "i64" | "usize" | "isize";
| "u64"
| "i64"
| "usize"
| "isize";
/** The native boolean type for interfacing to foreign functions. /** The native boolean type for interfacing to foreign functions.
* *
@ -5512,7 +5491,8 @@ declare namespace Deno {
: T extends NativeBigIntType ? bigint : T extends NativeBigIntType ? bigint
: T extends NativeBooleanType ? boolean : T extends NativeBooleanType ? boolean
: T extends NativePointerType : T extends NativePointerType
? T extends NativeTypedPointer<infer U> ? U | null : PointerValue ? T extends NativeTypedPointer<infer U> ? U | null
: PointerValue
: T extends NativeFunctionType : T extends NativeFunctionType
? T extends NativeTypedFunction<infer U> ? PointerValue<U> | null ? T extends NativeTypedFunction<infer U> ? PointerValue<U> | null
: PointerValue : PointerValue
@ -5536,7 +5516,8 @@ declare namespace Deno {
: T extends NativeBigIntType ? bigint : T extends NativeBigIntType ? bigint
: T extends NativeBooleanType ? boolean : T extends NativeBooleanType ? boolean
: T extends NativePointerType : T extends NativePointerType
? T extends NativeTypedPointer<infer U> ? U | null : PointerValue ? T extends NativeTypedPointer<infer U> ? U | null
: PointerValue
: T extends NativeFunctionType : T extends NativeFunctionType
? T extends NativeTypedFunction<infer U> ? PointerObject<U> | null ? T extends NativeTypedFunction<infer U> ? PointerObject<U> | null
: PointerValue : PointerValue
@ -5550,9 +5531,8 @@ declare namespace Deno {
*/ */
export type ToNativeParameterTypes<T extends readonly NativeType[]> = export type ToNativeParameterTypes<T extends readonly NativeType[]> =
// //
[(T[number])[]] extends [T] ? ToNativeType<T[number]>[] [T[number][]] extends [T] ? ToNativeType<T[number]>[]
: [readonly (T[number])[]] extends [T] : [readonly T[number][]] extends [T] ? readonly ToNativeType<T[number]>[]
? readonly ToNativeType<T[number]>[]
: T extends readonly [...NativeType[]] ? { : T extends readonly [...NativeType[]] ? {
[K in keyof T]: ToNativeType<T[K]>; [K in keyof T]: ToNativeType<T[K]>;
} }
@ -5575,7 +5555,8 @@ declare namespace Deno {
: T extends NativeBigIntType ? bigint : T extends NativeBigIntType ? bigint
: T extends NativeBooleanType ? boolean : T extends NativeBooleanType ? boolean
: T extends NativePointerType : T extends NativePointerType
? T extends NativeTypedPointer<infer U> ? U | null : PointerValue ? T extends NativeTypedPointer<infer U> ? U | null
: PointerValue
: T extends NativeBufferType ? PointerValue : T extends NativeBufferType ? PointerValue
: T extends NativeFunctionType : T extends NativeFunctionType
? T extends NativeTypedFunction<infer U> ? PointerObject<U> | null ? T extends NativeTypedFunction<infer U> ? PointerObject<U> | null
@ -5599,7 +5580,8 @@ declare namespace Deno {
: T extends NativeBigIntType ? bigint : T extends NativeBigIntType ? bigint
: T extends NativeBooleanType ? boolean : T extends NativeBooleanType ? boolean
: T extends NativePointerType : T extends NativePointerType
? T extends NativeTypedPointer<infer U> ? U | null : PointerValue ? T extends NativeTypedPointer<infer U> ? U | null
: PointerValue
: T extends NativeBufferType ? PointerValue : T extends NativeBufferType ? PointerValue
: T extends NativeFunctionType : T extends NativeFunctionType
? T extends NativeTypedFunction<infer U> ? PointerObject<U> | null ? T extends NativeTypedFunction<infer U> ? PointerObject<U> | null
@ -5609,12 +5591,10 @@ declare namespace Deno {
/** @category FFI /** @category FFI
*/ */
export type FromNativeParameterTypes< export type FromNativeParameterTypes<T extends readonly NativeType[]> =
T extends readonly NativeType[],
> =
// //
[(T[number])[]] extends [T] ? FromNativeType<T[number]>[] [T[number][]] extends [T] ? FromNativeType<T[number]>[]
: [readonly (T[number])[]] extends [T] : [readonly T[number][]] extends [T]
? readonly FromNativeType<T[number]>[] ? readonly FromNativeType<T[number]>[]
: T extends readonly [...NativeType[]] ? { : T extends readonly [...NativeType[]] ? {
[K in keyof T]: FromNativeType<T[K]>; [K in keyof T]: FromNativeType<T[K]>;
@ -5695,8 +5675,10 @@ declare namespace Deno {
/** @category FFI /** @category FFI
*/ */
export type ConditionalAsync<IsAsync extends boolean | undefined, T> = export type ConditionalAsync<
IsAsync extends true ? Promise<T> : T; IsAsync extends boolean | undefined,
T,
> = IsAsync extends true ? Promise<T> : T;
/** A utility type that infers a foreign library interface. /** A utility type that infers a foreign library interface.
* *
@ -5804,10 +5786,7 @@ declare namespace Deno {
getCString(offset?: number): string; getCString(offset?: number): string;
/** Gets a C string (`null` terminated string) at the specified byte offset /** Gets a C string (`null` terminated string) at the specified byte offset
* from the specified pointer. */ * from the specified pointer. */
static getCString( static getCString(pointer: PointerObject, offset?: number): string;
pointer: PointerObject,
offset?: number,
): string;
/** Gets an `ArrayBuffer` of length `byteLength` at the specified byte /** Gets an `ArrayBuffer` of length `byteLength` at the specified byte
* offset from the pointer. */ * offset from the pointer. */
getArrayBuffer(byteLength: number, offset?: number): ArrayBuffer; getArrayBuffer(byteLength: number, offset?: number): ArrayBuffer;
@ -5847,9 +5826,10 @@ declare namespace Deno {
/** The definition of the function. */ /** The definition of the function. */
definition: Fn; definition: Fn;
constructor(pointer: PointerObject<NoInfer<Fn>>, definition: Fn); constructor(
/** @deprecated Properly type {@linkcode pointer} using {@linkcode NativeTypedFunction} or {@linkcode UnsafeCallbackDefinition} types. */ pointer: PointerObject<NoInfer<Omit<Fn, "nonblocking">>>,
constructor(pointer: PointerObject, definition: Fn); definition: Fn,
);
/** Call the foreign function. */ /** Call the foreign function. */
call: FromForeignFunction<Fn>; call: FromForeignFunction<Fn>;
@ -5876,9 +5856,10 @@ declare namespace Deno {
export type UnsafeCallbackFunction< export type UnsafeCallbackFunction<
Parameters extends readonly NativeType[] = readonly NativeType[], Parameters extends readonly NativeType[] = readonly NativeType[],
Result extends NativeResultType = NativeResultType, Result extends NativeResultType = NativeResultType,
> = Parameters extends readonly [] ? () => ToNativeResultType<Result> : ( > = Parameters extends readonly [] ? () => ToNativeResultType<Result>
...args: FromNativeParameterTypes<Parameters> : (
) => ToNativeResultType<Result>; ...args: FromNativeParameterTypes<Parameters>
) => ToNativeResultType<Result>;
/** An unsafe function pointer for passing JavaScript functions as C function /** An unsafe function pointer for passing JavaScript functions as C function
* pointers to foreign function calls. * pointers to foreign function calls.

View file

@ -2,86 +2,78 @@
// deno-lint-ignore-file // deno-lint-ignore-file
// Only for testing types. Invoke with `deno cache` // Only for testing types. Invoke with `deno cache`
const remote = Deno.dlopen( const remote = Deno.dlopen("dummy_lib.so", {
"dummy_lib.so", method1: { parameters: ["usize", "bool"], result: "void" },
{ method2: { parameters: [], result: "void" },
method1: { parameters: ["usize", "bool"], result: "void" }, method3: { parameters: ["usize"], result: "void" },
method2: { parameters: [], result: "void" }, method4: { parameters: ["isize"], result: "void" },
method3: { parameters: ["usize"], result: "void" }, method5: { parameters: ["u8"], result: "void" },
method4: { parameters: ["isize"], result: "void" }, method6: { parameters: ["u16"], result: "void" },
method5: { parameters: ["u8"], result: "void" }, method7: { parameters: ["u32"], result: "void" },
method6: { parameters: ["u16"], result: "void" }, method8: { parameters: ["u64"], result: "void" },
method7: { parameters: ["u32"], result: "void" }, method9: { parameters: ["i8"], result: "void" },
method8: { parameters: ["u64"], result: "void" }, method10: { parameters: ["i16"], result: "void" },
method9: { parameters: ["i8"], result: "void" }, method11: { parameters: ["i32"], result: "void" },
method10: { parameters: ["i16"], result: "void" }, method12: { parameters: ["i64"], result: "void" },
method11: { parameters: ["i32"], result: "void" }, method13: { parameters: ["f32"], result: "void" },
method12: { parameters: ["i64"], result: "void" }, method14: { parameters: ["f64"], result: "void" },
method13: { parameters: ["f32"], result: "void" }, method15: { parameters: ["pointer"], result: "void" },
method14: { parameters: ["f64"], result: "void" }, method16: { parameters: [], result: "usize" },
method15: { parameters: ["pointer"], result: "void" }, method17: { parameters: [], result: "usize", nonblocking: true },
method16: { parameters: [], result: "usize" }, method18: { parameters: [], result: "pointer" },
method17: { parameters: [], result: "usize", nonblocking: true }, method19: { parameters: [], result: "pointer", nonblocking: true },
method18: { parameters: [], result: "pointer" }, method20: {
method19: { parameters: [], result: "pointer", nonblocking: true }, parameters: ["pointer"],
method20: { result: "void",
parameters: ["pointer"],
result: "void",
},
method21: {
parameters: [
"pointer",
],
result: "void",
},
method22: {
parameters: ["pointer"],
result: "void",
},
method23: {
parameters: ["buffer"],
result: "void",
},
method24: {
parameters: ["bool"],
result: "bool",
},
method25: {
parameters: [],
result: "void",
optional: true,
},
static1: { type: "usize" },
static2: { type: "pointer" },
static3: { type: "usize" },
static4: { type: "isize" },
static5: { type: "u8" },
static6: { type: "u16" },
static7: { type: "u32" },
static8: { type: "u64" },
static9: { type: "i8" },
static10: { type: "i16" },
static11: { type: "i32" },
static12: { type: "i64" },
static13: { type: "f32" },
static14: { type: "f64" },
static15: { type: "bool" },
static16: {
type: "bool",
optional: true,
},
}, },
); method21: {
parameters: ["pointer"],
result: "void",
},
method22: {
parameters: ["pointer"],
result: "void",
},
method23: {
parameters: ["buffer"],
result: "void",
},
method24: {
parameters: ["bool"],
result: "bool",
},
method25: {
parameters: [],
result: "void",
optional: true,
},
static1: { type: "usize" },
static2: { type: "pointer" },
static3: { type: "usize" },
static4: { type: "isize" },
static5: { type: "u8" },
static6: { type: "u16" },
static7: { type: "u32" },
static8: { type: "u64" },
static9: { type: "i8" },
static10: { type: "i16" },
static11: { type: "i32" },
static12: { type: "i64" },
static13: { type: "f32" },
static14: { type: "f64" },
static15: { type: "bool" },
static16: {
type: "bool",
optional: true,
},
});
Deno.dlopen( Deno.dlopen("dummy_lib_2.so", {
"dummy_lib_2.so", wrong_method1: {
{ parameters: [],
wrong_method1: { result: "function",
parameters: [],
result: "function",
},
}, },
); });
// @ts-expect-error: Invalid argument // @ts-expect-error: Invalid argument
remote.symbols.method1(0); remote.symbols.method1(0);
@ -173,7 +165,7 @@ result4.then((_0: Deno.BufferSource) => {});
result4.then((_1: null | Deno.UnsafePointer) => {}); result4.then((_1: null | Deno.UnsafePointer) => {});
const fnptr = new Deno.UnsafeFnPointer( const fnptr = new Deno.UnsafeFnPointer(
{} as Deno.PointerObject, {} as Deno.PointerObject<Deno.ForeignFunction<["u32", "pointer"], "void">>,
{ {
parameters: ["u32", "pointer"], parameters: ["u32", "pointer"],
result: "void", result: "void",
@ -340,16 +332,18 @@ const static16_right: boolean | null = remote.symbols.static16;
// Adapted from https://stackoverflow.com/a/53808212/10873797 // Adapted from https://stackoverflow.com/a/53808212/10873797
type Equal<T, U> = (<G>() => G extends T ? 1 : 2) extends type Equal<T, U> = (<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2) ? true <G>() => G extends U ? 1 : 2 ? true
: false; : false;
type AssertEqual< type AssertEqual<
Expected extends $, Expected extends $,
Got extends $$, Got extends $$,
$ = [Equal<Got, Expected>] extends [true] ? Expected $ = [Equal<Got, Expected>] extends [true] ? Expected
: ([Expected] extends [Got] ? never : Got), : [Expected] extends [Got] ? never
: Got,
$$ = [Equal<Expected, Got>] extends [true] ? Got $$ = [Equal<Expected, Got>] extends [true] ? Got
: ([Got] extends [Expected] ? never : Got), : [Got] extends [Expected] ? never
: Got,
> = never; > = never;
type AssertNotEqual< type AssertNotEqual<
@ -372,9 +366,7 @@ type MyFunctionDefinition = Deno.UnsafeCallbackDefinition<
[typeof foo, "u32"], [typeof foo, "u32"],
typeof myPointer typeof myPointer
>; >;
const myFunction = "function" as Deno.NativeTypedFunction< const myFunction = "function" as Deno.NativeTypedFunction<MyFunctionDefinition>;
MyFunctionDefinition
>;
type __Tests__ = [ type __Tests__ = [
empty: AssertEqual< empty: AssertEqual<
@ -396,14 +388,12 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{
{ pushBuf: {
pushBuf: { parameters: ["buffer", "pointer", "function"];
parameters: ["buffer", "pointer", "function"]; result: "void";
result: "void"; };
}; }>
}
>
>, >,
higher_order_returns: AssertEqual< higher_order_returns: AssertEqual<
{ {
@ -416,27 +406,23 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{
{ pushBuf: {
pushBuf: { parameters: ["buffer", "pointer", "function"];
parameters: ["buffer", "pointer", "function"]; result: "pointer";
result: "pointer"; };
}; }>
}
>
>, >,
non_exact_params: AssertEqual< non_exact_params: AssertEqual<
{ {
symbols: { symbols: {
foo: ( foo: (...args: (number | Deno.PointerValue | null)[]) => bigint;
...args: (number | Deno.PointerValue | null)[]
) => bigint;
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{
{ foo: { parameters: ("i32" | "pointer")[]; result: "u64" } } foo: { parameters: ("i32" | "pointer")[]; result: "u64" };
> }>
>, >,
non_exact_params_empty: AssertEqual< non_exact_params_empty: AssertEqual<
{ {
@ -445,9 +431,7 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{ foo: { parameters: []; result: "i32" } }>
{ foo: { parameters: []; result: "i32" } }
>
>, >,
non_exact_params_empty: AssertNotEqual< non_exact_params_empty: AssertNotEqual<
{ {
@ -456,9 +440,7 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{ foo: { parameters: []; result: "i32" } }>
{ foo: { parameters: []; result: "i32" } }
>
>, >,
enum_param: AssertEqual< enum_param: AssertEqual<
{ {
@ -467,9 +449,7 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{ foo: { parameters: [typeof foo]; result: "void" } }>
{ foo: { parameters: [typeof foo]; result: "void" } }
>
>, >,
enum_return: AssertEqual< enum_return: AssertEqual<
{ {
@ -478,9 +458,7 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{ foo: { parameters: []; result: typeof foo } }>
{ foo: { parameters: []; result: typeof foo } }
>
>, >,
typed_pointer_param: AssertEqual< typed_pointer_param: AssertEqual<
{ {
@ -489,9 +467,9 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{
{ foo: { parameters: [typeof myPointer]; result: "void" } } foo: { parameters: [typeof myPointer]; result: "void" };
> }>
>, >,
typed_pointer_return: AssertEqual< typed_pointer_return: AssertEqual<
{ {
@ -500,9 +478,7 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{ foo: { parameters: []; result: typeof myPointer } }>
{ foo: { parameters: []; result: typeof myPointer } }
>
>, >,
typed_function_param: AssertEqual< typed_function_param: AssertEqual<
{ {
@ -511,9 +487,9 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{
{ foo: { parameters: [typeof myFunction]; result: "void" } } foo: { parameters: [typeof myFunction]; result: "void" };
> }>
>, >,
typed_function_return: AssertEqual< typed_function_return: AssertEqual<
{ {
@ -522,8 +498,6 @@ type __Tests__ = [
}; };
close(): void; close(): void;
}, },
Deno.DynamicLibrary< Deno.DynamicLibrary<{ foo: { parameters: []; result: typeof myFunction } }>
{ foo: { parameters: []; result: typeof myFunction } }
>
>, >,
]; ];