Skip to content

Commit

Permalink
Add new REPL API JVM implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile authored and ligee committed Apr 16, 2020
1 parent 4c2c44b commit d2fec96
Show file tree
Hide file tree
Showing 51 changed files with 2,546 additions and 492 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ tasks {
// dependsOn(":kotlin-scripting-jvm-host-test:embeddableTest")
dependsOn(":kotlin-scripting-jsr223-test:embeddableTest")
dependsOn(":kotlin-main-kts-test:test")
dependsOn(":kotlin-scripting-ide-services-test:test")
}

register("compilerTest") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ import kotlin.concurrent.write

data class LineId(override val no: Int, override val generation: Int, private val codeHash: Int) : ILineId, Serializable {

constructor(codeLine: ReplCodeLine): this(codeLine.no, codeLine.generation, codeLine.code.hashCode())

override fun compareTo(other: ILineId): Int = (other as? LineId)?.let {
no.compareTo(it.no).takeIf { it != 0 }
?: generation.compareTo(it.generation).takeIf { it != 0 }
?: codeHash.compareTo(it.codeHash)
override fun compareTo(other: ILineId): Int = (other as? LineId)?.let { lineId ->
no.compareTo(lineId.no).takeIf { no -> no != 0 }
?: codeHash.compareTo(lineId.codeHash)
} ?: -1 // TODO: check if it doesn't break something

companion object {
private val serialVersionUID: Long = 8328353000L
private const val serialVersionUID: Long = 8328354000L
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.write

open class GenericReplCompilingEvaluatorBase(
val compiler: ReplCompiler,
val compiler: ReplCompilerWithoutCheck,
val evaluator: ReplEvaluator,
private val fallbackScriptArgs: ScriptArgsWithTypes? = null
) : ReplFullEvaluator {
Expand All @@ -46,7 +46,7 @@ open class GenericReplCompilingEvaluatorBase(
}
ReplEvalResult.Error.CompileTime(compiled.message, compiled.location)
}
is ReplCompileResult.Incomplete -> ReplEvalResult.Incomplete()
is ReplCompileResult.Incomplete -> ReplEvalResult.Incomplete(compiled.message)
is ReplCompileResult.CompiledClasses -> {
val result = eval(state, compiled, scriptArgs, invokeWrapper)
when (result) {
Expand Down Expand Up @@ -75,8 +75,6 @@ open class GenericReplCompilingEvaluatorBase(
override fun eval(state: IReplStageState<*>, compileResult: ReplCompileResult.CompiledClasses, scriptArgs: ScriptArgsWithTypes?, invokeWrapper: InvokeWrapper?): ReplEvalResult =
evaluator.eval(state, compileResult, scriptArgs, invokeWrapper)

override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult = compiler.check(state, codeLine)

override fun compileToEvaluable(state: IReplStageState<*>, codeLine: ReplCodeLine, defaultScriptArgs: ScriptArgsWithTypes?): Pair<ReplCompileResult, Evaluable?> {
val compiled = compiler.compile(state, codeLine)
return when (compiled) {
Expand All @@ -96,7 +94,7 @@ open class GenericReplCompilingEvaluatorBase(
}

class GenericReplCompilingEvaluator(
compiler: ReplCompiler,
compiler: ReplCompilerWithoutCheck,
baseClasspath: Iterable<File>,
baseClassloader: ClassLoader? = Thread.currentThread().contextClassLoader,
fallbackScriptArgs: ScriptArgsWithTypes? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const val KOTLIN_SCRIPT_ENGINE_BINDINGS_KEY = "kotlin.script.engine"

abstract class KotlinJsr223JvmScriptEngineBase(protected val myFactory: ScriptEngineFactory) : AbstractScriptEngine(), ScriptEngine, Compilable {

protected abstract val replCompiler: ReplCompiler
protected abstract val replCompiler: ReplCompilerWithoutCheck
protected abstract val replEvaluator: ReplFullEvaluator

override fun eval(script: String, context: ScriptContext): Any? = compileAndEval(script, context)
Expand Down Expand Up @@ -72,7 +72,7 @@ abstract class KotlinJsr223JvmScriptEngineBase(protected val myFactory: ScriptEn
val result = replCompiler.compile(state, codeLine)
val compiled = when (result) {
is ReplCompileResult.Error -> throw ScriptException("Error${result.locationString()}: ${result.message}")
is ReplCompileResult.Incomplete -> throw ScriptException("error: incomplete code")
is ReplCompileResult.Incomplete -> throw ScriptException("Error: incomplete code; ${result.message}")
is ReplCompileResult.CompiledClasses -> result
}
return CompiledKotlinScript(this, codeLine, compiled)
Expand Down Expand Up @@ -103,7 +103,7 @@ abstract class KotlinJsr223JvmScriptEngineBase(protected val myFactory: ScriptEn
throw ScriptException(result.message, result.location.path, result.location.line, result.location.column)
else -> throw ScriptException(result.message)
}
is ReplEvalResult.Incomplete -> throw ScriptException("error: incomplete code")
is ReplEvalResult.Incomplete -> throw ScriptException("Error: incomplete code. ${result.message}")
is ReplEvalResult.HistoryMismatch -> throw ScriptException("Repl history mismatch at line: ${result.lineNo}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sealed class ReplCompileResult : Serializable {
companion object { private val serialVersionUID: Long = 2L }
}

class Incomplete : ReplCompileResult() {
class Incomplete(val message: String) : ReplCompileResult() {
companion object { private val serialVersionUID: Long = 1L }
}

Expand All @@ -110,7 +110,9 @@ sealed class ReplCompileResult : Serializable {
}
}

interface ReplCompiler : ReplCompileAction, ReplCheckAction, CreateReplStageStateAction
interface ReplCompilerWithoutCheck : ReplCompileAction, CreateReplStageStateAction

interface ReplCompiler : ReplCompilerWithoutCheck, ReplCheckAction

// --- eval

Expand All @@ -137,7 +139,7 @@ sealed class ReplEvalResult : Serializable {
companion object { private val serialVersionUID: Long = 1L }
}

class Incomplete : ReplEvalResult() {
class Incomplete(val message: String) : ReplEvalResult() {
companion object { private val serialVersionUID: Long = 1L }
}

Expand Down Expand Up @@ -175,7 +177,7 @@ interface ReplAtomicEvalAction {
invokeWrapper: InvokeWrapper? = null): ReplEvalResult
}

interface ReplAtomicEvaluator : ReplAtomicEvalAction, ReplCheckAction
interface ReplAtomicEvaluator : ReplAtomicEvalAction

interface ReplDelayedEvalAction {
fun compileToEvaluable(state: IReplStageState<*>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ package org.jetbrains.kotlin.daemon.client.experimental
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.common.repl.*
import org.jetbrains.kotlin.daemon.client.KotlinRemoteReplCompilerClient
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.daemon.common.CompileServiceClientSide
import org.jetbrains.kotlin.daemon.common.experimental.findCallbackServerSocket
import org.jetbrains.kotlin.daemon.common.ReportCategory
import org.jetbrains.kotlin.daemon.common.ReportSeverity
import java.io.File
import java.util.concurrent.locks.ReentrantReadWriteLock
import org.jetbrains.kotlin.daemon.client.RemoteReplCompilerState

// TODO: reduce number of ports used then SOCKET_ANY_FREE_PORT is passed (same problem with other calls)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ compiler/testData/multiplatform/regressions/kt28385/jvm.kt:5:6: error: expecting
sdax = {
^
compiler/testData/multiplatform/regressions/kt28385/jvm.kt:5:8: error: expecting a top level declaration
sdax = {
^
compiler/testData/multiplatform/regressions/kt28385/jvm.kt:5:8: error: function declaration must have a name
sdax = {
^
compiler/testData/multiplatform/regressions/kt28385/jvm.kt:3:1: error: property must be initialized
val dasda
^
compiler/testData/multiplatform/regressions/kt28385/jvm.kt:5:8: error: function declaration must have a name
sdax = {
^
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class KotlinJsr223JvmScriptEngine4Idea(

private val messageCollector = MyMessageCollector()

override val replCompiler: ReplCompiler by lazy {
override val replCompiler: ReplCompilerWithoutCheck by lazy {
KotlinRemoteReplCompilerClient(
daemon,
makeAutodeletingFlagFile("idea-jsr223-repl-session"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.common.repl.ReplCodeLine
import org.jetbrains.kotlin.cli.common.repl.ReplCompileResult
import org.jetbrains.kotlin.cli.common.repl.ReplCompiler
import org.jetbrains.kotlin.cli.common.repl.ReplEvalResult
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluator
class LegacyReplTest : TestCase() {
fun testReplBasics() {
LegacyTestRepl().use { repl ->
val res1 = repl.replCompiler.check(repl.state, ReplCodeLine(0, 0, "val x ="))
TestCase.assertTrue("Unexpected check results: $res1", res1 is ReplCheckResult.Incomplete)
val res1 = repl.replCompiler.compile(repl.state, ReplCodeLine(0, 0, "val x ="))
TestCase.assertTrue("Unexpected check results: $res1", res1 is ReplCompileResult.Incomplete)

assertEvalResult(repl, "val l1 = listOf(1 + 2)\nl1.first()", 3)

Expand Down Expand Up @@ -54,8 +54,8 @@ class LegacyReplTest : TestCase() {
fun testReplCodeFormat() {
LegacyTestRepl().use { repl ->
val codeLine0 = ReplCodeLine(0, 0, "val l1 = 1\r\nl1\r\n")
val res0 = repl.replCompiler.check(repl.state, codeLine0)
val res0c = res0 as? ReplCheckResult.Ok
val res0 = repl.replCompiler.compile(repl.state, codeLine0)
val res0c = res0 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res0", res0c)
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ internal class LegacyTestRepl : Closeable {
fun nextCodeLine(code: String): ReplCodeLine = ReplCodeLine(currentLineCounter.getAndIncrement(), 0, code)

val replCompiler: JvmReplCompiler by lazy {
JvmReplCompiler(simpleScriptCompilationConfiguration)
JvmReplCompiler(simpleScriptCompilationConfiguration, false)
}

val compiledEvaluator: ReplEvaluator by lazy {
Expand Down
Loading

0 comments on commit d2fec96

Please sign in to comment.