[WASM] Add std methods for collections

This commit is contained in:
Igor Yakovlev
2021-12-21 12:58:35 +01:00
committed by igoriakovlev
parent c9a92d71ae
commit e58d4163ad
8 changed files with 42 additions and 54 deletions
-2
View File
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: WASM
// WASM_MUTE_REASON: STDLIB_GENERATED
// WITH_STDLIB // WITH_STDLIB
import Game.* import Game.*
-2
View File
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: WASM
// WASM_MUTE_REASON: STDLIB_SORT
// WITH_STDLIB // WITH_STDLIB
fun box(): String { fun box(): String {
-2
View File
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: WASM
// WASM_MUTE_REASON: STDLIB_GENERATED
// WITH_STDLIB // WITH_STDLIB
fun box(): String { fun box(): String {
-2
View File
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: WASM
// WASM_MUTE_REASON: IGNORED_IN_JS
// IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR
// IGNORE_BACKEND: JS_IR_ES6 // IGNORE_BACKEND: JS_IR_ES6
// TODO: muted automatically, investigate should it be ran for JS or not // TODO: muted automatically, investigate should it be ran for JS or not
@@ -22,22 +22,6 @@ internal inline fun <E> arrayOfUninitializedElements(size: Int): Array<E> {
return Array<E>(size) return Array<E>(size)
} }
/**
* Returns a new array which is a copy of the original array with new elements filled with null values.
*/
internal fun <E> Array<E>.copyOfNulls(newSize: Int): Array<E?> = copyOfNulls(0, newSize)
internal fun <E> Array<E>.copyOfNulls(fromIndex: Int, toIndex: Int): Array<E?> {
val newSize = toIndex - fromIndex
if (newSize < 0) {
throw IllegalArgumentException("$fromIndex > $toIndex")
}
val result = @Suppress("TYPE_PARAMETER_AS_REIFIED") arrayOfNulls<E>(newSize)
this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size))
return result
}
/** /**
* Copies elements of the [collection] into the given [array]. * Copies elements of the [collection] into the given [array].
* If the array is too small, allocates a new one of collection.size size. * If the array is too small, allocates a new one of collection.size size.
@@ -123,3 +123,18 @@ public actual inline fun <reified T> Collection<T>.toTypedArray(): Array<T> {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return result as Array<T> return result as Array<T>
} }
/**
* Returns a new array which is a copy of the original array with new elements filled with null values.
*/
internal fun <E> Array<E>.copyOfNulls(newSize: Int): Array<E?> = copyOfNulls(0, newSize)
internal fun <E> Array<E>.copyOfNulls(fromIndex: Int, toIndex: Int): Array<E?> {
val newSize = toIndex - fromIndex
if (newSize < 0) {
throw IllegalArgumentException("$fromIndex > $toIndex")
}
val result = @Suppress("TYPE_PARAMETER_AS_REIFIED") arrayOfNulls<E>(newSize)
this.copyInto(result, 0, fromIndex, toIndex.coerceAtMost(size))
return result
}
@@ -1499,7 +1499,7 @@ public actual fun CharArray.copyOf(newSize: Int): CharArray {
* @sample samples.collections.Arrays.CopyOfOperations.resizingCopyOf * @sample samples.collections.Arrays.CopyOfOperations.resizingCopyOf
*/ */
public actual fun <T> Array<T>.copyOf(newSize: Int): Array<T?> { public actual fun <T> Array<T>.copyOf(newSize: Int): Array<T?> {
TODO("Wasm stdlib: copyOf(newSize: Int)") return this.copyOfNulls(newSize)
} }
/** /**
@@ -2299,7 +2299,7 @@ public actual inline fun <T> Array<T>.plusElement(element: T): Array<T> {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun IntArray.sort(): Unit { public actual fun IntArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2308,7 +2308,7 @@ public actual fun IntArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun LongArray.sort(): Unit { public actual fun LongArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2317,7 +2317,7 @@ public actual fun LongArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun ByteArray.sort(): Unit { public actual fun ByteArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2326,7 +2326,7 @@ public actual fun ByteArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun ShortArray.sort(): Unit { public actual fun ShortArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2335,7 +2335,7 @@ public actual fun ShortArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun DoubleArray.sort(): Unit { public actual fun DoubleArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2344,7 +2344,7 @@ public actual fun DoubleArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun FloatArray.sort(): Unit { public actual fun FloatArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2353,7 +2353,7 @@ public actual fun FloatArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArray * @sample samples.collections.Arrays.Sorting.sortArray
*/ */
public actual fun CharArray.sort(): Unit { public actual fun CharArray.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2364,7 +2364,7 @@ public actual fun CharArray.sort(): Unit {
* @sample samples.collections.Arrays.Sorting.sortArrayOfComparable * @sample samples.collections.Arrays.Sorting.sortArrayOfComparable
*/ */
public actual fun <T : Comparable<T>> Array<out T>.sort(): Unit { public actual fun <T : Comparable<T>> Array<out T>.sort(): Unit {
TODO("Wasm stdlib: sort()") if (size > 1) sortArray(this, 0, size)
} }
/** /**
@@ -2383,7 +2383,8 @@ public actual fun <T : Comparable<T>> Array<out T>.sort(): Unit {
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2400,7 +2401,8 @@ public actual fun <T : Comparable<T>> Array<out T>.sort(fromIndex: Int = 0, toIn
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2417,7 +2419,8 @@ public actual fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2434,7 +2437,8 @@ public actual fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2451,7 +2455,8 @@ public actual fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2468,7 +2473,8 @@ public actual fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2485,7 +2491,8 @@ public actual fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2502,7 +2509,8 @@ public actual fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Uni
@SinceKotlin("1.4") @SinceKotlin("1.4")
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sort(fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArray(this, fromIndex, toIndex)
} }
/** /**
@@ -2527,7 +2535,8 @@ public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>): Unit
*/ */
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Unit { public actual fun <T> Array<out T>.sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Unit {
TODO("Wasm stdlib: sortWith(comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size)") AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
sortArrayWith(this, fromIndex, toIndex, comparator)
} }
/** /**
@@ -1174,9 +1174,6 @@ object ArrayOps : TemplateGroupBase() {
} }
on(Platform.Native) { on(Platform.Native) {
body { "return this.copyOfNulls(newSize)" } body { "return this.copyOfNulls(newSize)" }
on(Backend.Wasm) {
body { """TODO("Wasm stdlib: $signature")""" }
}
} }
} }
specialFor(ArraysOfPrimitives, InvariantArraysOfObjects) { specialFor(ArraysOfPrimitives, InvariantArraysOfObjects) {
@@ -1259,9 +1256,6 @@ object ArrayOps : TemplateGroupBase() {
} }
on(Platform.Native) { on(Platform.Native) {
body { """if (size > 1) sortArray(this, 0, size)""" } body { """if (size > 1) sortArray(this, 0, size)""" }
on(Backend.Wasm) {
body { """TODO("Wasm stdlib: $signature")""" }
}
} }
} }
} }
@@ -1396,9 +1390,6 @@ object ArrayOps : TemplateGroupBase() {
sortArray(this, fromIndex, toIndex) sortArray(this, fromIndex, toIndex)
""" """
} }
on(Backend.Wasm) {
body { """TODO("Wasm stdlib: $signature")""" }
}
} }
on(Platform.JS) { on(Platform.JS) {
since("1.4") since("1.4")
@@ -1455,9 +1446,6 @@ object ArrayOps : TemplateGroupBase() {
sortArrayWith(this, fromIndex, toIndex, comparator) sortArrayWith(this, fromIndex, toIndex, comparator)
""" """
} }
on(Backend.Wasm) {
body { """TODO("Wasm stdlib: $signature")""" }
}
} }
on(Platform.JS) { on(Platform.JS) {
since("1.4") since("1.4")