From 5c6d3b03df0be5288d6bd96e2fe4406bb003b901 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 29 Jan 2022 04:32:09 +0300 Subject: [PATCH] [stdlib-mpp] add JS-IR target #KT-56106 --- libraries/stdlib/build.gradle.kts | 95 ++++++++++++++++++- .../stdlib/js-ir/runtime/collectionsHacks.kt | 1 + 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/build.gradle.kts b/libraries/stdlib/build.gradle.kts index d681b6f5524..3cebe520b59 100644 --- a/libraries/stdlib/build.gradle.kts +++ b/libraries/stdlib/build.gradle.kts @@ -45,6 +45,10 @@ val jsV1Dir = "${projectDir}/../js-v1" val jsSrcDir = "$jsV1Dir/src" val jsSrcJsDir = "${jsSrcDir}/js" +// for js-ir +val jsIrDir = "${projectDir}/../js-ir" +val jsIrMainSources = "${buildDir}/src/jsMainSources" +lateinit var jsIrTarget: KotlinJsTargetDsl kotlin { metadata { @@ -106,6 +110,32 @@ kotlin { } } } + jsIrTarget = js("jsIr", IR) { + nodejs { + testTask { + useMocha { + timeout = "10s" + } + } + } + compilations { + all { + kotlinOptions { + freeCompilerArgs += "-Xallow-kotlin-package" + } + } + val main by getting + main.apply { + kotlinOptions { + freeCompilerArgs += "-Xir-module-name=kotlin" + + if (!kotlinBuildProperties.disableWerror) { + allWarningsAsErrors = true + } + } + } + } + } sourceSets { commonMain { @@ -187,6 +217,68 @@ kotlin { } } + val jsIrMain by getting { + kotlin.apply { + srcDir(jsIrMainSources) + srcDir("$jsIrDir/builtins") + srcDir("$jsIrDir/runtime") + srcDir("$jsIrDir/src") + } + + val prepareJsIrMainSources by tasks.registering(Sync::class) { + val unimplementedNativeBuiltIns = + (file("$builtinsDir/native/kotlin/").list()!!.toSortedSet() - file("$jsIrDir/builtins/").list()!!) + .map { "core/builtins/native/kotlin/$it" } + + // TODO: try to reuse absolute paths defined in the beginning + val sources = listOf( + "core/builtins/src/kotlin/", + "libraries/stdlib/js/src/", + "libraries/stdlib/js/runtime/" + ) + unimplementedNativeBuiltIns + + val excluded = listOf( + // stdlib/js/src/generated is used exclusively for current `js-v1` backend. + "libraries/stdlib/js/src/generated/**", + + // JS-specific optimized version of emptyArray() already defined + "core/builtins/src/kotlin/ArrayIntrinsics.kt" + ) + + sources.forEach { path -> + from("$rootDir/$path") { + into(path.dropLastWhile { it != '/' }) + excluded.filter { it.startsWith(path) }.forEach { + exclude(it.substring(path.length)) + } + } + } + + into(jsIrMainSources) + +// Required to compile native builtins with the rest of runtime + val builtInsHeader = """@file:Suppress( + "NON_ABSTRACT_FUNCTION_WITH_NO_BODY", + "MUST_BE_INITIALIZED_OR_BE_ABSTRACT", + "EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE", + "PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED", + "WRONG_MODIFIER_TARGET", + "UNUSED_PARAMETER" +) +""" + doLast { + unimplementedNativeBuiltIns.forEach { path -> + val file = File("$jsIrMainSources/$path") + val sourceCode = builtInsHeader + file.readText() + file.writeText(sourceCode) + } + } + } + jsIrTarget.compilations["main"].compileKotlinTaskProvider.configure { + dependsOn(prepareJsIrMainSources) + } + } + all { languageSettings { // TODO: progressiveMode = use build property 'test.progressive.mode' @@ -306,7 +398,7 @@ tasks { sourceMapFile.writeText(groovy.json.JsonOutput.toJson(sourceMap)) compileMetaFile.copyTo(jsOutputMetaFile, overwrite = true) } -} + } val jsMainClasses by existing { dependsOn(mergeJs) @@ -323,6 +415,7 @@ tasks { from(jsOutputMetaFileName) from(jsOutputMapFileName) from(kotlin.js().compilations["main"].output.allOutputs) + from(jsIrTarget.compilations["main"].output.allOutputs) filesMatching("*.*") { mode = 0b110100100 } // KTI-401 } diff --git a/libraries/stdlib/js-ir/runtime/collectionsHacks.kt b/libraries/stdlib/js-ir/runtime/collectionsHacks.kt index 09adcb30588..887f43fc478 100644 --- a/libraries/stdlib/js-ir/runtime/collectionsHacks.kt +++ b/libraries/stdlib/js-ir/runtime/collectionsHacks.kt @@ -9,6 +9,7 @@ import kotlin.js.* internal fun arrayToString(array: Array<*>) = array.joinToString(", ", "[", "]") { toString(it) } +@OptIn(ExperimentalUnsignedTypes::class) internal fun Array?.contentDeepHashCodeInternal(): Int { if (this == null) return 0 var result = 1