diff --git a/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt b/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt index f0617b7c9a4..869cd739670 100644 --- a/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt +++ b/libraries/stdlib/js/irRuntime/generated/_ArraysJs.kt @@ -1015,21 +1015,30 @@ public actual operator fun Array.plus(elements: Collection): Array * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ public actual operator fun ByteArray.plus(elements: Collection): ByteArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ public actual operator fun ShortArray.plus(elements: Collection): ShortArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ public actual operator fun IntArray.plus(elements: Collection): IntArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result } /** @@ -1043,14 +1052,20 @@ public actual operator fun LongArray.plus(elements: Collection): LongArray * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ public actual operator fun FloatArray.plus(elements: Collection): FloatArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result } /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ public actual operator fun DoubleArray.plus(elements: Collection): DoubleArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result } /** @@ -1064,7 +1079,10 @@ public actual operator fun BooleanArray.plus(elements: Collection): Boo * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ public actual operator fun CharArray.plus(elements: Collection): CharArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result } /** diff --git a/libraries/stdlib/js/irRuntime/kotlinHacks.kt b/libraries/stdlib/js/irRuntime/kotlinHacks.kt index 45eb42cc97b..a3b7e29fb76 100644 --- a/libraries/stdlib/js/irRuntime/kotlinHacks.kt +++ b/libraries/stdlib/js/irRuntime/kotlinHacks.kt @@ -66,13 +66,6 @@ internal fun arrayPlusCollection(array: dynamic, collection: Collection): return result } -internal fun fillFromCollection(dst: dynamic, startIndex: Int, collection: Collection): dynamic { - var index = startIndex - val arr = dst.unsafeCast>() - for (element in collection) arr[index++] = element - return dst -} - internal inline fun copyArrayType(from: dynamic, to: dynamic) { if (from.`$type$` !== undefined) { to.`$type$` = from.`$type$` diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 5849f683d28..9ca1227f775 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -532,8 +532,26 @@ object ArrayOps : TemplateGroupBase() { when (primitive) { null, PrimitiveType.Boolean, PrimitiveType.Long -> body { "return arrayPlusCollection(this, elements)" } - else -> - body { "return fillFromCollection(this.copyOf(size + elements.size), this.size, elements)" } + else -> { + on(Backend.Legacy) { + body { + "return fillFromCollection(this.copyOf(size + elements.size), this.size, elements)" + } + } + on(Backend.IR) { + // Don't use fillFromCollection because it treats arrays + // as `dynamic` but we need to concrete types to perform + // unboxing of collections elements + body { + """ + var index = size + val result = this.copyOf(size + elements.size) + for (element in elements) result[index++] = element + return result + """ + } + } + } } } on(Platform.Native) {