Null-terminate Collection.toArray destination only in JVM

In other platforms the elements following the collection elements
should not be changed.

As a part of efforts to stabilize Native stdlib.
This commit is contained in:
Abduqodiri Qurbonzoda
2023-06-01 12:52:46 +03:00
committed by Space Team
parent 60455daa9d
commit 6fdfd4e8dd
14 changed files with 46 additions and 40 deletions
@@ -85,6 +85,14 @@ internal actual inline fun copyToArrayImpl(collection: Collection<*>): Array<Any
internal actual inline fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> =
kotlin.jvm.internal.collectionToArray(collection, array as Array<Any?>) as Array<T>
internal actual fun <T> terminateCollectionToArray(collectionSize: Int, array: Array<T>): Array<T> {
if (collectionSize < array.size) {
@Suppress("UNCHECKED_CAST")
array[collectionSize] = null as T // null-terminate
}
return array
}
// copies typed varargs array to array of objects
internal actual fun <T> Array<out T>.copyToArrayOfAny(isVarargs: Boolean): Array<out Any?> =
if (isVarargs && this.javaClass == Array<Any?>::class.java)
@@ -176,12 +176,7 @@ internal class ListBuilder<E> private constructor(
@Suppress("UNCHECKED_CAST")
(array as Array<T>).copyInto(destination, 0, startIndex = offset, endIndex = offset + length)
if (destination.size > length) {
@Suppress("UNCHECKED_CAST")
destination[length] = null as T // null-terminate
}
return destination
return terminateCollectionToArray(length, destination)
}
override fun toArray(): Array<Any?> {