Skip to content

Commit fb8fcf0

Browse files
Create test fixture stub for K2JKlibCompiler
1 parent 2999770 commit fb8fcf0

File tree

10 files changed

+297
-0
lines changed

10 files changed

+297
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// IGNORE_BACKEND: JVM_IR
2+
// FILE: klib.kt
3+
package defs
4+
5+
inline class Wrapper(val s: String)
6+
7+
typealias StrList = List<String>
8+
9+
fun combine(w: Wrapper, list: StrList): String =
10+
w.s + list.joinToString()
11+
12+
// FILE: test.kt
13+
import defs.Wrapper
14+
import defs.StrList
15+
import defs.combine
16+
17+
fun box(): String {
18+
val w = Wrapper("X")
19+
val l: StrList = listOf("Y", "Z")
20+
return combine(w, l)
21+
}
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// IGNORE_BACKEND: JVM_IR
2+
// FILE: enums.kt
3+
package e
4+
5+
enum class Mode { A, B }
6+
7+
fun select(m: Mode = Mode.B): String =
8+
when (m) {
9+
Mode.A -> "A"
10+
Mode.B -> "B"
11+
}
12+
13+
// FILE: test.kt
14+
import e.Mode
15+
import e.select
16+
17+
fun box(): String {
18+
if (select(Mode.A) != "A") return "FAIL1"
19+
if (select() != "B") return "FAIL2"
20+
return "OK"
21+
}
22+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// IGNORE_BACKEND: JVM_IR
2+
// FILE: foo.kt
3+
package foo
4+
5+
class C {
6+
val inClass = "O"
7+
}
8+
9+
val toplevel get() = "K"
10+
11+
fun referByDescriptor(s: String) = s.length
12+
13+
inline fun foo() {
14+
println()
15+
}
16+
17+
// FILE: test.kt
18+
import foo.C
19+
import foo.referByDescriptor
20+
import foo.toplevel
21+
import foo.foo
22+
23+
fun box(): String {
24+
referByDescriptor("")
25+
foo()
26+
return C().inClass + toplevel
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// IGNORE_BACKEND: JVM_IR
2+
// FILE: base.kt
3+
package base
4+
5+
open class Parent {
6+
open fun run() = "P"
7+
}
8+
9+
inline fun doInline(block: () -> String) = block()
10+
11+
// FILE: impl.kt
12+
package impl
13+
14+
import base.Parent
15+
16+
class Child : Parent() {
17+
override fun run() = "C"
18+
}
19+
20+
// FILE: test.kt
21+
import impl.Child
22+
import base.doInline
23+
24+
fun box(): String {
25+
val c = Child()
26+
return doInline { c.run() }
27+
}
28+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// IGNORE_BACKEND: JVM_IR
2+
// FILE: nested.kt
3+
package n
4+
5+
class Outer(val base: Int) {
6+
class Inner(val x: Int)
7+
8+
companion object {
9+
val CONST = "X"
10+
}
11+
}
12+
13+
fun make(inner: Outer.Inner) = inner.x * 3
14+
15+
// FILE: test.kt
16+
import n.Outer
17+
import n.make
18+
19+
fun box(): String {
20+
val i = Outer.Inner(4)
21+
if (make(i) != 12) return "FAIL"
22+
return Outer.CONST
23+
}
24+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// IGNORE_BACKEND: JVM_IR
2+
// FILE: sealed.kt
3+
package s
4+
5+
sealed class Node
6+
class Leaf(val value: Int) : Node()
7+
class Branch(val left: Node, val right: Node) : Node()
8+
9+
fun <T : Node> eval(n: T): Int =
10+
when (n) {
11+
is Leaf -> n.value
12+
is Branch -> eval(n.left) + eval(n.right)
13+
}
14+
15+
// FILE: test.kt
16+
import s.Leaf
17+
import s.Branch
18+
import s.eval
19+
20+
fun box(): String {
21+
val tree = Branch(Leaf(1), Branch(Leaf(2), Leaf(3)))
22+
return eval(tree).toString()
23+
}
24+

compiler/tests-against-klib/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ compilerTests {
3232
// - compiler/testData/codegen/boxKlib/properties.kt
3333
// - compiler/testData/codegen/boxKlib/simple.kt
3434
testData(project(":compiler").isolated, "testData/codegen/boxKlib")
35+
36+
testData(project(":compiler").isolated, "testData/codegen/jklib")
3537
}
3638

3739
projectTest(parallel = true) {

compiler/tests-against-klib/tests-gen/org/jetbrains/kotlin/codegen/ir/JKlibTestGenerated.java

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.codegen.ir
7+
8+
import com.intellij.openapi.util.Disposer
9+
import org.jetbrains.kotlin.ObsoleteTestInfrastructure
10+
import org.jetbrains.kotlin.cli.common.CLICompiler
11+
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
12+
import org.jetbrains.kotlin.cli.common.arguments.K2JKlibCompilerArguments
13+
import org.jetbrains.kotlin.cli.common.computeKotlinPaths
14+
import org.jetbrains.kotlin.cli.common.messages.MessageCollectorImpl
15+
import org.jetbrains.kotlin.cli.jklib.K2JKlibCompiler
16+
import org.jetbrains.kotlin.codegen.AbstractBlackBoxCodegenTest
17+
import org.jetbrains.kotlin.config.CommonConfigurationKeys
18+
import org.jetbrains.kotlin.config.CompilerConfiguration
19+
import org.jetbrains.kotlin.config.JVMConfigurationKeys
20+
import org.jetbrains.kotlin.test.util.KtTestUtil
21+
import org.jetbrains.kotlin.utils.PathUtil
22+
import java.io.File
23+
import java.nio.file.Paths
24+
25+
@OptIn(ObsoleteTestInfrastructure::class)
26+
abstract class AbstractJKlibTest : AbstractBlackBoxCodegenTest() {
27+
lateinit var klibName: String
28+
lateinit var outputDir: File
29+
30+
override fun doMultiFileTest(wholeFile: File, files: List<TestFile>) {
31+
outputDir = KtTestUtil.tmpDir("java-files")
32+
klibName = wholeFile.nameWithoutExtension
33+
34+
compileToKlibAndDeserializeIr(
35+
wholeFile.nameWithoutExtension,
36+
KtTestUtil.tmpDir("java-files").toString(),
37+
files
38+
)
39+
}
40+
41+
override fun updateConfiguration(configuration: CompilerConfiguration) {
42+
super.updateConfiguration(configuration)
43+
configuration.put(JVMConfigurationKeys.KLIB_PATHS, listOf(klibName))
44+
}
45+
46+
// We need real (as opposed to virtual) files in order to produce a Klib.
47+
private fun loadMultiFilesReal(outputDir: String, files: List<TestFile>): List<String> {
48+
return files.map { testFile ->
49+
assert(testFile.name.endsWith(".kt"))
50+
val ktFile = File(Paths.get(outputDir, testFile.name).toString())
51+
ktFile.writeText(testFile.content)
52+
ktFile.toString()
53+
}
54+
}
55+
56+
private fun compileToKlibAndDeserializeIr(klibName: String, outputDir: String, files: List<TestFile>) {
57+
val sourceFiles = loadMultiFilesReal(outputDir, files)
58+
// TODO find a way to implement it properly
59+
val jsStdlibPath =
60+
File(".").absoluteFile.parentFile.parentFile.parentFile.absolutePath + "/" + PathUtil.kotlinPathsForCompiler.jsStdLibKlibPath.toString()
61+
val compiler = K2JKlibCompiler()
62+
63+
val messageCollector = MessageCollectorImpl()
64+
val configuration = CompilerConfiguration().apply {
65+
put(CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
66+
put(CLIConfigurationKeys.PERF_MANAGER, compiler.defaultPerformanceManager)
67+
}
68+
69+
val arguments = K2JKlibCompilerArguments().apply {
70+
destination = klibName
71+
klibLibraries = jsStdlibPath
72+
freeArgs = sourceFiles
73+
}
74+
75+
val rootDisposable = Disposer.newDisposable("Disposable for ${CLICompiler::class.simpleName}.execImpl")
76+
val paths = computeKotlinPaths(messageCollector, arguments)
77+
78+
K2JKlibCompiler().compileKlibAndDeserializeIr(
79+
arguments, configuration, rootDisposable, paths
80+
)
81+
}
82+
}

compiler/tests-against-klib/tests/org/jetbrains/kotlin/generators/tests/GenerateCompilerTestsAgainstKlib.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.jetbrains.kotlin.generators.tests
77

88
import org.jetbrains.kotlin.codegen.ir.AbstractCompileKotlinAgainstKlibTest
9+
import org.jetbrains.kotlin.codegen.ir.AbstractJKlibTest
910
import org.jetbrains.kotlin.generators.impl.generateTestGroupSuite
1011
import org.jetbrains.kotlin.test.TargetBackend
1112

@@ -17,6 +18,9 @@ fun main(args: Array<String>) {
1718
testClass<AbstractCompileKotlinAgainstKlibTest> {
1819
model("codegen/boxKlib", targetBackend = TargetBackend.JVM_IR)
1920
}
21+
testClass<AbstractJKlibTest> {
22+
model("codegen/jklib", targetBackend = TargetBackend.JVM_IR)
23+
}
2024
}
2125
}
2226
}

0 commit comments

Comments
 (0)