]> git.codecow.com Git - nano25519.git/commitdiff
Improve byte accessor bindings.
authorChris Duncan <chris@codecow.com>
Thu, 15 Oct 2026 17:15:26 +0000 (10:15 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 15 Oct 2026 17:15:26 +0000 (10:15 -0700)
src/lib/primordials.ts

index f630dd0ed5d9d04ff8b112f3f9c44ab3467c8b3a..f73e0fa3611b34b28f7cd93d545c7169b5613688 100644 (file)
@@ -78,24 +78,25 @@ const TypedArray = Object.getPrototypeOf(Bytes.prototype)
 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 {
+/** An accessor's getter, taking the object to read as its argument. */
+function getter (target: object, key: PropertyKey): (self: unknown) => any {
        const found = Object.getOwnPropertyDescriptor(target, key)
        if (typeof found?.get !== 'function') {
                throw new Nano25519TypeError(`Missing expected accessor while capturing builtins`)
        }
-       return found.get
+       return Function.prototype.call.bind(found.get)
 }
 
 /** Reads `[[TypedArrayName]]`; returns `undefined` for anything else. */
-const getTag = Function.prototype.call.bind(get<string>(TypedArray, Symbol.toStringTag))
+const getTag = getter(TypedArray, Symbol.toStringTag)
 /** Reads internal slot `[[ViewedArrayBuffer]]`. */
-const getBuffer = Function.prototype.call.bind(get<ArrayBufferLike>(TypedArray, 'buffer'))
+const getBuffer = getter(TypedArray, 'buffer')
 /** Throws if passed `SharedArrayBuffer`, distinguishing it from `ArrayBuffer`. */
-const getBufferByteLength = Function.prototype.call.bind(get<number>(ArrayBuffer.prototype, 'byteLength'))
+const getBufferByteLength = getter(ArrayBuffer.prototype, 'byteLength')
 /** Reads internal slot `[[ByteLength]]`. */
-const getByteLength = Function.prototype.call.bind(get<number>(TypedArray, 'byteLength'))
+const getByteLength = getter(TypedArray, 'byteLength')
 /** Reads internal slot `[[ByteOffset]]`. */
-const getByteOffset = Function.prototype.call.bind(get<number>(TypedArray, 'byteOffset'))
+const getByteOffset = getter(TypedArray, 'byteOffset')
 
 /** True for a genuine `ArrayBuffer`, false for a `SharedArrayBuffer`. */
 function isArrayBuffer (a: unknown): a is ArrayBuffer {