From b5d74bf4d15f0aa18b2bb2f083d31f6eb87e74b8 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 26 Sep 2026 19:03:44 -0700 Subject: [PATCH] Bind to call on declaration instead of recalling call. --- src/lib/primordials.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index 5af4e94..2d44108 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -49,8 +49,8 @@ const Bytes = Uint8Array const TypedArray = Object.getPrototypeOf(Bytes.prototype) const View = DataView -const bytesSet = Bytes.prototype.set -const bytesFill = Bytes.prototype.fill +const bytesSet = Function.prototype.call.bind(Bytes.prototype.set) +const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill) function get (target: object, key: PropertyKey): (this: unknown) => T { const found = Object.getOwnPropertyDescriptor(target, key) @@ -61,20 +61,20 @@ function get (target: object, key: PropertyKey): (this: unknown) => T { } /** Reads `[[TypedArrayName]]`; returns `undefined` for anything else. */ -const getTag = get(TypedArray, Symbol.toStringTag) +const getTag = Function.prototype.call.bind(get(TypedArray, Symbol.toStringTag)) /** Reads internal slot `[[ViewedArrayBuffer]]`. */ -const getBuffer = get(TypedArray, 'buffer') +const getBuffer = Function.prototype.call.bind(get(TypedArray, 'buffer')) /** Throws if passed `SharedArrayBuffer`, distinguishing it from `ArrayBuffer`. */ -const getBufferByteLength = get(ArrayBuffer.prototype, 'byteLength') +const getBufferByteLength = Function.prototype.call.bind(get(ArrayBuffer.prototype, 'byteLength')) /** Reads internal slot `[[ByteLength]]`. */ -const getByteLength = get(TypedArray, 'byteLength') +const getByteLength = Function.prototype.call.bind(get(TypedArray, 'byteLength')) /** Reads internal slot `[[ByteOffset]]`. */ -const getByteOffset = get(TypedArray, 'byteOffset') +const getByteOffset = Function.prototype.call.bind(get(TypedArray, 'byteOffset')) /** True for a genuine `ArrayBuffer`, false for a `SharedArrayBuffer`. */ function isArrayBuffer (a: unknown): a is ArrayBuffer { try { - getBufferByteLength.call(a) + getBufferByteLength(a) return true } catch { } return false @@ -87,7 +87,7 @@ export function allocate (length: number): Bytes { /** Captured version of `Uint8Array.prototype.byteLength`. */ export function byteLength (bytes: Uint8Array): number { - return getByteLength.call(bytes) + return getByteLength(bytes) } /** @@ -100,16 +100,16 @@ export function copy (src: Uint8Array): Bytes export function copy (dst: Uint8Array, src: Uint8Array, offset?: number): void export function copy (a: Uint8Array, b?: Uint8Array, o?: number): void | Bytes { if (b) { - return bytesSet.call(a, b, o) + return bytesSet(a, b, o) } - const t = new Bytes(getByteLength.call(a)) - bytesSet.call(t, a) + const t = new Bytes(getByteLength(a)) + bytesSet(t, a) return t } /** Captured version of `target.fill(value, start, end)`. */ export function fill (target: Uint8Array, value: number, start?: number, end?: number): void { - bytesFill.call(target, value, start, end) + bytesFill(target, value, start, end) } /** @@ -118,7 +118,7 @@ export function fill (target: Uint8Array, value: number, start?: number, end?: n * `Symbol.toStringTag` cannot pass, and a look-alike object cannot either. */ export function isBytes (a: unknown): a is Bytes { - return getTag.call(a) === 'Uint8Array' && isArrayBuffer(getBuffer.call(a)) + return getTag(a) === 'Uint8Array' && isArrayBuffer(getBuffer(a)) } /** @@ -134,11 +134,11 @@ export function view (bytes: Bytes): View { if (!isBytes(bytes)) { throw new TypeError('Error creating DataView', { cause: bytes }) } - const offset = getByteOffset.call(bytes) + const offset = getByteOffset(bytes) if (offset !== 0) { throw new RangeError('Unexpected byte offset', { cause: offset }) } - return new View(getBuffer.call(bytes) as ArrayBuffer, offset, getByteLength.call(bytes)) + return new View(getBuffer(bytes) as ArrayBuffer, offset, getByteLength(bytes)) } const LOWER = '0123456789abcdef' @@ -151,7 +151,7 @@ for (let i = 0; i < 16; i++) { /** Lowercase hexadecimal encoding of `bytes`. */ export function bytesToHex (bytes: Uint8Array): string { - const byteLength = getByteLength.call(bytes) + const byteLength = getByteLength(bytes) let hex = '' for (let i = 0; i < byteLength; i++) { const byte = bytes[i] @@ -209,7 +209,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n throw new TypeError(`${name} must be Uint8Array or ArrayBuffer`) } const bytes = copy(value) - const byteLength = getByteLength.call(bytes) + const byteLength = getByteLength(bytes) if (byteLength < byteLengthMin) { throw new TypeError(`${name} must be at least ${byteLengthMin} bytes`) } -- 2.52.0