Skip to content

Commit

Permalink
Fix: Float16Array#{toSorted, toSpliced}.length
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Jun 26, 2022
1 parent 6635294 commit 87fed88
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
24 changes: 10 additions & 14 deletions src/Float16Array.mjs
Expand Up @@ -28,7 +28,6 @@ import {
ArrayBufferIsView,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeToLocaleString,
NativeArrayBuffer,
NativeNumber,
Expand Down Expand Up @@ -938,7 +937,7 @@ export class Float16Array {
}

/** @see https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSorted */
toSorted(...opts) {
toSorted(compareFn) {
assertFloat16Array(this);
const float16bitsArray = getFloat16BitsArray(this);

Expand All @@ -955,9 +954,9 @@ export class Float16Array {
);

const clonedFloat16bitsArray = getFloat16BitsArray(cloned);
const compare = opts[0] !== undefined ? opts[0] : defaultCompare;
const sortCompare = compareFn !== undefined ? compareFn : defaultCompare;
TypedArrayPrototypeSort(clonedFloat16bitsArray, (x, y) => {
return compare(convertToNumber(x), convertToNumber(y));
return sortCompare(convertToNumber(x), convertToNumber(y));
});

return cloned;
Expand Down Expand Up @@ -1030,12 +1029,12 @@ export class Float16Array {
}

/** @see https://tc39.es/proposal-change-array-by-copy/#sec-%typedarray%.prototype.toSpliced */
toSpliced(...opts) {
toSpliced(start, deleteCount, ...items) {
assertFloat16Array(this);
const float16bitsArray = getFloat16BitsArray(this);

const length = TypedArrayPrototypeGetLength(float16bitsArray);
const relativeStart = ToIntegerOrInfinity(opts[0]);
const relativeStart = ToIntegerOrInfinity(start);

let actualStart;
if (relativeStart === -Infinity) {
Expand All @@ -1046,21 +1045,19 @@ export class Float16Array {
actualStart = relativeStart < length ? relativeStart : length;
}

let insertCount, actualDeleteCount;
switch (opts.length) {
const insertCount = items.length;
let actualDeleteCount;
switch (arguments.length) {
case 0:
insertCount = 0;
actualDeleteCount = 0;
break;

case 1:
insertCount = 0;
actualDeleteCount = length - actualStart;
break;

default: {
const dc = ToIntegerOrInfinity(opts[1]);
insertCount = opts.length - 2;
const dc = ToIntegerOrInfinity(deleteCount);
if (dc < 0) {
actualDeleteCount = 0;
} else if (dc > length - actualStart) {
Expand All @@ -1085,8 +1082,7 @@ export class Float16Array {
++k;
}

const items = ArrayPrototypeSlice(opts, 2);
for (let i = 0, l = items.length; i < l; ++i) {
for (let i = 0; i < insertCount; ++i) {
array[k] = roundToFloat16Bits(items[i]);
++k;
}
Expand Down
2 changes: 0 additions & 2 deletions src/_util/primordials.mjs
Expand Up @@ -95,8 +95,6 @@ const ArrayPrototype = NativeArray.prototype;
export const ArrayPrototypeJoin = uncurryThis(ArrayPrototype.join);
/** @type {<T>(array: T[], ...items: T[]) => number} */
export const ArrayPrototypePush = uncurryThis(ArrayPrototype.push);
/** @type {<T>(array: T[], start?: number, end?: number) => T[]} */
export const ArrayPrototypeSlice = uncurryThis(ArrayPrototype.slice);
/** @type {(array: ArrayLike<unknown>, ...opts: any[]) => string} */
export const ArrayPrototypeToLocaleString = uncurryThis(
ArrayPrototype.toLocaleString
Expand Down
8 changes: 4 additions & 4 deletions test/Float16Array.js
Expand Up @@ -1616,8 +1616,8 @@ describe("Float16Array", () => {
assert(Float16Array.prototype.toSorted.name === "toSorted");
});

it("property `length` is 0", () => {
assert(Float16Array.prototype.toSorted.length === 0);
it("property `length` is 1", () => {
assert(Float16Array.prototype.toSorted.length === 1);
});

it("check default compare", () => {
Expand Down Expand Up @@ -1814,8 +1814,8 @@ describe("Float16Array", () => {
assert(Float16Array.prototype.toSpliced.name === "toSpliced");
});

it("property `length` is 0", () => {
assert(Float16Array.prototype.toSpliced.length === 0);
it("property `length` is 2", () => {
assert(Float16Array.prototype.toSpliced.length === 2);
});

it("get spliced Array", () => {
Expand Down

0 comments on commit 87fed88

Please sign in to comment.