Skip to content

Commit

Permalink
Fix to check the object created by SpeciesConstructor is kind of Ty…
Browse files Browse the repository at this point in the history
…pedArray that `[[ContentType]]` is not a BigInt
  • Loading branch information
petamoriken committed Oct 15, 2021
1 parent 47803fa commit 572fe6e
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ function copyToArray(float16bitsArray) {
return array;
}

/**
* @param {unknown} target
* @throws {TypeError}
*/
function assertSpeciesTypedArray(target) {
if (isFloat16Array(target)) {
return;
}

if (!isTypedArray(target)) {
throw new TypeError("This is not a TypedArray");
}

if (isBigIntTypedArray(target)) {
throw new TypeError("Cannot mix BigInt and other types, use explicit conversions");
}
}

const TypedArrayPrototype = Reflect.getPrototypeOf(Uint8Array).prototype;

const TypedArrayPrototypeGetters = new Set();
Expand Down Expand Up @@ -403,6 +421,7 @@ export class Float16Array extends Uint16Array {
}

const array = new Constructor(length);
assertSpeciesTypedArray(array);

for (let i = 0; i < length; ++i) {
const val = convertToNumber(float16bitsArray[i]);
Expand Down Expand Up @@ -431,6 +450,7 @@ export class Float16Array extends Uint16Array {

const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
const array = new Constructor(kept);
assertSpeciesTypedArray(array);

return array;
}
Expand Down Expand Up @@ -724,6 +744,7 @@ export class Float16Array extends Uint16Array {

const count = final - k > 0 ? final - k : 0;
const array = new Constructor(count);
assertSpeciesTypedArray(array);

if (count === 0) {
return array;
Expand All @@ -746,10 +767,13 @@ export class Float16Array extends Uint16Array {
const float16bitsArray = getFloat16BitsArray(this);

const uint16 = new Uint16Array(float16bitsArray.buffer, float16bitsArray.byteOffset, float16bitsArray.length);
const array = uint16.subarray(...opts);
const subarray = uint16.subarray(...opts);

const Constructor = SpeciesConstructor(float16bitsArray, Float16Array);
return new Constructor(array.buffer, array.byteOffset, array.length);
const array = new Constructor(subarray.buffer, subarray.byteOffset, subarray.length);
assertSpeciesTypedArray(array);

return array;
}

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

0 comments on commit 572fe6e

Please sign in to comment.