From 7b2ea001c14bad4f363dd3449fbb3c3297bdde6a Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Mon, 6 Feb 2017 17:18:42 +0100 Subject: [PATCH] Fix state conversion, fix tests --- .../cli/common/repl/AggregatedReplState.kt | 7 +- .../repl/GenericReplCompilingEvaluator.kt | 2 +- .../cli/common/repl/GenericReplEvaluator.kt | 2 +- .../KotlinJsr223JvmInvocableScriptEngine.kt | 7 +- .../kotlin/cli/common/repl/ReplState.kt | 4 +- .../kotlin/cli/jvm/repl/GenericReplChecker.kt | 2 +- .../cli/jvm/repl/GenericReplCompiler.kt | 2 +- ...t.kt => KotlinRemoteReplCompilerClient.kt} | 6 +- .../kotlin/daemon/KotlinRemoteReplService.kt | 2 +- .../kotlin/cli/jvm/repl/GenericReplTest.kt | 34 ++++--- .../kotlin/daemon/CompilerDaemonTest.kt | 88 +++++++------------ .../KotlinJsr223JvmScriptEngine4Idea.kt | 18 ++-- 12 files changed, 83 insertions(+), 91 deletions(-) rename compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/{KotlinRemoteReplClient.kt => KotlinRemoteReplCompilerClient.kt} (90%) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt index c8c3a6ff5b5..98088c6e02d 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/AggregatedReplState.kt @@ -74,6 +74,11 @@ open class AggregatedReplStageState(val state1: IReplStageState, val { override val history: IReplStageHistory> = AggregatedReplStateHistory(state1.history, state2.history, lock) - override fun > asState(): StateT = (state1 as? StateT) ?: (state2 as StateT) ?: super.asState() + override fun > asState(target: Class): StateT = + when { + target.isAssignableFrom(state1::class.java) -> state1 as StateT + target.isAssignableFrom(state2::class.java) -> state2 as StateT + else -> super.asState(target) + } } diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompilingEvaluator.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompilingEvaluator.kt index 92d22a939a0..d231d8f25ea 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompilingEvaluator.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompilingEvaluator.kt @@ -32,7 +32,7 @@ class GenericReplCompilingEvaluator(val compiler: ReplCompiler, override fun compileAndEval(state: IReplStageState<*>, codeLine: ReplCodeLine, scriptArgs: ScriptArgsWithTypes?, invokeWrapper: InvokeWrapper?): ReplEvalResult { return state.lock.write { - val aggregatedState = state.asState>() + val aggregatedState = state.asState(AggregatedReplStageState::class.java) val compiled = compiler.compile(state, codeLine) when (compiled) { is ReplCompileResult.Error -> ReplEvalResult.Error.CompileTime(compiled.message, compiled.location) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt index b07c00a756b..a33cc71efcd 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt @@ -47,7 +47,7 @@ open class GenericReplEvaluator(val baseClasspath: Iterable, scriptArgs: ScriptArgsWithTypes?, invokeWrapper: InvokeWrapper?): ReplEvalResult { state.lock.write { - val evalState = state.asState() + val evalState = state.asState(GenericReplEvaluatorState::class.java) // val verifyHistory = compileResult.state.dropLast(1) // val defaultHistoryActor = HistoryActions( // effectiveHistory = evalState.history.map { it.item }, diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt index 501092cbbc5..40254f10b1f 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/KotlinJsr223JvmInvocableScriptEngine.kt @@ -29,10 +29,11 @@ import kotlin.reflect.full.safeCast @Suppress("unused") // used externally (kotlin.script.utils) interface KotlinJsr223JvmInvocableScriptEngine : Invocable { - val replScriptEvaluator: ReplEvaluatorExposedInternalHistory + val state: IReplStageState<*> // The Invokable interface do not allow Context/Bindings substitution, so state is supplied via property private fun prioritizedHistory(receiverClass: KClass<*>?, receiverInstance: Any?): List { - return replScriptEvaluator.lastEvaluatedScripts.map { it.second }.filter { it.instance != null }.reversed().ensureNotEmpty("no script ").let { history -> + val evalState = state.asState(GenericReplEvaluatorState::class.java) + return evalState.history.map { it.item }.filter { it.instance != null }.reversed().ensureNotEmpty("no script ").let { history -> if (receiverInstance != null) { val receiverKlass = receiverClass ?: receiverInstance.javaClass.kotlin val receiverInHistory = history.find { it.instance == receiverInstance } ?: @@ -94,7 +95,7 @@ interface KotlinJsr223JvmInvocableScriptEngine : Invocable { } private fun proxyInterface(thiz: Any?, clasz: Class?): T? { - replScriptEvaluator.lastEvaluatedScripts.ensureNotEmpty("no script") + if (state.history.size == 0) throw IllegalStateException("no script") val priority = prioritizedHistory(thiz?.javaClass?.kotlin, thiz) if (clasz == null) throw IllegalArgumentException("class object cannot be null") diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplState.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplState.kt index e639151ddff..3a121482045 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplState.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/ReplState.kt @@ -77,7 +77,9 @@ interface IReplStageState { val lock: ReentrantReadWriteLock - fun > asState(): StateT = (this as? StateT) ?: throw IllegalArgumentException("$this is not an expected instance of IReplStageState") + fun > asState(target: Class): StateT = + if (target.isAssignableFrom(this::class.java)) this as StateT + else throw IllegalArgumentException("$this is not an expected instance of IReplStageState") } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt index e9e92ccc6e1..7f8d378762a 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplChecker.kt @@ -70,7 +70,7 @@ open class GenericReplChecker( override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult { state.lock.write { - val checkerState = state.asState() + val checkerState = state.asState(GenericReplCheckerState::class.java) val scriptFileName = makeScriptBaseName(codeLine, checkerState.generation.get()) val virtualFile = LightVirtualFile("$scriptFileName${KotlinParserDefinition.STD_SCRIPT_EXT}", KotlinLanguage.INSTANCE, StringUtil.convertLineSeparators(codeLine.code)).apply { diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt index 213e0c788eb..5bbd9a74f49 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericReplCompiler.kt @@ -47,7 +47,7 @@ open class GenericReplCompiler(disposable: Disposable, override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult { state.lock.write { - val compilerState = state.asState() + val compilerState = state.asState(GenericReplCompilerState::class.java) val currentGeneration = compilerState.generation.get() diff --git a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplClient.kt b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt similarity index 90% rename from compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplClient.kt rename to compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt index 22ae017f481..b0f1c8bdbc5 100644 --- a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplClient.kt +++ b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinRemoteReplCompilerClient.kt @@ -26,7 +26,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock // TODO: reduce number of ports used then SOCKET_ANY_FREE_PORT is passed (same problem with other calls) -open class KotlinRemoteReplClient( +open class KotlinRemoteReplCompilerClient( protected val compileService: CompileService, clientAliveFlagFile: File?, targetPlatform: CompileService.TargetPlatform, @@ -68,8 +68,8 @@ open class KotlinRemoteReplClient( RemoteReplCompilerState(compileService.replCreateState(sessionId).get(), lock) override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult = - compileService.replCheck(sessionId, state.asState().replStateFacade.id, codeLine).get() + compileService.replCheck(sessionId, state.asState(RemoteReplCompilerState::class.java).replStateFacade.id, codeLine).get() override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult = - compileService.replCompile(sessionId, state.asState().replStateFacade.id, codeLine).get() + compileService.replCompile(sessionId, state.asState(RemoteReplCompilerState::class.java).replStateFacade.id, codeLine).get() } diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt index 1df1f952a4c..26bc0e37d82 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt @@ -151,7 +151,7 @@ open class KotlinJvmReplService( fun createRemoteState(port: Int = portForServers): RemoteReplStateFacadeServer = statesLock.write { val id = getValidId(stateIdCounter) { id -> states.none { it.key.id == id} } - val stateFacade = RemoteReplStateFacadeServer(id, createState().asState(), port) + val stateFacade = RemoteReplStateFacadeServer(id, createState().asState(GenericReplCompilerState::class.java), port) states.put(stateFacade, true) stateFacade } diff --git a/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt b/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt index e809a8678cf..e08ba78cc19 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/cli/jvm/repl/GenericReplTest.kt @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.test.TestJdkKind import org.junit.Test import java.io.File import java.net.URLClassLoader +import java.util.concurrent.locks.ReentrantReadWriteLock class GenericReplTest : TestCase() { @Test @@ -44,37 +45,39 @@ class GenericReplTest : TestCase() { listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")), "kotlin.script.templates.standard.ScriptTemplateWithArgs") - val res1 = repl.replCompiler?.check(ReplCodeLine(0, "val x =")) + val state = repl.createState() + + val res1 = repl.replCompiler.check(state, ReplCodeLine(0, "val x =")) TestCase.assertTrue("Unexpected check results: $res1", res1 is ReplCheckResult.Incomplete) val codeLine0 = ReplCodeLine(0, "val l1 = listOf(1 + 2)\nl1.first()") - val res2 = repl.replCompiler?.compile(codeLine0, emptyList()) + val res2 = repl.replCompiler.compile(state, codeLine0) val res2c = res2 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res2", res2c) - val res21 = repl.compiledEvaluator.eval(res2c!!) + val res21 = repl.compiledEvaluator.eval(state, res2c!!) val res21e = res21 as? ReplEvalResult.ValueResult TestCase.assertNotNull("Unexpected eval result: $res21", res21e) TestCase.assertEquals(3, res21e!!.value) val codeLine1 = ReplCodeLine(1, "val x = 5") - val res3 = repl.replCompiler?.compile(codeLine1, listOf(codeLine0)) + val res3 = repl.replCompiler.compile(state, codeLine1) val res3c = res3 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res3", res3c) - val res31 = repl.compiledEvaluator.eval(res3c!!) + val res31 = repl.compiledEvaluator.eval(state, res3c!!) val res31e = res31 as? ReplEvalResult.UnitResult TestCase.assertNotNull("Unexpected eval result: $res31", res31e) val codeLine2 = ReplCodeLine(2, "x + 2") - val res4x = repl.replCompiler?.compile(codeLine2, listOf(codeLine1)) + val res4x = repl.replCompiler.compile(state, codeLine2) TestCase.assertNotNull("Unexpected compile result: $res4x", res4x as? ReplCompileResult.HistoryMismatch) - val res4 = repl.replCompiler?.compile(codeLine2, listOf(codeLine0, codeLine1)) + val res4 = repl.replCompiler.compile(state, codeLine2) val res4c = res4 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res4", res4c) - val res41 = repl.compiledEvaluator.eval(res4c!!) + val res41 = repl.compiledEvaluator.eval(state, res4c!!) val res41e = res41 as? ReplEvalResult.ValueResult TestCase.assertNotNull("Unexpected eval result: $res41", res41e) TestCase.assertEquals(7, res41e!!.value) @@ -108,22 +111,24 @@ class GenericReplTest : TestCase() { listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")), "kotlin.script.templates.standard.ScriptTemplateWithArgs") + val state = repl.createState() + val codeLine1 = ReplCodeLine(0, "package mypackage\n\nval x = 1\nx+2") - val res1 = repl.replCompiler?.compile(codeLine1, emptyList()) + val res1 = repl.replCompiler.compile(state, codeLine1) val res1c = res1 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res1", res1c) - val res11 = repl.compiledEvaluator.eval(res1c!!) + val res11 = repl.compiledEvaluator.eval(state, res1c!!) val res11e = res11 as? ReplEvalResult.ValueResult TestCase.assertNotNull("Unexpected eval result: $res11", res11e) TestCase.assertEquals(3, res11e!!.value) val codeLine2 = ReplCodeLine(1, "x+4") - val res2 = repl.replCompiler?.compile(codeLine2, listOf(codeLine1)) + val res2 = repl.replCompiler.compile(state, codeLine2) val res2c = res2 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res2", res2c) - val res21 = repl.compiledEvaluator.eval(res2c!!) + val res21 = repl.compiledEvaluator.eval(state, res2c!!) val res21e = res21 as? ReplEvalResult.ValueResult TestCase.assertNotNull("Unexpected eval result: $res21", res21e) TestCase.assertEquals(5, res21e!!.value) @@ -151,12 +156,15 @@ internal class TestRepl( private val scriptDef = makeScriptDefinition(templateClasspath, templateClassName) - val replCompiler : GenericReplCompiler? by lazy { + val replCompiler : GenericReplCompiler by lazy { GenericReplCompiler(disposable, scriptDef, configuration, PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, false)) } val compiledEvaluator: ReplEvaluator by lazy { GenericReplEvaluator(configuration.jvmClasspathRoots, null, emptyScriptArgs, ReplRepeatingMode.NONE) } + + fun createState(lock: ReentrantReadWriteLock = ReentrantReadWriteLock()): IReplStageState<*> = + AggregatedReplStageState(replCompiler.createState(lock), compiledEvaluator.createState(lock), lock) } diff --git a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt index 48244b4fc84..3f31035eaeb 100644 --- a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt @@ -25,8 +25,6 @@ import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase import org.jetbrains.kotlin.progress.CompilationCanceledStatus import org.jetbrains.kotlin.script.* import org.jetbrains.kotlin.test.KotlinTestUtils -import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase -import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File import java.net.URL @@ -111,7 +109,7 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { } } assertTrue("Expecting that compilation 1 ($compileTime1 ms) is at least two times longer than compilation 2 ($compileTime2 ms)", - compileTime1 > compileTime2 * 2) + compileTime1 > compileTime2 * 2) logFile.delete() run("hello.run", "-cp", jar, "Hello.HelloKt") } @@ -536,10 +534,12 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { fun testDaemonReplLocalEvalNoParams() { withDaemon { daemon -> - val repl = KotlinRemoteReplCompiler(daemon, null, CompileService.TargetPlatform.JVM, - classpathFromClassloader(), - ScriptWithNoParam::class.qualifiedName!!, - System.err) + val repl = KotlinRemoteReplCompilerClient(daemon!!, null, CompileService.TargetPlatform.JVM, + emptyArray(), + TestMessageCollector(), + classpathFromClassloader(), + ScriptWithNoParam::class.qualifiedName!!, + ScriptArgsWithTypes(emptyArray(), emptyArray())) val localEvaluator = GenericReplEvaluator(emptyList(), Thread.currentThread().contextClassLoader) @@ -550,10 +550,11 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { fun testDaemonReplLocalEvalStandardTemplate() { withDaemon { daemon -> - val repl = KotlinRemoteReplCompiler(daemon, null, CompileService.TargetPlatform.JVM, - classpathFromClassloader(), - "kotlin.script.templates.standard.ScriptTemplateWithArgs", - System.err) + val repl = KotlinRemoteReplCompilerClient(disposable, daemon!!, null, CompileService.TargetPlatform.JVM, emptyArray(), + TestMessageCollector(), + classpathFromClassloader(), + "kotlin.script.templates.standard.ScriptTemplateWithArgs", + ScriptArgsWithTypes(emptyArray(), emptyArray())) val localEvaluator = GenericReplEvaluator(emptyList(), Thread.currentThread().contextClassLoader, ScriptArgsWithTypes(arrayOf(emptyArray()), arrayOf(Array::class))) @@ -563,66 +564,37 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { } } - private fun doReplTestWithLocalEval(repl: KotlinRemoteReplCompiler, localEvaluator: ReplEvaluator) { - val res0 = repl.check(ReplCodeLine(0, "val x =")) + private fun doReplTestWithLocalEval(replCompiler: KotlinRemoteReplCompilerClient, localEvaluator: ReplEvaluator) { + + val compilerState = replCompiler.createState() + val evaluatorState = localEvaluator.createState() + + val res0 = replCompiler.check(compilerState, ReplCodeLine(0, "val x =")) TestCase.assertTrue("Unexpected check results: $res0", res0 is ReplCheckResult.Incomplete) val codeLine1 = ReplCodeLine(1, "val lst = listOf(1)\nval x = 5") - val res1 = repl.compile(codeLine1, emptyList()) + val res1 = replCompiler.compile(compilerState, codeLine1) val res1c = res1 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res1", res1c) - val res11 = localEvaluator.eval(res1c!!) + val res11 = localEvaluator.eval(evaluatorState, res1c!!) val res11e = res11 as? ReplEvalResult.UnitResult TestCase.assertNotNull("Unexpected eval result: $res11", res11e) val codeLine2 = ReplCodeLine(2, "x + 2") - val res2x = repl.compile(codeLine2, listOf(codeLine2)) + val res2x = replCompiler.compile(compilerState, codeLine2) TestCase.assertNotNull("Unexpected compile result: $res2x", res2x as? ReplCompileResult.HistoryMismatch) - val res2 = repl.compile(codeLine2, listOf(codeLine1)) + val res2 = replCompiler.compile(compilerState, codeLine2) val res2c = res2 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res2", res2c) - val res21 = localEvaluator.eval(res2c!!) + val res21 = localEvaluator.eval(evaluatorState, res2c!!) val res21e = res21 as? ReplEvalResult.ValueResult TestCase.assertNotNull("Unexpected eval result: $res21", res21e) TestCase.assertEquals(7, res21e!!.value) } - fun testDaemonReplRemoteEval() { - withDaemon { daemon -> - val evalOut = ByteArrayOutputStream() - val evalErr = ByteArrayOutputStream() - val evalIn = ByteArrayInputStream("42\n".toByteArray()) - - val repl = KotlinRemoteReplEvaluator(daemon, null, CompileService.TargetPlatform.JVM, - listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")), - "kotlin.script.templates.standard.ScriptTemplateWithArgs", - arrayOf(emptyArray()), - arrayOf(Array::class.java), - System.err, evalOut, evalErr, evalIn) - - val res0 = repl.check(ReplCodeLine(0, "val x =")) - TestCase.assertTrue("Unexpected check results: $res0", res0 is ReplCheckResult.Incomplete) - - val codeLine1 = ReplCodeLine(1, "val x = 5") - val res1 = repl.compileAndEval(codeLine1, verifyHistory = emptyList()) - val res1e = res1 as? ReplEvalResult.UnitResult - TestCase.assertNotNull("Unexpected eval result: $res1", res1e) - - val codeLine2 = ReplCodeLine(2, "x + 2") - val res2x = repl.compileAndEval(codeLine2, verifyHistory = listOf(codeLine2)) - TestCase.assertNotNull("Unexpected compile result: $res2x", res2x as? ReplEvalResult.HistoryMismatch) - - val res2 = repl.compileAndEval(codeLine2, verifyHistory = listOf(codeLine1)) - val res2e = res2 as? ReplEvalResult.ValueResult - TestCase.assertNotNull("Unexpected eval result: $res2", res2e) - TestCase.assertEquals(7, res2e!!.value) - repl.dispose() - } - } - fun testDaemonReplAutoshutdownOnIdle() { withFlagFile(getTestName(true), ".alive") { flagFile -> val daemonOptions = DaemonOptions(autoshutdownIdleSeconds = 1, autoshutdownUnusedSeconds = 1, shutdownDelayMilliseconds = 1, runFilesPath = File(tmpdir, getTestName(true)).absolutePath) @@ -636,15 +608,19 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() { val daemon = KotlinCompilerClient.connectToCompileService(compilerId, flagFile, daemonJVMOptions, daemonOptions, DaemonReportingTargets(out = System.err), autostart = true) assertNotNull("failed to connect daemon", daemon) - val repl = KotlinRemoteReplCompiler(daemon!!, null, CompileService.TargetPlatform.JVM, - classpathFromClassloader(), - ScriptWithNoParam::class.qualifiedName!!, - System.err) + val replCompiler = KotlinRemoteReplCompilerClient(daemon!!, null, CompileService.TargetPlatform.JVM, + emptyArray(), + TestMessageCollector(), + classpathFromClassloader(), + ScriptWithNoParam::class.qualifiedName!!, + ScriptArgsWithTypes(emptyArray(), emptyArray())) + + val compilerState = replCompiler.createState() // use repl compiler for >> 1s, making sure that idle/unused timeouts are not firing for (attempts in 1..10) { val codeLine1 = ReplCodeLine(attempts, "3 + 5") - val res1 = repl.compile(codeLine1) + val res1 = replCompiler.compile(compilerState, codeLine1) val res1c = res1 as? ReplCompileResult.CompiledClasses TestCase.assertNotNull("Unexpected compile result: $res1", res1c) Thread.sleep(200) diff --git a/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJsr223JvmScriptEngine4Idea.kt b/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJsr223JvmScriptEngine4Idea.kt index 4224603e8b4..801577ec6b4 100644 --- a/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJsr223JvmScriptEngine4Idea.kt +++ b/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJsr223JvmScriptEngine4Idea.kt @@ -23,7 +23,7 @@ import org.jetbrains.kotlin.cli.common.repl.* import org.jetbrains.kotlin.daemon.client.DaemonReportMessage import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient -import org.jetbrains.kotlin.daemon.client.KotlinRemoteReplClient +import org.jetbrains.kotlin.daemon.client.KotlinRemoteReplCompilerClient import org.jetbrains.kotlin.daemon.common.* import org.jetbrains.kotlin.utils.PathUtil import java.io.File @@ -59,14 +59,14 @@ class KotlinJsr223JvmScriptEngine4Idea( override val replCompiler: ReplCompiler by lazy { daemon.let { - KotlinRemoteReplClient(it, - makeAutodeletingFlagFile("idea-jsr223-repl-session"), - CompileService.TargetPlatform.JVM, - emptyArray(), - messageCollector, - templateClasspath, - templateClassName, - ScriptArgsWithTypes(emptyArray(), emptyArray())) + KotlinRemoteReplCompilerClient(it, + makeAutodeletingFlagFile("idea-jsr223-repl-session"), + CompileService.TargetPlatform.JVM, + emptyArray(), + messageCollector, + templateClasspath, + templateClassName, + ScriptArgsWithTypes(emptyArray(), emptyArray())) } }