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)
}
/** 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
/** Captured version of `Uint8Array.prototype.byteLength`. */
export function byteLength (bytes: Uint8Array): number {
- return getByteLength.call(bytes)
+ return getByteLength(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)
}
/**
* `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))
}
/**
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'
/** 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]
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`)
}