summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r--ext/ffi/00_ffi.js38
1 files changed, 31 insertions, 7 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js
index 3664ae73b..ea75df65c 100644
--- a/ext/ffi/00_ffi.js
+++ b/ext/ffi/00_ffi.js
@@ -4,13 +4,19 @@ const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
const {
+ ArrayBufferIsView,
+ ArrayBufferPrototypeGetByteLength,
ArrayPrototypeMap,
ArrayPrototypeJoin,
+ DataViewPrototypeGetByteLength,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
ObjectPrototypeIsPrototypeOf,
Number,
NumberIsSafeInteger,
+ TypedArrayPrototypeGetBuffer,
+ TypedArrayPrototypeGetByteLength,
+ TypedArrayPrototypeGetSymbolToStringTag,
TypeError,
Uint8Array,
Int32Array,
@@ -29,11 +35,27 @@ const {
} = primordials;
import { pathFromURL } from "ext:deno_web/00_infra.js";
+/**
+ * @param {BufferSource} source
+ * @returns {number}
+ */
+function getBufferSourceByteLength(source) {
+ if (ArrayBufferIsView(source)) {
+ if (TypedArrayPrototypeGetSymbolToStringTag(source) !== undefined) {
+ // TypedArray
+ return TypedArrayPrototypeGetByteLength(source);
+ } else {
+ // DataView
+ return DataViewPrototypeGetByteLength(source);
+ }
+ }
+ return ArrayBufferPrototypeGetByteLength(source);
+}
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
const U32_BUFFER = new Uint32Array(2);
-const U64_BUFFER = new BigUint64Array(U32_BUFFER.buffer);
-const I64_BUFFER = new BigInt64Array(U32_BUFFER.buffer);
+const U64_BUFFER = new BigUint64Array(TypedArrayPrototypeGetBuffer(U32_BUFFER));
+const I64_BUFFER = new BigInt64Array(TypedArrayPrototypeGetBuffer(U32_BUFFER));
class UnsafePointerView {
pointer;
@@ -164,7 +186,7 @@ class UnsafePointerView {
this.pointer,
offset,
destination,
- destination.byteLength,
+ getBufferSourceByteLength(destination),
);
}
@@ -173,13 +195,15 @@ class UnsafePointerView {
pointer,
offset,
destination,
- destination.byteLength,
+ getBufferSourceByteLength(destination),
);
}
}
const OUT_BUFFER = new Uint32Array(2);
-const OUT_BUFFER_64 = new BigInt64Array(OUT_BUFFER.buffer);
+const OUT_BUFFER_64 = new BigInt64Array(
+ TypedArrayPrototypeGetBuffer(OUT_BUFFER),
+);
const POINTER_TO_BUFFER_WEAK_MAP = new WeakMap();
class UnsafePointer {
static create(value) {
@@ -492,8 +516,8 @@ class DynamicLibrary {
const call = this.symbols[symbol];
const parameters = symbols[symbol].parameters;
const vi = new Int32Array(2);
- const vui = new Uint32Array(vi.buffer);
- const b = new BigInt64Array(vi.buffer);
+ const vui = new Uint32Array(TypedArrayPrototypeGetBuffer(vi));
+ const b = new BigInt64Array(TypedArrayPrototypeGetBuffer(vi));
const params = ArrayPrototypeJoin(
ArrayPrototypeMap(parameters, (_, index) => `p${index}`),