Generate additional sources for JS instead of copying them from JVM
This commit is contained in:
@@ -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) }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user