Skip to content

Commit

Permalink
[JVM] Load in-test defined classes by STDLIB_COMPILATION directive
Browse files Browse the repository at this point in the history
In the current implementation, box test classloader loads
classes from `kotlin-stdlib.jar` even if we redefine it in the test
sources. We need to change this behavior, e.g., to test stdlib
compilation. However, implementation of such classloader consumes more
metaspace memory as we cannot share the stdlib classloader between
different test instances. Thus, we leave the old behavior by-default and
change it only if the test has `STDLIB_COMPILATION` directive.
  • Loading branch information
grechkovlad authored and Space Team committed May 21, 2024
1 parent 5375950 commit dd6bcec
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
MODULE main
CLASS kotlin/UIntArray.class
CLASS METADATA
K1
---
K2
equals(Ljava/lang/Object;)Z
K1
equals-impl([ILjava/lang/Object;)Z
K2
---
K1
---
K2
hashCode()I
K1
hashCode-impl([I)I
K2
---
K1
---
K2
toString()Ljava/lang/String;
K1
toString-impl([I)Ljava/lang/String;
K2
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TARGET_BACKEND: JVM
// WITH_STDLIB
// LANGUAGE: +MultiPlatformProjects
// JVM_ABI_K1_K2_DIFF: KT-67645
// STDLIB_COMPILATION

// MODULE: common
// FILE: array.kt

package kotlin

@kotlin.jvm.JvmInline
value class UIntArray(val delegate: IntArray) : Collection<UInt> {
override val size: Int
get() = delegate.size

override fun isEmpty(): Boolean = null!!
override fun iterator(): Iterator<UInt> = null!!
override fun containsAll(elements: Collection<UInt>): Boolean = null!!
override fun contains(element: UInt): Boolean = null!!
operator fun get(index: Int): Int = 42
operator fun set(index: Int, value: UInt) {}
}

// MODULE: main()()(common)
// FILE: test.kt

fun box(): String = if (UIntArray(intArrayOf(1))[0] == 42) "OK" else "Fail"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.ATTACH_DEBUGGE
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.REQUIRES_SEPARATE_PROCESS
import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives.JDK_KIND
import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives.ENABLE_JVM_PREVIEW
import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives.STDLIB_COMPILATION
import org.jetbrains.kotlin.test.directives.model.singleOrZeroValue
import org.jetbrains.kotlin.test.model.BinaryArtifacts
import org.jetbrains.kotlin.test.model.DependencyKind
Expand Down Expand Up @@ -330,13 +331,24 @@ internal fun generatedTestClassLoader(
classFileFactory: ClassFileFactory,
): GeneratedClassLoader {
val configuration = testServices.compilerConfigurationProvider.getCompilerConfiguration(module)
val parentClassLoader = if (configuration[TEST_CONFIGURATION_KIND_KEY]?.withReflection == true) {
testServices.standardLibrariesPathProvider.getRuntimeAndReflectJarClassLoader()
val classpath = computeTestRuntimeClasspath(testServices, module)
if (STDLIB_COMPILATION in module.directives) {
val libPathProvider = testServices.standardLibrariesPathProvider
classpath += libPathProvider.runtimeJarForTests()
if (configuration[TEST_CONFIGURATION_KIND_KEY]?.withReflection == true) {
classpath += libPathProvider.reflectJarForTests()
}
classpath += libPathProvider.scriptRuntimeJarForTests()
classpath += libPathProvider.kotlinTestJarForTests()
return GeneratedClassLoader(classFileFactory, null, *(classpath.map { it.toURI().toURL() }.toTypedArray()))
} else {
testServices.standardLibrariesPathProvider.getRuntimeJarClassLoader()
val parentClassLoader = if (configuration[TEST_CONFIGURATION_KIND_KEY]?.withReflection == true) {
testServices.standardLibrariesPathProvider.getRuntimeAndReflectJarClassLoader()
} else {
testServices.standardLibrariesPathProvider.getRuntimeJarClassLoader()
}
return GeneratedClassLoader(classFileFactory, parentClassLoader, *classpath.map { it.toURI().toURL() }.toTypedArray())
}
val classpath = computeTestRuntimeClasspath(testServices, module)
return GeneratedClassLoader(classFileFactory, parentClassLoader, *classpath.map { it.toURI().toURL() }.toTypedArray())
}

private fun computeTestRuntimeClasspath(testServices: TestServices, rootModule: TestModule): MutableList<File> {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd6bcec

Please sign in to comment.