K2 Scripting: fix order of arguments processing
and base class handling: Since in K2 we do not distinguish between script arguments taken from the base class and provided properties, we need this extra functionality to preserve the argument order of K1 scripts. This is a temporary measure, since we're going to deprecate base class usage at some point (KT-60449), so the relevant constructor arguments should disappear too.
This commit is contained in:
committed by
Space Team
parent
96bde033e1
commit
020a590df7
@@ -239,6 +239,7 @@ class Fir2IrVisitor(
|
||||
}
|
||||
if (isSelf) {
|
||||
irScript.thisReceiver = irReceiver
|
||||
irScript.baseClass = irReceiver.type
|
||||
null
|
||||
} else irReceiver
|
||||
}
|
||||
|
||||
+4
@@ -305,6 +305,10 @@ private class ScriptsToClassesLowering(val context: JvmBackendContext, val inner
|
||||
} else {
|
||||
+irDelegatingConstructorCall(baseClassCtor).also {
|
||||
explicitParameters.forEachIndexed { idx, valueParameter ->
|
||||
// Since in K2 we're not distinguishing between base class ctor args and other script args, we need this check.
|
||||
// The logic is fragile, but since we plan to deprecate baseClass support (see KT-60449), and this delegating
|
||||
// call will go away, let's leave it as is for now
|
||||
if (idx >= it.valueArgumentsCount) return@forEachIndexed
|
||||
it.putValueArgument(
|
||||
idx,
|
||||
IrGetValueImpl(
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.impl
|
||||
|
||||
import kotlin.script.experimental.api.ScriptCompilationConfigurationKeys
|
||||
import kotlin.script.experimental.util.PropertiesCollection
|
||||
|
||||
/**
|
||||
* Set by the compiler: the language version used for script compilation
|
||||
*/
|
||||
val ScriptCompilationConfigurationKeys._languageVersion by PropertiesCollection.key<String>()
|
||||
+1
-1
@@ -22,7 +22,7 @@ import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTe
|
||||
class ConfigurationDslTest : TestCase() {
|
||||
|
||||
@Test
|
||||
fun testComposableRefinementHandlers() = expectTestToFailOnK2 {
|
||||
fun testComposableRefinementHandlers() {
|
||||
val baseConfig = createJvmCompilationConfigurationFromTemplate<SimpleScript> {
|
||||
updateClasspath(classpathFromClass<SimpleScript>())
|
||||
defaultImports(MyTestAnnotation1::class, MyTestAnnotation2::class)
|
||||
|
||||
+1
-1
@@ -36,7 +36,7 @@ class ConstructorArgumentsOrderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithBoth() = expectTestToFailOnK2 {
|
||||
fun testScriptWithBoth() {
|
||||
val res = evalString<ScriptWithBoth>("""println(providedString + receiverString)""") {
|
||||
providedProperties("providedString" to "Hello")
|
||||
implicitReceivers(ImplicitReceiverClass(" Both!"))
|
||||
|
||||
+19
-2
@@ -8,6 +8,7 @@ package kotlin.script.experimental.jvm
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.impl._languageVersion
|
||||
|
||||
open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
||||
|
||||
@@ -71,6 +72,13 @@ open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
||||
refinedEvalConfiguration: ScriptEvaluationConfiguration,
|
||||
importedScriptsEvalResults: List<EvaluationResult>
|
||||
): Any {
|
||||
val isCompiledWithK2 =
|
||||
refinedEvalConfiguration[ScriptEvaluationConfiguration.compilationConfiguration]
|
||||
?.get(ScriptCompilationConfiguration._languageVersion)
|
||||
?.let {
|
||||
it.substringBefore('.').toIntOrNull()?.let { it >= 2 }
|
||||
} == true
|
||||
|
||||
val args = ArrayList<Any?>()
|
||||
|
||||
refinedEvalConfiguration[ScriptEvaluationConfiguration.previousSnippets]?.let {
|
||||
@@ -81,6 +89,12 @@ open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
||||
args.addAll(it)
|
||||
}
|
||||
|
||||
if (isCompiledWithK2) {
|
||||
refinedEvalConfiguration[ScriptEvaluationConfiguration.providedProperties]?.forEach {
|
||||
args.add(it.value)
|
||||
}
|
||||
}
|
||||
|
||||
importedScriptsEvalResults.forEach {
|
||||
args.add(it.returnValue.scriptInstance)
|
||||
}
|
||||
@@ -88,8 +102,11 @@ open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
||||
refinedEvalConfiguration[ScriptEvaluationConfiguration.implicitReceivers]?.let {
|
||||
args.addAll(it)
|
||||
}
|
||||
refinedEvalConfiguration[ScriptEvaluationConfiguration.providedProperties]?.forEach {
|
||||
args.add(it.value)
|
||||
|
||||
if (!isCompiledWithK2) {
|
||||
refinedEvalConfiguration[ScriptEvaluationConfiguration.providedProperties]?.forEach {
|
||||
args.add(it.value)
|
||||
}
|
||||
}
|
||||
|
||||
val ctor = java.constructors.single()
|
||||
|
||||
+2
-2
@@ -66,7 +66,7 @@ class MainKtsIT {
|
||||
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
@Test
|
||||
fun testCache() = expectTestToFailOnK2 {
|
||||
fun testCache() {
|
||||
val script = File("$TEST_DATA_ROOT/import-test.main.kts").absolutePath
|
||||
val cache = createTempDirectory("main.kts.test")
|
||||
|
||||
@@ -93,7 +93,7 @@ class MainKtsIT {
|
||||
|
||||
@OptIn(ExperimentalPathApi::class)
|
||||
@Test
|
||||
fun testCacheInProcess() = expectTestToFailOnK2 {
|
||||
fun testCacheInProcess() {
|
||||
val script = File("$TEST_DATA_ROOT/import-test.main.kts").absolutePath
|
||||
val cache = createTempDirectory("main.kts.test")
|
||||
|
||||
|
||||
+1
-1
@@ -126,7 +126,7 @@ class MainKtsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testImport() = expectTestToFailOnK2 {
|
||||
fun testImport() {
|
||||
|
||||
val out = captureOut {
|
||||
val res = evalFile(File("$TEST_DATA_ROOT/import-test.main.kts"))
|
||||
|
||||
+2
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.scripting.resolve.resolvedImportScripts
|
||||
import org.jetbrains.kotlin.utils.topologicalSort
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
||||
import kotlin.script.experimental.impl._languageVersion
|
||||
import kotlin.script.experimental.jvm.JvmDependency
|
||||
import kotlin.script.experimental.jvm.JvmDependencyFromClassLoader
|
||||
import kotlin.script.experimental.jvm.compilationCache
|
||||
@@ -155,6 +156,7 @@ private fun compileImpl(
|
||||
dependenciesProvider?.getScriptConfigurationResult(ktFile, context.baseScriptCompilationConfiguration)
|
||||
?.valueOrNull()?.configuration ?: context.baseScriptCompilationConfiguration
|
||||
refinedConfiguration.with {
|
||||
_languageVersion(context.environment.configuration.languageVersionSettings.languageVersion.versionString)
|
||||
// Adjust definitions so all compiler dependencies are saved in the resulting compilation configuration, so evaluation
|
||||
// performed with the expected classpath
|
||||
// TODO: make this logic obsolete by injecting classpath earlier in the pipeline
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ class ScriptTemplateTest : TestCase() {
|
||||
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||
}
|
||||
|
||||
fun testScriptWithBaseClassWithParam() = expectTestToFailOnK2 {
|
||||
fun testScriptWithBaseClassWithParam() {
|
||||
val messageCollector = TestMessageCollector()
|
||||
val aClass =
|
||||
compileScript("fib_dsl.kts", ScriptWithBaseClass::class, null, runIsolated = false, messageCollector = messageCollector)
|
||||
|
||||
Reference in New Issue
Block a user