From: Chris Duncan Date: Thu, 15 Oct 2026 17:15:26 +0000 (-0700) Subject: Improve byte accessor bindings. X-Git-Tag: v2.0.0~1^2~24 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=2ec1b0f7f3bc8508080a9d9874e4e981be4c76c3;p=nano25519.git Improve byte accessor bindings. --- diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index f630dd0..f73e0fa 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -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 (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(TypedArray, Symbol.toStringTag)) +const getTag = getter(TypedArray, Symbol.toStringTag) /** Reads internal slot `[[ViewedArrayBuffer]]`. */ -const getBuffer = Function.prototype.call.bind(get(TypedArray, 'buffer')) +const getBuffer = getter(TypedArray, 'buffer') /** Throws if passed `SharedArrayBuffer`, distinguishing it from `ArrayBuffer`. */ -const getBufferByteLength = Function.prototype.call.bind(get(ArrayBuffer.prototype, 'byteLength')) +const getBufferByteLength = getter(ArrayBuffer.prototype, 'byteLength') /** Reads internal slot `[[ByteLength]]`. */ -const getByteLength = Function.prototype.call.bind(get(TypedArray, 'byteLength')) +const getByteLength = getter(TypedArray, 'byteLength') /** Reads internal slot `[[ByteOffset]]`. */ -const getByteOffset = Function.prototype.call.bind(get(TypedArray, 'byteOffset')) +const getByteOffset = getter(TypedArray, 'byteOffset') /** True for a genuine `ArrayBuffer`, false for a `SharedArrayBuffer`. */ function isArrayBuffer (a: unknown): a is ArrayBuffer {