Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Nov 11, 2021
1 parent 199178d commit 8e5ce04
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,16 @@ export class Float16Array {
TypedArrayPrototypeGetByteOffset(float16bitsArray),
TypedArrayPrototypeGetLength(float16bitsArray)
);
const proxy = new Float16Array(
const cloned = new Float16Array(
TypedArrayPrototypeGetBuffer(
TypedArrayPrototypeSlice(uint16)
)
);
const array = getFloat16BitsArray(proxy);
const array = getFloat16BitsArray(cloned);

array[k] = roundToFloat16Bits(value);

return proxy;
return cloned;
}

/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map */
Expand Down Expand Up @@ -898,17 +898,22 @@ export class Float16Array {
assertFloat16Array(this);
const float16bitsArray = getFloat16BitsArray(this);

const length = TypedArrayPrototypeGetLength(float16bitsArray);

// don't use SpeciesConstructor
const proxy = new Float16Array(length);
const array = getFloat16BitsArray(proxy);
const uint16 = new NativeUint16Array(
TypedArrayPrototypeGetBuffer(float16bitsArray),
TypedArrayPrototypeGetByteOffset(float16bitsArray),
TypedArrayPrototypeGetLength(float16bitsArray)
);
const cloned = new Float16Array(
TypedArrayPrototypeGetBuffer(
TypedArrayPrototypeSlice(uint16)
)
);

for (let i = 0; i < length; ++i) {
array[i] = float16bitsArray[length - 1 - i];
}
const clonedFloat16bitsArray = getFloat16BitsArray(cloned);
TypedArrayPrototypeReverse(clonedFloat16bitsArray);

return proxy;
return cloned;
}

/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill */
Expand Down Expand Up @@ -959,11 +964,19 @@ export class Float16Array {
TypedArrayPrototypeGetByteOffset(float16bitsArray),
TypedArrayPrototypeGetLength(float16bitsArray)
);
return new Float16Array(
const cloned = new Float16Array(
TypedArrayPrototypeGetBuffer(
TypedArrayPrototypeSlice(uint16)
)
).sort(...toSafe(opts));
);

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

return cloned;
}

/** @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice */
Expand Down

0 comments on commit 8e5ce04

Please sign in to comment.