diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/kotlin_special.kt index 897655626a6..a8e8ea898eb 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/kotlin_special.kt @@ -286,7 +286,7 @@ public inline fun ShortArray.copyOfRange(from: Int, to: Int): ShortArray { * Returns an array containing all elements of the original array and then all elements of the given [array]. */ suppress("NOTHING_TO_INLINE") -public inline fun Array.plus(array: Array): Array { +public inline fun Array.plus(array: Array): Array { return (this: dynamic).concat(array) } @@ -357,7 +357,7 @@ public inline fun ShortArray.plus(array: ShortArray): ShortArray { /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ -public fun Array.plus(collection: Collection): Array { +public fun Array.plus(collection: Collection): Array { return arrayPlusCollection(this, collection) } @@ -420,81 +420,72 @@ public fun ShortArray.plus(collection: Collection): ShortArray { /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun Array.plus(element: T): Array { - val result = this.copyOf() - (result: dynamic).push(element) - return result as Array +suppress("NOTHING_TO_INLINE") +public inline fun Array.plus(element: T): Array { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun BooleanArray.plus(element: Boolean): BooleanArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as BooleanArray +suppress("NOTHING_TO_INLINE") +public inline fun BooleanArray.plus(element: Boolean): BooleanArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun ByteArray.plus(element: Byte): ByteArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as ByteArray +suppress("NOTHING_TO_INLINE") +public inline fun ByteArray.plus(element: Byte): ByteArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun CharArray.plus(element: Char): CharArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as CharArray +suppress("NOTHING_TO_INLINE") +public inline fun CharArray.plus(element: Char): CharArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun DoubleArray.plus(element: Double): DoubleArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as DoubleArray +suppress("NOTHING_TO_INLINE") +public inline fun DoubleArray.plus(element: Double): DoubleArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun FloatArray.plus(element: Float): FloatArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as FloatArray +suppress("NOTHING_TO_INLINE") +public inline fun FloatArray.plus(element: Float): FloatArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun IntArray.plus(element: Int): IntArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as IntArray +suppress("NOTHING_TO_INLINE") +public inline fun IntArray.plus(element: Int): IntArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun LongArray.plus(element: Long): LongArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as LongArray +suppress("NOTHING_TO_INLINE") +public inline fun LongArray.plus(element: Long): LongArray { + return (this: dynamic).concat(arrayOf(element)) } /** * Returns an array containing all elements of the original array and then the given [element]. */ -public fun ShortArray.plus(element: Short): ShortArray { - val result = this.copyOf() - (result: dynamic).push(element) - return result as ShortArray +suppress("NOTHING_TO_INLINE") +public inline fun ShortArray.plus(element: Short): ShortArray { + return (this: dynamic).concat(arrayOf(element)) } diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt index 9aa542ba942..004e2e79538 100644 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ b/libraries/stdlib/src/generated/_SpecialJVM.kt @@ -196,8 +196,8 @@ public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: /** * Returns new array which is a copy of the original array. */ -public fun Array.copyOf(): Array { - return Arrays.copyOf(this, size()) as Array +public fun Array.copyOf(): Array { + return Arrays.copyOf(this, size()) } /** @@ -259,8 +259,16 @@ public fun ShortArray.copyOf(): ShortArray { /** * Returns new array which is a copy of the original array. */ -public fun Array.copyOf(newSize: Int): Array { - return Arrays.copyOf(this, newSize) as Array +platformName("mutableCopyOf") +public fun Array.copyOf(): Array { + return Arrays.copyOf(this, size()) +} + +/** + * Returns new array which is a copy of the original array. + */ +public fun Array.copyOf(newSize: Int): Array { + return Arrays.copyOf(this, newSize) } /** @@ -319,10 +327,18 @@ public fun ShortArray.copyOf(newSize: Int): ShortArray { return Arrays.copyOf(this, newSize) } +/** + * Returns new array which is a copy of the original array. + */ +platformName("mutableCopyOf") +public fun Array.copyOf(newSize: Int): Array { + return Arrays.copyOf(this, newSize) +} + /** * Returns new array which is a copy of range of original array. */ -public fun Array.copyOfRange(from: Int, to: Int): Array { +public fun Array.copyOfRange(from: Int, to: Int): Array { return Arrays.copyOfRange(this, from, to) } @@ -383,11 +399,11 @@ public fun ShortArray.copyOfRange(from: Int, to: Int): ShortArray { } /** - * Fills original array with the provided value. + * Returns new array which is a copy of range of original array. */ -public fun Array.fill(element: T): Array { - Arrays.fill(this, element) - return this +platformName("mutableCopyOfRange") +public fun Array.copyOfRange(from: Int, to: Int): Array { + return Arrays.copyOfRange(this, from, to) } /** @@ -454,6 +470,14 @@ public fun ShortArray.fill(element: Short): ShortArray { return this } +/** + * Fills original array with the provided value. + */ +public fun Array.fill(element: T): Array { + Arrays.fill(this, element) + return this +} + /** * Returns a list containing all elements that are instances of specified type parameter R. */ @@ -544,26 +568,15 @@ public fun , R> Sequence<*>.filterIsInstanceTo(desti return destination } -/** - * Returns an array containing all elements of the original array and then all elements of the given [array]. - */ -public fun Array.plus(array: Array): Array { - val thisSize = size() - val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) - System.arraycopy(array, 0, result, thisSize, arraySize) - return result as Array -} - /** * Returns an array containing all elements of the original array and then all elements of the given [array]. */ public fun BooleanArray.plus(array: BooleanArray): BooleanArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as BooleanArray + return result } /** @@ -572,9 +585,9 @@ public fun BooleanArray.plus(array: BooleanArray): BooleanArray { public fun ByteArray.plus(array: ByteArray): ByteArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as ByteArray + return result } /** @@ -583,9 +596,9 @@ public fun ByteArray.plus(array: ByteArray): ByteArray { public fun CharArray.plus(array: CharArray): CharArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as CharArray + return result } /** @@ -594,9 +607,9 @@ public fun CharArray.plus(array: CharArray): CharArray { public fun DoubleArray.plus(array: DoubleArray): DoubleArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as DoubleArray + return result } /** @@ -605,9 +618,9 @@ public fun DoubleArray.plus(array: DoubleArray): DoubleArray { public fun FloatArray.plus(array: FloatArray): FloatArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as FloatArray + return result } /** @@ -616,9 +629,9 @@ public fun FloatArray.plus(array: FloatArray): FloatArray { public fun IntArray.plus(array: IntArray): IntArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as IntArray + return result } /** @@ -627,9 +640,9 @@ public fun IntArray.plus(array: IntArray): IntArray { public fun LongArray.plus(array: LongArray): LongArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as LongArray + return result } /** @@ -638,198 +651,200 @@ public fun LongArray.plus(array: LongArray): LongArray { public fun ShortArray.plus(array: ShortArray): ShortArray { val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as ShortArray + return result } /** - * Returns an array containing all elements of the original array and then all elements of the given [collection]. + * Returns an array containing all elements of the original array and then all elements of the given [array]. */ -public fun Array.plus(collection: Collection): Array { +public fun Array.plus(array: Array): Array { val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as Array + val arraySize = array.size() + val result = Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(array, 0, result, thisSize, arraySize) + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun BooleanArray.plus(collection: Collection): BooleanArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as BooleanArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun ByteArray.plus(collection: Collection): ByteArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as ByteArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun CharArray.plus(collection: Collection): CharArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as CharArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun DoubleArray.plus(collection: Collection): DoubleArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as DoubleArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun FloatArray.plus(collection: Collection): FloatArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as FloatArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun IntArray.plus(collection: Collection): IntArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as IntArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun LongArray.plus(collection: Collection): LongArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as LongArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ public fun ShortArray.plus(collection: Collection): ShortArray { - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as ShortArray + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** - * Returns an array containing all elements of the original array and then the given [element]. + * Returns an array containing all elements of the original array and then all elements of the given [collection]. */ -public fun Array.plus(element: T): Array { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as Array +public fun Array.plus(collection: Collection): Array { + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun BooleanArray.plus(element: Boolean): BooleanArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as BooleanArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun ByteArray.plus(element: Byte): ByteArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as ByteArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun CharArray.plus(element: Char): CharArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as CharArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun DoubleArray.plus(element: Double): DoubleArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as DoubleArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun FloatArray.plus(element: Float): FloatArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as FloatArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun IntArray.plus(element: Int): IntArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as IntArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun LongArray.plus(element: Long): LongArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as LongArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** * Returns an array containing all elements of the original array and then the given [element]. */ public fun ShortArray.plus(element: Short): ShortArray { - val result = this.copyOf(size() + 1) - result[size()] = element - return result as ShortArray + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun Array.plus(element: T): Array { + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result } /** diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index 18bd82d9717..829ec53a024 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -77,12 +77,13 @@ fun specialJS(): List { templates add f("plus(element: T)") { only(ArraysOfObjects, ArraysOfPrimitives) returns("SELF") + returns(ArraysOfObjects) { "Array" } + inline(true) + annotations("""suppress("NOTHING_TO_INLINE")""") doc { "Returns an array containing all elements of the original array and then the given [element]." } - body(ArraysOfObjects, ArraysOfPrimitives) { + body() { """ - val result = this.copyOf() - (result: dynamic).push(element) - return result as SELF + return (this: dynamic).concat(arrayOf(element)) """ } } @@ -90,6 +91,7 @@ fun specialJS(): List { templates add f("plus(collection: Collection)") { only(ArraysOfObjects, ArraysOfPrimitives) returns("SELF") + returns(ArraysOfObjects) { "Array" } doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." } body { """ @@ -105,6 +107,7 @@ fun specialJS(): List { inline(true) annotations("""suppress("NOTHING_TO_INLINE")""") returns("SELF") + returns(ArraysOfObjects) { "Array" } body { """ return (this: dynamic).concat(array) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 7a17e3a7bcd..fc7c95dfe33 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -6,92 +6,87 @@ fun specialJVM(): List { val templates = arrayListOf() templates add f("plus(element: T)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) returns("SELF") doc { "Returns an array containing all elements of the original array and then the given [element]." } - body(ArraysOfObjects, ArraysOfPrimitives) { + body() { """ - val result = this.copyOf(size() + 1) - result[size()] = element - return result as SELF + val index = size() + val result = Arrays.copyOf(this, index + 1) + result[index] = element + return result """ } } templates add f("plus(collection: Collection)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) returns("SELF") doc { "Returns an array containing all elements of the original array and then all elements of the given [collection]." } body { """ - val thisSize = size() - val result = this.copyOf(thisSize + collection.size()) - collection.forEachIndexed { i, element -> - result[thisSize + i] = element - } - return result as SELF + var index = size() + val result = Arrays.copyOf(this, index + collection.size()) + for (element in collection) result[index++] = element + return result """ } } templates add f("plus(array: SELF)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) + customSignature(InvariantArraysOfObjects) { "plus(array: Array)" } doc { "Returns an array containing all elements of the original array and then all elements of the given [array]." } returns("SELF") body { """ val thisSize = size() val arraySize = array.size() - val result = this.copyOf(thisSize + arraySize) + val result = Arrays.copyOf(this, thisSize + arraySize) System.arraycopy(array, 0, result, thisSize, arraySize) - return result as SELF + return result """ } } templates add f("copyOfRange(from: Int, to: Int)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives) doc { "Returns new array which is a copy of range of original array." } returns("SELF") - returns(ArraysOfObjects) { "Array" } + annotations(InvariantArraysOfObjects) { """platformName("mutableCopyOfRange")"""} body { "return Arrays.copyOfRange(this, from, to)" } } templates add f("copyOf()") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives) doc { "Returns new array which is a copy of the original array." } returns("SELF") - returns(ArraysOfObjects) { "Array" } + annotations(InvariantArraysOfObjects) { """platformName("mutableCopyOf")"""} body { "return Arrays.copyOf(this, size())" } - body(ArraysOfObjects) { - "return Arrays.copyOf(this, size()) as Array" - } } // This overload can cause nulls if array size is expanding, hence different return overload templates add f("copyOf(newSize: Int)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(ArraysOfObjects, InvariantArraysOfObjects, ArraysOfPrimitives) doc { "Returns new array which is a copy of the original array." } returns("SELF") body { "return Arrays.copyOf(this, newSize)" } - returns(ArraysOfObjects) { "Array" } - body(ArraysOfObjects) { - "return Arrays.copyOf(this, newSize) as Array" - } + returns(ArraysOfObjects) { "Array" } + returns(InvariantArraysOfObjects) { "Array" } + annotations(InvariantArraysOfObjects) { """platformName("mutableCopyOf")"""} } templates add f("fill(element: T)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) doc { "Fills original array with the provided value." } returns { "SELF" } - returns(ArraysOfObjects) { "Array" } body { """ Arrays.fill(this, element)