Generate additional sources for JS instead of copying them from JVM

This commit is contained in:
Ilya Gorbunov
2016-12-03 03:11:00 +03:00
parent bc2d7dda2c
commit 6ea1cde449
17 changed files with 19135 additions and 713 deletions
+301 -300
View File
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("ArraysKt")
@file:kotlin.jvm.JvmVersion
package kotlin.collections
@@ -12599,6 +12600,306 @@ public fun CharArray.asSequence(): Sequence<Char> {
return Sequence { this.iterator() }
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfByte")
public fun Array<out Byte>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfShort")
public fun Array<out Short>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfInt")
public fun Array<out Int>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfLong")
public fun Array<out Long>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfFloat")
public fun Array<out Float>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfDouble")
public fun Array<out Double>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun ByteArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun ShortArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun IntArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun LongArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun FloatArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun DoubleArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfByte")
public fun Array<out Byte>.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfShort")
public fun Array<out Short>.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfInt")
public fun Array<out Int>.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfLong")
public fun Array<out Long>.sum(): Long {
var sum: Long = 0L
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfFloat")
public fun Array<out Float>.sum(): Float {
var sum: Float = 0.0f
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfDouble")
public fun Array<out Double>.sum(): Double {
var sum: Double = 0.0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun ByteArray.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun ShortArray.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun IntArray.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun LongArray.sum(): Long {
var sum: Long = 0L
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun FloatArray.sum(): Float {
var sum: Float = 0.0f
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun DoubleArray.sum(): Double {
var sum: Double = 0.0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns a [List] that wraps the original array.
*/
@@ -13759,303 +14060,3 @@ public fun CharArray.toTypedArray(): Array<Char> {
return result as Array<Char>
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfByte")
public fun Array<out Byte>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfShort")
public fun Array<out Short>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfInt")
public fun Array<out Int>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfLong")
public fun Array<out Long>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfFloat")
public fun Array<out Float>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
@kotlin.jvm.JvmName("averageOfDouble")
public fun Array<out Double>.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun ByteArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun ShortArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun IntArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun LongArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun FloatArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns an average value of elements in the array.
*/
public fun DoubleArray.average(): Double {
var sum: Double = 0.0
var count: Int = 0
for (element in this) {
sum += element
count += 1
}
return if (count == 0) 0.0 else sum / count
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfByte")
public fun Array<out Byte>.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfShort")
public fun Array<out Short>.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfInt")
public fun Array<out Int>.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfLong")
public fun Array<out Long>.sum(): Long {
var sum: Long = 0L
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfFloat")
public fun Array<out Float>.sum(): Float {
var sum: Float = 0.0f
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
@kotlin.jvm.JvmName("sumOfDouble")
public fun Array<out Double>.sum(): Double {
var sum: Double = 0.0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun ByteArray.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun ShortArray.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun IntArray.sum(): Int {
var sum: Int = 0
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun LongArray.sum(): Long {
var sum: Long = 0L
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun FloatArray.sum(): Float {
var sum: Float = 0.0f
for (element in this) {
sum += element
}
return sum
}
/**
* Returns the sum of all elements in the array.
*/
public fun DoubleArray.sum(): Double {
var sum: Double = 0.0
for (element in this) {
sum += element
}
return sum
}
+19 -18
View File
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")
@file:kotlin.jvm.JvmVersion
package kotlin.collections
@@ -1959,24 +1960,6 @@ public fun <T> Iterable<T>.asSequence(): Sequence<T> {
return Sequence { this.iterator() }
}
/**
* Returns a list containing all elements that are instances of specified class.
*/
@kotlin.jvm.JvmVersion
public fun <R> Iterable<*>.filterIsInstance(klass: Class<R>): List<R> {
return filterIsInstanceTo(ArrayList<R>(), klass)
}
/**
* Appends all elements that are instances of specified class to the given [destination].
*/
@kotlin.jvm.JvmVersion
public fun <C : MutableCollection<in R>, R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C {
@Suppress("UNCHECKED_CAST")
for (element in this) if (klass.isInstance(element)) destination.add(element as R)
return destination
}
/**
* Returns an average value of elements in the collection.
*/
@@ -2133,3 +2116,21 @@ public fun Iterable<Double>.sum(): Double {
return sum
}
/**
* Returns a list containing all elements that are instances of specified class.
*/
@kotlin.jvm.JvmVersion
public fun <R> Iterable<*>.filterIsInstance(klass: Class<R>): List<R> {
return filterIsInstanceTo(ArrayList<R>(), klass)
}
/**
* Appends all elements that are instances of specified class to the given [destination].
*/
@kotlin.jvm.JvmVersion
public fun <C : MutableCollection<in R>, R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C {
@Suppress("UNCHECKED_CAST")
for (element in this) if (klass.isInstance(element)) destination.add(element as R)
return destination
}
+1
View File
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MapsKt")
@file:kotlin.jvm.JvmVersion
package kotlin.collections
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("RangesKt")
@file:kotlin.jvm.JvmVersion
package kotlin.ranges
+20 -19
View File
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("SequencesKt")
@file:kotlin.jvm.JvmVersion
package kotlin.sequences
@@ -1237,25 +1238,6 @@ public inline fun <T> Sequence<T>.asSequence(): Sequence<T> {
return this
}
/**
* Returns a sequence containing all elements that are instances of specified class.
*/
@kotlin.jvm.JvmVersion
public fun <R> Sequence<*>.filterIsInstance(klass: Class<R>): Sequence<R> {
@Suppress("UNCHECKED_CAST")
return filter { klass.isInstance(it) } as Sequence<R>
}
/**
* Appends all elements that are instances of specified class to the given [destination].
*/
@kotlin.jvm.JvmVersion
public fun <C : MutableCollection<in R>, R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C {
@Suppress("UNCHECKED_CAST")
for (element in this) if (klass.isInstance(element)) destination.add(element as R)
return destination
}
/**
* Returns an average value of elements in the sequence.
*/
@@ -1412,3 +1394,22 @@ public fun Sequence<Double>.sum(): Double {
return sum
}
/**
* Returns a sequence containing all elements that are instances of specified class.
*/
@kotlin.jvm.JvmVersion
public fun <R> Sequence<*>.filterIsInstance(klass: Class<R>): Sequence<R> {
@Suppress("UNCHECKED_CAST")
return filter { klass.isInstance(it) } as Sequence<R>
}
/**
* Appends all elements that are instances of specified class to the given [destination].
*/
@kotlin.jvm.JvmVersion
public fun <C : MutableCollection<in R>, R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class<R>): C {
@Suppress("UNCHECKED_CAST")
for (element in this) if (klass.isInstance(element)) destination.add(element as R)
return destination
}
+1
View File
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("SetsKt")
@file:kotlin.jvm.JvmVersion
package kotlin.collections
@@ -1,5 +1,6 @@
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("StringsKt")
@file:kotlin.jvm.JvmVersion
package kotlin.text
@@ -31,54 +31,63 @@ fun main(args: Array<String>) {
}
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 commonGenerators = sequenceOf(
::elements,
::filtering,
::ordering,
::arrays,
::snapshots,
::mapping,
::sets,
::aggregates,
::guards,
::generators,
::strings,
::sequences,
::ranges,
::numeric,
::comparables
)
templates.groupByFileAndWrite(outDir)
fun generateCollectionsAPI(outDir: File) {
val templates = (commonGenerators + ::specialJVM).flatMap { it().sortedBy { it.signature }.asSequence() }
templates.groupByFileAndWrite(outDir, Platform.JVM)
}
fun generateCollectionsJsAPI(outDir: File) {
specialJS().asSequence().groupByFileAndWrite(outDir, { "_${it.name.capitalize()}Js.kt"})
(commonGenerators + ::specialJS).flatMap { it().sortedBy { it.signature }.asSequence() }
.groupByFileAndWrite(outDir, Platform.JS, { "_${it.name.capitalize()}Js.kt"})
}
private fun Sequence<GenericFunction>.groupByFileAndWrite(
outDir: File,
platform: Platform,
fileNameBuilder: (SourceFile) -> String = { "_${it.name.capitalize()}.kt" }
) {
val groupedConcreteFunctions = flatMap { it.instantiate().asSequence() }.groupBy { it.sourceFile }
val groupedConcreteFunctions = map { it.instantiate(platform) }.flatten().groupBy { it.sourceFile }
for ((sourceFile, functions) in groupedConcreteFunctions) {
val file = outDir.resolve(fileNameBuilder(sourceFile))
functions.writeTo(file, sourceFile)
functions.writeTo(file, sourceFile, platform)
}
}
private fun List<ConcreteFunction>.writeTo(file: File, sourceFile: SourceFile) {
private fun List<ConcreteFunction>.writeTo(file: File, sourceFile: SourceFile, platform: Platform) {
println("Generating file: $file")
FileWriter(file).use { writer ->
if (sourceFile.multifile) {
writer.append("@file:kotlin.jvm.JvmMultifileClass\n")
writer.appendln("@file:kotlin.jvm.JvmMultifileClass")
}
writer.append("@file:kotlin.jvm.JvmName(\"${sourceFile.jvmClassName}\")\n\n")
writer.appendln("@file:kotlin.jvm.JvmName(\"${sourceFile.jvmClassName}\")")
if (platform == Platform.JVM)
writer.appendln("@file:kotlin.jvm.JvmVersion")
writer.appendln()
writer.append("package ${sourceFile.packageName ?: "kotlin"}\n\n")
writer.append("$COMMON_AUTOGENERATED_WARNING\n\n")
writer.append("import kotlin.comparisons.*\n\n")
@@ -269,6 +269,16 @@ fun specialJVM(): List<GenericFunction> {
}
"""
}
//
// body(ArraysOfObjects) {
// """
// return ArrayList<T>(this.unsafeCast<Array<Any?>>())
// """
// }
//
// inline(true, ArraysOfPrimitives)
// body(ArraysOfPrimitives) {"""return this.unsafeCast<Array<T>>().asList()"""}
}
templates add f("toTypedArray()") {
@@ -288,6 +298,11 @@ fun specialJVM(): List<GenericFunction> {
return result as Array<T>
"""
}
// body {
// """
// return copyOf().unsafeCast<Array<T>>()
// """
// }
}
templates.forEach { it.apply { jvmOnly(true) } }
@@ -70,6 +70,11 @@ enum class Inline {
fun isInline() = this != No
}
enum class Platform {
JVM,
JS
}
data class Deprecation(val message: String, val replaceWith: String? = null, val level: DeprecationLevel = DeprecationLevel.WARNING)
val forBinaryCompatibility = Deprecation("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
@@ -100,6 +105,9 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
open class FamilyProperty<TValue: Any>() : SpecializedProperty<Family, TValue>()
open class PrimitiveProperty<TValue: Any>() : SpecializedProperty<PrimitiveType, TValue>()
//
// operator fun <TValue : Any> FamilyProperty<TValue>.invoke(vararg keys: Family, valueBuilder: (Family) -> TValue) = set(keys.map { null to it }, valueBuilder)
// operator fun <TKey: Any, TValue : Any> SpecializedProperty<TKey, TValue>.invoke(value: TValue, vararg keys: TKey) = set(keys.asList(), { value })
class DeprecationProperty() : FamilyProperty<Deprecation>()
operator fun DeprecationProperty.invoke(value: String, vararg keys: Family) = set(keys.asList(), { Deprecation(value) })
@@ -207,10 +215,11 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
buildPrimitives.addAll(p.toList())
}
fun instantiate(vararg families: Family = Family.values()): List<ConcreteFunction> {
fun instantiate(platform: Platform, vararg families: Family = Family.values()): List<ConcreteFunction> {
return families
.sortedBy { it.ordinal }
.filter { buildFamilies.contains(it) }
.filter { platform == Platform.JVM || jvmOnly[it] != true }
.flatMap { family -> instantiate(family) }
}