diff options
author | Kenta Moriuchi <moriken@kimamass.com> | 2023-04-03 02:41:41 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-02 19:41:41 +0200 |
commit | 03edd48edd004cec091541e6b71095cfbc4b4c87 (patch) | |
tree | 72aed1dae803334b73479ffebc7ca8c83d10addf /ext/ffi/00_ffi.js | |
parent | ad8d0c90d1887beb8a5f2c6d30f9dc71cc63e4fe (diff) |
chore: Turn back on dlintPreferPrimordials (#17715)
Closes #17709
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r-- | ext/ffi/00_ffi.js | 38 |
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}`), |