@petamoriken/float16
Advanced tools
Comparing version 3.5.6 to 3.5.7
@@ -1,2 +0,2 @@ | ||
/*! @petamoriken/float16 v3.5.6 | MIT License - https://git.io/float16 */ | ||
/*! @petamoriken/float16 v3.5.7 | MIT License - https://git.io/float16 */ | ||
@@ -6,6 +6,2 @@ const float16 = (function (exports) { | ||
/* eslint-disable no-restricted-globals */ | ||
/* global SharedArrayBuffer */ | ||
/** @type {(target: Function) => (thisArg: any, ...args: any[]) => any} */ | ||
function uncurryThis(target) { | ||
@@ -16,4 +12,2 @@ return (thisArg, ...args) => { | ||
} | ||
/** @type {(target: any, key: string | symbol) => (thisArg: any, ...args: any[]) => any} */ | ||
function uncurryThisGetter(target, key) { | ||
@@ -27,4 +21,2 @@ return uncurryThis( | ||
} | ||
// Reflect | ||
const { | ||
@@ -42,7 +34,3 @@ apply: ReflectApply, | ||
} = Reflect; | ||
// Proxy | ||
const NativeProxy = Proxy; | ||
// Number | ||
const NativeNumber = Number; | ||
@@ -53,4 +41,2 @@ const { | ||
} = NativeNumber; | ||
// Symbol | ||
const { | ||
@@ -62,4 +48,2 @@ iterator: SymbolIterator, | ||
} = Symbol; | ||
// Object | ||
const NativeObject = Object; | ||
@@ -72,15 +56,9 @@ const { | ||
} = NativeObject; | ||
/** @type {(object: object, key: PropertyKey) => boolean} */ | ||
const ObjectHasOwn = /** @type {any} */ (NativeObject).hasOwn || | ||
const ObjectHasOwn = (NativeObject).hasOwn || | ||
uncurryThis(NativeObject.prototype.hasOwnProperty); | ||
// Array | ||
const NativeArray = Array; | ||
const ArrayIsArray = NativeArray.isArray; | ||
const ArrayPrototype = NativeArray.prototype; | ||
/** @type {(array: ArrayLike<unknown>, separator?: string) => string} */ | ||
const ArrayPrototypeJoin = uncurryThis(ArrayPrototype.join); | ||
/** @type {<T>(array: T[], ...items: T[]) => number} */ | ||
const ArrayPrototypePush = uncurryThis(ArrayPrototype.push); | ||
/** @type {(array: ArrayLike<unknown>) => string} */ | ||
const ArrayPrototypeToLocaleString = uncurryThis( | ||
@@ -90,62 +68,34 @@ ArrayPrototype.toLocaleString | ||
const NativeArrayPrototypeSymbolIterator = ArrayPrototype[SymbolIterator]; | ||
/** @type {<T>(array: T[]) => IterableIterator<T>} */ | ||
const ArrayPrototypeSymbolIterator = uncurryThis(NativeArrayPrototypeSymbolIterator); | ||
// Math | ||
const MathTrunc = Math.trunc; | ||
// ArrayBuffer | ||
const NativeArrayBuffer = ArrayBuffer; | ||
const ArrayBufferIsView = NativeArrayBuffer.isView; | ||
/** @type {(buffer: ArrayBuffer, begin?: number, end?: number) => number} */ | ||
const ArrayBufferPrototypeSlice = uncurryThis(NativeArrayBuffer.prototype.slice); | ||
/** @type {(buffer: ArrayBuffer) => ArrayBuffer} */ | ||
const ArrayBufferPrototypeGetByteLength = uncurryThisGetter(NativeArrayBuffer.prototype, "byteLength"); | ||
// SharedArrayBuffer | ||
const NativeSharedArrayBuffer = typeof SharedArrayBuffer !== "undefined" ? SharedArrayBuffer : null; | ||
/** @type {(buffer: SharedArrayBuffer, begin?: number, end?: number) => number} */ | ||
const SharedArrayBufferPrototypeSlice = NativeSharedArrayBuffer | ||
&& uncurryThis(NativeSharedArrayBuffer.prototype.slice); | ||
/** @type {(buffer: SharedArrayBuffer) => SharedArrayBuffer} */ | ||
const SharedArrayBufferPrototypeGetByteLength = NativeSharedArrayBuffer | ||
&& uncurryThisGetter(NativeSharedArrayBuffer.prototype, "byteLength"); | ||
// TypedArray | ||
/** @typedef {Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array} TypedArray */ | ||
/** @type {any} */ | ||
const TypedArray = ReflectGetPrototypeOf(Uint8Array); | ||
const TypedArrayFrom = TypedArray.from; | ||
const TypedArrayPrototype = TypedArray.prototype; | ||
/** @type {(typedArray: TypedArray) => IterableIterator<number>} */ | ||
const TypedArrayPrototypeKeys = uncurryThis(TypedArrayPrototype.keys); | ||
/** @type {(typedArray: TypedArray) => IterableIterator<number>} */ | ||
const TypedArrayPrototypeValues = uncurryThis( | ||
TypedArrayPrototype.values | ||
); | ||
/** @type {(typedArray: TypedArray) => IterableIterator<[number, number]>} */ | ||
const TypedArrayPrototypeEntries = uncurryThis( | ||
TypedArrayPrototype.entries | ||
); | ||
/** @type {(typedArray: TypedArray, array: ArrayLike<number>, offset?: number) => void} */ | ||
const TypedArrayPrototypeSet = uncurryThis(TypedArrayPrototype.set); | ||
/** @type {<T extends TypedArray>(typedArray: T) => T} */ | ||
const TypedArrayPrototypeReverse = uncurryThis( | ||
TypedArrayPrototype.reverse | ||
); | ||
/** @type {<T extends TypedArray>(typedArray: T, value: number, start?: number, end?: number) => T} */ | ||
const TypedArrayPrototypeFill = uncurryThis(TypedArrayPrototype.fill); | ||
/** @type {<T extends TypedArray>(typedArray: T, target: number, start: number, end?: number) => T} */ | ||
const TypedArrayPrototypeCopyWithin = uncurryThis( | ||
TypedArrayPrototype.copyWithin | ||
); | ||
/** @type {<T extends TypedArray>(typedArray: T, compareFn?: (a: number, b: number) => number) => T} */ | ||
const TypedArrayPrototypeSort = uncurryThis(TypedArrayPrototype.sort); | ||
/** @type {<T extends TypedArray>(typedArray: T, start?: number, end?: number) => T} */ | ||
const TypedArrayPrototypeSlice = uncurryThis(TypedArrayPrototype.slice); | ||
/** @type {<T extends TypedArray>(typedArray: T, start?: number, end?: number) => T} */ | ||
const TypedArrayPrototypeSubarray = uncurryThis( | ||
TypedArrayPrototype.subarray | ||
); | ||
/** @type {((typedArray: TypedArray) => ArrayBuffer)} */ | ||
const TypedArrayPrototypeGetBuffer = uncurryThisGetter( | ||
@@ -155,3 +105,2 @@ TypedArrayPrototype, | ||
); | ||
/** @type {((typedArray: TypedArray) => number)} */ | ||
const TypedArrayPrototypeGetByteOffset = uncurryThisGetter( | ||
@@ -161,3 +110,2 @@ TypedArrayPrototype, | ||
); | ||
/** @type {((typedArray: TypedArray) => number)} */ | ||
const TypedArrayPrototypeGetLength = uncurryThisGetter( | ||
@@ -167,3 +115,2 @@ TypedArrayPrototype, | ||
); | ||
/** @type {(target: unknown) => string} */ | ||
const TypedArrayPrototypeGetSymbolToStringTag = uncurryThisGetter( | ||
@@ -173,85 +120,38 @@ TypedArrayPrototype, | ||
); | ||
// Uint16Array | ||
const NativeUint16Array = Uint16Array; | ||
/** @type {Uint16ArrayConstructor["from"]} */ | ||
const Uint16ArrayFrom = (...args) => { | ||
return ReflectApply(TypedArrayFrom, NativeUint16Array, args); | ||
}; | ||
// Uint32Array | ||
const NativeUint32Array = Uint32Array; | ||
// Float32Array | ||
const NativeFloat32Array = Float32Array; | ||
// ArrayIterator | ||
/** @type {any} */ | ||
const ArrayIteratorPrototype = ReflectGetPrototypeOf([][SymbolIterator]()); | ||
/** @type {<T>(arrayIterator: IterableIterator<T>) => IteratorResult<T>} */ | ||
const ArrayIteratorPrototypeNext = uncurryThis(ArrayIteratorPrototype.next); | ||
// Generator | ||
/** @type {<T = unknown, TReturn = any, TNext = unknown>(generator: Generator<T, TReturn, TNext>, value?: TNext) => T} */ | ||
const GeneratorPrototypeNext = uncurryThis((function* () {})().next); | ||
// Iterator | ||
const IteratorPrototype = ReflectGetPrototypeOf(ArrayIteratorPrototype); | ||
// DataView | ||
const DataViewPrototype = DataView.prototype; | ||
/** @type {(dataView: DataView, byteOffset: number, littleEndian?: boolean) => number} */ | ||
const DataViewPrototypeGetUint16 = uncurryThis( | ||
DataViewPrototype.getUint16 | ||
); | ||
/** @type {(dataView: DataView, byteOffset: number, value: number, littleEndian?: boolean) => void} */ | ||
const DataViewPrototypeSetUint16 = uncurryThis( | ||
DataViewPrototype.setUint16 | ||
); | ||
// Error | ||
const NativeTypeError = TypeError; | ||
const NativeRangeError = RangeError; | ||
// Set | ||
/** | ||
* Do not construct with arguments to avoid calling the "add" method | ||
* | ||
* @type {{new <T = any>(): Set<T>}} | ||
*/ | ||
const NativeSet = Set; | ||
const SetPrototype = NativeSet.prototype; | ||
/** @type {<T>(set: Set<T>, value: T) => Set<T>} */ | ||
const SetPrototypeAdd = uncurryThis(SetPrototype.add); | ||
/** @type {<T>(set: Set<T>, value: T) => boolean} */ | ||
const SetPrototypeHas = uncurryThis(SetPrototype.has); | ||
// WeakMap | ||
/** | ||
* Do not construct with arguments to avoid calling the "set" method | ||
* | ||
* @type {{new <K extends {}, V>(): WeakMap<K, V>}} | ||
*/ | ||
const NativeWeakMap = WeakMap; | ||
const WeakMapPrototype = NativeWeakMap.prototype; | ||
/** @type {<K extends {}, V>(weakMap: WeakMap<K, V>, key: K) => V} */ | ||
const WeakMapPrototypeGet = uncurryThis(WeakMapPrototype.get); | ||
/** @type {<K extends {}, V>(weakMap: WeakMap<K, V>, key: K) => boolean} */ | ||
const WeakMapPrototypeHas = uncurryThis(WeakMapPrototype.has); | ||
/** @type {<K extends {}, V>(weakMap: WeakMap<K, V>, key: K, value: V) => WeakMap} */ | ||
const WeakMapPrototypeSet = uncurryThis(WeakMapPrototype.set); | ||
// algorithm: http://fox-toolkit.org/ftp/fasthalffloatconversion.pdf | ||
const buffer = new NativeArrayBuffer(4); | ||
const floatView = new NativeFloat32Array(buffer); | ||
const uint32View = new NativeUint32Array(buffer); | ||
const baseTable = new NativeUint32Array(512); | ||
const shiftTable = new NativeUint32Array(512); | ||
for (let i = 0; i < 256; ++i) { | ||
const e = i - 127; | ||
// very small number (0, -0) | ||
if (e < -27) { | ||
@@ -262,4 +162,2 @@ baseTable[i] = 0x0000; | ||
shiftTable[i | 0x100] = 24; | ||
// small number (denorm) | ||
} else if (e < -14) { | ||
@@ -270,4 +168,2 @@ baseTable[i] = 0x0400 >> (-e - 14); | ||
shiftTable[i | 0x100] = -e - 1; | ||
// normal number | ||
} else if (e <= 15) { | ||
@@ -278,4 +174,2 @@ baseTable[i] = (e + 15) << 10; | ||
shiftTable[i | 0x100] = 13; | ||
// large number (Infinity, -Infinity) | ||
} else if (e < 128) { | ||
@@ -286,4 +180,2 @@ baseTable[i] = 0x7c00; | ||
shiftTable[i | 0x100] = 24; | ||
// stay (NaN, Infinity, -Infinity) | ||
} else { | ||
@@ -296,11 +188,4 @@ baseTable[i] = 0x7c00; | ||
} | ||
/** | ||
* round a number to a half float number bits. | ||
* | ||
* @param {unknown} num - double float | ||
* @returns {number} half float number bits | ||
*/ | ||
function roundToFloat16Bits(num) { | ||
floatView[0] = /** @type {any} */ (num); | ||
floatView[0] = (num); | ||
const f = uint32View[0]; | ||
@@ -310,21 +195,15 @@ const e = (f >> 23) & 0x1ff; | ||
} | ||
const mantissaTable = new NativeUint32Array(2048); | ||
const exponentTable = new NativeUint32Array(64); | ||
const offsetTable = new NativeUint32Array(64); | ||
mantissaTable[0] = 0; | ||
for (let i = 1; i < 1024; ++i) { | ||
let m = i << 13; // zero pad mantissa bits | ||
let e = 0; // zero exponent | ||
// normalized | ||
let m = i << 13; | ||
let e = 0; | ||
while((m & 0x00800000) === 0) { | ||
e -= 0x00800000; // decrement exponent | ||
e -= 0x00800000; | ||
m <<= 1; | ||
} | ||
m &= ~0x00800000; // clear leading 1 bit | ||
e += 0x38800000; // adjust bias | ||
m &= ~0x00800000; | ||
e += 0x38800000; | ||
mantissaTable[i] = m | e; | ||
@@ -335,3 +214,2 @@ } | ||
} | ||
exponentTable[0] = 0; | ||
@@ -347,3 +225,2 @@ for (let i = 1; i < 31; ++i) { | ||
exponentTable[63] = 0xc7800000; | ||
offsetTable[0] = 0; | ||
@@ -357,9 +234,2 @@ for (let i = 1; i < 64; ++i) { | ||
} | ||
/** | ||
* convert a half float number bits to a number. | ||
* | ||
* @param {number} float16bits - half float number bits | ||
* @returns {number} double float | ||
*/ | ||
function convertToNumber(float16bits) { | ||
@@ -393,8 +263,2 @@ const m = float16bits >> 10; | ||
/** | ||
* returns the nearest half-precision float representation of a number. | ||
* | ||
* @param {number} num | ||
* @returns {number} | ||
*/ | ||
function hfround(num) { | ||
@@ -404,10 +268,6 @@ if (typeof num === "bigint") { | ||
} | ||
num = NativeNumber(num); | ||
// for optimization | ||
if (!NumberIsFinite(num) || num === 0) { | ||
return num; | ||
} | ||
const x16 = roundToFloat16Bits(num); | ||
@@ -417,7 +277,2 @@ return convertToNumber(x16); | ||
/** | ||
* Wrap ArrayIterator If Array.prototype [@@iterator] has been modified | ||
* | ||
* @type {<T>(array: T[]) => Iterable<T>} | ||
*/ | ||
function toSafe(array) { | ||
@@ -427,5 +282,4 @@ if (array[SymbolIterator] === NativeArrayPrototypeSymbolIterator) { | ||
} | ||
const arrayIterator = ArrayPrototypeSymbolIterator(array); | ||
return ObjectFreeze(/** @type {any} */ ({ | ||
return ObjectFreeze( ({ | ||
next() { | ||
@@ -436,3 +290,2 @@ return ArrayIteratorPrototypeNext(arrayIterator); | ||
throw: undefined, | ||
[SymbolIterator]() { | ||
@@ -443,7 +296,3 @@ return this; | ||
} | ||
/** @type {WeakMap<{}, Generator<any>>} */ | ||
const generators = new NativeWeakMap(); | ||
/** @see https://tc39.es/ecma262/#sec-%arrayiteratorprototype%-object */ | ||
const DummyArrayIteratorPrototype = ObjectCreate(IteratorPrototype, { | ||
@@ -458,3 +307,2 @@ next: { | ||
}, | ||
[SymbolToStringTag]: { | ||
@@ -465,4 +313,2 @@ value: "Array Iterator", | ||
}); | ||
/** @type {<T>(generator: Generator<T>) => IterableIterator<T>} */ | ||
function wrapGenerator(generator) { | ||
@@ -474,6 +320,2 @@ const dummy = ObjectCreate(DummyArrayIteratorPrototype); | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is {}} | ||
*/ | ||
function isObject(value) { | ||
@@ -483,26 +325,8 @@ return (value !== null && typeof value === "object") || | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is {}} | ||
*/ | ||
function isObjectLike(value) { | ||
return value !== null && typeof value === "object"; | ||
} | ||
// Inspired by util.types implementation of Node.js | ||
/** @typedef {Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array} TypedArray */ | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is TypedArray} | ||
*/ | ||
function isTypedArray(value) { | ||
return TypedArrayPrototypeGetSymbolToStringTag(value) !== undefined; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is BigInt64Array|BigUint64Array} | ||
*/ | ||
function isBigIntTypedArray(value) { | ||
@@ -513,10 +337,5 @@ const typedArrayName = TypedArrayPrototypeGetSymbolToStringTag(value); | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is ArrayBuffer} | ||
*/ | ||
function isArrayBuffer(value) { | ||
try { | ||
ArrayBufferPrototypeGetByteLength(/** @type {any} */ (value)); | ||
ArrayBufferPrototypeGetByteLength( (value)); | ||
return true; | ||
@@ -527,7 +346,2 @@ } catch (e) { | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is SharedArrayBuffer} | ||
*/ | ||
function isSharedArrayBuffer(value) { | ||
@@ -537,5 +351,4 @@ if (NativeSharedArrayBuffer === null) { | ||
} | ||
try { | ||
SharedArrayBufferPrototypeGetByteLength(/** @type {any} */ (value)); | ||
SharedArrayBufferPrototypeGetByteLength( (value)); | ||
return true; | ||
@@ -546,16 +359,5 @@ } catch (e) { | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @throws {TypeError} | ||
* @returns {value is Iterable<unknown>} | ||
*/ | ||
function isIterable(value) { | ||
return typeof value[SymbolIterator] === "function"; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is unknown[]} | ||
*/ | ||
function isOrdinaryArray(value) { | ||
@@ -565,3 +367,2 @@ if (!ArrayIsArray(value)) { | ||
} | ||
const iterator = value[SymbolIterator](); | ||
@@ -571,10 +372,4 @@ if (iterator[SymbolToStringTag] !== "Array Iterator") { | ||
} | ||
return true; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is TypedArray} | ||
*/ | ||
function isOrdinaryTypedArray(value) { | ||
@@ -584,3 +379,2 @@ if (!isTypedArray(value)) { | ||
} | ||
const iterator = value[SymbolIterator](); | ||
@@ -590,10 +384,4 @@ if (iterator[SymbolToStringTag] !== "Array Iterator") { | ||
} | ||
return true; | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {value is string} | ||
*/ | ||
function isCanonicalIntegerIndexString(value) { | ||
@@ -603,3 +391,2 @@ if (typeof value !== "string") { | ||
} | ||
const number = NativeNumber(value); | ||
@@ -609,19 +396,12 @@ if (value !== number + "") { | ||
} | ||
if (!NumberIsFinite(number)) { | ||
return false; | ||
} | ||
if (number !== MathTrunc(number)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity | ||
* @param {unknown} target | ||
* @returns {number} | ||
*/ | ||
const MAX_SAFE_INTEGER = NativeNumber.MAX_SAFE_INTEGER; | ||
function ToIntegerOrInfinity(target) { | ||
@@ -631,17 +411,8 @@ if (typeof target === "bigint") { | ||
} | ||
const number = NativeNumber(target); | ||
if (NumberIsNaN(number) || number === 0) { | ||
return 0; | ||
} | ||
return MathTrunc(number); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-tolength | ||
* @param {unknown} target | ||
* @returns {number} | ||
*/ | ||
function ToLength(target) { | ||
@@ -652,27 +423,6 @@ const length = ToIntegerOrInfinity(target); | ||
} | ||
return length < NativeNumber.MAX_SAFE_INTEGER | ||
return length < MAX_SAFE_INTEGER | ||
? length | ||
: NativeNumber.MAX_SAFE_INTEGER; | ||
: MAX_SAFE_INTEGER; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-lengthofarraylike | ||
* @param {object} arrayLike | ||
* @returns {number} | ||
*/ | ||
function LengthOfArrayLike(arrayLike) { | ||
if (!isObject(arrayLike)) { | ||
throw NativeTypeError(THIS_IS_NOT_AN_OBJECT); | ||
} | ||
return ToLength(/** @type {any} */ (arrayLike).length); | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-speciesconstructor | ||
* @param {object} target | ||
* @param {{ new(...args: any[]): any; }} defaultConstructor | ||
* @returns {{ new(...args: any[]): any; }} | ||
*/ | ||
function SpeciesConstructor(target, defaultConstructor) { | ||
@@ -682,3 +432,2 @@ if (!isObject(target)) { | ||
} | ||
const constructor = target.constructor; | ||
@@ -691,3 +440,2 @@ if (constructor === undefined) { | ||
} | ||
const species = constructor[SymbolSpecies]; | ||
@@ -697,67 +445,38 @@ if (species == null) { | ||
} | ||
return species; | ||
} | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-isdetachedbuffer | ||
* @param {ArrayBufferLike} buffer | ||
* @returns {boolean} | ||
*/ | ||
function IsDetachedBuffer(buffer) { | ||
if (isSharedArrayBuffer(buffer)) { | ||
return false; | ||
} | ||
try { | ||
ArrayBufferPrototypeSlice(buffer, 0, 0); | ||
return false; | ||
} catch (e) {/* empty */} | ||
if (NativeSharedArrayBuffer !== null) { | ||
try { | ||
SharedArrayBufferPrototypeSlice(/** @type {SharedArrayBuffer} */ (buffer), 0, 0); | ||
return false; | ||
} catch (e) {/* empty */} | ||
} | ||
} catch (e) {} | ||
return true; | ||
} | ||
/** | ||
* bigint comparisons are not supported | ||
* | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort | ||
* @param {number} x | ||
* @param {number} y | ||
* @returns {-1 | 0 | 1} | ||
*/ | ||
function defaultCompare(x, y) { | ||
const isXNaN = NumberIsNaN(x); | ||
const isYNaN = NumberIsNaN(y); | ||
if (isXNaN && isYNaN) { | ||
return 0; | ||
} | ||
if (isXNaN) { | ||
return 1; | ||
} | ||
if (isYNaN) { | ||
return -1; | ||
} | ||
if (x < y) { | ||
return -1; | ||
} | ||
if (x > y) { | ||
return 1; | ||
} | ||
if (x === 0 && y === 0) { | ||
const isXPlusZero = ObjectIs(x, 0); | ||
const isYPlusZero = ObjectIs(y, 0); | ||
if (!isXPlusZero && isYPlusZero) { | ||
return -1; | ||
} | ||
if (isXPlusZero && !isYPlusZero) { | ||
@@ -767,3 +486,2 @@ return 1; | ||
} | ||
return 0; | ||
@@ -773,13 +491,4 @@ } | ||
const BYTES_PER_ELEMENT = 2; | ||
const brand = SymbolFor("__Float16Array__"); | ||
/** @type {WeakMap<Float16Array, Uint16Array & { __float16bits: never }>} */ | ||
const float16bitsArrays = new NativeWeakMap(); | ||
/** | ||
* @param {unknown} target | ||
* @throws {TypeError} | ||
* @returns {boolean} | ||
*/ | ||
function hasFloat16ArrayBrand(target) { | ||
@@ -789,3 +498,2 @@ if (!isObjectLike(target)) { | ||
} | ||
const prototype = ReflectGetPrototypeOf(target); | ||
@@ -795,3 +503,2 @@ if (!isObjectLike(prototype)) { | ||
} | ||
const constructor = prototype.constructor; | ||
@@ -804,10 +511,4 @@ if (constructor === undefined) { | ||
} | ||
return ReflectHas(constructor, brand); | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @returns {target is Float16Array} | ||
*/ | ||
function isFloat16Array(target) { | ||
@@ -817,8 +518,2 @@ return WeakMapPrototypeHas(float16bitsArrays, target) || | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @throws {TypeError} | ||
* @returns {asserts target is Float16Array} | ||
*/ | ||
function assertFloat16Array(target) { | ||
@@ -829,17 +524,8 @@ if (!isFloat16Array(target)) { | ||
} | ||
/** | ||
* @param {unknown} target | ||
* @param {number=} count | ||
* @throws {TypeError} | ||
* @returns {asserts target is Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float16Array|Float32Array|Float64Array} | ||
*/ | ||
function assertSpeciesTypedArray(target, count) { | ||
const isTargetFloat16Array = isFloat16Array(target); | ||
const isTargetTypedArray = isTypedArray(target); | ||
if (!isTargetFloat16Array && !isTargetTypedArray) { | ||
throw NativeTypeError(SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT); | ||
} | ||
if (typeof count === "number") { | ||
@@ -853,3 +539,2 @@ let length; | ||
} | ||
if (length < count) { | ||
@@ -861,3 +546,2 @@ throw NativeTypeError( | ||
} | ||
if (isBigIntTypedArray(target)) { | ||
@@ -867,8 +551,2 @@ throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES); | ||
} | ||
/** | ||
* @param {Float16Array} float16 | ||
* @throws {TypeError} | ||
* @returns {Uint16Array & { __float16bits: never }} | ||
*/ | ||
function getFloat16BitsArray(float16) { | ||
@@ -883,16 +561,9 @@ const float16bitsArray = WeakMapPrototypeGet(float16bitsArrays, float16); | ||
} | ||
// from another Float16Array instance (a different version?) | ||
// @ts-ignore | ||
const buffer = float16.buffer; | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
const cloned = ReflectConstruct(Float16Array, [ | ||
buffer, | ||
// @ts-ignore | ||
float16.byteOffset, | ||
// @ts-ignore | ||
float16.length, | ||
@@ -902,10 +573,4 @@ ], float16.constructor); | ||
} | ||
/** | ||
* @param {Uint16Array & { __float16bits: never }} float16bitsArray | ||
* @returns {number[]} | ||
*/ | ||
function copyToArray(float16bitsArray) { | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const array = []; | ||
@@ -915,14 +580,9 @@ for (let i = 0; i < length; ++i) { | ||
} | ||
return array; | ||
} | ||
/** @type {Set<string | symbol>} */ | ||
const TypedArrayPrototypeGetterKeys = new NativeSet(); | ||
for (const key of ReflectOwnKeys(TypedArrayPrototype)) { | ||
// @@toStringTag method is defined in Float16Array.prototype | ||
if (key === SymbolToStringTag) { | ||
continue; | ||
} | ||
const descriptor = ReflectGetOwnPropertyDescriptor(TypedArrayPrototype, key); | ||
@@ -933,4 +593,3 @@ if (ObjectHasOwn(descriptor, "get")) { | ||
} | ||
const handler = ObjectFreeze(/** @type {ProxyHandler<Float16Array>} */ ({ | ||
const handler = ObjectFreeze( ({ | ||
get(target, key, receiver) { | ||
@@ -940,11 +599,7 @@ if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) { | ||
} | ||
// %TypedArray%.prototype getter properties cannot called by Proxy receiver | ||
if (SetPrototypeHas(TypedArrayPrototypeGetterKeys, key)) { | ||
return ReflectGet(target, key); | ||
} | ||
return ReflectGet(target, key, receiver); | ||
}, | ||
set(target, key, value, receiver) { | ||
@@ -954,6 +609,4 @@ if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) { | ||
} | ||
return ReflectSet(target, key, value, receiver); | ||
}, | ||
getOwnPropertyDescriptor(target, key) { | ||
@@ -965,6 +618,4 @@ if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) { | ||
} | ||
return ReflectGetOwnPropertyDescriptor(target, key); | ||
}, | ||
defineProperty(target, key, descriptor) { | ||
@@ -979,28 +630,19 @@ if ( | ||
} | ||
return ReflectDefineProperty(target, key, descriptor); | ||
}, | ||
})); | ||
class Float16Array { | ||
/** @see https://tc39.es/ecma262/#sec-typedarray */ | ||
constructor(input, _byteOffset, _length) { | ||
/** @type {Uint16Array & { __float16bits: never }} */ | ||
let float16bitsArray; | ||
if (isFloat16Array(input)) { | ||
float16bitsArray = ReflectConstruct(NativeUint16Array, [getFloat16BitsArray(input)], new.target); | ||
} else if (isObject(input) && !isArrayBuffer(input)) { // object without ArrayBuffer | ||
/** @type {ArrayLike<unknown>} */ | ||
} else if (isObject(input) && !isArrayBuffer(input)) { | ||
let list; | ||
/** @type {number} */ | ||
let length; | ||
if (isTypedArray(input)) { // TypedArray | ||
if (isTypedArray(input)) { | ||
list = input; | ||
length = TypedArrayPrototypeGetLength(input); | ||
const buffer = TypedArrayPrototypeGetBuffer(input); | ||
const BufferConstructor = !isSharedArrayBuffer(buffer) | ||
? /** @type {ArrayBufferConstructor} */ (SpeciesConstructor( | ||
? (SpeciesConstructor( | ||
buffer, | ||
@@ -1010,11 +652,8 @@ NativeArrayBuffer | ||
: NativeArrayBuffer; | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
if (isBigIntTypedArray(input)) { | ||
throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES); | ||
} | ||
const data = new BufferConstructor( | ||
@@ -1025,4 +664,3 @@ length * BYTES_PER_ELEMENT | ||
} else { | ||
if (isIterable(input)) { // Iterable (Array) | ||
// for optimization | ||
if (isIterable(input)) { | ||
if (isOrdinaryArray(input)) { | ||
@@ -1032,37 +670,23 @@ list = input; | ||
} else { | ||
// eslint-disable-next-line no-restricted-syntax | ||
list = [...input]; | ||
length = list.length; | ||
} | ||
} else { // ArrayLike | ||
list = /** @type {ArrayLike<unknown>} */ (input); | ||
length = LengthOfArrayLike(input); | ||
} else { | ||
list = (input); | ||
length = ToLength(list.length); | ||
} | ||
float16bitsArray = ReflectConstruct(NativeUint16Array, [length], new.target); | ||
} | ||
// set values | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = roundToFloat16Bits(list[i]); | ||
} | ||
} else { // primitive, ArrayBuffer | ||
} else { | ||
float16bitsArray = ReflectConstruct(NativeUint16Array, arguments, new.target); | ||
} | ||
const proxy = new NativeProxy(/** @type {any} */ (float16bitsArray), handler); | ||
// proxy private storage | ||
const proxy = new NativeProxy( (float16bitsArray), handler); | ||
WeakMapPrototypeSet(float16bitsArrays, proxy, float16bitsArray); | ||
return proxy; | ||
} | ||
/** | ||
* limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key | ||
* | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.from | ||
*/ | ||
static from(src, ...opts) { | ||
const Constructor = this; | ||
if (!ReflectHas(Constructor, brand)) { | ||
@@ -1073,4 +697,2 @@ throw NativeTypeError( | ||
} | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
@@ -1088,3 +710,2 @@ if (isFloat16Array(src) && opts.length === 0) { | ||
} | ||
if (opts.length === 0) { | ||
@@ -1097,6 +718,4 @@ return new Float16Array( | ||
} | ||
const mapFunc = opts[0]; | ||
const thisArg = opts[1]; | ||
return new Float16Array( | ||
@@ -1112,10 +731,5 @@ TypedArrayPrototypeGetBuffer( | ||
} | ||
/** @type {ArrayLike<unknown>} */ | ||
let list; | ||
/** @type {number} */ | ||
let length; | ||
if (isIterable(src)) { // Iterable (TypedArray, Array) | ||
// for optimization | ||
if (isIterable(src)) { | ||
if (isOrdinaryArray(src)) { | ||
@@ -1128,16 +742,18 @@ list = src; | ||
} else { | ||
// eslint-disable-next-line no-restricted-syntax | ||
list = [...src]; | ||
length = list.length; | ||
} | ||
} else { // ArrayLike | ||
list = src; | ||
length = LengthOfArrayLike(src); | ||
} else { | ||
if (src == null) { | ||
throw NativeTypeError( | ||
CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT | ||
); | ||
} | ||
list = NativeObject(src); | ||
length = ToLength(list.length); | ||
} | ||
const array = new Constructor(length); | ||
if (opts.length === 0) { | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = /** @type {number} */ (list[i]); | ||
array[i] = (list[i]); | ||
} | ||
@@ -1151,14 +767,6 @@ } else { | ||
} | ||
return array; | ||
} | ||
/** | ||
* limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key | ||
* | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.of | ||
*/ | ||
static of(...items) { | ||
const Constructor = this; | ||
if (!ReflectHas(Constructor, brand)) { | ||
@@ -1169,45 +777,26 @@ throw NativeTypeError( | ||
} | ||
const length = items.length; | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
const proxy = new Float16Array(length); | ||
const float16bitsArray = getFloat16BitsArray(proxy); | ||
for (let i = 0; i < length; ++i) { | ||
float16bitsArray[i] = roundToFloat16Bits(items[i]); | ||
} | ||
return proxy; | ||
} | ||
const array = new Constructor(length); | ||
for (let i = 0; i < length; ++i) { | ||
array[i] = items[i]; | ||
} | ||
return array; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.keys */ | ||
keys() { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
return TypedArrayPrototypeKeys(float16bitsArray); | ||
} | ||
/** | ||
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
* | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.values | ||
*/ | ||
values() { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
return wrapGenerator((function* () { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const val of TypedArrayPrototypeValues(float16bitsArray)) { | ||
@@ -1218,51 +807,31 @@ yield convertToNumber(val); | ||
} | ||
/** | ||
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%` | ||
* | ||
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.entries | ||
*/ | ||
entries() { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
return wrapGenerator((function* () { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const [i, val] of TypedArrayPrototypeEntries(float16bitsArray)) { | ||
yield /** @type {[Number, number]} */ ([i, convertToNumber(val)]); | ||
yield ([i, convertToNumber(val)]); | ||
} | ||
})()); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.at */ | ||
at(index) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const relativeIndex = ToIntegerOrInfinity(index); | ||
const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex; | ||
if (k < 0 || k >= length) { | ||
return; | ||
} | ||
return convertToNumber(float16bitsArray[k]); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map */ | ||
map(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
const Constructor = SpeciesConstructor(float16bitsArray, Float16Array); | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
const proxy = new Float16Array(length); | ||
const array = getFloat16BitsArray(proxy); | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1274,9 +843,6 @@ const val = convertToNumber(float16bitsArray[i]); | ||
} | ||
return proxy; | ||
} | ||
const array = new Constructor(length); | ||
assertSpeciesTypedArray(array, length); | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1286,14 +852,9 @@ const val = convertToNumber(float16bitsArray[i]); | ||
} | ||
return /** @type {any} */ (array); | ||
return (array); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.filter */ | ||
filter(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
const kept = []; | ||
@@ -1306,15 +867,10 @@ for (let i = 0; i < length; ++i) { | ||
} | ||
const Constructor = SpeciesConstructor(float16bitsArray, Float16Array); | ||
const array = new Constructor(kept); | ||
assertSpeciesTypedArray(array); | ||
return /** @type {any} */ (array); | ||
return (array); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce */ | ||
reduce(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
@@ -1324,3 +880,2 @@ if (length === 0 && opts.length === 0) { | ||
} | ||
let accumulator, start; | ||
@@ -1334,3 +889,2 @@ if (opts.length === 0) { | ||
} | ||
for (let i = start; i < length; ++i) { | ||
@@ -1344,11 +898,7 @@ accumulator = callback( | ||
} | ||
return accumulator; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduceright */ | ||
reduceRight(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
@@ -1358,3 +908,2 @@ if (length === 0 && opts.length === 0) { | ||
} | ||
let accumulator, start; | ||
@@ -1368,3 +917,2 @@ if (opts.length === 0) { | ||
} | ||
for (let i = start; i >= 0; --i) { | ||
@@ -1378,14 +926,9 @@ accumulator = callback( | ||
} | ||
return accumulator; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach */ | ||
forEach(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1399,11 +942,7 @@ ReflectApply(callback, thisArg, [ | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.find */ | ||
find(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1416,11 +955,7 @@ const value = convertToNumber(float16bitsArray[i]); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.findindex */ | ||
findIndex(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1432,14 +967,9 @@ const value = convertToNumber(float16bitsArray[i]); | ||
} | ||
return -1; | ||
} | ||
/** @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast */ | ||
findLast(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = length - 1; i >= 0; --i) { | ||
@@ -1452,11 +982,7 @@ const value = convertToNumber(float16bitsArray[i]); | ||
} | ||
/** @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlastindex */ | ||
findLastIndex(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = length - 1; i >= 0; --i) { | ||
@@ -1468,14 +994,9 @@ const value = convertToNumber(float16bitsArray[i]); | ||
} | ||
return -1; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.every */ | ||
every(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1492,14 +1013,9 @@ if ( | ||
} | ||
return true; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.some */ | ||
some(callback, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const thisArg = opts[0]; | ||
for (let i = 0; i < length; ++i) { | ||
@@ -1516,11 +1032,7 @@ if ( | ||
} | ||
return false; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.set */ | ||
set(input, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const targetOffset = ToIntegerOrInfinity(opts[0]); | ||
@@ -1530,3 +1042,2 @@ if (targetOffset < 0) { | ||
} | ||
if (input == null) { | ||
@@ -1537,3 +1048,2 @@ throw NativeTypeError( | ||
} | ||
if (isBigIntTypedArray(input)) { | ||
@@ -1544,6 +1054,3 @@ throw NativeTypeError( | ||
} | ||
// for optimization | ||
if (isFloat16Array(input)) { | ||
// peel off Proxy | ||
return TypedArrayPrototypeSet( | ||
@@ -1555,3 +1062,2 @@ getFloat16BitsArray(this), | ||
} | ||
if (isTypedArray(input)) { | ||
@@ -1563,12 +1069,8 @@ const buffer = TypedArrayPrototypeGetBuffer(input); | ||
} | ||
const targetLength = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const src = NativeObject(input); | ||
const srcLength = LengthOfArrayLike(src); | ||
const srcLength = ToLength(src.length); | ||
if (targetOffset === Infinity || srcLength + targetOffset > targetLength) { | ||
throw NativeRangeError(OFFSET_IS_OUT_OF_BOUNDS); | ||
} | ||
for (let i = 0; i < srcLength; ++i) { | ||
@@ -1578,18 +1080,11 @@ float16bitsArray[i + targetOffset] = roundToFloat16Bits(src[i]); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse */ | ||
reverse() { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
TypedArrayPrototypeReverse(float16bitsArray); | ||
return this; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill */ | ||
fill(value, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
TypedArrayPrototypeFill( | ||
@@ -1600,21 +1095,13 @@ float16bitsArray, | ||
); | ||
return this; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin */ | ||
copyWithin(target, start, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
TypedArrayPrototypeCopyWithin(float16bitsArray, target, start, ...toSafe(opts)); | ||
return this; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort */ | ||
sort(...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const compare = opts[0] !== undefined ? opts[0] : defaultCompare; | ||
@@ -1624,14 +1111,8 @@ TypedArrayPrototypeSort(float16bitsArray, (x, y) => { | ||
}); | ||
return this; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice */ | ||
slice(...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const Constructor = SpeciesConstructor(float16bitsArray, Float16Array); | ||
// for optimization | ||
if (Constructor === Float16Array) { | ||
@@ -1649,7 +1130,5 @@ const uint16 = new NativeUint16Array( | ||
} | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
const start = ToIntegerOrInfinity(opts[0]); | ||
const end = opts[1] === undefined ? length : ToIntegerOrInfinity(opts[1]); | ||
let k; | ||
@@ -1663,3 +1142,2 @@ if (start === -Infinity) { | ||
} | ||
let final; | ||
@@ -1673,11 +1151,8 @@ if (end === -Infinity) { | ||
} | ||
const count = final - k > 0 ? final - k : 0; | ||
const array = new Constructor(count); | ||
assertSpeciesTypedArray(array, count); | ||
if (count === 0) { | ||
return array; | ||
} | ||
const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray); | ||
@@ -1687,3 +1162,2 @@ if (IsDetachedBuffer(buffer)) { | ||
} | ||
let n = 0; | ||
@@ -1695,13 +1169,8 @@ while (k < final) { | ||
} | ||
return /** @type {any} */ (array); | ||
return (array); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray */ | ||
subarray(...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const Constructor = SpeciesConstructor(float16bitsArray, Float16Array); | ||
const uint16 = new NativeUint16Array( | ||
@@ -1713,3 +1182,2 @@ TypedArrayPrototypeGetBuffer(float16bitsArray), | ||
const uint16Subarray = TypedArrayPrototypeSubarray(uint16, ...toSafe(opts)); | ||
const array = new Constructor( | ||
@@ -1721,13 +1189,8 @@ TypedArrayPrototypeGetBuffer(uint16Subarray), | ||
assertSpeciesTypedArray(array); | ||
return /** @type {any} */ (array); | ||
return (array); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.indexof */ | ||
indexOf(element, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
let from = ToIntegerOrInfinity(opts[0]); | ||
@@ -1737,3 +1200,2 @@ if (from === Infinity) { | ||
} | ||
if (from < 0) { | ||
@@ -1745,3 +1207,2 @@ from += length; | ||
} | ||
for (let i = from; i < length; ++i) { | ||
@@ -1755,13 +1216,8 @@ if ( | ||
} | ||
return -1; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof */ | ||
lastIndexOf(element, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
let from = opts.length >= 1 ? ToIntegerOrInfinity(opts[0]) : length - 1; | ||
@@ -1771,3 +1227,2 @@ if (from === -Infinity) { | ||
} | ||
if (from >= 0) { | ||
@@ -1778,3 +1233,2 @@ from = from < length - 1 ? from : length - 1; | ||
} | ||
for (let i = from; i >= 0; --i) { | ||
@@ -1788,13 +1242,8 @@ if ( | ||
} | ||
return -1; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes */ | ||
includes(element, ...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const length = TypedArrayPrototypeGetLength(float16bitsArray); | ||
let from = ToIntegerOrInfinity(opts[0]); | ||
@@ -1804,3 +1253,2 @@ if (from === Infinity) { | ||
} | ||
if (from < 0) { | ||
@@ -1812,11 +1260,8 @@ from += length; | ||
} | ||
const isNaN = NumberIsNaN(element); | ||
for (let i = from; i < length; ++i) { | ||
const value = convertToNumber(float16bitsArray[i]); | ||
if (isNaN && NumberIsNaN(value)) { | ||
return true; | ||
} | ||
if (value === element) { | ||
@@ -1826,54 +1271,31 @@ return true; | ||
} | ||
return false; | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.join */ | ||
join(...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const array = copyToArray(float16bitsArray); | ||
return ArrayPrototypeJoin(array, ...toSafe(opts)); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring */ | ||
toLocaleString(...opts) { | ||
assertFloat16Array(this); | ||
const float16bitsArray = getFloat16BitsArray(this); | ||
const array = copyToArray(float16bitsArray); | ||
// @ts-ignore | ||
return ArrayPrototypeToLocaleString(array, ...toSafe(opts)); | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag */ | ||
get [SymbolToStringTag]() { | ||
if (isFloat16Array(this)) { | ||
return /** @type {any} */ ("Float16Array"); | ||
return ("Float16Array"); | ||
} | ||
} | ||
} | ||
/** @see https://tc39.es/ecma262/#sec-typedarray.bytes_per_element */ | ||
ObjectDefineProperty(Float16Array, "BYTES_PER_ELEMENT", { | ||
value: BYTES_PER_ELEMENT, | ||
}); | ||
// limitation: It is peaked by `Object.getOwnPropertySymbols(Float16Array)` and `Reflect.ownKeys(Float16Array)` | ||
ObjectDefineProperty(Float16Array, brand, {}); | ||
/** @see https://tc39.es/ecma262/#sec-properties-of-the-typedarray-constructors */ | ||
ReflectSetPrototypeOf(Float16Array, TypedArray); | ||
const Float16ArrayPrototype = Float16Array.prototype; | ||
/** @see https://tc39.es/ecma262/#sec-typedarray.prototype.bytes_per_element */ | ||
ObjectDefineProperty(Float16ArrayPrototype, "BYTES_PER_ELEMENT", { | ||
value: BYTES_PER_ELEMENT, | ||
}); | ||
/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype-@@iterator */ | ||
ObjectDefineProperty(Float16ArrayPrototype, SymbolIterator, { | ||
@@ -1884,14 +1306,4 @@ value: Float16ArrayPrototype.values, | ||
}); | ||
// To make `new Float16Array() instanceof Uint16Array` returns `false` | ||
ReflectSetPrototypeOf(Float16ArrayPrototype, TypedArrayPrototype); | ||
/** | ||
* returns an unsigned 16-bit float at the specified byte offset from the start of the DataView. | ||
* | ||
* @param {DataView} dataView | ||
* @param {number} byteOffset | ||
* @param {[boolean]} opts | ||
* @returns {number} | ||
*/ | ||
function getFloat16(dataView, byteOffset, ...opts) { | ||
@@ -1902,11 +1314,2 @@ return convertToNumber( | ||
} | ||
/** | ||
* stores an unsigned 16-bit float value at the specified byte offset from the start of the DataView. | ||
* | ||
* @param {DataView} dataView | ||
* @param {number} byteOffset | ||
* @param {number} value | ||
* @param {[boolean]} opts | ||
*/ | ||
function setFloat16(dataView, byteOffset, value, ...opts) { | ||
@@ -1913,0 +1316,0 @@ return DataViewPrototypeSetUint16( |
{ | ||
"name": "@petamoriken/float16", | ||
"version": "3.5.6", | ||
"version": "3.5.7", | ||
"description": "IEEE 754 half-precision floating-point for JavaScript", | ||
@@ -95,2 +95,3 @@ "keywords": [ | ||
"rollup": "^2.58.3", | ||
"rollup-plugin-cleanup": "^3.2.1", | ||
"source-map-support": "^0.5.20" | ||
@@ -97,0 +98,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
215114
23
5689