diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArraysNative.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArraysNative.kt new file mode 100644 index 00000000000..de95ebf79d4 --- /dev/null +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/collections/ArraysNative.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin.collections + +/** + * Returns a *typed* array containing all of the elements of this collection. + * + * Allocates an array of runtime type `T` having its size equal to the size of this collection + * and populates the array with the elements of this collection. + * @sample samples.collections.Collections.Collections.collectionToTypedArray + */ +public actual inline fun Collection.toTypedArray(): Array { + val result = arrayOfNulls(size) + var index = 0 + for (element in this) result[index++] = element + @Suppress("UNCHECKED_CAST") + return result as Array +} + +@Suppress("UNCHECKED_CAST") +internal actual fun copyToArrayImpl(collection: Collection<*>, array: Array): Array { + if (array.size < collection.size) + return copyToArrayImpl(collection) as Array + + val iterator = collection.iterator() + var index = 0 + while (iterator.hasNext()) { + array[index++] = iterator.next() as T + } + if (index < array.size) { + return array.copyOf(index) as Array + } + return array +} \ No newline at end of file diff --git a/libraries/stdlib/native-wasm/src/kotlin/collections/Arrays.kt b/libraries/stdlib/native-wasm/src/kotlin/collections/Arrays.kt index b94a7935607..ceacd204d47 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/collections/Arrays.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/collections/Arrays.kt @@ -93,37 +93,6 @@ internal actual fun copyToArrayImpl(collection: Collection<*>): Array { return array } -@Suppress("UNCHECKED_CAST") -internal actual fun copyToArrayImpl(collection: Collection<*>, array: Array): Array { - if (array.size < collection.size) - return copyToArrayImpl(collection) as Array - - val iterator = collection.iterator() - var index = 0 - while (iterator.hasNext()) { - array[index++] = iterator.next() as T - } - if (index < array.size) { - return array.copyOf(index) as Array - } - return array -} - -/** - * Returns a *typed* array containing all of the elements of this collection. - * - * Allocates an array of runtime type `T` having its size equal to the size of this collection - * and populates the array with the elements of this collection. - * @sample samples.collections.Collections.Collections.collectionToTypedArray - */ -public actual inline fun Collection.toTypedArray(): Array { - val result = arrayOfNulls(size) - var index = 0 - for (element in this) result[index++] = element - @Suppress("UNCHECKED_CAST") - return result as Array -} - /** * Returns a new array which is a copy of the original array with new elements filled with null values. */ diff --git a/libraries/stdlib/wasm/src/kotlin/collections/ArraysWasm.kt b/libraries/stdlib/wasm/src/kotlin/collections/ArraysWasm.kt new file mode 100644 index 00000000000..7143e735332 --- /dev/null +++ b/libraries/stdlib/wasm/src/kotlin/collections/ArraysWasm.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package kotlin.collections + +/** + * Returns a *typed* array containing all of the elements of this collection. + * + * Allocates an array of runtime type `T` having its size equal to the size of this collection + * and populates the array with the elements of this collection. + * @sample samples.collections.Collections.Collections.collectionToTypedArray + */ +@kotlin.internal.InlineOnly +public actual inline fun Collection.toTypedArray(): Array = copyToArray(this) + +@PublishedApi +internal fun copyToArray(collection: Collection): Array = + if (collection is AbstractCollection) + //TODO: Find more proper way to call abstract collection's toArray + @Suppress("INVISIBLE_MEMBER") collection.toArray() as Array + else + copyToArrayImpl(collection) as Array + +@Suppress("UNCHECKED_CAST") +internal actual fun copyToArrayImpl(collection: Collection<*>, array: Array): Array { + if (array.size < collection.size) + return copyToArrayImpl(collection) as Array + + val iterator = collection.iterator() + var index = 0 + while (iterator.hasNext()) { + array[index++] = iterator.next() as T + } + if (index < array.size) { + (array as Array).fill(null, index) + } + return array +} \ No newline at end of file