Skip to content

Commit

Permalink
Fix the detection of BigInt TypedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Oct 15, 2021
1 parent 8adf001 commit 47803fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { wrapInArrayIterator } from "./_arrayIterator.mjs";
import { convertToNumber, roundToFloat16Bits } from "./_converter.mjs";
import { LengthOfArrayLike, SpeciesConstructor, ToIntegerOrInfinity, defaultCompare } from "./_spec.mjs";
import { hasOwn } from "./_util/hasOwn.mjs";
import { isArrayBuffer, isCanonicalIntegerIndexString, isIterable, isObject, isObjectLike, isOrdinaryArray, isOrdinaryTypedArray, isSharedArrayBuffer, isTypedArray, isUint16Array } from "./_util/is.mjs";
import { isArrayBuffer, isBigIntTypedArray, isCanonicalIntegerIndexString, isIterable, isObject, isObjectLike, isOrdinaryArray, isOrdinaryTypedArray, isSharedArrayBuffer, isTypedArray, isUint16Array } from "./_util/is.mjs";
import { createPrivateStorage } from "./_util/private.mjs";

const brand = Symbol.for("__Float16Array__");
Expand Down Expand Up @@ -144,6 +144,10 @@ export class Float16Array extends Uint16Array {

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

list = input;
length = input.length;

Expand Down Expand Up @@ -616,6 +620,10 @@ export class Float16Array extends Uint16Array {
throw RangeError("offset is out of bounds");
}

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

// for optimization
if (isFloat16Array(input)) {
// peel off Proxy
Expand Down
9 changes: 9 additions & 0 deletions src/_util/is.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ export function isUint16Array(value) {
return getTypedArrayPrototypeSymbolToStringTag.call(value) === "Uint16Array";
}

/**
* @param {unknown} value
* @returns {value is BigInt64Array|BigUint64Array}
*/
export function isBigIntTypedArray(value) {
const typedArrayName = getTypedArrayPrototypeSymbolToStringTag.call(value);
return typedArrayName === "BigInt64Array" || typedArrayName === "BigUint64Array";
}

/**
* @param {unknown} value
* @returns {value is DataView}
Expand Down
19 changes: 19 additions & 0 deletions test/Float16Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ describe("Float16Array", () => {
assert.equalFloat16ArrayValues( float16_2, checkArray );
});

it("input BigInt TypedArray", function () {
// Safari 13 doesn't have BigInt
if (typeof BigUint64Array === "undefined") {
this.skip();
}

assert.throws(() => new Float16Array(new BigUint64Array()), TypeError);
});

it("input custom Array", () => {
class FooArray extends Array {}
const array = FooArray.from([1, 1.1, 1.2, 1.3]);
Expand Down Expand Up @@ -1140,6 +1149,16 @@ describe("Float16Array", () => {
assert.equalFloat16ArrayValues( float16, [1, 20, 21, 11, 5] );
});

it("set BigInt TypedArray", function () {
// Safari 13 doesn't have BigInt
if (typeof BigUint64Array === "undefined") {
this.skip();
}

const float16 = new Float16Array([1, 2, 3, 4, 5]);
assert.throws(() => float16.set(new BigUint64Array()), TypeError);
});

it("set ArrayLike", () => {
const float16 = new Float16Array([1, 2, 3, 4, 5]);
const arrayLike = { 0: 10, 1: 11, length: 2 };
Expand Down

0 comments on commit 47803fa

Please sign in to comment.