[WASM] Workaround about call toTypedArray on AbstractCollection
This commit is contained in:
@@ -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 <reified T> Collection<T>.toTypedArray(): Array<T> {
|
||||||
|
val result = arrayOfNulls<T>(size)
|
||||||
|
var index = 0
|
||||||
|
for (element in this) result[index++] = element
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
return result as Array<T>
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> {
|
||||||
|
if (array.size < collection.size)
|
||||||
|
return copyToArrayImpl(collection) as Array<T>
|
||||||
|
|
||||||
|
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<T>
|
||||||
|
}
|
||||||
|
return array
|
||||||
|
}
|
||||||
@@ -93,37 +93,6 @@ internal actual fun copyToArrayImpl(collection: Collection<*>): Array<Any?> {
|
|||||||
return array
|
return array
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> {
|
|
||||||
if (array.size < collection.size)
|
|
||||||
return copyToArrayImpl(collection) as Array<T>
|
|
||||||
|
|
||||||
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<T>
|
|
||||||
}
|
|
||||||
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 <reified T> Collection<T>.toTypedArray(): Array<T> {
|
|
||||||
val result = arrayOfNulls<T>(size)
|
|
||||||
var index = 0
|
|
||||||
for (element in this) result[index++] = element
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
return result as Array<T>
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new array which is a copy of the original array with new elements filled with null values.
|
* Returns a new array which is a copy of the original array with new elements filled with null values.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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 <T> Collection<T>.toTypedArray(): Array<T> = copyToArray(this)
|
||||||
|
|
||||||
|
@PublishedApi
|
||||||
|
internal fun <T> copyToArray(collection: Collection<T>): Array<T> =
|
||||||
|
if (collection is AbstractCollection<T>)
|
||||||
|
//TODO: Find more proper way to call abstract collection's toArray
|
||||||
|
@Suppress("INVISIBLE_MEMBER") collection.toArray() as Array<T>
|
||||||
|
else
|
||||||
|
copyToArrayImpl(collection) as Array<T>
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
internal actual fun <T> copyToArrayImpl(collection: Collection<*>, array: Array<T>): Array<T> {
|
||||||
|
if (array.size < collection.size)
|
||||||
|
return copyToArrayImpl(collection) as Array<T>
|
||||||
|
|
||||||
|
val iterator = collection.iterator()
|
||||||
|
var index = 0
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
array[index++] = iterator.next() as T
|
||||||
|
}
|
||||||
|
if (index < array.size) {
|
||||||
|
(array as Array<T?>).fill(null, index)
|
||||||
|
}
|
||||||
|
return array
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user