Fix script compiler options processing

This commit is contained in:
Ilya Chernikov
2019-06-07 16:04:27 +02:00
parent 38a692abcf
commit c39896165e
2 changed files with 62 additions and 12 deletions
@@ -7,6 +7,9 @@ package kotlin.script.experimental.jvmhost.test
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.org.objectweb.asm.ClassReader
import org.jetbrains.org.objectweb.asm.ClassVisitor
import org.jetbrains.org.objectweb.asm.Opcodes
import org.junit.Assert
import org.junit.Test
import java.io.*
@@ -25,6 +28,7 @@ import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.experimental.jvm.impl.KJvmCompiledScript
import kotlin.script.experimental.jvmhost.*
import kotlin.script.experimental.jvmhost.impl.CompiledScriptClassLoader
import kotlin.script.experimental.jvmhost.impl.KJvmCompiledModuleInMemory
import kotlin.script.templates.standard.SimpleScriptTemplate
class ScriptingHostTest : TestCase() {
@@ -275,6 +279,38 @@ class ScriptingHostTest : TestCase() {
assertNotNull(res.reports.find { it.message == "The following compiler arguments are ignored when configured from refinement callbacks: -no-jdk, -no-stdlib" })
}
fun jvmTargetTestImpl(target: String, expectedVersion: Int) {
val script = "println(\"Hi\")"
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
compilerOptions("-jvm-target", target)
}
val compiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration)
val compiledScript = runBlocking { compiler(script.toScriptSource(name = "SavedScript.kts"), compilationConfiguration) }
assertTrue(compiledScript is ResultWithDiagnostics.Success)
val jvmCompiledScript = compiledScript.valueOrNull()!! as KJvmCompiledScript
val jvmCompiledModule = jvmCompiledScript.compiledModule as KJvmCompiledModuleInMemory
val bytes = jvmCompiledModule.compilerOutputFiles["SavedScript.class"]!!
var classFileVersion: Int? = null
ClassReader(bytes).accept(object : ClassVisitor(Opcodes.API_VERSION) {
override fun visit(
version: Int, access: Int, name: String?, signature: String?, superName: String?, interfaces: Array<out String>?
) {
classFileVersion = version
}
}, 0)
assertEquals(expectedVersion, classFileVersion)
}
@Test
fun testJvmTarget() {
jvmTargetTestImpl("1.6", 50)
jvmTargetTestImpl("1.8", 52)
jvmTargetTestImpl("9", 53)
}
@Test
fun testMemoryCache() {
val script = "val x = 1\nprintln(\"x = \$x\")"
@@ -99,9 +99,34 @@ internal fun createInitialConfigurations(
)
)
initialScriptCompilationConfiguration[ScriptCompilationConfiguration.compilerOptions]?.let { compilerOptions ->
kotlinCompilerConfiguration.updateWithCompilerOptions(compilerOptions, messageCollector, ignoredOptionsReportingState, false)
}
return Pair(initialScriptCompilationConfiguration, kotlinCompilerConfiguration)
}
private fun CompilerConfiguration.updateWithCompilerOptions(
compilerOptions: List<String>,
messageCollector: ScriptDiagnosticsMessageCollector,
ignoredOptionsReportingState: IgnoredOptionsReportingState,
isRefinement: Boolean
) {
val compilerArguments = K2JVMCompilerArguments()
parseCommandLineArguments(compilerOptions, compilerArguments)
reportArgumentsIgnoredGenerally(compilerArguments, messageCollector, ignoredOptionsReportingState)
if (isRefinement) {
reportArgumentsIgnoredFromRefinement(compilerArguments, messageCollector, ignoredOptionsReportingState)
}
setupCommonArguments(compilerArguments)
setupJvmSpecificArguments(compilerArguments)
configureAdvancedJvmOptions(compilerArguments)
}
private fun ScriptCompilationConfiguration.withUpdatesFromCompilerConfiguration(kotlinCompilerConfiguration: CompilerConfiguration) =
withUpdatedClasspath(kotlinCompilerConfiguration.jvmClasspathRoots)
@@ -212,17 +237,6 @@ private fun CompilerConfiguration.updateWithRefinedConfigurations(
if (updatedCompilerOptions.isNotEmpty() &&
updatedCompilerOptions != initialScriptCompilationConfiguration[ScriptCompilationConfiguration.compilerOptions]
) {
val updatedArguments = K2JVMCompilerArguments()
parseCommandLineArguments(updatedCompilerOptions, updatedArguments)
reportArgumentsIgnoredGenerally(updatedArguments, messageCollector, reportingState)
reportArgumentsIgnoredFromRefinement(updatedArguments, messageCollector, reportingState)
setupCommonArguments(updatedArguments)
setupJvmSpecificArguments(updatedArguments)
configureAdvancedJvmOptions(updatedArguments)
updateWithCompilerOptions(updatedCompilerOptions, messageCollector, reportingState, true)
}
}