diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt similarity index 99% rename from js/js.libraries/src/core/kotlin_special.kt rename to js/js.libraries/src/core/generated/_ArraysJs.kt index e450ca328cd..c4d8db5b6ed 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -1,3 +1,6 @@ +@file:kotlin.jvm.JvmMultifileClass +@file:kotlin.jvm.JvmName("ArraysKt") + package kotlin.collections // @@ -5,6 +8,7 @@ package kotlin.collections // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // +import kotlin.comparisons.* import java.util.* /** @@ -14,13 +18,6 @@ public fun Array.asList(): List { return ArrayList(this as Array) } -/** - * Returns a [List] that wraps the original array. - */ -public inline fun BooleanArray.asList(): List { - return (this as Array).asList() -} - /** * Returns a [List] that wraps the original array. */ @@ -31,22 +28,8 @@ public inline fun ByteArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -public inline fun CharArray.asList(): List { - return (this as Array).asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -public inline fun DoubleArray.asList(): List { - return (this as Array).asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -public inline fun FloatArray.asList(): List { - return (this as Array).asList() +public inline fun ShortArray.asList(): List { + return (this as Array).asList() } /** @@ -66,8 +49,157 @@ public inline fun LongArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -public inline fun ShortArray.asList(): List { - return (this as Array).asList() +public inline fun FloatArray.asList(): List { + return (this as Array).asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +public inline fun DoubleArray.asList(): List { + return (this as Array).asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +public inline fun BooleanArray.asList(): List { + return (this as Array).asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +public inline fun CharArray.asList(): List { + return (this as Array).asList() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + return copyOf() as Array +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { + return this.asDynamic().slice(fromIndex, toIndex) } /** @@ -78,14 +210,6 @@ public inline fun Array.copyOf(): Array { return this.asDynamic().slice() } -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun BooleanArray.copyOf(): BooleanArray { - return this.asDynamic().slice() -} - /** * Returns new array which is a copy of the original array. */ @@ -98,23 +222,7 @@ public inline fun ByteArray.copyOf(): ByteArray { * Returns new array which is a copy of the original array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun CharArray.copyOf(): CharArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.copyOf(): DoubleArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.copyOf(): FloatArray { +public inline fun ShortArray.copyOf(): ShortArray { return this.asDynamic().slice() } @@ -138,7 +246,31 @@ public inline fun LongArray.copyOf(): LongArray { * Returns new array which is a copy of the original array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.copyOf(): ShortArray { +public inline fun FloatArray.copyOf(): FloatArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun DoubleArray.copyOf(): DoubleArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun BooleanArray.copyOf(): BooleanArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun CharArray.copyOf(): CharArray { return this.asDynamic().slice() } @@ -205,83 +337,11 @@ public fun Array.copyOf(newSize: Int): Array { return arrayCopyResize(this, newSize, null) } -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - /** * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.plus(element: T): Array { +public inline fun Array.plusElement(element: T): Array { return this.asDynamic().concat(arrayOf(element)) } @@ -289,7 +349,7 @@ public inline operator fun Array.plus(element: T): Array { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { +public inline operator fun Array.plus(element: T): Array { return this.asDynamic().concat(arrayOf(element)) } @@ -305,23 +365,7 @@ public inline operator fun ByteArray.plus(element: Byte): ByteArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.plus(element: Char): CharArray { - return this.asDynamic().concat(arrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.plus(element: Double): DoubleArray { - return this.asDynamic().concat(arrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.plus(element: Float): FloatArray { +public inline operator fun ShortArray.plus(element: Short): ShortArray { return this.asDynamic().concat(arrayOf(element)) } @@ -345,7 +389,31 @@ public inline operator fun LongArray.plus(element: Long): LongArray { * Returns an array containing all elements of the original array and then the given [element]. */ @Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.plus(element: Short): ShortArray { +public inline operator fun FloatArray.plus(element: Float): FloatArray { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.plus(element: Double): DoubleArray { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.plus(element: Char): CharArray { return this.asDynamic().concat(arrayOf(element)) } @@ -356,13 +424,6 @@ public operator fun Array.plus(elements: Collection): Array { return arrayPlusCollection(this, elements) } -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun BooleanArray.plus(elements: Collection): BooleanArray { - return arrayPlusCollection(this, elements) -} - /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ @@ -373,21 +434,7 @@ public operator fun ByteArray.plus(elements: Collection): ByteArray { /** * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. */ -public operator fun CharArray.plus(elements: Collection): CharArray { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun DoubleArray.plus(elements: Collection): DoubleArray { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun FloatArray.plus(elements: Collection): FloatArray { +public operator fun ShortArray.plus(elements: Collection): ShortArray { return arrayPlusCollection(this, elements) } @@ -408,7 +455,28 @@ public 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 operator fun ShortArray.plus(elements: Collection): ShortArray { +public operator fun FloatArray.plus(elements: Collection): FloatArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun DoubleArray.plus(elements: Collection): DoubleArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun BooleanArray.plus(elements: Collection): BooleanArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun CharArray.plus(elements: Collection): CharArray { return arrayPlusCollection(this, elements) } @@ -420,14 +488,6 @@ public inline operator fun Array.plus(elements: Array): Array< return this.asDynamic().concat(elements) } -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { - return this.asDynamic().concat(elements) -} - /** * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @@ -440,23 +500,7 @@ public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.plus(elements: CharArray): CharArray { - return this.asDynamic().concat(elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { - return this.asDynamic().concat(elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { +public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { return this.asDynamic().concat(elements) } @@ -480,80 +524,32 @@ public inline operator fun LongArray.plus(elements: LongArray): LongArray { * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { +public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { return this.asDynamic().concat(elements) } /** - * 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 [elements] array. */ @Suppress("NOTHING_TO_INLINE") -public inline fun Array.plusElement(element: T): Array { - return this.asDynamic().concat(arrayOf(element)) +public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { + return this.asDynamic().concat(elements) } /** - * Sorts the array in-place. + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ -@library("primitiveArraySort") -public fun ByteArray.sort(): Unit { - noImpl +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { + return this.asDynamic().concat(elements) } /** - * Sorts the array in-place. + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. */ -@library("primitiveArraySort") -public fun CharArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun DoubleArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun FloatArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun IntArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ShortArray.sort(): Unit { - noImpl -} - -/** - * Sorts the array in-place. - */ -public fun > Array.sort(): Unit { - if (size > 1) - sort { a: T, b: T -> a.compareTo(b) } -} - -/** - * Sorts the array in-place. - */ -public fun LongArray.sort(): Unit { - if (size > 1) - sort { a: Long, b: Long -> a.compareTo(b) } +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.plus(elements: CharArray): CharArray { + return this.asDynamic().concat(elements) } /** @@ -576,23 +572,7 @@ public fun ByteArray.sort(comparison: (Byte, Byte) -> Int): Unit { * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native -public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit { - noImpl -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@native -public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit { - noImpl -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@native -public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit { +public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit { noImpl } @@ -616,7 +596,23 @@ public fun LongArray.sort(comparison: (Long, Long) -> Int): Unit { * Sorts the array in-place according to the order specified by the given [comparison] function. */ @native -public fun ShortArray.sort(comparison: (Short, Short) -> Int): Unit { +public fun FloatArray.sort(comparison: (Float, Float) -> Int): Unit { + noImpl +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@native +public fun DoubleArray.sort(comparison: (Double, Double) -> Int): Unit { + noImpl +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@native +public fun CharArray.sort(comparison: (Char, Char) -> Int): Unit { noImpl } @@ -629,58 +625,66 @@ public fun Array.sortWith(comparator: Comparator): Unit { } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun BooleanArray.toTypedArray(): Array { - return copyOf() as Array +@library("primitiveArraySort") +public fun ByteArray.sort(): Unit { + noImpl } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun ByteArray.toTypedArray(): Array { - return copyOf() as Array +@library("primitiveArraySort") +public fun ShortArray.sort(): Unit { + noImpl } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun CharArray.toTypedArray(): Array { - return copyOf() as Array +@library("primitiveArraySort") +public fun IntArray.sort(): Unit { + noImpl } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun DoubleArray.toTypedArray(): Array { - return copyOf() as Array +@library("primitiveArraySort") +public fun FloatArray.sort(): Unit { + noImpl } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun FloatArray.toTypedArray(): Array { - return copyOf() as Array +@library("primitiveArraySort") +public fun DoubleArray.sort(): Unit { + noImpl } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun IntArray.toTypedArray(): Array { - return copyOf() as Array +@library("primitiveArraySort") +public fun CharArray.sort(): Unit { + noImpl } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun LongArray.toTypedArray(): Array { - return copyOf() as Array +public fun > Array.sort(): Unit { + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } } /** - * Returns a *typed* object array containing all of the elements of this primitive array. + * Sorts the array in-place. */ -public fun ShortArray.toTypedArray(): Array { - return copyOf() as Array +public fun LongArray.sort(): Unit { + if (size > 1) + sort { a: Long, b: Long -> a.compareTo(b) } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt deleted file mode 100644 index 45cbfca319f..00000000000 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateCollections.kt +++ /dev/null @@ -1,37 +0,0 @@ -package generators - -import java.io.* -import templates.Family.* -import templates.* -import templates.PrimitiveType.* - -fun generateCollectionsAPI(outDir: File) { - val templates = sequenceOf( - ::elements, - ::filtering, - ::ordering, - ::arrays, - ::snapshots, - ::mapping, - ::sets, - ::aggregates, - ::guards, - ::generators, - ::strings, - ::sequences, - ::specialJVM, - ::ranges, - ::numeric, - ::comparables - ).flatMap { it().sortedBy { it.signature }.asSequence() } - - val groupedConcreteFunctions = templates.flatMap { it.instantiate().asSequence() }.groupBy { it.sourceFile } - - for ((sourceFile, functions) in groupedConcreteFunctions) { - functions.writeTo(outDir, sourceFile) - } -} - -fun generateCollectionsJsAPI(outDir: File) { - specialJS().writeTo(File(outDir, "kotlin_special.kt")) { build() } -} diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index c34b2f3e674..70d5feb4a65 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -18,11 +18,12 @@ private val COMMON_AUTOGENERATED_WARNING: String = """// */ fun main(args: Array) { require(args.size == 1) { "Expecting Kotlin project home path as an argument" } + val baseDir = File(args.first()) - val outDir = File(File(args[0]), "libraries/stdlib/src/generated") + val outDir = baseDir.resolve("libraries/stdlib/src/generated") require(outDir.exists()) { "$outDir doesn't exist!" } - val jsCoreDir = File(args[0], "js/js.libraries/src/core") + val jsCoreDir = baseDir.resolve("js/js.libraries/src/core/generated") require(jsCoreDir.exists()) { "$jsCoreDir doesn't exist!" } generateCollectionsAPI(outDir) @@ -30,37 +31,60 @@ fun main(args: Array) { } -fun List.writeTo(file: File, builder: GenericFunction.() -> String) { - println("Generating file: $file") - val its = FileWriter(file) +fun generateCollectionsAPI(outDir: File) { + val templates = sequenceOf( + ::elements, + ::filtering, + ::ordering, + ::arrays, + ::snapshots, + ::mapping, + ::sets, + ::aggregates, + ::guards, + ::generators, + ::strings, + ::sequences, + ::specialJVM, + ::ranges, + ::numeric, + ::comparables + ).flatMap { it().sortedBy { it.signature }.asSequence() } - its.use { - its.append("package kotlin.collections\n\n") - its.append("$COMMON_AUTOGENERATED_WARNING\n\n") - its.append("import java.util.*\n\n") - for (t in this.sortedBy { it.signature }) { - its.append(t.builder()) - } + templates.groupByFileAndWrite(outDir) +} + +fun generateCollectionsJsAPI(outDir: File) { + specialJS().asSequence().groupByFileAndWrite(outDir, { "_${it.name.capitalize()}Js.kt"}) +} + + +private fun Sequence.groupByFileAndWrite( + outDir: File, + fileNameBuilder: (SourceFile) -> String = { "_${it.name.capitalize()}.kt" } +) { + val groupedConcreteFunctions = flatMap { it.instantiate().asSequence() }.groupBy { it.sourceFile } + + for ((sourceFile, functions) in groupedConcreteFunctions) { + val file = outDir.resolve(fileNameBuilder(sourceFile)) + functions.writeTo(file, sourceFile) } } -fun List.writeTo(outDir: File, sourceFile: SourceFile) { - val file = File(outDir, sourceFile.fileName) +private fun List.writeTo(file: File, sourceFile: SourceFile) { println("Generating file: $file") - val its = FileWriter(file) - its.use { + FileWriter(file).use { writer -> if (sourceFile.multifile) { - its.append("@file:kotlin.jvm.JvmMultifileClass\n") + writer.append("@file:kotlin.jvm.JvmMultifileClass\n") } - its.append("@file:kotlin.jvm.JvmName(\"${sourceFile.jvmClassName}\")\n\n") - its.append("package ${sourceFile.packageName ?: "kotlin"}\n\n") - its.append("$COMMON_AUTOGENERATED_WARNING\n\n") - its.append("import kotlin.comparisons.*\n") - its.append("import java.util.*\n\n") - + writer.append("@file:kotlin.jvm.JvmName(\"${sourceFile.jvmClassName}\")\n\n") + writer.append("package ${sourceFile.packageName ?: "kotlin"}\n\n") + writer.append("$COMMON_AUTOGENERATED_WARNING\n\n") + writer.append("import kotlin.comparisons.*\n") + writer.append("import java.util.*\n\n") for (f in this) { - f.textBuilder(its) + f.textBuilder(writer) } } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/SourceFile.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/SourceFile.kt index 4f4782f0d90..fbd15e76569 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/SourceFile.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/SourceFile.kt @@ -13,6 +13,4 @@ enum class SourceFile(jvmClassName: String? = null, val multifile: Boolean = tru ; val jvmClassName = jvmClassName ?: (name.capitalize() + "Kt") - - val fileName: String get() = "_${name.capitalize()}.kt" }