Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING(ext/ffi): Remove Deno.UnsafePointer indirection #14915

Merged
merged 4 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 12 additions & 21 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,17 @@ declare namespace Deno {
? void
: T extends StaticNativeBigIntType ? bigint
: T extends StaticNativeNumberType ? number
: T extends "pointer" ? UnsafePointer
: T extends "pointer" ? bigint
: never;

type StaticForeignFunctionParameter<T> = T extends "void" ? void
: T extends StaticNativeNumberType | StaticNativeBigIntType
? number | bigint
: T extends "pointer" ? UnsafePointer | TypedArray | null
: T extends "pointer" ? bigint | TypedArray | null
: T extends NativeCallbackType<
infer U extends readonly NativeType[],
infer V extends NativeParameterType
> ? UnsafeCallback<U, V> | UnsafePointer | null
> ? UnsafeCallback<U, V> | bigint | null
littledivy marked this conversation as resolved.
Show resolved Hide resolved
: unknown;

/** Infers a foreign function parameter list. */
Expand Down Expand Up @@ -449,19 +449,10 @@ declare namespace Deno {
* An unsafe pointer to a memory location for passing and returning pointers to and from the ffi
*/
export class UnsafePointer {
constructor(value: bigint);

value: bigint;

/**
* Return the direct memory pointer to the typed array in memory
*/
static of(typedArray: TypedArray): UnsafePointer;

/**
* Returns the value of the pointer which is useful in certain scenarios.
*/
valueOf(): bigint;
static of(value: Deno.UnsafeCallback | TypedArray): bigint;
}

/** **UNSTABLE**: Unsafe and new API, beware!
Expand All @@ -472,9 +463,9 @@ declare namespace Deno {
* (numbers, strings and raw bytes).
*/
export class UnsafePointerView {
constructor(pointer: UnsafePointer);
constructor(pointer: bigint);

pointer: UnsafePointer;
pointer: bigint;

/** Gets an unsigned 8-bit integer at the specified byte offset from the pointer. */
getUint8(offset?: number): number;
Expand Down Expand Up @@ -511,10 +502,10 @@ declare namespace Deno {
* present as symbols.
*/
export class UnsafeFnPointer<Fn extends ForeignFunction> {
pointer: UnsafePointer;
pointer: bigint;
definition: Fn;

constructor(pointer: UnsafePointer, definition: Fn);
constructor(pointer: bigint, definition: Fn);

call(
...args: StaticForeignFunctionParameters<Fn["parameters"]>
Expand Down Expand Up @@ -552,18 +543,18 @@ declare namespace Deno {
type UnsafeCallbackParameter<T extends NativeType> = T extends
StaticNativeBigIntType ? bigint
: T extends StaticNativeNumberType ? number
: T extends "pointer" ? UnsafePointer
: T extends "pointer" ? bigint
: never;

type UnsafeCallbackResult<T extends NativeParameterType> = T extends "void"
? void
: T extends StaticNativeBigIntType ? number | bigint
: T extends StaticNativeNumberType ? number
: T extends "pointer" ? UnsafePointer | TypedArray | null
: T extends "pointer" ? bigint | TypedArray | null
: T extends NativeCallbackType<
infer U extends readonly NativeType[],
infer V extends NativeParameterType
> ? UnsafeCallback<U, V> | UnsafePointer | null
> ? UnsafeCallback<U, V> | bigint | null
: never;

type UnsafeCallbackFunction<
Expand Down Expand Up @@ -594,7 +585,7 @@ declare namespace Deno {
callback: UnsafeCallbackFunction<Parameters, Result>,
);

pointer: UnsafePointer;
pointer: bigint;
definition: UnsafeCallbackDefinition<Parameters, Result>;
callback: UnsafeCallbackFunction<Parameters, Result>;

Expand Down
5 changes: 2 additions & 3 deletions cli/tests/unit/ffi_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ Deno.test({ permissions: { ffi: false } }, function ffiPermissionDenied() {
assertThrows(() => {
Deno.dlopen("/usr/lib/libc.so.6", {});
}, Deno.errors.PermissionDenied);
const ptr = new Deno.UnsafePointer(0n);
const fnptr = new Deno.UnsafeFnPointer(
ptr,
0n,
{
parameters: ["u32", "pointer"],
result: "void",
Expand All @@ -42,7 +41,7 @@ Deno.test({ permissions: { ffi: false } }, function ffiPermissionDenied() {
assertThrows(() => {
Deno.UnsafePointer.of(new Uint8Array(0));
}, Deno.errors.PermissionDenied);
const ptrView = new Deno.UnsafePointerView(ptr);
const ptrView = new Deno.UnsafePointerView(0n);
assertThrows(() => {
ptrView.copyInto(new Uint8Array(0));
}, Deno.errors.PermissionDenied);
Expand Down