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
@@ -541,12 +541,9 @@ public class ArrayDeque<E> : AbstractMutableList<E> {
elementData.copyInto(dest, destinationOffset = 0, startIndex = head, endIndex = elementData.size)
elementData.copyInto(dest, destinationOffset = elementData.size - head, startIndex = 0, endIndex = tail)
}
if (dest.size > size) {
dest[size] = null // null-terminate
}
@Suppress("UNCHECKED_CAST")
return dest as Array<T>
return terminateCollectionToArray(size, dest) as Array<T>
}
@Suppress("NOTHING_TO_OVERRIDE")
@@ -479,3 +479,9 @@ internal fun throwIndexOverflow() { throw ArithmeticException("Index overflow ha
@SinceKotlin("1.3")
internal fun throwCountOverflow() { throw ArithmeticException("Count overflow has happened.") }
/**
* In JVM if the size of [array] is bigger than [collectionSize], sets `array[collectionSize] = null`.
* In other platforms does nothing.
* Returns the given [array].
*/
internal expect fun <T> terminateCollectionToArray(collectionSize: Int, array: Array<T>): Array<T>
@@ -146,9 +146,8 @@ private class RingBuffer<T>(private val buffer: Array<Any?>, filledSize: Int) :
widx++
idx++
}
if (result.size > this.size) result[this.size] = null
return result as Array<T>
return terminateCollectionToArray(size, result) as Array<T>
}
override fun toArray(): Array<Any?> {