Refactor JSR223 to support Compilable interface, drop daemon eval engine and sample, simplify

This commit is contained in:
Ilya Chernikov
2016-12-06 19:14:05 +01:00
parent b19d61e2f4
commit cb7f22ffec
13 changed files with 227 additions and 451 deletions
@@ -17,8 +17,10 @@
package org.jetbrains.kotlin.jsr223
import com.intellij.openapi.Disposable
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.repl.*
import org.jetbrains.kotlin.cli.common.repl.GenericReplCompiledEvaluator
import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase
import org.jetbrains.kotlin.cli.common.repl.ReplCompiledEvaluator
import org.jetbrains.kotlin.cli.common.repl.ReplCompiler
import org.jetbrains.kotlin.daemon.client.DaemonReportMessage
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
@@ -52,7 +54,7 @@ class KotlinJsr223JvmScriptEngine4Idea(
?: throw ScriptException("Unable to connect to repl server:" + daemonReportMessages.joinToString("\n ", prefix = "\n ") { "${it.category.name} ${it.message}" })
}
private val replCompiler by lazy {
override val replCompiler: ReplCompiler by lazy {
daemon.let {
KotlinRemoteReplCompiler(disposable,
it,
@@ -65,22 +67,7 @@ class KotlinJsr223JvmScriptEngine4Idea(
}
// TODO: bindings passing works only once on the first eval, subsequent setContext/setBindings call have no effect. Consider making it dynamic, but take history into account
val localEvaluator by lazy { GenericReplCompiledEvaluator(templateClasspath, Thread.currentThread().contextClassLoader, getScriptArgs(getContext()), scriptArgsTypes) }
val localEvaluator: ReplCompiledEvaluator by lazy { GenericReplCompiledEvaluator(templateClasspath, Thread.currentThread().contextClassLoader, getScriptArgs(getContext()), scriptArgsTypes) }
override fun eval(codeLine: ReplCodeLine, history: List<ReplCodeLine>): ReplEvalResult {
fun ReplCompileResult.Error.locationString() =
if (location == CompilerMessageLocation.NO_LOCATION) ""
else " at ${location.line}:${location.column}"
val compileResult = replCompiler.compile(codeLine, history)
val compiled = when (compileResult) {
is ReplCompileResult.Error -> throw ScriptException("Error${compileResult.locationString()}: ${compileResult.message}")
is ReplCompileResult.Incomplete -> throw ScriptException("error: incomplete code")
is ReplCompileResult.HistoryMismatch -> throw ScriptException("Repl history mismatch at line: ${compileResult.lineNo}")
is ReplCompileResult.CompiledClasses -> compileResult
}
return localEvaluator.eval(codeLine, history, compiled.classes, compiled.hasResult, compiled.classpathAddendum)
}
override val replEvaluator: ReplCompiledEvaluator get() = localEvaluator
}