@petamoriken/float16
Advanced tools
Comparing version 3.5.3 to 3.5.4
@@ -1,2 +0,2 @@ | ||
/*! @petamoriken/float16 v3.5.3 | MIT License - https://git.io/float16 */ | ||
/*! @petamoriken/float16 v3.5.4 | MIT License - https://git.io/float16 */ | ||
@@ -7,2 +7,3 @@ const float16 = (function (exports) { | ||
/* eslint-disable no-restricted-globals */ | ||
/* global SharedArrayBuffer */ | ||
@@ -85,3 +86,16 @@ const { bind, call } = Function.prototype; | ||
const NativeArrayBuffer = ArrayBuffer; | ||
/** @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 | ||
@@ -323,2 +337,3 @@ /** @typedef {Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array} TypedArray */ | ||
"Derived TypedArray constructor created an array which was too small"; | ||
const THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED = "This buffer has already been detached"; | ||
const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT = | ||
@@ -426,3 +441,8 @@ "Cannot convert undefined or null to object"; | ||
function isArrayBuffer(value) { | ||
return isObjectLike(value) && value[SymbolToStringTag] === "ArrayBuffer"; | ||
try { | ||
ArrayBufferPrototypeGetByteLength(/** @type {any} */ (value)); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
@@ -435,4 +455,12 @@ | ||
function isSharedArrayBuffer(value) { | ||
return isObjectLike(value) && | ||
value[SymbolToStringTag] === "SharedArrayBuffer"; | ||
if (NativeSharedArrayBuffer === null) { | ||
return false; | ||
} | ||
try { | ||
SharedArrayBufferPrototypeGetByteLength(/** @type {any} */ (value)); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
@@ -584,2 +612,23 @@ | ||
/** | ||
* @see https://tc39.es/ecma262/#sec-isdetachedbuffer | ||
* @param {ArrayBuffer | SharedArrayBuffer} buffer | ||
* @returns {boolean} | ||
*/ | ||
function IsDetachedBuffer(buffer) { | ||
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 */} | ||
} | ||
return true; | ||
} | ||
/** | ||
* bigint comparisons are not supported | ||
@@ -637,3 +686,3 @@ * | ||
/** @type {WeakMap<Float16Array, Uint16Array & { __float16bits: never }>} */ | ||
const targets = new NativeWeakMap(); | ||
const float16bitsArrays = new NativeWeakMap(); | ||
@@ -725,7 +774,16 @@ /** | ||
function getFloat16BitsArray(float16) { | ||
const target = WeakMapPrototypeGet(targets, float16); | ||
if (target !== undefined) { | ||
return target; | ||
const float16bitsArray = WeakMapPrototypeGet(float16bitsArrays, float16); | ||
if (float16bitsArray !== undefined) { | ||
const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray); | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
} | ||
return float16bitsArray; | ||
} | ||
// @ts-ignore | ||
if (IsDetachedBuffer(float16.buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
} | ||
// from another Float16Array instance (a different version?) | ||
@@ -740,3 +798,3 @@ const cloned = ReflectConstruct(Float16Array, [ | ||
], float16.constructor); | ||
return WeakMapPrototypeGet(targets, cloned); | ||
return WeakMapPrototypeGet(float16bitsArrays, cloned); | ||
} | ||
@@ -812,8 +870,2 @@ | ||
if (isTypedArray(input)) { // TypedArray | ||
if (isBigIntTypedArray(input)) { | ||
throw NativeTypeError( | ||
CANNOT_MIX_BIGINT_AND_OTHER_TYPES | ||
); | ||
} | ||
list = input; | ||
@@ -829,2 +881,11 @@ length = TypedArrayPrototypeGetLength(input); | ||
: NativeArrayBuffer; | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
} | ||
if (isBigIntTypedArray(input)) { | ||
throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES); | ||
} | ||
const data = new BufferConstructor( | ||
@@ -862,3 +923,3 @@ length * BYTES_PER_ELEMENT | ||
// proxy private storage | ||
WeakMapPrototypeSet(targets, proxy, float16bitsArray); | ||
WeakMapPrototypeSet(float16bitsArrays, proxy, float16bitsArray); | ||
@@ -1334,2 +1395,9 @@ return proxy; | ||
if (isTypedArray(input)) { | ||
const buffer = TypedArrayPrototypeGetBuffer(input); | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
} | ||
} | ||
const targetLength = TypedArrayPrototypeGetLength(float16bitsArray); | ||
@@ -1447,2 +1515,7 @@ | ||
const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray); | ||
if (IsDetachedBuffer(buffer)) { | ||
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED); | ||
} | ||
let n = 0; | ||
@@ -1449,0 +1522,0 @@ while (k < final) { |
{ | ||
"name": "@petamoriken/float16", | ||
"version": "3.5.3", | ||
"version": "3.5.4", | ||
"description": "half precision floating point for JavaScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
241757
6355