diff --git a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml index 4a281a2a665ff68dbf7425d5f6ae1ebdbba79254..89b23d3e5ef66b2aa9c285b10b318c4e2d5a9301 100644 --- a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml +++ b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml @@ -4048,6 +4048,17 @@ intrinsics: - i32\[ impl: ark::ets::intrinsics::EtsEscompatBigInt64ArrayOfInt + - name: BigInt64ArrayOfBigInt + space: ets + class_name: escompat.BigInt64Array + method_name: ofBigInt + static: false + signature: + ret: void + args: + - std.core.Object[] + impl: ark::ets::intrinsics::EtsEscompatBigInt64ArrayOfBigInt + - name: BigInt64ArrayOfLong space: ets class_name: escompat.BigInt64Array @@ -4268,6 +4279,17 @@ intrinsics: - i64\[ impl: ark::ets::intrinsics::EtsEscompatBigUint64ArrayOfLong + - name: BigUint64ArrayOfBigInt + space: ets + class_name: escompat.BigUint64Array + method_name: ofBigInt + static: false + signature: + ret: void + args: + - std.core.Object[] + impl: ark::ets::intrinsics::EtsEscompatBigUint64ArrayOfBigInt + - name: Uint8ClampedArrayToUint8Clamped space: ets class_name: escompat.Uint8ClampedArray diff --git a/static_core/plugins/ets/runtime/intrinsics/escompat_TypedArrays.cpp b/static_core/plugins/ets/runtime/intrinsics/escompat_TypedArrays.cpp index 79059cea65e36400a50c5e0574cba31e08adcb62..94563ad9fad999a42d5582a665383824b0007095 100644 --- a/static_core/plugins/ets/runtime/intrinsics/escompat_TypedArrays.cpp +++ b/static_core/plugins/ets/runtime/intrinsics/escompat_TypedArrays.cpp @@ -223,6 +223,26 @@ namespace { namespace unbox { +constexpr uint64_t LONG_SHIFT = 32U; +constexpr uint64_t LONG_MASK = (uint64_t {1} << LONG_SHIFT) - uint64_t {1U}; + +[[nodiscard]] EtsLong GetLong(const EtsBigInt *bigint) +{ + const auto *bytes = bigint->GetBytes(); + ASSERT(bytes != nullptr); + auto buff = EtsLong {0}; + const auto len = std::min(uint32_t {2U}, static_cast(bytes->GetLength())); + for (uint32_t i = 0; i < len; ++i) { + buff += static_cast((static_cast(bytes->Get(i)) & LONG_MASK) << i * LONG_SHIFT); + } + return bigint->GetSign() * buff; +} + +[[nodiscard]] EtsUlong GetULong(const EtsBigInt *bigint) +{ + return static_cast(GetLong(bigint)); +} + template bool CheckCastedClass(Cond cond, const EtsClass *expectedClass, const EtsClass *objectClass) { @@ -339,26 +359,6 @@ public: } private: - static constexpr uint64_t LONG_SHIFT = 32U; - static constexpr uint64_t LONG_MASK = (uint64_t {1} << LONG_SHIFT) - uint64_t {1U}; - - [[nodiscard]] static EtsLong GetLong(const EtsBigInt *bigint) - { - const auto *bytes = bigint->GetBytes(); - ASSERT(bytes != nullptr); - auto buff = EtsLong {0}; - const auto len = std::min(uint32_t {2U}, static_cast(bytes->GetLength())); - for (uint32_t i = 0; i < len; ++i) { - buff += static_cast((static_cast(bytes->Get(i)) & LONG_MASK) << i * LONG_SHIFT); - } - return bigint->GetSign() * buff; - } - - [[nodiscard]] static EtsUlong GetULong(const EtsBigInt *bigint) - { - return static_cast(GetLong(bigint)); - } - const EtsBigInt *Cast(EtsObject *object) const { if (CheckCastedClass([](const EtsClass *klass) { return klass->IsBigInt(); }, escompatBigIntClass_, @@ -2174,6 +2174,29 @@ extern "C" void EtsEscompatBigInt64ArrayOfInt(ark::ets::EtsEscompatBigInt64Array EtsEscompatArrayOfImpl(thisArray, srcAddress); } +template +void EtsEscompatArrayOfBigIntImpl(T *thisArray, EtsTypedObjectArray *src, ToLong toLong) +{ + auto *arrayPtr = GetNativeData(thisArray); + if (UNLIKELY(arrayPtr == nullptr)) { + return; + } + + using ElementType = typename T::ElementType; + auto *dst = reinterpret_cast(ToUintPtr(arrayPtr) + static_cast(thisArray->GetByteOffset())); + ASSERT(thisArray->GetLengthInt() >= 0 && static_cast(thisArray->GetLengthInt()) >= src->GetLength()); + std::generate_n(dst, src->GetLength(), [idx = 0, src, toLong]() mutable { return toLong(src->Get(idx++)); }); +} + +extern "C" void EtsEscompatBigInt64ArrayOfBigInt(ark::ets::EtsEscompatBigInt64Array *thisArray, ark::ObjectHeader *src) +{ + // The method fills the typed array from a FixedArray and in contrast to + // EtsEscompatBigInt64ArraySetValuesFromArray, which fills a typed array from escompat.Array, we can be + // sure that `src` is an instance of the EtsTypedObjectArray class. + EtsEscompatArrayOfBigIntImpl(thisArray, EtsTypedObjectArray::FromCoreType(src), + [](EtsBigInt *bigint) { return unbox::GetLong(bigint); }); +} + extern "C" void EtsEscompatBigInt64ArrayOfLong(ark::ets::EtsEscompatBigInt64Array *thisArray, EtsCharArray *src) { auto *srcAddress = reinterpret_cast(ToUintPtr(src->GetCoreType()->GetData())); @@ -2295,4 +2318,14 @@ extern "C" void EtsEscompatBigUint64ArrayOfLong(ark::ets::EtsEscompatBigUInt64Ar auto *srcAddress = reinterpret_cast(ToUintPtr(src->GetCoreType()->GetData())); EtsEscompatArrayOfImpl(thisArray, srcAddress); } + +extern "C" void EtsEscompatBigUint64ArrayOfBigInt(ark::ets::EtsEscompatBigUInt64Array *thisArray, + ark::ObjectHeader *src) +{ + // The method fills the typed array from a FixedArray and in contrast to + // EtsEscompatBigUint64ArraySetValuesFromArray, which fills a typed array from escompat.Array, we can be + // sure that `src` is an instance of the EtsTypedObjectArray class. + EtsEscompatArrayOfBigIntImpl(thisArray, EtsTypedObjectArray::FromCoreType(src), + [](EtsBigInt *bigint) { return unbox::GetULong(bigint); }); +} } // namespace ark::ets::intrinsics diff --git a/static_core/plugins/ets/stdlib/escompat/TypedArrays.ets b/static_core/plugins/ets/stdlib/escompat/TypedArrays.ets index ad82424461e4effa832572647fd1f721e778fd95..bfd8e93dcba8bc2165be4759a1db6c1517a81efe 100644 --- a/static_core/plugins/ets/stdlib/escompat/TypedArrays.ets +++ b/static_core/plugins/ets/stdlib/escompat/TypedArrays.ets @@ -662,6 +662,19 @@ export final class Int8Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Int8Array + */ + public static fromFixedArrayint(arr: FixedArray): Int8Array { + let result = new Int8Array(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -674,7 +687,7 @@ export final class Int8Array implements Iterable, ArrayLike { public static fromArrayint(arr: Array): Int8Array { let result = new Int8Array(arr.length) result.set(arr) - return result; + return result } /** @@ -757,7 +770,8 @@ export final class Int8Array implements Iterable, ArrayLike { public static overload from { fromSetint, fromInt8Array ,fromUint8Array, - fromArrayint, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArrayint, fromArrayint, fromArrayLikeNumber, + fromArrayLike, fromIterable }; /** @@ -2163,6 +2177,19 @@ export final class Int16Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Int16Array + */ + public static fromFixedArrayint(arr: FixedArray): Int16Array { + let result = new Int16Array(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -2175,7 +2202,7 @@ export final class Int16Array implements Iterable, ArrayLike { public static fromArrayint(arr: Array): Int16Array { let result = new Int16Array(arr.length) result.set(arr) - return result; + return result } /** @@ -2258,7 +2285,8 @@ export final class Int16Array implements Iterable, ArrayLike { public static overload from { fromSetint, fromInt16Array ,fromUint16Array, - fromArrayint, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArrayint, fromArrayint, fromArrayLikeNumber, + fromArrayLike, fromIterable }; /** @@ -3641,6 +3669,19 @@ export final class Int32Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Int32Array + */ + public static fromFixedArrayint(arr: FixedArray): Int32Array { + let result = new Int32Array(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -3653,7 +3694,7 @@ export final class Int32Array implements Iterable, ArrayLike { public static fromArrayint(arr: Array): Int32Array { let result = new Int32Array(arr.length) result.set(arr) - return result; + return result } /** @@ -3736,7 +3777,8 @@ export final class Int32Array implements Iterable, ArrayLike { public static overload from { fromSetint, fromInt32Array, - fromArrayint, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArrayint, fromArrayint, fromArrayLikeNumber, + fromArrayLike, fromIterable }; /** @@ -5073,15 +5115,14 @@ export final class BigInt64Array implements Iterable, ArrayLike */ public static of(...items: FixedArray): BigInt64Array { let res = new BigInt64Array(items.length.toInt()) - for (let i: int = 0; i < items.length; i++) { - res.setUnsafe(i, items[i].getLong()) - } + res.ofBigInt(items) return res } private final native ofInt(items: FixedArray): void private final native ofNumber(items: FixedArray): void private final native ofLong(items: FixedArray): void + private final native ofBigInt(items: FixedArray): void /** * Returns a new array from a set of elements. @@ -5139,6 +5180,19 @@ export final class BigInt64Array implements Iterable, ArrayLike return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new BigInt64Array + */ + public static fromFixedArrayBigInt(arr: FixedArray): BigInt64Array { + let result = new BigInt64Array(arr.length) + result.ofBigInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -5151,7 +5205,7 @@ export final class BigInt64Array implements Iterable, ArrayLike public static fromArrayBigInt(arr: Array): BigInt64Array { let result = new BigInt64Array(arr.length) result.set(arr) - return result; + return result } /** @@ -5233,7 +5287,8 @@ export final class BigInt64Array implements Iterable, ArrayLike public static overload from { fromSetBigInt, fromBigInt64Array, - fromArrayBigInt, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArrayBigInt, fromArrayBigInt, fromArrayLikeNumber, + fromArrayLike, fromIterable }; /** @@ -6606,6 +6661,19 @@ export final class Float32Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Float32Array + */ + public static fromFixedArraynumber(arr: FixedArray): Float32Array { + let result = new Float32Array(arr.length) + result.ofNumber(arr) + return result + } + private native final set(array: Array): void; /** @@ -6618,7 +6686,7 @@ export final class Float32Array implements Iterable, ArrayLike { public static fromArraynumber(arr: Array): Float32Array { let result = new Float32Array(arr.length) result.set(arr) - return result; + return result } /** @@ -6700,7 +6768,8 @@ export final class Float32Array implements Iterable, ArrayLike { public static overload from { fromSetnumber, fromFloat32Array, - fromArraynumber, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArraynumber, fromArraynumber, fromArrayLikeNumber, + fromArrayLike, fromIterable }; private final native containsNaN(fromIndex: int): boolean; @@ -8013,6 +8082,19 @@ export final class Float64Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Float64Array + */ + public static fromFixedArraynumber(arr: FixedArray): Float64Array { + let result = new Float64Array(arr.length) + result.ofNumber(arr) + return result + } + private native final set(array: Array): void; /** @@ -8025,7 +8107,7 @@ export final class Float64Array implements Iterable, ArrayLike { public static fromArraynumber(arr: Array): Float64Array { let result = new Float64Array(arr.length) result.set(arr) - return result; + return result } /** @@ -8107,7 +8189,8 @@ export final class Float64Array implements Iterable, ArrayLike { public static overload from { fromSetnumber, fromFloat64Array, - fromArraynumber, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArraynumber, fromArraynumber, fromArrayLikeNumber, + fromArrayLike, fromIterable }; private final native containsNaN(fromIndex: int): boolean; diff --git a/static_core/plugins/ets/stdlib/escompat/TypedUArrays.ets b/static_core/plugins/ets/stdlib/escompat/TypedUArrays.ets index e797811ef82da30072b2341682c96074e5d45078..ea01a9cd4f3499cbd12813d969316e599f7ac4c2 100644 --- a/static_core/plugins/ets/stdlib/escompat/TypedUArrays.ets +++ b/static_core/plugins/ets/stdlib/escompat/TypedUArrays.ets @@ -654,6 +654,19 @@ export final class Uint8ClampedArray implements Iterable, ArrayLike. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Uint8ClampedArray + */ + public static fromFixedArrayint(arr: FixedArray): Uint8ClampedArray { + let result = new Uint8ClampedArray(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -666,7 +679,7 @@ export final class Uint8ClampedArray implements Iterable, ArrayLike): Uint8ClampedArray { let result = new Uint8ClampedArray(arr.length) result.set(arr) - return result; + return result } /** @@ -746,7 +759,8 @@ export final class Uint8ClampedArray implements Iterable, ArrayLike, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Uint8Array + */ + public static fromFixedArrayint(arr: FixedArray): Uint8Array { + let result = new Uint8Array(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -2173,7 +2200,7 @@ export final class Uint8Array implements Iterable, ArrayLike { public static fromArrayint(arr: Array): Uint8Array { let result = new Uint8Array(arr.length) result.set(arr) - return result; + return result } /** @@ -2253,7 +2280,8 @@ export final class Uint8Array implements Iterable, ArrayLike { public static overload from { fromSetint, fromUint8Array ,fromInt8Array - ,fromArrayint, fromArrayLikenumber, fromArrayLike, fromIterable + ,fromFixedArrayint, fromArrayint, fromArrayLikenumber, + fromArrayLike, fromIterable }; /** @@ -3652,6 +3680,19 @@ export final class Uint16Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Uint16Array + */ + public static fromFixedArrayint(arr: FixedArray): Uint16Array { + let result = new Uint16Array(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -3664,7 +3705,7 @@ export final class Uint16Array implements Iterable, ArrayLike { public static fromArrayint(arr: Array): Uint16Array { let result = new Uint16Array(arr.length) result.set(arr) - return result; + return result } /** @@ -3744,7 +3785,8 @@ export final class Uint16Array implements Iterable, ArrayLike { public static overload from { fromSetint, fromUint16Array ,fromInt16Array - ,fromArrayint, fromArrayLikenumber, fromArrayLike, fromIterable + ,fromFixedArrayint, fromArrayint, fromArrayLikenumber, + fromArrayLike, fromIterable }; /** @@ -5177,6 +5219,19 @@ export final class Uint32Array implements Iterable, ArrayLike { return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new Uint32Array + */ + public static fromFixedArrayint(arr: FixedArray): Uint32Array { + let result = new Uint32Array(arr.length) + result.ofInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -5189,7 +5244,7 @@ export final class Uint32Array implements Iterable, ArrayLike { public static fromArrayint(arr: Array): Uint32Array { let result = new Uint32Array(arr.length) result.set(arr) - return result; + return result } /** @@ -5269,7 +5324,8 @@ export final class Uint32Array implements Iterable, ArrayLike { public static overload from { fromSetint, fromUint32Array ,fromInt32Array - ,fromArrayint, fromArrayLikenumber, fromArrayLike, fromIterable + ,fromFixedArrayint, fromArrayint, fromArrayLikenumber, + fromArrayLike, fromIterable }; /** @@ -6626,15 +6682,14 @@ export final class BigUint64Array implements Iterable, ArrayLike */ public static of(...items: FixedArray): BigUint64Array { let res = new BigUint64Array(items.length.toInt()) - for (let i: int = 0; i < items.length; i++) { - res.setUnsafeClamp(i, items[i].getULong()) - } + res.ofBigInt(items) return res } private final native ofInt(items: FixedArray): void private final native ofNumber(items: FixedArray): void private final native ofLong(items: FixedArray): void + private final native ofBigInt(items: FixedArray): void /** * Returns a new array from a set of elements. @@ -6692,6 +6747,19 @@ export final class BigUint64Array implements Iterable, ArrayLike return result } + /** + * Creates an array from an object of FixedArray. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new BigUint64Array + */ + public static fromFixedArrayBigInt(arr: FixedArray): BigUint64Array { + let result = new BigUint64Array(arr.length) + result.ofBigInt(arr) + return result + } + private native final set(array: Array): void; /** @@ -6704,7 +6772,7 @@ export final class BigUint64Array implements Iterable, ArrayLike public static fromArrayBigInt(arr: Array): BigUint64Array { let result = new BigUint64Array(arr.length) result.set(arr) - return result; + return result } /** @@ -6784,7 +6852,8 @@ export final class BigUint64Array implements Iterable, ArrayLike public static overload from { fromSetBigInt, fromBigUint64Array - ,fromArrayBigInt, fromArrayLikeBigInt, fromArrayLike, fromIterable + ,fromFixedArrayBigInt, fromArrayBigInt, fromArrayLikeBigInt, + fromArrayLike, fromIterable }; /** diff --git a/static_core/plugins/ets/templates/stdlib/typedArray.ets.j2 b/static_core/plugins/ets/templates/stdlib/typedArray.ets.j2 index 75d75a966cc1b026d46ce27717d141c3158920f9..c43bda5616d5933a78c6d0961491b227c419f775 100644 --- a/static_core/plugins/ets/templates/stdlib/typedArray.ets.j2 +++ b/static_core/plugins/ets/templates/stdlib/typedArray.ets.j2 @@ -737,9 +737,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}>, ArrayLi {%- elif argType == 'float' %} res.ofFloat(items) {%- elif argType == 'bigint' %} - for (let i: int = 0; i < items.length; i++) { - res.setUnsafe(i, items[i].getLong()) - } + res.ofBigInt(items) {%- else %} res.ofNumber(items) {%- endif %} @@ -757,6 +755,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}>, ArrayLi private final native ofShort(items: FixedArray): void {%- elif N == 'BigInt64' %} private final native ofLong(items: FixedArray): void + private final native ofBigInt(items: FixedArray): void {%- elif N == 'Float32' %} private final native ofFloat(items: FixedArray): void {%- endif %} @@ -836,6 +835,25 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}>, ArrayLi } {%- endif %} + /** + * Creates an array from an object of FixedArray<{{updatedElementCompat}}>. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new {{N}}Array + */ + public static fromFixedArray{{updatedElementCompat}}(arr: FixedArray<{{updatedElementCompat}}>): {{N}}Array { + let result = new {{N}}Array(arr.length) + {%- if updatedElementCompat == 'number' %} + result.ofNumber(arr) + {%- elif updatedElementCompat == 'BigInt' %} + result.ofBigInt(arr) + {%- else %} + result.ofInt(arr) + {%- endif %} + return result + } + private native final set(array: Array<{{updatedElementCompat}}>): void; /** @@ -848,7 +866,7 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}>, ArrayLi public static fromArray{{updatedElementCompat}}(arr: Array<{{updatedElementCompat}}>): {{N}}Array { let result = new {{N}}Array(arr.length) result.set(arr) - return result; + return result } /** @@ -946,7 +964,8 @@ export final class {{N}}Array implements Iterable<{{subsetTypeValues}}>, ArrayLi public static overload from { fromSet{{updatedElementCompat}}, from{{N}}Array{%- if U|length %} ,from{{U}}Array{%- endif %}, - fromArray{{updatedElementCompat}}, fromArrayLikeNumber, fromArrayLike, fromIterable + fromFixedArray{{updatedElementCompat}}, fromArray{{updatedElementCompat}}, fromArrayLikeNumber, + fromArrayLike, fromIterable }; {%- if (N == 'Float32') or (N == 'Float64') %} diff --git a/static_core/plugins/ets/templates/stdlib/typedUArray.ets.j2 b/static_core/plugins/ets/templates/stdlib/typedUArray.ets.j2 index 4ba79a75d7f8ad2a20d18cd89d368aadaaea6159..ec3401c68df9c798892c3b189f00af361b065417 100644 --- a/static_core/plugins/ets/templates/stdlib/typedUArray.ets.j2 +++ b/static_core/plugins/ets/templates/stdlib/typedUArray.ets.j2 @@ -685,9 +685,7 @@ export final class {{element['name']}}Array implements Iterable<{{element['subse {%- elif argType == 'long' %} res.ofLong(items) {%- elif argType == 'bigint' %} - for (let i: int = 0; i < items.length; i++) { - res.setUnsafeClamp(i, items[i].getULong()) - } + res.ofBigInt(items) {%- else %} {%- if element.get('name') == 'BigUint64' %} for (let i: int = 0; i < items.length; i++) { @@ -712,6 +710,7 @@ export final class {{element['name']}}Array implements Iterable<{{element['subse private final native ofLong(items: FixedArray): void {%- elif element.get('name') == 'BigUint64' %} private final native ofLong(items: FixedArray): void + private final native ofBigInt(items: FixedArray): void {%- endif %} /** @@ -829,6 +828,23 @@ export final class {{element['name']}}Array implements Iterable<{{element['subse } {%- endif %} + /** + * Creates an array from an object of FixedArray<{{updatedSubsetType}}>. + * + * @param arr An instance of the FixedArray type to convert to an array. + * + * @returns new {{element['name']}}Array + */ + public static fromFixedArray{{updatedSubsetType}}(arr: FixedArray<{{updatedSubsetType}}>): {{element['name']}}Array { + let result = new {{element['name']}}Array(arr.length) + {%- if updatedSubsetType == 'BigInt' %} + result.ofBigInt(arr) + {%- else %} + result.ofInt(arr) + {%- endif %} + return result + } + private native final set(array: Array<{{updatedSubsetType}}>): void; /** @@ -841,7 +857,7 @@ export final class {{element['name']}}Array implements Iterable<{{element['subse public static fromArray{{updatedSubsetType}}(arr: Array<{{updatedSubsetType}}>): {{element['name']}}Array { let result = new {{element['name']}}Array(arr.length) result.set(arr) - return result; + return result } /** @@ -940,7 +956,8 @@ export final class {{element['name']}}Array implements Iterable<{{element['subse public static overload from { fromSet{{updatedSubsetType}}, from{{element['name']}}Array {%- if element['signed'] is defined and element['signed']|length %} ,from{{element['signed']}}Array{%- endif %} - ,fromArray{{updatedSubsetType}}, fromArrayLike{{element['subsetType']}}, fromArrayLike, fromIterable + ,fromFixedArray{{updatedSubsetType}}, fromArray{{updatedSubsetType}}, fromArrayLike{{element['subsetType']}}, + fromArrayLike, fromIterable }; /** diff --git a/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_from.ets b/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_from.ets index acf45aaf1d18b60a598174fb6486397f30226a14..db25a5d31ad445243fd2bd7acfcf83e81520f728 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_from.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_from.ets @@ -23,8 +23,10 @@ function testIU8FromArrayWrongType() { function testIU8FromIterableNumber() { checkNumberArray(Int8Array.of(), Int8Array.from(new Set())); checkNumberArray(Uint8Array.of(), Uint8Array.from(new Set())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(new Set())); checkNumberArray(Int8Array.of(), Int8Array.from(new Set())); checkNumberArray(Uint8Array.of(), Uint8Array.from(new Set())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(new Set())); const len: int = 127; let iexpected: Int8Array = new Int8Array(len); @@ -58,16 +60,28 @@ function testIU8FromIterableNumber() { } function testIU8FromArrayLikeNumber() { + let empty: FixedArray = []; checkNumberArray(Int8Array.of(), Int8Array.from(new Array())); + checkNumberArray(Int8Array.of(), Int8Array.from([])); + checkNumberArray(Int8Array.of(), Int8Array.from(empty)); checkNumberArray(Int8Array.of(), Int8Array.from(Int8Array.of())); checkNumberArray(Int8Array.of(), Int8Array.from(Uint8Array.of())); checkNumberArray(Int8Array.of(), Int8Array.from(Int32Array.of())); checkNumberArray(Int8Array.of(), Int8Array.from(Uint32Array.of())); checkNumberArray(Uint8Array.of(), Uint8Array.from(new Array())); + checkNumberArray(Uint8Array.of(), Uint8Array.from([])); + checkNumberArray(Uint8Array.of(), Uint8Array.from(empty)); checkNumberArray(Uint8Array.of(), Uint8Array.from(Uint8Array.of())); checkNumberArray(Uint8Array.of(), Uint8Array.from(Int8Array.of())); checkNumberArray(Uint8Array.of(), Uint8Array.from(Uint32Array.of())); checkNumberArray(Uint8Array.of(), Uint8Array.from(Int32Array.of())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(new Array())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from([])); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(empty)); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(Uint8Array.of())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(Int8Array.of())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(Uint32Array.of())); + checkNumberArray(Uint8ClampedArray.of(), Uint8ClampedArray.from(Int32Array.of())); const len: int = 127; let iexpected: Int8Array = new Int8Array(len); @@ -81,35 +95,65 @@ function testIU8FromArrayLikeNumber() { uexpected[i] = u8arr[i] = u16arr[i] = Int.toDouble(i + 1); } - let data = prepareTestData(len, 1.0); + let data = prepareTestData(len, 1); let arr = data[1]; + let fixarr = data[2]; checkNumberArray(iexpected, Int8Array.from(arr)); + checkNumberArray(iexpected, Int8Array.from(fixarr)); checkNumberArray(iexpected, Int8Array.from(i8arr)); checkNumberArray(iexpected, Int8Array.from(u8arr)); checkNumberArray(iexpected, Int8Array.from(i16arr)); checkNumberArray(iexpected, Int8Array.from(u16arr)); checkNumberArray(uexpected, Uint8Array.from(arr)); + checkNumberArray(uexpected, Uint8Array.from(fixarr)); checkNumberArray(uexpected, Uint8Array.from(i8arr)); checkNumberArray(uexpected, Uint8Array.from(u8arr)); checkNumberArray(uexpected, Uint8Array.from(i16arr)); checkNumberArray(uexpected, Uint8Array.from(u16arr)); + let source: FixedArray = [0, 1, -1, 65535, -65535, 2147483647, -2147483648]; checkNumberArray(Int8Array.of(0, 1, -1, -1, 1, -1, 0), Int8Array.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Int8Array.of(0, 1, -1, -1, 1, -1, 0), + Int8Array.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Int8Array.of(0, 1, -1, -1, 1, -1, 0), Int8Array.from(source)); checkNumberArray(Uint8Array.of(0, 1, 255, 255, 1, 255, 0), Uint8Array.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Uint8Array.of(0, 1, 255, 255, 1, 255, 0), + Uint8Array.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Uint8Array.of(0, 1, 255, 255, 1, 255, 0), Uint8Array.from(source)); checkNumberArray(Uint8ClampedArray.of(0, 1, 0, 255, 0, 255, 0), Uint8ClampedArray.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Uint8ClampedArray.of(0, 1, 0, 255, 0, 255, 0), + Uint8ClampedArray.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Uint8ClampedArray.of(0, 1, 0, 255, 0, 255, 0), Uint8ClampedArray.from(source)); + let source2: FixedArray = [1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, + 254, 256, 257, 10000]; checkNumberArray(Int8Array.of(1, -1, -126, -127, -128, 127, 0, -1, 0, 12, -1, 126, 127, -128, -2, 0, 1, 16), Int8Array.from(Array.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000))); + checkNumberArray(Int8Array.of(1, -1, -126, -127, -128, 127, 0, -1, 0, 12, -1, 126, 127, -128, -2, 0, 1, 16), + Int8Array.from([1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, + 254, 256, 257, 10000])); + checkNumberArray(Int8Array.of(1, -1, -126, -127, -128, 127, 0, -1, 0, 12, -1, 126, 127, -128, -2, 0, 1, 16), + Int8Array.from(source2)); checkNumberArray(Uint8Array.of(1, 255, 130, 129, 128, 127, 0, 255, 0, 12, 255, 126, 127, 128, 254, 0, 1, 16), Uint8Array.from(Array.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000))); + checkNumberArray(Uint8Array.of(1, 255, 130, 129, 128, 127, 0, 255, 0, 12, 255, 126, 127, 128, 254, 0, 1, 16), + Uint8Array.from([1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, + 254, 256, 257, 10000])); + checkNumberArray(Uint8Array.of(1, 255, 130, 129, 128, 127, 0, 255, 0, 12, 255, 126, 127, 128, 254, 0, 1, 16), + Uint8Array.from(source2)); checkNumberArray(Uint8ClampedArray.of(1, 0, 0, 0, 0, 0, 0, 0, 0, 12, 255, 126, 127, 128, 254, 255, 255, 255), Uint8ClampedArray.from(Array.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000))); + checkNumberArray(Uint8ClampedArray.of(1, 0, 0, 0, 0, 0, 0, 0, 0, 12, 255, 126, 127, 128, 254, 255, 255, 255), + Uint8ClampedArray.from([1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, + 127, 128, 254, 256, 257, 10000])); + checkNumberArray(Uint8ClampedArray.of(1, 0, 0, 0, 0, 0, 0, 0, 0, 12, 255, 126, 127, 128, 254, 255, 255, 255), + Uint8ClampedArray.from(source2)); Uint8Array.from(Int8Array.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000)); } @@ -125,7 +169,7 @@ function testIU16FromIterableNumber() { uexpected[i] = Int.toDouble(i + 128); } - let data = prepareTestData(len, 128.0); + let data = prepareTestData(len, 128); let set = data[0]; checkNumberArray(iexpected, Int16Array.from(set)); checkNumberArray(uexpected, Uint16Array.from(set)); @@ -146,7 +190,10 @@ function testIU16FromIterableNumber() { } function testIU16FromArrayLikeNumber() { + let empty: FixedArray = []; checkNumberArray(Int16Array.of(), Int16Array.from(new Array())); + checkNumberArray(Int16Array.of(), Int16Array.from([])); + checkNumberArray(Int16Array.of(), Int16Array.from(empty)); checkNumberArray(Int16Array.of(), Int16Array.from(Int8Array.of())); checkNumberArray(Int16Array.of(), Int16Array.from(Uint8Array.of())); checkNumberArray(Int16Array.of(), Int16Array.from(Int16Array.of())); @@ -154,6 +201,8 @@ function testIU16FromArrayLikeNumber() { checkNumberArray(Int16Array.of(), Int16Array.from(Int32Array.of())); checkNumberArray(Int16Array.of(), Int16Array.from(Uint32Array.of())); checkNumberArray(Uint16Array.of(), Uint16Array.from(new Array())); + checkNumberArray(Uint16Array.of(), Uint16Array.from([])); + checkNumberArray(Uint16Array.of(), Uint16Array.from(empty)); checkNumberArray(Uint16Array.of(), Uint16Array.from(Int8Array.of())); checkNumberArray(Uint16Array.of(), Uint16Array.from(Uint8Array.of())); checkNumberArray(Uint16Array.of(), Uint16Array.from(Uint16Array.of())); @@ -176,9 +225,11 @@ function testIU16FromArrayLikeNumber() { uexpected[i] = u8arr[i] = u16arr[i] = u32arr[i] = Int.toDouble(i + 1); } - let data = prepareTestData(len, 1.0); + let data = prepareTestData(len, 1); let arr = data[1]; + let fixarr = data[2]; checkNumberArray(iexpected, Int16Array.from(arr)); + checkNumberArray(iexpected, Int16Array.from(fixarr)); checkNumberArray(iexpected, Int16Array.from(i8arr)); checkNumberArray(iexpected, Int16Array.from(u8arr)); checkNumberArray(iexpected, Int16Array.from(i16arr)); @@ -186,6 +237,7 @@ function testIU16FromArrayLikeNumber() { checkNumberArray(iexpected, Int16Array.from(i32arr)); checkNumberArray(iexpected, Int16Array.from(u32arr)); checkNumberArray(uexpected, Uint16Array.from(arr)); + checkNumberArray(uexpected, Uint16Array.from(fixarr)); checkNumberArray(uexpected, Uint16Array.from(i8arr)); checkNumberArray(uexpected, Uint16Array.from(u8arr)); checkNumberArray(uexpected, Uint16Array.from(i16arr)); @@ -198,32 +250,56 @@ function testIU16FromArrayLikeNumber() { uexpected[i] = u16arr[i] = u32arr[i] = Int.toDouble(i + 128); } - data = prepareTestData(len, 128.0); + data = prepareTestData(len, 128); arr = data[1]; + fixarr = data[2]; checkNumberArray(iexpected, Int16Array.from(arr)); + checkNumberArray(iexpected, Int16Array.from(fixarr)); checkNumberArray(iexpected, Int16Array.from(i16arr)); checkNumberArray(iexpected, Int16Array.from(u16arr)); checkNumberArray(iexpected, Int16Array.from(i32arr)); checkNumberArray(iexpected, Int16Array.from(u32arr)); checkNumberArray(uexpected, Uint16Array.from(arr)); + checkNumberArray(uexpected, Uint16Array.from(fixarr)); checkNumberArray(uexpected, Uint16Array.from(i16arr)); checkNumberArray(uexpected, Uint16Array.from(u16arr)); checkNumberArray(uexpected, Uint16Array.from(i32arr)); checkNumberArray(uexpected, Uint16Array.from(u32arr)); + let source: FixedArray = [0, 1, -1, 65535, -65535, 2147483647, -2147483648]; checkNumberArray(Int16Array.of(0, 1, -1, -1, 1, -1, 0), - Int16Array.from(new Set(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)))); + Int16Array.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Int16Array.of(0, 1, -1, -1, 1, -1, 0), + Int16Array.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Int16Array.of(0, 1, -1, -1, 1, -1, 0), Int16Array.from(source)); checkNumberArray(Uint16Array.of(0, 1, 65535, 65535, 1, 65535, 0), - Uint16Array.from(new Set(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)))); + Uint16Array.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Uint16Array.of(0, 1, 65535, 65535, 1, 65535, 0), + Uint16Array.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Uint16Array.of(0, 1, 65535, 65535, 1, 65535, 0), Uint16Array.from(source)); + let source2: FixedArray = [-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648]; checkNumberArray(Int16Array.of(-1, -32766, -32767, -32768, 32767, 1, 0, -1, 0, 12, 255, 32766, 32765, -32768, -32767, -2, -1, 0, -31072, -1, 0), - Int16Array.from(new Set(Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, - 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648)))); + Int16Array.from(Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648))); + checkNumberArray(Int16Array.of(-1, -32766, -32767, -32768, 32767, 1, 0, -1, 0, 12, 255, 32766, 32765, -32768, + -32767, -2, -1, 0, -31072, -1, 0), + Int16Array.from([-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648])); + checkNumberArray(Int16Array.of(-1, -32766, -32767, -32768, 32767, 1, 0, -1, 0, 12, 255, 32766, 32765, -32768, + -32767, -2, -1, 0, -31072, -1, 0), Int16Array.from(source2)); checkNumberArray(Uint16Array.of(65535, 32770, 32769, 32768, 32767, 1, 0, 65535, 0, 12, 255, 32766, 32765, 32768, 32769, 65534, 65535, 0, 34464, 65535, 0), - Uint16Array.from(new Set(Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, - 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648)))); + Uint16Array.from(Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648))); + checkNumberArray(Uint16Array.of(65535, 32770, 32769, 32768, 32767, 1, 0, 65535, 0, 12, 255, 32766, 32765, 32768, + 32769, 65534, 65535, 0, 34464, 65535, 0), + Uint16Array.from([-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648])); + checkNumberArray(Uint16Array.of(65535, 32770, 32769, 32768, 32767, 1, 0, 65535, 0, 12, 255, 32766, 32765, 32768, + 32769, 65534, 65535, 0, 34464, 65535, 0), Uint16Array.from(source2)); checkNumberArray(Int16Array.of(-1, -32766, -32767, -32768, 32767, 1, 0, -1, 0, 12, 255, 32766, 32765, -32768, -32767, -2, -1, 0, -31072), Int16Array.from(Uint16Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, @@ -246,7 +322,7 @@ function testIU32FromIterableNumber() { uexpected[i] = Int.toDouble(i + 16536); } - let data = prepareTestData(len, 16536.0); + let data = prepareTestData(len, 16536); let set = data[0]; checkNumberArray(iexpected, Int32Array.from(set)); checkNumberArray(uexpected, Uint32Array.from(set)); @@ -268,12 +344,17 @@ function testIU32FromIterableNumber() { } function testIU32FromArrayLikeNumber() { + let empty: FixedArray = []; checkNumberArray(Int32Array.of(), Int32Array.from(new Array())); + checkNumberArray(Int32Array.of(), Int32Array.from([])); + checkNumberArray(Int32Array.of(), Int32Array.from(empty)); checkNumberArray(Int32Array.of(), Int32Array.from(Int8Array.of())); checkNumberArray(Int32Array.of(), Int32Array.from(Uint8Array.of())); checkNumberArray(Int32Array.of(), Int32Array.from(Int32Array.of())); checkNumberArray(Int32Array.of(), Int32Array.from(Uint32Array.of())); checkNumberArray(Uint32Array.of(), Uint32Array.from(new Array())); + checkNumberArray(Uint32Array.of(), Uint32Array.from([])); + checkNumberArray(Uint32Array.of(), Uint32Array.from(empty)); checkNumberArray(Uint32Array.of(), Uint32Array.from(Uint8Array.of())); checkNumberArray(Uint32Array.of(), Uint32Array.from(Int8Array.of())); checkNumberArray(Uint32Array.of(), Uint32Array.from(Uint32Array.of())); @@ -291,14 +372,17 @@ function testIU32FromArrayLikeNumber() { uexpected[i] = u8arr[i] = u32arr[i] = Int.toDouble(i + 1); } - let data = prepareTestData(len, 1.0); + let data = prepareTestData(len, 1); let arr = data[1]; + let fixarr = data[2]; checkNumberArray(iexpected, Int32Array.from(arr)); + checkNumberArray(iexpected, Int32Array.from(fixarr)); checkNumberArray(iexpected, Int32Array.from(i8arr)); checkNumberArray(iexpected, Int32Array.from(i32arr)); checkNumberArray(iexpected, Int32Array.from(u8arr)); checkNumberArray(iexpected, Int32Array.from(u32arr)); checkNumberArray(uexpected, Uint32Array.from(arr)); + checkNumberArray(uexpected, Uint32Array.from(fixarr)); checkNumberArray(uexpected, Uint32Array.from(u8arr)); checkNumberArray(uexpected, Uint32Array.from(u32arr)); checkNumberArray(uexpected, Uint32Array.from(i8arr)); @@ -309,29 +393,56 @@ function testIU32FromArrayLikeNumber() { uexpected[i] = u32arr[i] = Int.toDouble(i + 16536); } - data = prepareTestData(len, 16536.0); + data = prepareTestData(len, 16536); arr = data[1]; + fixarr = data[2]; checkNumberArray(iexpected, Int32Array.from(arr)); + checkNumberArray(iexpected, Int32Array.from(fixarr)); checkNumberArray(iexpected, Int32Array.from(i32arr)); checkNumberArray(iexpected, Int32Array.from(u32arr)); checkNumberArray(uexpected, Uint32Array.from(arr)); + checkNumberArray(uexpected, Uint32Array.from(fixarr)); checkNumberArray(uexpected, Uint32Array.from(u32arr)); checkNumberArray(uexpected, Uint32Array.from(i32arr)); + let source: FixedArray = [0, 1, -1, 65535, -65535, 2147483647, -2147483648]; checkNumberArray(Int32Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648), Int32Array.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Int32Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648), + Int32Array.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Int32Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648), Int32Array.from(source)); checkNumberArray(Uint32Array.of(0.0, 1.0, 4294967295.0, 65535.0, 4294901761.0, 2147483647.0, 2147483648.0), Uint32Array.from(Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648))); + checkNumberArray(Uint32Array.of(0.0, 1.0, 4294967295.0, 65535.0, 4294901761.0, 2147483647.0, 2147483648.0), + Uint32Array.from([0, 1, -1, 65535, -65535, 2147483647, -2147483648])); + checkNumberArray(Uint32Array.of(0.0, 1.0, 4294967295.0, 65535.0, 4294901761.0, 2147483647.0, 2147483648.0), + Uint32Array.from(source)); + let source2: FixedArray = [-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648]; checkNumberArray(Int32Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648), Int32Array.from(Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648))); + checkNumberArray(Int32Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, + 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648), + Int32Array.from([-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648])); + checkNumberArray(Int32Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, + 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648), Int32Array.from(source2)); checkNumberArray(Uint32Array.of(4294967295.0, 4294934530.0, 4294934529.0, 4294934528.0, 4294934527.0, 4294901761.0, 4294901760.0, 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, 65536.0, 100000.0, 2147483647.0, 2147483648.0), Uint32Array.from(Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648))); + checkNumberArray(Uint32Array.of(4294967295.0, 4294934530.0, 4294934529.0, 4294934528.0, 4294934527.0, 4294901761.0, + 4294901760.0, 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, + 65536.0, 100000.0, 2147483647.0, 2147483648.0), + Uint32Array.from([-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, + 255, 32766, 32765, 32768, 32769, 65534, 65535, 65536, 100000, 2147483647, -2147483648])); + checkNumberArray(Uint32Array.of(4294967295.0, 4294934530.0, 4294934529.0, 4294934528.0, 4294934527.0, 4294901761.0, + 4294901760.0, 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, + 65536.0, 100000.0, 2147483647.0, 2147483648.0), Uint32Array.from(source2)); checkNumberArray(Int32Array.of(2147483647.0, 2147483647.0, 2147483647.0, 2147483647.0, 2.0, 1.0, 0.0, 2147483647.0, 0.0, 12.0, 255.0, 2147483646.0, 2147483647.0, 2147483647.0, 2147483647.0, 2147483647.0, 2147483647.0, 0.0), @@ -368,7 +479,10 @@ function testF32FromIterableNumber() { } function testF32FromArrayLikeNumber() { + let empty: FixedArray = []; checkNumberArray(Float32Array.of(), Float32Array.from(new Array())); + checkNumberArray(Float32Array.of(), Float32Array.from([])); + checkNumberArray(Float32Array.of(), Float32Array.from(empty)); checkNumberArray(Float32Array.of(), Float32Array.from(Float64Array.of())); checkNumberArray(Float32Array.of(), Float32Array.from(Float32Array.of())); @@ -382,19 +496,41 @@ function testF32FromArrayLikeNumber() { let data = prepareTestData(len, 1.0); let arr = data[1]; + let fixarr = data[2]; checkNumberArray(expected, Float32Array.from(arr)); + checkNumberArray(expected, Float32Array.from(fixarr)); checkNumberArray(expected, Float32Array.from(f64arr)); checkNumberArray(expected, Float32Array.from(f32arr)); + let source: FixedArray = [1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105, + -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.402823466385289e+38, 3.402823466385289e+38, + -3.4028235677973359e+38, 3.4028235677973359e+38, -3.4028235677973362e+38, 3.4028235677973362e+38, + -3.4028235677973362e+38 - 1.0e+23, 3.4028235677973362e+38 + 1.0e+23]; checkNumberArray(Float32Array.of(1, NaN, Infinity, 0.00001f, Infinity, -Infinity, -Infinity, -0.6e+16f, Infinity, -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -Infinity, Infinity), - Float32Array.from(Array.of(1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105, - -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.402823466385289e+38, 3.402823466385289e+38, - -3.4028235677973359e+38, 3.4028235677973359e+38, -3.4028235677973362e+38, 3.4028235677973362e+38, - -3.4028235677973362e+38 - 1.0e+23, 3.4028235677973362e+38 + 1.0e+23))); + Float32Array.from(Array.of(1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105, + -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.402823466385289e+38, 3.402823466385289e+38, + -3.4028235677973359e+38, 3.4028235677973359e+38, -3.4028235677973362e+38, 3.4028235677973362e+38, + -3.4028235677973362e+38 - 1.0e+23, 3.4028235677973362e+38 + 1.0e+23))); + checkNumberArray(Float32Array.of(1, NaN, Infinity, 0.00001f, Infinity, -Infinity, -Infinity, -0.6e+16f, Infinity, + -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, + -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -Infinity, Infinity), + Float32Array.from([1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105, + -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.402823466385289e+38, 3.402823466385289e+38, + -3.4028235677973359e+38, 3.4028235677973359e+38, -3.4028235677973362e+38, 3.4028235677973362e+38, + -3.4028235677973362e+38 - 1.0e+23, 3.4028235677973362e+38 + 1.0e+23])); + checkNumberArray(Float32Array.of(1, NaN, Infinity, 0.00001f, Infinity, -Infinity, -Infinity, -0.6e+16f, Infinity, + -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, + -3.4028234663852886e+38, 3.4028234663852886e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -Infinity, Infinity), + Float32Array.from(source)); + + let shortsource: FixedArray = [1, 65535, -65535, NaN, Infinity]; checkNumberArray(Float32Array.of(1, 65535, -65535, NaN, Infinity), Float32Array.from(Array.of(1, 65535, -65535, NaN, Infinity))); + checkNumberArray(Float32Array.of(1, 65535, -65535, NaN, Infinity), + Float32Array.from([1, 65535, -65535, NaN, Infinity])); + checkNumberArray(Float32Array.of(1, 65535, -65535, NaN, Infinity), Float32Array.from(shortsource)); } function testF64FromIterableNumber() { @@ -412,7 +548,10 @@ function testF64FromIterableNumber() { } function testF64FromArrayLikeNumber() { + let empty: FixedArray = []; checkNumberArray(Float64Array.of(), Float64Array.from(new Array())); + checkNumberArray(Float64Array.of(), Float64Array.from([])); + checkNumberArray(Float64Array.of(), Float64Array.from(empty)); checkNumberArray(Float64Array.of(), Float64Array.from(Float64Array.of())); checkNumberArray(Float64Array.of(), Float64Array.from(Float32Array.of())); @@ -426,14 +565,25 @@ function testF64FromArrayLikeNumber() { let data = prepareTestData(len, 1.0); let arr = data[1]; + let fixarr = data[2]; checkNumberArray(expected, Float64Array.from(arr)); + checkNumberArray(expected, Float64Array.from(fixarr)); checkNumberArray(expected, Float64Array.from(f64arr)); checkNumberArray(expected, Float64Array.from(f32arr)); + let source: FixedArray = [1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105]; checkNumberArray(Float64Array.of(1, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105), Float64Array.from(Array.of(1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105))); + checkNumberArray(Float64Array.of(1, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105), + Float64Array.from([1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105])); + checkNumberArray(Float64Array.of(1, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105), + Float64Array.from(source)); + let shortsource: FixedArray = [1, 65535, -65535, NaN, Infinity]; checkNumberArray(Float64Array.of(1, 65535, -65535, NaN, Infinity), Float64Array.from(Array.of(1, 65535, -65535, NaN, Infinity))); + checkNumberArray(Float64Array.of(1, 65535, -65535, NaN, Infinity), + Float64Array.from([1, 65535, -65535, NaN, Infinity])); + checkNumberArray(Float64Array.of(1, 65535, -65535, NaN, Infinity), Float64Array.from(shortsource)); } function testIU64FromArrayLikeNumber() { @@ -467,7 +617,7 @@ function __noinline__Float32ArrayFromCheckException(obj: Object, factType: strin }); } -function __noinline__BigInt64ArrayFromCheckException(obj: Object, factType: string): void +function __noinline__BigInt64ArrayFromArrayCheckException(obj: Object, factType: string): void { let arr: Array = obj as Array; arktest.expectThrow(() => { BigInt64Array.from(arr); }, (e: Error) => { @@ -475,31 +625,53 @@ function __noinline__BigInt64ArrayFromCheckException(obj: Object, factType: stri }); } +function __noinline__BigInt64ArrayFromFixedArrayCheckException(obj: Object, factType: string): void +{ + arktest.expectThrow(() => { let arr: FixedArray = obj as FixedArray }, (e: Error) => { + return e instanceof ClassCastError && e.message == factType + ' cannot be cast to [Lescompat/BigInt;'; + }); +} + function testIU64FromArrayWrongType() { let strings: Array = Array.of(new string("hi"), new string("hello"), new string("new")); - __noinline__BigInt64ArrayFromCheckException(strings, 'std.core.LineString'); + __noinline__BigInt64ArrayFromArrayCheckException(strings, 'std.core.LineString'); let ints: Object = Array.of(1, 2, 3); - __noinline__BigInt64ArrayFromCheckException(ints, 'std.core.Int'); + __noinline__BigInt64ArrayFromArrayCheckException(ints, 'std.core.Int'); + let fixstrings: FixedArray = [new string("hi"), new string("hello"), new string("new")]; + __noinline__BigInt64ArrayFromFixedArrayCheckException(fixstrings, '[Lstd/core/String;'); } function testIU64FromArrayBigInt() { + let empty: FixedArray = []; checkBigIntArray(BigInt64Array.of(), BigInt64Array.from(new Array())); + checkBigIntArray(BigInt64Array.of(), BigInt64Array.from([])); + checkBigIntArray(BigInt64Array.of(), BigInt64Array.from(empty)); const length: int = 128; - let arr: Array = new Array(); + let arr: Array = new Array(length); + let fixarr: FixedArray = new bigint[length]; let iexpected: BigInt64Array = new BigInt64Array(length); for (let i = 0; i < length; i++) { const v = new BigInt(i); - arr.push(v); + arr[i] = v; + fixarr[i] = v; iexpected[i] = v; } checkBigIntArray(iexpected, BigInt64Array.from(arr)); + checkBigIntArray(iexpected, BigInt64Array.from(fixarr)); let arr2: Array = Array.of(1n, BigInt(1.e+5), BigInt(1.7e+308), BigInt(-1.7e+308), BigInt(-0.6e+16), BigInt(0.4e+105), 9007199254740991n * 9007199254740991n, -9007199254740991n * 9007199254740991n, -9007199254740991n * 9007199254740991n * 9007199254740991n * 9007199254740991n * 9007199254740991n); + + let fixarr2: FixedArray = [1n, BigInt(1.e+5), BigInt(1.7e+308), BigInt(-1.7e+308), BigInt(-0.6e+16), + BigInt(0.4e+105), 9007199254740991n * 9007199254740991n, -9007199254740991n * 9007199254740991n, + -9007199254740991n * 9007199254740991n * 9007199254740991n * 9007199254740991n * 9007199254740991n]; checkBigIntArray(BigInt64Array.of(1n, BigInt(1.e+5), 0n, 0n, BigInt(-0.6e+16), 0n, -18014398509481983n, 18014398509481983n, -45035996273704959n), BigInt64Array.from(arr2)); + checkBigIntArray(BigInt64Array.of(1n, BigInt(1.e+5), 0n, 0n, BigInt(-0.6e+16), 0n, -18014398509481983n, + 18014398509481983n, -45035996273704959n), + BigInt64Array.from(fixarr2)); // Currently, we have no method to build a BigUint64Array from an Array let uexpected: BigUint64Array = new BigUint64Array(arr2.length); for (let i = 0; i < arr2.length; i++) { @@ -536,28 +708,32 @@ function testIU64FromIterableBigInt() { checkBigIntArray(iexpected, BigInt64Array.from(u64Arr)); } -function prepareTestData(length: int, from: number): [Set, Array] { +function prepareTestData(length: int, from: number): [Set, Array, FixedArray] { let set: Set = new Set(); - let arr: Array = new Array(); + let arr: Array = new Array(length); + let fixarr: FixedArray = new number[length]; let v: number = from; for (let i = 0; i < length; i++) { - arr.push(v); + arr[i] = v; + fixarr[i] = v; set.add(v); v++; } - return [set, arr]; + return [set, arr, fixarr]; } -function prepareTestData(length: int, from: int): [Set, Array] { +function prepareTestData(length: int, from: int): [Set, Array, FixedArray] { let set: Set = new Set(); - let arr: Array = new Array(); + let arr: Array = new Array(length); + let fixarr: FixedArray = new int[length]; let v: int = from; for (let i = 0; i < length; i++) { - arr.push(v); + arr[i] = v; + fixarr[i] = v; set.add(v); v++; } - return [set, arr]; + return [set, arr, fixarr]; } function checkNumberArray(expected: ArrayLike, data: ArrayLike): void { diff --git a/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_of.ets b/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_of.ets new file mode 100644 index 0000000000000000000000000000000000000000..b1cc49b9e9d4f6e926d90ee87d69855c28ac7136 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-common-tests/intrinsics/typed_arrays_of.ets @@ -0,0 +1,299 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function testIU8OfInt() { + checkIntArray(new int[0], Int8Array.of()); + checkIntArray(new int[0], Uint8Array.of()); + checkIntArray(new int[0], Uint8ClampedArray.of()); + + let arr = prepareTestData(127, 1); + checkIntArray(arr, Int8Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + checkIntArray(arr, Uint8Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + checkIntArray(arr, Uint8ClampedArray.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, + 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + + checkIntArray([0, 1, -1, -1, 1, -1, 0], Int8Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + checkIntArray([0, 1, 255, 255, 1, 255, 0], Uint8Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + checkIntArray([0, 1, 0, 255, 0, 255, 0], Uint8ClampedArray.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + + checkIntArray([1, -1, -126, -127, -128, 127, 0, -1, 0, 12, -1, 126, 127, -128, -2, 0, 1, 16], + Int8Array.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000)); + checkIntArray([1, 255, 130, 129, 128, 127, 0, 255, 0, 12, 255, 126, 127, 128, 254, 0, 1, 16], + Uint8Array.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000)); + checkIntArray([1, 0, 0, 0, 0, 0, 0, 0, 0, 12, 255, 126, 127, 128, 254, 255, 255, 255], + Uint8ClampedArray.of(1, -1, -126, -127, -128, -129, -256, -257, 0, 12, 255, 126, 127, 128, 254, 256, 257, 10000)); +} + +function testIU16OfInt() { + checkIntArray(new int[0], Int16Array.of()); + checkIntArray(new int[0], Uint16Array.of()); + + let arr = prepareTestData(127, 1); + checkIntArray(arr, Int16Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + checkIntArray(arr, Uint16Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + + arr = prepareTestData(127, 128); + checkIntArray(arr, Int16Array.of(128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, + 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, + 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, + 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, + 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, + 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254)); + checkIntArray(arr, Uint16Array.of(128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, + 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, + 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, + 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, + 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, + 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254)); + + checkIntArray([0, 1, -1, -1, 1, -1, 0], Int16Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + checkIntArray([0, 1, 65535, 65535, 1, 65535, 0], Uint16Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + + checkIntArray([-1, -32766, -32767, -32768, 32767, 1, 0, -1, 0, 12, 255, 32766, 32765, -32768, -32767, -2, -1, 0, -31072, -1, 0], + Int16Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, + 65534, 65535, 65536, 100000, 2147483647, -2147483648)); + checkIntArray([65535, 32770, 32769, 32768, 32767, 1, 0, 65535, 0, 12, 255, 32766, 32765, 32768, 32769, 65534, 65535, + 0, 34464, 65535, 0], + Uint16Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, + 65534, 65535, 65536, 100000, 2147483647, -2147483648)); +} + +function testIU32OfInt() { + checkIntArray(new int[0], Int32Array.of()); + checkIntArray(new int[0], Uint32Array.of()); + + let arr = prepareTestData(127, 1); + checkIntArray(arr, Int32Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + checkIntArray(arr, Uint32Array.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, + 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127)); + + arr = prepareTestData(127, 16536); + checkIntArray(arr, Int32Array.of(16536, 16537, 16538, 16539, 16540, 16541, 16542, 16543, 16544, 16545, 16546, 16547, + 16548, 16549, 16550, 16551, 16552, 16553, 16554, 16555, 16556, 16557, 16558, 16559, 16560, 16561, 16562, 16563, + 16564, 16565, 16566, 16567, 16568, 16569, 16570, 16571, 16572, 16573, 16574, 16575, 16576, 16577, 16578, 16579, + 16580, 16581, 16582, 16583, 16584, 16585, 16586, 16587, 16588, 16589, 16590, 16591, 16592, 16593, 16594, 16595, + 16596, 16597, 16598, 16599, 16600, 16601, 16602, 16603, 16604, 16605, 16606, 16607, 16608, 16609, 16610, 16611, + 16612, 16613, 16614, 16615, 16616, 16617, 16618, 16619, 16620, 16621, 16622, 16623, 16624, 16625, 16626, 16627, + 16628, 16629, 16630, 16631, 16632, 16633, 16634, 16635, 16636, 16637, 16638, 16639, 16640, 16641, 16642, 16643, + 16644, 16645, 16646, 16647, 16648, 16649, 16650, 16651, 16652, 16653, 16654, 16655, 16656, 16657, 16658, 16659, + 16660, 16661, 16662)); + checkIntArray(arr, Uint32Array.of(16536, 16537, 16538, 16539, 16540, 16541, 16542, 16543, 16544, 16545, 16546, 16547, + 16548, 16549, 16550, 16551, 16552, 16553, 16554, 16555, 16556, 16557, 16558, 16559, 16560, 16561, 16562, 16563, + 16564, 16565, 16566, 16567, 16568, 16569, 16570, 16571, 16572, 16573, 16574, 16575, 16576, 16577, 16578, 16579, + 16580, 16581, 16582, 16583, 16584, 16585, 16586, 16587, 16588, 16589, 16590, 16591, 16592, 16593, 16594, 16595, + 16596, 16597, 16598, 16599, 16600, 16601, 16602, 16603, 16604, 16605, 16606, 16607, 16608, 16609, 16610, 16611, + 16612, 16613, 16614, 16615, 16616, 16617, 16618, 16619, 16620, 16621, 16622, 16623, 16624, 16625, 16626, 16627, + 16628, 16629, 16630, 16631, 16632, 16633, 16634, 16635, 16636, 16637, 16638, 16639, 16640, 16641, 16642, 16643, + 16644, 16645, 16646, 16647, 16648, 16649, 16650, 16651, 16652, 16653, 16654, 16655, 16656, 16657, 16658, 16659, + 16660, 16661, 16662)); + + checkIntArray([0, 1, -1, 65535, -65535, 2147483647, -2147483648], + Int32Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + checkNumberArray([0.0, 1.0, 4294967295.0, 65535.0, 4294901761.0, 2147483647.0, 2147483648.0], + Uint32Array.of(0, 1, -1, 65535, -65535, 2147483647, -2147483648)); + + checkIntArray([-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, + 65534, 65535, 65536, 100000, 2147483647, -2147483648], + Int32Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, + 65534, 65535, 65536, 100000, 2147483647, -2147483648)); + checkNumberArray([4294967295.0, 4294934530.0, 4294934529.0, 4294934528.0, 4294934527.0, 4294901761.0, + 4294901760.0, 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, + 65536.0, 100000.0, 2147483647.0, 2147483648.0], + Uint32Array.of(-1, -32766, -32767, -32768, -32769, -65535, -65536, -65537, 0, 12, 255, 32766, 32765, 32768, 32769, + 65534, 65535, 65536, 100000, 2147483647, -2147483648)); +} + +function testIU32OfNumber() { + checkNumberArray([0.0, 1.0, 1.0, -1.0, 65535.0, -65535.0, 2147483647.0, -2147483648.0], + Int32Array.of(0.1, 1.5, 1.9, -1.0, 65535.0, -65535.0, 2147483647.0, -2147483648.0)); + checkNumberArray([0.0, 1.0, 1.0, 4294967295.0, 65535.0, 4294901761.0, 2147483647.0, 2147483648.0], + Uint32Array.of(0.1, 1.5, 1.9, 4294967295.0, 65535.0, 4294901761.0, 2147483647.0, 2147483648.0)); + + checkNumberArray([-1.0, -32766.0, -32767.0, -32768.0, -32769.0, -65535.0, -65536.0, -65537.0, 0.0, 12.0, 255.0, + 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, 65536.0, 100000.0, 2147483647.0, -2147483648.0], + Int32Array.of(-1.001, -32766.999, -32767.0, -32768.0, -32769.0, -65535.0, -65536.0, -65537.0, 0.0, 12.0, 255.0, + 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, 65536.0, 100000.0, 2147483647.0, -2147483648.0)); + checkNumberArray([4294967295.0, 4294934530.0, 4294934529.0, 4294934528.0, 4294934527.0, 4294901761.0, 4294901760.0, + 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, 65536.0, 100000.0, + 2147483647.0, 2147483648.0], + Uint32Array.of(-1.0, -32766.0, -32767.0, -32768.0, -32769.0, -65535.0, -65536.0, -65537.0, 0.0, 12.0, 255.0, + 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, 65536.0, 100000.0, 2147483647.0, -2147483648.0)); + + checkNumberArray([4294967295.0, 4294934530.0, 4294934529.0, 4294934528.0, 4294934527.0, 4294901761.0, + 4294901760.0, 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, + 65536.0, 100000.0, 2147483647.0, 2147483648.0], + Uint32Array.of(4294967295.1, 4294934530.5, 4294934529.9, 4294934528.0, 4294934527.0, 4294901761.0, + 4294901760.0, 4294901759.0, 0.0, 12.0, 255.0, 32766.0, 32765.0, 32768.0, 32769.0, 65534.0, 65535.0, + 65536.0, 100000.0, 2147483647.0, 2147483648.0)); +} + +function testF32OfNumber() { + checkNumberArray(new number[0], Float32Array.of()); + + let arr = prepareTestData(127, 1.0); + checkNumberArray(arr, Float32Array.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, + 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, + 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, + 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, + 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, + 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, + 109.0, 110.0, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0, 117.0, 118.0, 119.0, 120.0, 121.0, 122.0, 123.0, 124.0, + 125.0, 126.0, 127.0)); + + checkNumberArray([1, 65535, -65535, NaN, Infinity], Float32Array.of(1, 65535, -65535, NaN, Infinity)); + + checkNumberArray([1, NaN, Infinity, 0.00001f, Infinity, -Infinity, -Infinity, -0.6e+16f, Infinity, -3.31899e+38f, + 3.31899e+38f, -3.40282346e+38f, 3.40282346e+38f, -3.40282346e+38f, 3.40282346e+38f, -3.40282346e+38f, + 3.40282346e+38f, -3.40282346e+38f, 3.40282346e+38f, -Infinity, Infinity], + Float32Array.of(1.0, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105, + -3.31899e+38, 3.31899e+38, -3.4028234663852886e+38, 3.4028234663852886e+38, -3.402823466385289e+38, + 3.402823466385289e+38, -3.4028235677973359e+38, 3.4028235677973359e+38, -3.4028235677973362e+38, + 3.4028235677973362e+38, -3.4028235677973362e+38 - 1.0e+23, 3.4028235677973362e+38 + 1.0e+23)); +} + +function testF64OfNumber() { + checkNumberArray(new number[0], Float64Array.of()); + + let arr = prepareTestData(127, 1.0); + checkNumberArray(arr, Float64Array.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, + 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, + 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, + 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, + 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, + 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, + 109.0, 110.0, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0, 117.0, 118.0, 119.0, 120.0, 121.0, 122.0, 123.0, 124.0, + 125.0, 126.0, 127.0)); + + checkNumberArray([1, 65535, -65535, NaN, Infinity], Float64Array.of(1, 65535, -65535, NaN, Infinity)); + checkNumberArray([1, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105], + Float64Array.of(1, NaN, Infinity, 0.00001, 1.7e+308, -Infinity, -1.7e+308, -0.6e+16, 0.4e+105)); +} + +function testIU64OfBigInt() { + checkBigIntArray(new bigint[0], BigInt64Array.of()); + + const length: int = 127; + let arr: FixedArray = new bigint[length]; + for (let i = 0; i < length; i++) { + const v = new BigInt(i + 1); + arr[i] = v; + } + checkBigIntArray(arr, BigInt64Array.of(1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n, 9n, 10n, 11n, 12n, 13n, 14n, 15n, 16n, 17n, + 18n, 19n, 20n, 21n, 22n, 23n, 24n, 25n, 26n, 27n, 28n, 29n, 30n, 31n, 32n, 33n, 34n, 35n, 36n, 37n, 38n, 39n, + 40n, 41n, 42n, 43n, 44n, 45n, 46n, 47n, 48n, 49n, 50n, 51n, 52n, 53n, 54n, 55n, 56n, 57n, 58n, 59n, 60n, 61n, + 62n, 63n, 64n, 65n, 66n, 67n, 68n, 69n, 70n, 71n, 72n, 73n, 74n, 75n, 76n, 77n, 78n, 79n, 80n, 81n, 82n, 83n, + 84n, 85n, 86n, 87n, 88n, 89n, 90n, 91n, 92n, 93n, 94n, 95n, 96n, 97n, 98n, 99n, 100n, 101n, 102n, 103n, 104n, + 105n, 106n, 107n, 108n, 109n, 110n, 111n, 112n, 113n, 114n, 115n, 116n, 117n, 118n, 119n, 120n, 121n, 122n, 123n, + 124n, 125n, 126n, 127n)); + checkBigIntArray(arr, BigUint64Array.of(1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n, 9n, 10n, 11n, 12n, 13n, 14n, 15n, 16n, 17n, + 18n, 19n, 20n, 21n, 22n, 23n, 24n, 25n, 26n, 27n, 28n, 29n, 30n, 31n, 32n, 33n, 34n, 35n, 36n, 37n, 38n, 39n, + 40n, 41n, 42n, 43n, 44n, 45n, 46n, 47n, 48n, 49n, 50n, 51n, 52n, 53n, 54n, 55n, 56n, 57n, 58n, 59n, 60n, 61n, + 62n, 63n, 64n, 65n, 66n, 67n, 68n, 69n, 70n, 71n, 72n, 73n, 74n, 75n, 76n, 77n, 78n, 79n, 80n, 81n, 82n, 83n, + 84n, 85n, 86n, 87n, 88n, 89n, 90n, 91n, 92n, 93n, 94n, 95n, 96n, 97n, 98n, 99n, 100n, 101n, 102n, 103n, 104n, + 105n, 106n, 107n, 108n, 109n, 110n, 111n, 112n, 113n, 114n, 115n, 116n, 117n, 118n, 119n, 120n, 121n, 122n, 123n, + 124n, 125n, 126n, 127n)); + + checkBigIntArray([1n, BigInt(1.e+5), 0n, 0n, BigInt(-0.6e+16), 0n, -18014398509481983n, 18014398509481983n, + -45035996273704959n], + BigInt64Array.of(1n, BigInt(1.e+5), 0n, 0n, BigInt(-0.6e+16), 0n, -18014398509481983n, 18014398509481983n, + -45035996273704959n)); + checkBigIntArray([1n, BigInt(1.e+5), 0n, 0n, 18440744073709551616n, 0n, 18428729675200069633n, 18014398509481983n, + 18401708077435846657n], + BigUint64Array.of(1n, BigInt(1.e+5), 0n, 0n, BigInt(-0.6e+16), 0n, -18014398509481983n, 18014398509481983n, + -45035996273704959n)); + checkBigIntArray([1n, 100000n, 0n, 0n, 18440744073709551616n, 0n, 18428729675200069633n, 18014398509481983n, + 18401708077435846657n], + BigUint64Array.of(1n, 100000n, 0n, 0n, 18440744073709551616n, 0n, 18428729675200069633n, 18014398509481983n, + 18401708077435846657n)); +} + +function prepareTestData(length: int, from: number): FixedArray { + let fixarr: FixedArray = new number[length]; + let v: number = from; + for (let i = 0; i < length; i++) { + fixarr[i] = v++; + } + return fixarr; +} + +function prepareTestData(length: int, from: int): FixedArray { + let fixarr: FixedArray = new int[length]; + let v: int = from; + for (let i = 0; i < length; i++) { + fixarr[i] = v++; + } + return fixarr; +} + +function checkNumberArray(expected: FixedArray, data: ArrayLike): void { + arktest.assertEQ(data.length, expected.length, 'Unexpected array length'); + for (let i: int = 0; i < expected.length; i++) { + arktest.assertTrue((isNaN(data[i]) && isNaN(expected[i])) || (data[i] == expected[i]), + 'Unexpected element with index ' + i + ': expected ' + expected[i] + ' but was ' + data[i]); + } +} + +function checkIntArray(expected: FixedArray, data: ArrayLike): void { + arktest.assertEQ(data.length, expected.length, 'Unexpected array length'); + for (let i: int = 0; i < expected.length; i++) { + arktest.assertTrue(data[i] == expected[i], + 'Unexpected element with index ' + i + ': expected ' + expected[i] + ' but was ' + data[i]); + } +} + +function checkBigIntArray(expected: FixedArray, data: ArrayLike): void { + arktest.assertEQ(data.length, expected.length, 'Unexpected array length'); + for (let i: int = 0; i < expected.length; i++) { + arktest.assertEQ(data[i], expected[i], 'Unexpected element with index ' + i); + } +} + +function main(): int { + let testSuite = new arktest.ArkTestsuite('typedArrays.of'); + testSuite.addTest('BigInt64,BigUint64 Arrays: of test', testIU64OfBigInt); + testSuite.addTest('Int8,Uint8 Arrays: of test', testIU8OfInt); + testSuite.addTest('Int16,Uint16 Arrays: of test', testIU16OfInt); + testSuite.addTest('Int32,Uint32 Arrays: of test', testIU32OfInt); + testSuite.addTest('Int32,Uint32 Arrays: of test', testIU32OfNumber); + testSuite.addTest('Float32 Arrays: of test', testF32OfNumber); + testSuite.addTest('Float64 Arrays: of test', testF64OfNumber); + return testSuite.run(); +}