@petamoriken/float16
Advanced tools
Comparing version 3.5.4 to 3.5.5
@@ -1,2 +0,2 @@ | ||
/*! @petamoriken/float16 v3.5.4 | MIT License - https://git.io/float16 */ | ||
/*! @petamoriken/float16 v3.5.5 | MIT License - https://git.io/float16 */ | ||
@@ -9,8 +9,10 @@ const float16 = (function (exports) { | ||
const { bind, call } = Function.prototype; | ||
/** @type {(target: Function) => (thisArg: any, ...args: any[]) => any} */ | ||
function uncurryThis(target) { | ||
return (thisArg, ...args) => { | ||
return ReflectApply(target, thisArg, args); | ||
}; | ||
} | ||
/** @type {(target: any) => any} */ | ||
const uncurryThis = bind.bind(call); | ||
/** @type {(target: any, key: string | symbol) => any} */ | ||
/** @type {(target: any, key: string | symbol) => (thisArg: any, ...args: any[]) => any} */ | ||
function uncurryThisGetter(target, key) { | ||
@@ -29,2 +31,3 @@ return uncurryThis( | ||
construct: ReflectConstruct, | ||
defineProperty: ReflectDefineProperty, | ||
get: ReflectGet, | ||
@@ -87,3 +90,4 @@ getOwnPropertyDescriptor: ReflectGetOwnPropertyDescriptor, | ||
const NativeArrayBuffer = ArrayBuffer; | ||
/** @type {(buffer: ArrayBuffer, begin: number, end?: number) => number} */ | ||
const ArrayBufferIsView = NativeArrayBuffer.isView; | ||
/** @type {(buffer: ArrayBuffer, begin?: number, end?: number) => number} */ | ||
const ArrayBufferPrototypeSlice = uncurryThis(NativeArrayBuffer.prototype.slice); | ||
@@ -95,3 +99,3 @@ /** @type {(buffer: ArrayBuffer) => ArrayBuffer} */ | ||
const NativeSharedArrayBuffer = typeof SharedArrayBuffer !== "undefined" ? SharedArrayBuffer : null; | ||
/** @type {(buffer: SharedArrayBuffer, begin: number, end?: number) => number} */ | ||
/** @type {(buffer: SharedArrayBuffer, begin?: number, end?: number) => number} */ | ||
const SharedArrayBufferPrototypeSlice = NativeSharedArrayBuffer | ||
@@ -107,2 +111,3 @@ && uncurryThis(NativeSharedArrayBuffer.prototype.slice); | ||
const TypedArray = ReflectGetPrototypeOf(Uint8Array); | ||
const TypedArrayFrom = TypedArray.from; | ||
const TypedArrayPrototype = TypedArray.prototype; | ||
@@ -140,7 +145,5 @@ /** @type {(typedArray: TypedArray) => IterableIterator<number>} */ | ||
/** @type {((typedArray: TypedArray) => ArrayBuffer)} */ | ||
const TypedArrayPrototypeGetBuffer = uncurryThis( | ||
ReflectGetOwnPropertyDescriptor( | ||
TypedArrayPrototype, | ||
"buffer" | ||
).get | ||
const TypedArrayPrototypeGetBuffer = uncurryThisGetter( | ||
TypedArrayPrototype, | ||
"buffer" | ||
); | ||
@@ -165,3 +168,6 @@ /** @type {((typedArray: TypedArray) => number)} */ | ||
const NativeUint16Array = Uint16Array; | ||
const Uint16ArrayFrom = TypedArray.from.bind(NativeUint16Array); | ||
/** @type {Uint16ArrayConstructor["from"]} */ | ||
const Uint16ArrayFrom = (...args) => { | ||
return ReflectApply(TypedArrayFrom, NativeUint16Array, args); | ||
}; | ||
@@ -180,5 +186,4 @@ // Uint32Array | ||
// Generator | ||
const GeneratorPrototype = (function* () {}).constructor.prototype.prototype; | ||
/** @type {<T = unknown, TReturn = any, TNext = unknown>(generator: Generator<T, TReturn, TNext>, value?: TNext) => T} */ | ||
const GeneratorPrototypeNext = uncurryThis(GeneratorPrototype.next); | ||
const GeneratorPrototypeNext = uncurryThis((function* () {})().next); | ||
@@ -201,2 +206,7 @@ // DataView | ||
// Set | ||
/** | ||
* Do not construct with arguments to avoid calling the "add" method | ||
* | ||
* @type {{new <T = any>(): Set<T>}} | ||
*/ | ||
const NativeSet = Set; | ||
@@ -210,2 +220,7 @@ const SetPrototype = NativeSet.prototype; | ||
// WeakMap | ||
/** | ||
* Do not construct with arguments to avoid calling the "set" method | ||
* | ||
* @type {{new <K extends {}, V>(): WeakMap<K, V>}} | ||
*/ | ||
const NativeWeakMap = WeakMap; | ||
@@ -215,2 +230,4 @@ const WeakMapPrototype = NativeWeakMap.prototype; | ||
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} */ | ||
@@ -337,12 +354,14 @@ const WeakMapPrototypeSet = uncurryThis(WeakMapPrototype.set); | ||
const CONSTRUCTOR_IS_NOT_A_OBJECT = "Constructor is not a object"; | ||
const THIS_IS_NOT_A_OBJECT = "This is not a object"; | ||
const THIS_IS_NOT_A_FLOAT16ARRAY = "This is not a Float16Array"; | ||
const THIS_IS_NOT_AN_OBJECT = "This is not an object"; | ||
const THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT = "This is not a Float16Array object"; | ||
const THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY = | ||
"This constructor is not a subclass of Float16Array"; | ||
const SPECIESCONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY = | ||
"SpeciesConstructor didn't return TypedArray"; | ||
const DERIVED_TYPEDARRAY_CONSTRUCTOR_CREATED_AN_ARRAY_WHICH_WAS_TOO_SMALL = | ||
"Derived TypedArray constructor created an array which was too small"; | ||
const THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED = "This buffer has already been detached"; | ||
const THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT = | ||
"The constructor property value is not an object"; | ||
const SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT = | ||
"Species constructor didn't return TypedArray object"; | ||
const DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH = | ||
"Derived constructor created TypedArray object which was too small length"; | ||
const ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER = | ||
"Attempting to access detached ArrayBuffer"; | ||
const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT = | ||
@@ -359,3 +378,3 @@ "Cannot convert undefined or null to object"; | ||
/** | ||
* returns the nearest half precision float representation of a number. | ||
* returns the nearest half-precision float representation of a number. | ||
* | ||
@@ -586,3 +605,3 @@ * @param {number} num | ||
if (!isObject(arrayLike)) { | ||
throw NativeTypeError(THIS_IS_NOT_A_OBJECT); | ||
throw NativeTypeError(THIS_IS_NOT_AN_OBJECT); | ||
} | ||
@@ -601,3 +620,3 @@ | ||
if (!isObject(target)) { | ||
throw NativeTypeError(THIS_IS_NOT_A_OBJECT); | ||
throw NativeTypeError(THIS_IS_NOT_AN_OBJECT); | ||
} | ||
@@ -610,3 +629,3 @@ | ||
if (!isObject(constructor)) { | ||
throw NativeTypeError(CONSTRUCTOR_IS_NOT_A_OBJECT); | ||
throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT); | ||
} | ||
@@ -624,3 +643,3 @@ | ||
* @see https://tc39.es/ecma262/#sec-isdetachedbuffer | ||
* @param {ArrayBuffer | SharedArrayBuffer} buffer | ||
* @param {ArrayBufferLike} buffer | ||
* @returns {boolean} | ||
@@ -719,3 +738,3 @@ */ | ||
if (!isObject(constructor)) { | ||
throw NativeTypeError(CONSTRUCTOR_IS_NOT_A_OBJECT); | ||
throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT); | ||
} | ||
@@ -731,3 +750,4 @@ | ||
function isFloat16Array(target) { | ||
return hasFloat16ArrayBrand(target) && !isTypedArray(target); | ||
return WeakMapPrototypeHas(float16bitsArrays, target) || | ||
(hasFloat16ArrayBrand(target) && !ArrayBufferIsView(target)); | ||
} | ||
@@ -742,3 +762,3 @@ | ||
if (!isFloat16Array(target)) { | ||
throw NativeTypeError(THIS_IS_NOT_A_FLOAT16ARRAY); | ||
throw NativeTypeError(THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT); | ||
} | ||
@@ -758,3 +778,3 @@ } | ||
if (!isTargetFloat16Array && !isTargetTypedArray) { | ||
throw NativeTypeError(SPECIESCONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY); | ||
throw NativeTypeError(SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT); | ||
} | ||
@@ -773,3 +793,3 @@ | ||
throw NativeTypeError( | ||
DERIVED_TYPEDARRAY_CONSTRUCTOR_CREATED_AN_ARRAY_WHICH_WAS_TOO_SMALL | ||
DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH | ||
); | ||
@@ -786,2 +806,3 @@ } | ||
* @param {Float16Array} float16 | ||
* @throws {TypeError} | ||
* @returns {Uint16Array & { __float16bits: never }} | ||
@@ -794,3 +815,3 @@ */ | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
@@ -800,12 +821,13 @@ return float16bitsArray; | ||
// from another Float16Array instance (a different version?) | ||
// @ts-ignore | ||
if (IsDetachedBuffer(float16.buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
const buffer = float16.buffer; | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
// from another Float16Array instance (a different version?) | ||
const cloned = ReflectConstruct(Float16Array, [ | ||
buffer, | ||
// @ts-ignore | ||
float16.buffer, | ||
// @ts-ignore | ||
float16.byteOffset, | ||
@@ -833,3 +855,4 @@ // @ts-ignore | ||
const TypedArrayPrototypeGetters = new NativeSet(); | ||
/** @type {Set<string | symbol>} */ | ||
const TypedArrayPrototypeGetterKeys = new NativeSet(); | ||
for (const key of ReflectOwnKeys(TypedArrayPrototype)) { | ||
@@ -843,8 +866,7 @@ // @@toStringTag method is defined in Float16Array.prototype | ||
if (ObjectHasOwn(descriptor, "get")) { | ||
SetPrototypeAdd(TypedArrayPrototypeGetters, key); | ||
SetPrototypeAdd(TypedArrayPrototypeGetterKeys, key); | ||
} | ||
} | ||
/** @type {ProxyHandler<Float16Array>} */ | ||
const handler = ObjectFreeze({ | ||
const handler = ObjectFreeze(/** @type {ProxyHandler<Float16Array>} */ ({ | ||
get(target, key, receiver) { | ||
@@ -856,3 +878,3 @@ if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) { | ||
// %TypedArray%.prototype getter properties cannot called by Proxy receiver | ||
if (SetPrototypeHas(TypedArrayPrototypeGetters, key)) { | ||
if (SetPrototypeHas(TypedArrayPrototypeGetterKeys, key)) { | ||
return ReflectGet(target, key); | ||
@@ -871,4 +893,27 @@ } | ||
}, | ||
}); | ||
getOwnPropertyDescriptor(target, key) { | ||
if (isCanonicalIntegerIndexString(key) && ObjectHasOwn(target, key)) { | ||
const descriptor = ReflectGetOwnPropertyDescriptor(target, key); | ||
descriptor.value = convertToNumber(descriptor.value); | ||
return descriptor; | ||
} | ||
return ReflectGetOwnPropertyDescriptor(target, key); | ||
}, | ||
defineProperty(target, key, descriptor) { | ||
if ( | ||
isCanonicalIntegerIndexString(key) && | ||
ObjectHasOwn(target, key) && | ||
ObjectHasOwn(descriptor, "value") | ||
) { | ||
descriptor.value = roundToFloat16Bits(descriptor.value); | ||
return ReflectDefineProperty(target, key, descriptor); | ||
} | ||
return ReflectDefineProperty(target, key, descriptor); | ||
}, | ||
})); | ||
class Float16Array { | ||
@@ -902,3 +947,3 @@ /** @see https://tc39.es/ecma262/#sec-typedarray */ | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
@@ -1416,3 +1461,3 @@ | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
@@ -1535,3 +1580,3 @@ } | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER); | ||
} | ||
@@ -1538,0 +1583,0 @@ |
@@ -465,5 +465,5 @@ /** | ||
/** | ||
* Returns the nearest half precision float representation of a number. | ||
* Returns the nearest half-precision float representation of a number. | ||
* @param x A numeric expression. | ||
*/ | ||
export declare function hfround(x: number): number; |
{ | ||
"name": "@petamoriken/float16", | ||
"version": "3.5.4", | ||
"description": "half precision floating point for JavaScript", | ||
"version": "3.5.5", | ||
"description": "IEEE 754 half-precision floating-point for JavaScript", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "float16", |
# <a href="https://git.io/float16">float16</a> | ||
<p align="center"> | ||
half precision floating point for JavaScript<br> | ||
IEEE 754 half-precision floating-point for JavaScript<br> | ||
See <a href="https://esdiscuss.org/topic/float16array">the archive of the ES Discuss Float16Array topic</a> for details | ||
@@ -142,3 +142,3 @@ </p> | ||
**This package only uses up to ES2015 features** and does not use | ||
**This package only requires ES2015 features** and does not use | ||
environment-dependent features (except for `inspect/`), so you can use it | ||
@@ -148,3 +148,4 @@ without any problems. It works fine with | ||
`Float16Array` implemented by the `Proxy`, so IE11 is never supported. | ||
`Float16Array` implemented by `Proxy` and `Reflect`, so IE11 is never supported | ||
even if you use polyfills. | ||
@@ -194,3 +195,3 @@ ### Pre-transpiled JavaScript files (CommonJS, IIFE) | ||
### `DataView` | ||
### `getFloat16`, `setFloat16` | ||
@@ -229,3 +230,3 @@ `getFloat16` and `setFloat16` are similar to `DataView` methods such as | ||
([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround)). | ||
This function returns nearest half precision float representation of a number. | ||
This function returns nearest half-precision float representation of a number. | ||
@@ -352,3 +353,3 @@ ```ts | ||
<details> | ||
<summary>Manual build and test:</summary> | ||
<summary>Manual build and test</summary> | ||
@@ -355,0 +356,0 @@ ### Manual build |
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
248932
6520
409