Refactor repl and JSR 223 support code, add separate example jars for each type of JSR 223 repl (with tests)

This commit is contained in:
Ilya Chernikov
2016-09-26 17:56:26 +02:00
parent 079fece9b4
commit 10d259771c
22 changed files with 925 additions and 152 deletions
@@ -1 +1 @@
org.jetbrains.kotlin.jsr223.KotlinJvmJsr223StandardScriptEngineFactory4Idea
org.jetbrains.kotlin.jsr223.KotlinJsr223StandardScriptEngineFactory4Idea
@@ -0,0 +1,85 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.daemon.client.DaemonReportMessage
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.client.KotlinRemoteReplCompiler
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.utils.PathUtil
import java.io.File
import javax.script.ScriptContext
import javax.script.ScriptEngineFactory
import javax.script.ScriptException
class KotlinJsr223JvmScriptEngine4Idea(
disposable: Disposable,
factory: ScriptEngineFactory,
templateClasspath: List<File>,
templateClassName: String,
getScriptArgs: (ScriptContext) -> Array<Any?>?,
scriptArgsTypes: Array<Class<*>>?
) : KotlinJsr223JvmScriptEngineBase(factory) {
private val daemon by lazy {
val path = PathUtil.getKotlinPathsForIdeaPlugin().compilerPath
assert(path.exists())
val compilerId = CompilerId.makeCompilerId(path)
val daemonOptions = configureDaemonOptions()
val daemonJVMOptions = DaemonJVMOptions()
val daemonReportMessages = arrayListOf<DaemonReportMessage>()
KotlinCompilerClient.connectToCompileService(compilerId, daemonJVMOptions, daemonOptions, DaemonReportingTargets(null, daemonReportMessages), true, true)
?: throw ScriptException("Unable to connect to repl server:" + daemonReportMessages.joinToString("\n ", prefix = "\n ") { "${it.category.name} ${it.message}" })
}
private val replCompiler by lazy {
daemon.let {
KotlinRemoteReplCompiler(disposable,
it,
makeAutodeletingFlagFile("idea-jsr223-repl-session"),
CompileService.TargetPlatform.JVM,
templateClasspath,
templateClassName,
System.out)
}
}
// 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) }
override fun eval(codeLine: ReplCodeLine, history: Iterable<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.newClasspath)
}
}
@@ -17,26 +17,16 @@
package org.jetbrains.kotlin.jsr223
import com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.cli.common.KotlinVersion
import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase
import org.jetbrains.kotlin.utils.PathUtil
import javax.script.Bindings
import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.ScriptEngineFactory
@Suppress("unused") // used in javax.script.ScriptEngineFactory META-INF file
class KotlinJvmJsr223StandardScriptEngineFactory4Idea : ScriptEngineFactory {
override fun getLanguageName(): String = "kotlin"
override fun getLanguageVersion(): String = KotlinVersion.VERSION
override fun getEngineName(): String = "kotlin"
override fun getEngineVersion(): String = KotlinVersion.VERSION
override fun getExtensions(): List<String> = listOf("kts")
override fun getMimeTypes(): List<String> = listOf("text/x-kotlin")
override fun getNames(): List<String> = listOf("kotlin")
class KotlinJsr223StandardScriptEngineFactory4Idea : KotlinJsr223JvmScriptEngineFactoryBase() {
override fun getScriptEngine(): ScriptEngine =
KotlinJvmJsr223ScriptEngine4Idea(
KotlinJsr223JvmScriptEngine4Idea(
Disposer.newDisposable(),
this,
listOf(PathUtil.getKotlinPathsForIdeaPlugin().runtimePath),
@@ -48,22 +38,4 @@ class KotlinJvmJsr223StandardScriptEngineFactory4Idea : ScriptEngineFactory {
bindings) },
arrayOf(Array<String>::class.java, java.util.Map::class.java)
)
override fun getOutputStatement(toDisplay: String?): String = "print(\"$toDisplay\")"
override fun getMethodCallSyntax(obj: String, m: String, vararg args: String): String = "$obj.$m(${args.joinToString()})"
override fun getProgram(vararg statements: String): String {
val sep = System.getProperty("line.separator")
return statements.joinToString(sep) + sep
}
override fun getParameter(key: String?): Any? =
when (key) {
ScriptEngine.NAME -> engineName
ScriptEngine.LANGUAGE -> languageName
ScriptEngine.LANGUAGE_VERSION -> languageVersion
ScriptEngine.ENGINE -> engineName
ScriptEngine.ENGINE_VERSION -> engineVersion
else -> null
}
}