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

feat(ext/ffi): Struct-by-value passing #17098

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ declare namespace Deno {
*/
type NativeVoidType = "void";

type NativeStructType = { readonly struct: readonly NativeType[] };

/** **UNSTABLE**: New API, yet to be vetted.
*
* All supported types for interfacing with foreign functions.
Expand All @@ -106,7 +108,8 @@ declare namespace Deno {
| NativeBooleanType
| NativePointerType
| NativeBufferType
| NativeFunctionType;
| NativeFunctionType
| NativeStructType;

/** **UNSTABLE**: New API, yet to be vetted.
*
Expand Down Expand Up @@ -136,7 +139,9 @@ declare namespace Deno {
*
* @category FFI
*/
type ToNativeType<T extends NativeType = NativeType> = ToNativeTypeMap[T];
type ToNativeType<T extends NativeType = NativeType> = T extends
NativeStructType ? BufferSource
: ToNativeTypeMap[Exclude<T, NativeStructType>];

/** **UNSTABLE**: New API, yet to be vetted.
*
Expand All @@ -153,7 +158,8 @@ declare namespace Deno {
* @category FFI
*/
type ToNativeResultType<T extends NativeResultType = NativeResultType> =
ToNativeResultTypeMap[T];
T extends NativeStructType ? BufferSource
: ToNativeResultTypeMap[Exclude<T, NativeStructType>];

/** **UNSTABLE**: New API, yet to be vetted.
*
Expand Down Expand Up @@ -193,7 +199,9 @@ declare namespace Deno {
*
* @category FFI
*/
type FromNativeType<T extends NativeType = NativeType> = FromNativeTypeMap[T];
type FromNativeType<T extends NativeType = NativeType> = T extends
NativeStructType ? Uint8Array
: FromNativeTypeMap[Exclude<T, NativeStructType>];

/** **UNSTABLE**: New API, yet to be vetted.
*
Expand All @@ -212,7 +220,8 @@ declare namespace Deno {
* @category FFI
*/
type FromNativeResultType<T extends NativeResultType = NativeResultType> =
FromNativeResultTypeMap[T];
T extends NativeStructType ? Uint8Array
: FromNativeResultTypeMap[Exclude<T, NativeStructType>];

/** **UNSTABLE**: New API, yet to be vetted.
*
Expand Down
177 changes: 153 additions & 24 deletions ext/ffi/00_ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
const {
ArrayPrototypeMap,
ArrayPrototypeJoin,
BigInt64Array,
BigUint64Array,
Function,
Int32Array,
Map,
MathCeil,
MathMax,
Number,
NumberIsSafeInteger,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
ObjectPrototypeIsPrototypeOf,
Number,
NumberIsSafeInteger,
PromisePrototypeThen,
ReflectHas,
SafeArrayIterator,
TypeError,
Int32Array,
Uint32Array,
BigInt64Array,
BigUint64Array,
Function,
ReflectHas,
Uint8Array,
} = window.__bootstrap.primordials;

const U32_BUFFER = new Uint32Array(2);
Expand Down Expand Up @@ -181,26 +187,55 @@
class UnsafeFnPointer {
pointer;
definition;
#structSize;

constructor(pointer, definition) {
this.pointer = pointer;
this.definition = definition;
this.#structSize = isStruct(definition.result)
? getTypeSizeAndAlignment(definition.result)[0]
: null;
}

call(...parameters) {
if (this.definition.nonblocking) {
return core.opAsync(
"op_ffi_call_ptr_nonblocking",
this.pointer,
this.definition,
parameters,
);
if (this.#structSize === null) {
return core.opAsync(
"op_ffi_call_ptr_nonblocking",
this.pointer,
this.definition,
parameters,
);
} else {
const buffer = new Uint8Array(this.#structSize);
return PromisePrototypeThen(
core.opAsync(
"op_ffi_call_ptr_nonblocking",
this.pointer,
this.definition,
parameters,
buffer,
),
() => buffer,
);
}
} else {
return ops.op_ffi_call_ptr(
this.pointer,
this.definition,
parameters,
);
if (this.#structSize === null) {
return ops.op_ffi_call_ptr(
this.pointer,
this.definition,
parameters,
);
} else {
const buffer = new Uint8Array(this.#structSize);
ops.op_ffi_call_ptr(
this.pointer,
this.definition,
parameters,
buffer,
);
return buffer;
}
}
}
}
Expand All @@ -215,6 +250,66 @@
return type === "i64" || type === "isize";
}

function isStruct(type) {
return typeof type === "object" && type !== null &&
ReflectHas(type, "struct");
}

function getTypeSizeAndAlignment(type, cache = new Map()) {
if (isStruct(type)) {
const cached = cache.get(type);
if (cached !== undefined) {
if (cached === null) {
throw new TypeError("Recursive struct definition");
}
return cached;
}
cache.set(type, null);
let size = 0;
let alignment = 1;
for (const field of new SafeArrayIterator(type.struct)) {
const [fieldSize, fieldAlignment] = getTypeSizeAndAlignment(
field,
cache,
);
alignment = MathMax(alignment, fieldAlignment);
// Insert padding to ensure that the next field starts at natural alignment.
size = MathCeil(size / fieldAlignment) * fieldAlignment;
// Add next field to the struct
size += fieldSize;
}
// Insert padding to ensure that the struct itself ends at natural alignment.
size = MathCeil(size / alignment) * alignment;
cache.set(type, [size, alignment]);
return [size, alignment];
}

switch (type) {
case "bool":
case "u8":
case "i8":
return [1, 1];
case "u16":
case "i16":
return [2, 2];
case "u32":
case "i32":
case "f32":
return [4, 4];
case "u64":
case "i64":
case "f64":
case "pointer":
case "buffer":
case "function":
case "usize":
case "isize":
return [8, 8];
default:
throw new TypeError(`Unsupported type: ${type}`);
}
}

class UnsafeCallback {
#refcount;
// Internal promise only meant to keep Deno from exiting
Expand Down Expand Up @@ -306,6 +401,10 @@
continue;
}
const resultType = symbols[symbol].result;
const isStructResult = isStruct(resultType);
const structSize = isStructResult
? getTypeSizeAndAlignment(resultType)[0]
: 0;
const needsUnpacking = isReturnedAsBigInt(resultType);

const isNonBlocking = symbols[symbol].nonblocking;
Expand All @@ -317,12 +416,27 @@
configurable: false,
enumerable: true,
value: (...parameters) => {
return core.opAsync(
"op_ffi_call_nonblocking",
this.#rid,
symbol,
parameters,
);
if (isStructResult) {
const buffer = new Uint8Array(structSize);
const ret = core.opAsync(
"op_ffi_call_nonblocking",
this.#rid,
symbol,
parameters,
buffer,
);
return PromisePrototypeThen(
ret,
() => buffer,
);
} else {
return core.opAsync(
"op_ffi_call_nonblocking",
this.#rid,
symbol,
parameters,
);
}
},
writable: false,
},
Expand Down Expand Up @@ -359,6 +473,21 @@
return b[0];
}`,
)(vi, vui, b, call, NumberIsSafeInteger, Number);
} else if (isStructResult && !isNonBlocking) {
const call = this.symbols[symbol];
const parameters = symbols[symbol].parameters;
const params = ArrayPrototypeJoin(
ArrayPrototypeMap(parameters, (_, index) => `p${index}`),
", ",
);
this.symbols[symbol] = new Function(
"call",
`return function (${params}) {
const buffer = new Uint8Array(${structSize});
call(${params}${parameters.length > 0 ? ", " : ""}buffer);
return buffer;
}`,
)(call);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "lib.rs"
deno_core.workspace = true
dlopen.workspace = true
dynasmrt = "1.2.3"
libffi = "3.0.0"
libffi = "3.1.0"
serde.workspace = true
tokio.workspace = true

Expand Down
Loading