summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
authorAapo Alasuutari <aapo.alasuutari@gmail.com>2024-05-30 05:30:11 +0300
committerGitHub <noreply@github.com>2024-05-30 08:00:11 +0530
commitd67ee9a08be200dc1ce9a416c9cda82730e24b68 (patch)
tree40cfd61cfbf89e6a1347d40caaa9ab3cdd3b7e25 /ext/ffi/00_ffi.js
parenta379009bfdddc56d6400740ad7be86f8930952ab (diff)
BREAKING(ffi/unstable): use BigInt representation in turbocall (#23983)
Built ontop of #23981, this sets FFI turbocalls (Fast Call API) to use the BigInt representation.
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r--ext/ffi/00_ffi.js65
1 files changed, 11 insertions, 54 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js
index 7f39db13e..06caf7c6c 100644
--- a/ext/ffi/00_ffi.js
+++ b/ext/ffi/00_ffi.js
@@ -42,19 +42,14 @@ const {
ArrayBufferPrototypeGetByteLength,
ArrayPrototypeMap,
ArrayPrototypeJoin,
+ BigInt,
DataViewPrototypeGetByteLength,
ObjectDefineProperty,
ObjectHasOwn,
ObjectPrototypeIsPrototypeOf,
- NumberIsSafeInteger,
- TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
TypeError,
Uint8Array,
- Int32Array,
- Uint32Array,
- BigInt64Array,
- BigUint64Array,
Function,
ReflectHas,
PromisePrototypeThen,
@@ -79,9 +74,6 @@ function getBufferSourceByteLength(source) {
}
return ArrayBufferPrototypeGetByteLength(source);
}
-const U32_BUFFER = new Uint32Array(2);
-const U64_BUFFER = new BigUint64Array(TypedArrayPrototypeGetBuffer(U32_BUFFER));
-const I64_BUFFER = new BigInt64Array(TypedArrayPrototypeGetBuffer(U32_BUFFER));
class UnsafePointerView {
pointer;
@@ -139,21 +131,21 @@ class UnsafePointerView {
}
getBigUint64(offset = 0) {
- op_ffi_read_u64(
+ return op_ffi_read_u64(
this.pointer,
- offset,
- U32_BUFFER,
+ // We return a BigInt, so the turbocall
+ // is forced to use BigInts everywhere.
+ BigInt(offset),
);
- return U64_BUFFER[0];
}
getBigInt64(offset = 0) {
- op_ffi_read_i64(
+ return op_ffi_read_i64(
this.pointer,
- offset,
- U32_BUFFER,
+ // We return a BigInt, so the turbocall
+ // is forced to use BigInts everywhere.
+ BigInt(offset),
);
- return I64_BUFFER[0];
}
getFloat32(offset = 0) {
@@ -226,10 +218,6 @@ class UnsafePointerView {
}
}
-const OUT_BUFFER = new Uint32Array(2);
-const OUT_BUFFER_64 = new BigInt64Array(
- TypedArrayPrototypeGetBuffer(OUT_BUFFER),
-);
const POINTER_TO_BUFFER_WEAK_MAP = new SafeWeakMap();
class UnsafePointer {
static create(value) {
@@ -279,12 +267,7 @@ class UnsafePointer {
if (ObjectPrototypeIsPrototypeOf(UnsafeCallbackPrototype, value)) {
value = value.pointer;
}
- op_ffi_ptr_value(value, OUT_BUFFER);
- const result = OUT_BUFFER[0] + 2 ** 32 * OUT_BUFFER[1];
- if (NumberIsSafeInteger(result)) {
- return result;
- }
- return OUT_BUFFER_64[0];
+ return op_ffi_ptr_value(value);
}
}
@@ -342,11 +325,6 @@ class UnsafeFnPointer {
}
}
-function isReturnedAsBigInt(type) {
- return type === "u64" || type === "i64" ||
- type === "usize" || type === "isize";
-}
-
function isStruct(type) {
return typeof type === "object" && type !== null &&
typeof type.struct === "object";
@@ -517,7 +495,6 @@ class DynamicLibrary {
const structSize = isStructResult
? getTypeSizeAndAlignment(resultType)[0]
: 0;
- const needsUnpacking = isReturnedAsBigInt(resultType);
const isNonBlocking = symbols[symbol].nonblocking;
if (isNonBlocking) {
@@ -553,27 +530,7 @@ class DynamicLibrary {
);
}
- if (needsUnpacking && !isNonBlocking) {
- const call = this.symbols[symbol];
- const parameters = symbols[symbol].parameters;
- const vi = new Int32Array(2);
- const b = new BigInt64Array(TypedArrayPrototypeGetBuffer(vi));
-
- const params = ArrayPrototypeJoin(
- ArrayPrototypeMap(parameters, (_, index) => `p${index}`),
- ", ",
- );
- // Make sure V8 has no excuse to not optimize this function.
- this.symbols[symbol] = new Function(
- "vi",
- "b",
- "call",
- `return function (${params}) {
- call(${params}${parameters.length > 0 ? ", " : ""}vi);
- return b[0];
- }`,
- )(vi, b, call);
- } else if (isStructResult && !isNonBlocking) {
+ if (isStructResult && !isNonBlocking) {
const call = this.symbols[symbol];
const parameters = symbols[symbol].parameters;
const params = ArrayPrototypeJoin(