diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/JsIrTestRuntime.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/JsIrTestRuntime.kt index 80e0961eb71..41b58480ad4 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/JsIrTestRuntime.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/JsIrTestRuntime.kt @@ -127,6 +127,7 @@ val reducedRuntimeSources = fullRuntimeSources - listOfKtFilesFrom( "libraries/stdlib/common/src/generated/_Strings.kt", "libraries/stdlib/common/src/generated/_UArrays.kt", "libraries/stdlib/common/src/generated/_URanges.kt", + "libraries/stdlib/common/src/generated/_UCollections.kt", "libraries/stdlib/common/src/kotlin/SequencesH.kt", "libraries/stdlib/common/src/kotlin/TextH.kt", "libraries/stdlib/common/src/kotlin/collections/", diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index cc15c5828d8..e27e576445d 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -6740,80 +6740,56 @@ public expect fun Array.sortWith(comparator: Comparator): Unit * Returns an array of Boolean containing all of the elements of this generic array. */ public fun Array.toBooleanArray(): BooleanArray { - val result = BooleanArray(size) - for (index in indices) - result[index] = this[index] - return result + return BooleanArray(size) { index -> this[index] } } /** * Returns an array of Byte containing all of the elements of this generic array. */ public fun Array.toByteArray(): ByteArray { - val result = ByteArray(size) - for (index in indices) - result[index] = this[index] - return result + return ByteArray(size) { index -> this[index] } } /** * Returns an array of Char containing all of the elements of this generic array. */ public fun Array.toCharArray(): CharArray { - val result = CharArray(size) - for (index in indices) - result[index] = this[index] - return result + return CharArray(size) { index -> this[index] } } /** * Returns an array of Double containing all of the elements of this generic array. */ public fun Array.toDoubleArray(): DoubleArray { - val result = DoubleArray(size) - for (index in indices) - result[index] = this[index] - return result + return DoubleArray(size) { index -> this[index] } } /** * Returns an array of Float containing all of the elements of this generic array. */ public fun Array.toFloatArray(): FloatArray { - val result = FloatArray(size) - for (index in indices) - result[index] = this[index] - return result + return FloatArray(size) { index -> this[index] } } /** * Returns an array of Int containing all of the elements of this generic array. */ public fun Array.toIntArray(): IntArray { - val result = IntArray(size) - for (index in indices) - result[index] = this[index] - return result + return IntArray(size) { index -> this[index] } } /** * Returns an array of Long containing all of the elements of this generic array. */ public fun Array.toLongArray(): LongArray { - val result = LongArray(size) - for (index in indices) - result[index] = this[index] - return result + return LongArray(size) { index -> this[index] } } /** * Returns an array of Short containing all of the elements of this generic array. */ public fun Array.toShortArray(): ShortArray { - val result = ShortArray(size) - for (index in indices) - result[index] = this[index] - return result + return ShortArray(size) { index -> this[index] } } /** diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index a8929bdf2a2..c0b1c2c18f8 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -1215,6 +1215,15 @@ public fun UShortArray.toTypedArray(): Array { return Array(size) { index -> this[index] } } +/** + * Returns an array of UByte containing all of the elements of this generic array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Array.toUByteArray(): UByteArray { + return UByteArray(size) { index -> this[index] } +} + /** * Returns an array of type [UByteArray], which is a copy of this array where each element is an unsigned reinterpretation * of the corresponding element of this array. @@ -1226,6 +1235,15 @@ public inline fun ByteArray.toUByteArray(): UByteArray { return UByteArray(this.copyOf()) } +/** + * Returns an array of UInt containing all of the elements of this generic array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Array.toUIntArray(): UIntArray { + return UIntArray(size) { index -> this[index] } +} + /** * Returns an array of type [UIntArray], which is a copy of this array where each element is an unsigned reinterpretation * of the corresponding element of this array. @@ -1237,6 +1255,15 @@ public inline fun IntArray.toUIntArray(): UIntArray { return UIntArray(this.copyOf()) } +/** + * Returns an array of ULong containing all of the elements of this generic array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Array.toULongArray(): ULongArray { + return ULongArray(size) { index -> this[index] } +} + /** * Returns an array of type [ULongArray], which is a copy of this array where each element is an unsigned reinterpretation * of the corresponding element of this array. @@ -1248,6 +1275,15 @@ public inline fun LongArray.toULongArray(): ULongArray { return ULongArray(this.copyOf()) } +/** + * Returns an array of UShort containing all of the elements of this generic array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Array.toUShortArray(): UShortArray { + return UShortArray(size) { index -> this[index] } +} + /** * Returns an array of type [UShortArray], which is a copy of this array where each element is an unsigned reinterpretation * of the corresponding element of this array. diff --git a/libraries/stdlib/common/src/generated/_UCollections.kt b/libraries/stdlib/common/src/generated/_UCollections.kt new file mode 100644 index 00000000000..04fe1df8587 --- /dev/null +++ b/libraries/stdlib/common/src/generated/_UCollections.kt @@ -0,0 +1,69 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("UCollectionsKt") + +package kotlin.collections + +// +// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// + +import kotlin.random.* + +/** + * Returns an array of UByte containing all of the elements of this collection. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Collection.toUByteArray(): UByteArray { + val result = UByteArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of UInt containing all of the elements of this collection. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Collection.toUIntArray(): UIntArray { + val result = UIntArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of ULong containing all of the elements of this collection. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Collection.toULongArray(): ULongArray { + val result = ULongArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} + +/** + * Returns an array of UShort containing all of the elements of this collection. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun Collection.toUShortArray(): UShortArray { + val result = UShortArray(size) + var index = 0 + for (element in this) + result[index++] = element + return result +} + diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index d714934d09e..8ee462e674c 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -334,4 +334,18 @@ class UnsignedArraysTest { assertEquals(0.0, uintArrayOf(0, 2, 4).sumByDouble { (it % 2u).toInt().toDouble() }) assertEquals(6.0, ulongArrayOf(2, 3, 4).sumByDouble { (it - 1u).toInt().toDouble() }) } + + @Test + fun toUnsignedArray() { + val uintList = listOf(1u, 2u, 3u) + val uintArray: UIntArray = uintList.toUIntArray() + expect(3) { uintArray.size } + assertEquals(uintList, uintArray.toList()) + + val genericArray: Array = arrayOf(1, 2, 3) + val ulongArray: ULongArray = genericArray.toULongArray() + expect(3) { ulongArray.size } + assertEquals(genericArray.toList(), ulongArray.toList()) + } + } \ No newline at end of file diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index bca46db0125..67ee17b87be 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -2327,6 +2327,10 @@ public final class kotlin/collections/UArraysKt { public static final fun toTypedArray-GBYM_sE ([B)[Lkotlin/UByte; public static final fun toTypedArray-QwZRm1k ([J)[Lkotlin/ULong; public static final fun toTypedArray-rL5Bavg ([S)[Lkotlin/UShort; + public static final fun toUByteArray ([Lkotlin/UByte;)[B + public static final fun toUIntArray ([Lkotlin/UInt;)[I + public static final fun toULongArray ([Lkotlin/ULong;)[J + public static final fun toUShortArray ([Lkotlin/UShort;)[S } public abstract class kotlin/collections/UByteIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker { @@ -2337,6 +2341,13 @@ public abstract class kotlin/collections/UByteIterator : java/util/Iterator, kot public fun remove ()V } +public final class kotlin/collections/UCollectionsKt { + public static final fun toUByteArray (Ljava/util/Collection;)[B + public static final fun toUIntArray (Ljava/util/Collection;)[I + public static final fun toULongArray (Ljava/util/Collection;)[J + public static final fun toUShortArray (Ljava/util/Collection;)[S +} + public abstract class kotlin/collections/UIntIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker { public fun ()V public synthetic fun next ()Ljava/lang/Object; diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 65a9e25ed53..fb4e1dbee19 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -322,26 +322,35 @@ object ArrayOps : TemplateGroupBase() { } val f_toPrimitiveArray = fn("toPrimitiveArray()") { - include(ArraysOfObjects, PrimitiveType.defaultPrimitives) - include(Collections, PrimitiveType.defaultPrimitives) + include(ArraysOfObjects, PrimitiveType.values().toSet()) + include(Collections, PrimitiveType.values().toSet()) } builder { val primitive = checkNotNull(primitive) val arrayType = primitive.name + "Array" signature("to$arrayType()") returns(arrayType) + + if (primitive in PrimitiveType.unsignedPrimitives) { + since("1.3") + annotation("@ExperimentalUnsignedTypes") + } + // TODO: Use different implementations for JS specialFor(ArraysOfObjects) { + if (primitive in PrimitiveType.unsignedPrimitives) { + sourceFile(SourceFile.UArrays) + } doc { "Returns an array of ${primitive.name} containing all of the elements of this generic array." } body { """ - val result = $arrayType(size) - for (index in indices) - result[index] = this[index] - return result + return $arrayType(size) { index -> this[index] } """ } } specialFor(Collections) { + if (primitive in PrimitiveType.unsignedPrimitives) { + sourceFile(SourceFile.UCollections) + } doc { "Returns an array of ${primitive.name} containing all of the elements of this collection." } body { """ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/SourceFile.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/SourceFile.kt index 425f511cfc8..23201c3a587 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/SourceFile.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/dsl/SourceFile.kt @@ -10,6 +10,7 @@ enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = tru Arrays(packageName = "kotlin.collections"), UArrays(packageName = "kotlin.collections"), Collections(packageName = "kotlin.collections"), + UCollections(packageName = "kotlin.collections"), Sets(packageName = "kotlin.collections"), Maps(packageName = "kotlin.collections"), Sequences(packageName = "kotlin.sequences"),