From 6635294b887fe01f06d5f1bcffe25184bd1e51b0 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Sat, 11 Jun 2022 02:42:43 +0900 Subject: [PATCH] `.with` should convert `value` up front https://github.com/tc39/proposal-change-array-by-copy/pull/86 --- src/Float16Array.mjs | 5 ++++- src/_util/spec.mjs | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Float16Array.mjs b/src/Float16Array.mjs index 35b0980c..f67c5149 100644 --- a/src/Float16Array.mjs +++ b/src/Float16Array.mjs @@ -82,6 +82,7 @@ import { SpeciesConstructor, ToIntegerOrInfinity, ToLength, + ToNumber, defaultCompare, } from "./_util/spec.mjs"; @@ -537,6 +538,8 @@ export class Float16Array { const relativeIndex = ToIntegerOrInfinity(index); const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex; + const number = ToNumber(value); + if (k < 0 || k >= length) { throw new NativeRangeError(OFFSET_IS_OUT_OF_BOUNDS); } @@ -554,7 +557,7 @@ export class Float16Array { ); const array = getFloat16BitsArray(cloned); - array[k] = roundToFloat16Bits(value); + array[k] = roundToFloat16Bits(number); return cloned; } diff --git a/src/_util/spec.mjs b/src/_util/spec.mjs index d7289523..1b71f7a5 100644 --- a/src/_util/spec.mjs +++ b/src/_util/spec.mjs @@ -17,16 +17,25 @@ import { const MAX_SAFE_INTEGER = NativeNumber.MAX_SAFE_INTEGER; /** - * @see https://tc39.es/ecma262/#sec-tointegerorinfinity + * @see https://tc39.es/ecma262/#sec-tonumber * @param {unknown} target * @returns {number} */ -export function ToIntegerOrInfinity(target) { +export function ToNumber(target) { if (typeof target === "bigint") { throw NativeTypeError(CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER); } - const number = NativeNumber(target); + return NativeNumber(target); +} + +/** + * @see https://tc39.es/ecma262/#sec-tointegerorinfinity + * @param {unknown} target + * @returns {number} + */ +export function ToIntegerOrInfinity(target) { + const number = ToNumber(target); if (NumberIsNaN(number) || number === 0) { return 0;