Fix imported script functionality in JSR-223/REPL:
- setup compilation properly to take imported scripts into account - add compiled imported scripts into generated results = calculate ScriptLightVirtualFile path as relative - simplifies imported scripts location
This commit is contained in:
+1
-1
@@ -267,7 +267,7 @@ class ScriptingHostTest : TestCase() {
|
||||
assertTrue(res is ResultWithDiagnostics.Failure)
|
||||
val report = res.reports.find { it.message.startsWith("Source file or directory not found") }
|
||||
assertNotNull(report)
|
||||
assertEquals("/script.kts", report?.sourcePath)
|
||||
assertEquals("script.kts", report?.sourcePath)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+17
@@ -1,4 +1,6 @@
|
||||
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
|
||||
import org.jetbrains.kotlin.mainKts.test.TEST_DATA_ROOT
|
||||
import org.jetbrains.kotlin.mainKts.test.captureOut
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import javax.script.ScriptEngineManager
|
||||
@@ -32,5 +34,20 @@ class MainKtsJsr223Test {
|
||||
val res2 = engine.eval("z * x")
|
||||
Assert.assertEquals(42, res2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWithImport() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("main.kts")!!
|
||||
val out = captureOut {
|
||||
val res1 = engine.eval("""
|
||||
@file:Import("$TEST_DATA_ROOT/import-common.main.kts")
|
||||
@file:Import("$TEST_DATA_ROOT/import-middle.main.kts")
|
||||
sharedVar = sharedVar + 1
|
||||
println(sharedVar)
|
||||
""".trimIndent())
|
||||
Assert.assertNull(res1)
|
||||
}.lines()
|
||||
Assert.assertEquals(listOf("Hi from common", "Hi from middle", "5"), out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -12,10 +12,10 @@ import java.io.File
|
||||
import java.io.PrintStream
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.host.toScriptSource
|
||||
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
|
||||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
|
||||
import kotlin.script.experimental.jvm.baseClassLoader
|
||||
import kotlin.script.experimental.jvm.jvm
|
||||
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
|
||||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
|
||||
|
||||
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
|
||||
|
||||
@@ -107,7 +107,7 @@ class MainKtsTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun captureOut(body: () -> Unit): String {
|
||||
internal fun captureOut(body: () -> Unit): String {
|
||||
val outStream = ByteArrayOutputStream()
|
||||
val prevOut = System.out
|
||||
System.setOut(PrintStream(outStream))
|
||||
|
||||
+2
-2
@@ -30,7 +30,6 @@ import kotlin.script.experimental.dependencies.ScriptDependencies
|
||||
import kotlin.script.experimental.host.*
|
||||
import kotlin.script.experimental.jvm.*
|
||||
import kotlin.script.experimental.jvm.compat.mapToDiagnostics
|
||||
import kotlin.script.experimental.jvm.impl.refineWith
|
||||
import kotlin.script.experimental.jvm.impl.toClassPathOrEmpty
|
||||
import kotlin.script.experimental.jvm.impl.toDependencies
|
||||
|
||||
@@ -97,7 +96,8 @@ class ScriptLightVirtualFile(name: String, private val _path: String?, text: Str
|
||||
charset = CharsetToolkit.UTF8_CHARSET
|
||||
}
|
||||
|
||||
override fun getPath(): String = _path ?: super.getPath()
|
||||
override fun getPath(): String = _path ?: if (parent != null) parent.path + "/" + name else name
|
||||
|
||||
override fun getCanonicalPath(): String? = path
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -140,7 +140,7 @@ class KJvmReplCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) :
|
||||
KotlinCodegenFacade.generatePackage(
|
||||
generationState,
|
||||
snippetKtFile.script!!.containingKtFile.packageFqName,
|
||||
setOf(snippetKtFile.script!!.containingKtFile),
|
||||
sourceFiles,
|
||||
CompilationErrorHandler.THROW_EXCEPTION
|
||||
)
|
||||
|
||||
|
||||
+3
-1
@@ -103,7 +103,9 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) {
|
||||
}
|
||||
|
||||
private fun doAnalyze(linePsi: KtFile, importedScripts: List<KtFile>, codeLine: ReplCodeLine): ReplLineAnalysisResult {
|
||||
scriptDeclarationFactory.setDelegateFactory(FileBasedDeclarationProviderFactory(resolveSession.storageManager, listOf(linePsi)))
|
||||
scriptDeclarationFactory.setDelegateFactory(
|
||||
FileBasedDeclarationProviderFactory(resolveSession.storageManager, listOf(linePsi) + importedScripts)
|
||||
)
|
||||
replState.submitLine(linePsi, codeLine)
|
||||
|
||||
val context = topDownAnalyzer.analyzeDeclarations(topDownAnalysisContext.topDownAnalysisMode, listOf(linePsi) + importedScripts)
|
||||
|
||||
Reference in New Issue
Block a user