Skip to content

Commit

Permalink
Fix: Add value type check for @@iterator property
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Nov 11, 2021
1 parent b7a707a commit 7dcfd3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
17 changes: 14 additions & 3 deletions src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
isArrayBuffer,
isBigIntTypedArray,
isCanonicalIntegerIndexString,
isIterable,
isObject,
isObjectLike,
isOrdinaryArray,
Expand All @@ -17,6 +16,7 @@ import {
CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT,
CANNOT_MIX_BIGINT_AND_OTHER_TYPES,
DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH,
ITERATOR_PROPERTY_IS_NOT_CALLABLE,
OFFSET_IS_OUT_OF_BOUNDS,
REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE,
SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT,
Expand Down Expand Up @@ -324,12 +324,18 @@ export class Float16Array {
);
float16bitsArray = ReflectConstruct(NativeUint16Array, [data], new.target);
} else {
if (isIterable(input)) { // Iterable (Array)
const iterator = input[SymbolIterator];
if (iterator != null && typeof iterator !== "function") {
throw NativeTypeError(ITERATOR_PROPERTY_IS_NOT_CALLABLE);
}

if (iterator != null) { // Iterable (Array)
// for optimization
if (isOrdinaryArray(input)) {
list = input;
length = input.length;
} else {
// @ts-ignore
// eslint-disable-next-line no-restricted-syntax
list = [...input];
length = list.length;
Expand Down Expand Up @@ -412,7 +418,12 @@ export class Float16Array {
/** @type {number} */
let length;

if (isIterable(src)) { // Iterable (TypedArray, Array)
const iterator = src[SymbolIterator];
if (iterator != null && typeof iterator !== "function") {
throw NativeTypeError(ITERATOR_PROPERTY_IS_NOT_CALLABLE);
}

if (iterator != null) { // Iterable (TypedArray, Array)
// for optimization
if (isOrdinaryArray(src)) {
list = src;
Expand Down
8 changes: 0 additions & 8 deletions src/_util/is.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ export function isSharedArrayBuffer(value) {
}
}

/**
* @param {unknown} value
* @returns {value is Iterable<unknown>}
*/
export function isIterable(value) {
return value != null && typeof value[SymbolIterator] === "function";
}

/**
* @param {unknown} value
* @returns {value is unknown[]}
Expand Down
1 change: 1 addition & 0 deletions src/_util/messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER =
"Cannot convert a BigInt value to a number";
export const CANNOT_MIX_BIGINT_AND_OTHER_TYPES =
"Cannot mix BigInt and other types, use explicit conversions";
export const ITERATOR_PROPERTY_IS_NOT_CALLABLE = "@@iterator property is not callable";
export const REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE =
"Reduce of empty array with no initial value";
export const OFFSET_IS_OUT_OF_BOUNDS = "Offset is out of bounds";

0 comments on commit 7dcfd3f

Please sign in to comment.