]> git.codecow.com Git - nano25519.git/commitdiff
Bind to call on declaration instead of recalling call.
authorChris Duncan <chris@zoso.dev>
Sun, 27 Sep 2026 02:03:44 +0000 (19:03 -0700)
committerChris Duncan <chris@zoso.dev>
Sun, 27 Sep 2026 02:03:44 +0000 (19:03 -0700)
src/lib/primordials.ts

index 5af4e945f1301e72a4572d77b6b10f61cf6b8233..2d44108e756f50df2bbbfd84102ad7a51b99e830 100644 (file)
@@ -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<T> (target: object, key: PropertyKey): (this: unknown) => T {
        const found = Object.getOwnPropertyDescriptor(target, key)
@@ -61,20 +61,20 @@ function get<T> (target: object, key: PropertyKey): (this: unknown) => T {
 }
 
 /** Reads `[[TypedArrayName]]`; returns `undefined` for anything else. */
-const getTag = get<string>(TypedArray, Symbol.toStringTag)
+const getTag = Function.prototype.call.bind(get<string>(TypedArray, Symbol.toStringTag))
 /** Reads internal slot `[[ViewedArrayBuffer]]`. */
-const getBuffer = get<ArrayBufferLike>(TypedArray, 'buffer')
+const getBuffer = Function.prototype.call.bind(get<ArrayBufferLike>(TypedArray, 'buffer'))
 /** Throws if passed `SharedArrayBuffer`, distinguishing it from `ArrayBuffer`. */
-const getBufferByteLength = get<number>(ArrayBuffer.prototype, 'byteLength')
+const getBufferByteLength = Function.prototype.call.bind(get<number>(ArrayBuffer.prototype, 'byteLength'))
 /** Reads internal slot `[[ByteLength]]`. */
-const getByteLength = get<number>(TypedArray, 'byteLength')
+const getByteLength = Function.prototype.call.bind(get<number>(TypedArray, 'byteLength'))
 /** Reads internal slot `[[ByteOffset]]`. */
-const getByteOffset = get<number>(TypedArray, 'byteOffset')
+const getByteOffset = Function.prototype.call.bind(get<number>(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`)
        }