Skip to content

Commit

Permalink
.with should convert value up front
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Jun 10, 2022
1 parent 640585a commit cd56857
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Float16Array.mjs
Expand Up @@ -82,6 +82,7 @@ import {
SpeciesConstructor,
ToIntegerOrInfinity,
ToLength,
ToNumber,
defaultCompare,
} from "./_util/spec.mjs";

Expand Down Expand Up @@ -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);
}
Expand All @@ -554,7 +557,7 @@ export class Float16Array {
);
const array = getFloat16BitsArray(cloned);

array[k] = roundToFloat16Bits(value);
array[k] = roundToFloat16Bits(number);

return cloned;
}
Expand Down
15 changes: 12 additions & 3 deletions src/_util/spec.mjs
Expand Up @@ -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;
Expand Down

0 comments on commit cd56857

Please sign in to comment.