Refactor JSR223 to support Compilable interface, drop daemon eval engine and sample, simplify
This commit is contained in:
+76
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.script.jsr223
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
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 java.io.File
|
||||
import java.io.OutputStream
|
||||
import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import javax.script.ScriptException
|
||||
|
||||
class KotlinJsr223JvmDaemonCompileScriptEngine(
|
||||
disposable: Disposable,
|
||||
factory: ScriptEngineFactory,
|
||||
compilerJar: File,
|
||||
templateClasspath: List<File>,
|
||||
templateClassName: String,
|
||||
getScriptArgs: (ScriptContext) -> Array<Any?>?,
|
||||
scriptArgsTypes: Array<Class<*>>?,
|
||||
compilerOut: OutputStream = System.err
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory), KotlinJsr223JvmInvocableScriptEngine {
|
||||
|
||||
private val daemon by lazy { connectToCompileService(compilerJar) }
|
||||
|
||||
override val replCompiler by lazy {
|
||||
daemon.let {
|
||||
KotlinRemoteReplCompiler(
|
||||
disposable,
|
||||
it,
|
||||
makeAutodeletingFlagFile("jsr223-repl-session"),
|
||||
CompileService.TargetPlatform.JVM,
|
||||
templateClasspath,
|
||||
templateClassName,
|
||||
compilerOut)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 val replScriptInvoker: ReplScriptInvoker get() = localEvaluator
|
||||
|
||||
override val replEvaluator: ReplCompiledEvaluator get() = localEvaluator
|
||||
}
|
||||
|
||||
|
||||
private fun connectToCompileService(compilerJar: File): CompileService {
|
||||
val compilerId = CompilerId.makeCompilerId(compilerJar)
|
||||
val daemonOptions = configureDaemonOptions()
|
||||
val daemonJVMOptions = DaemonJVMOptions()
|
||||
|
||||
val daemonReportMessages = arrayListOf<DaemonReportMessage>()
|
||||
|
||||
return 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}" })
|
||||
}
|
||||
-128
@@ -1,128 +0,0 @@
|
||||
/*
|
||||
* 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.script.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.*
|
||||
import org.jetbrains.kotlin.daemon.common.*
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import javax.script.ScriptException
|
||||
|
||||
class KotlinJsr223JvmDaemonLocalEvalScriptEngine(
|
||||
disposable: Disposable,
|
||||
factory: ScriptEngineFactory,
|
||||
compilerJar: File,
|
||||
templateClasspath: List<File>,
|
||||
templateClassName: String,
|
||||
getScriptArgs: (ScriptContext) -> Array<Any?>?,
|
||||
scriptArgsTypes: Array<Class<*>>?,
|
||||
compilerOut: OutputStream = System.err
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory), KotlinJsr223JvmInvocableScriptEngine {
|
||||
|
||||
private val daemon by lazy { connectToCompileService(compilerJar) }
|
||||
|
||||
private val replCompiler by lazy {
|
||||
daemon.let {
|
||||
KotlinRemoteReplCompiler(
|
||||
disposable,
|
||||
it,
|
||||
makeAutodeletingFlagFile("jsr223-repl-session"),
|
||||
CompileService.TargetPlatform.JVM,
|
||||
templateClasspath,
|
||||
templateClassName,
|
||||
compilerOut)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 val replScriptInvoker: ReplScriptInvoker
|
||||
get() = localEvaluator
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class KotlinJsr223JvmDaemonRemoteEvalScriptEngine(
|
||||
disposable: Disposable,
|
||||
factory: ScriptEngineFactory,
|
||||
compilerJar: File,
|
||||
templateClasspath: List<File>,
|
||||
templateClassName: String,
|
||||
getScriptArgs: (ScriptContext) -> Array<Any?>?,
|
||||
scriptArgsTypes: Array<Class<*>>?,
|
||||
compilerOutputStream: OutputStream = System.err,
|
||||
evallOutputStream: OutputStream = System.out,
|
||||
evalErrrorStream: OutputStream = System.err,
|
||||
evalInputStream: InputStream = System.`in`
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory) {
|
||||
|
||||
private val daemon by lazy { connectToCompileService(compilerJar) }
|
||||
|
||||
// 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
|
||||
private val repl by lazy {
|
||||
daemon.let {
|
||||
KotlinRemoteReplEvaluator(
|
||||
disposable,
|
||||
it,
|
||||
makeAutodeletingFlagFile("jsr223-repl-session"),
|
||||
CompileService.TargetPlatform.JVM,
|
||||
templateClasspath,
|
||||
templateClassName,
|
||||
getScriptArgs(getContext()),
|
||||
scriptArgsTypes,
|
||||
compilerOutputStream,
|
||||
evallOutputStream,
|
||||
evalErrrorStream,
|
||||
evalInputStream)
|
||||
}
|
||||
}
|
||||
|
||||
override fun eval(codeLine: ReplCodeLine, history: List<ReplCodeLine>): ReplEvalResult = repl.eval(codeLine, history)
|
||||
}
|
||||
|
||||
private fun connectToCompileService(compilerJar: File): CompileService {
|
||||
val compilerId = CompilerId.makeCompilerId(compilerJar)
|
||||
val daemonOptions = configureDaemonOptions()
|
||||
val daemonJVMOptions = DaemonJVMOptions()
|
||||
|
||||
val daemonReportMessages = arrayListOf<DaemonReportMessage>()
|
||||
|
||||
return 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}" })
|
||||
}
|
||||
+13
-59
@@ -17,13 +17,11 @@
|
||||
package org.jetbrains.kotlin.script.jsr223
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
|
||||
import org.jetbrains.kotlin.cli.common.repl.*
|
||||
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
|
||||
import org.jetbrains.kotlin.cli.jvm.repl.GenericRepl
|
||||
import org.jetbrains.kotlin.cli.jvm.repl.GenericReplCompiler
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||
@@ -33,7 +31,6 @@ import java.io.File
|
||||
import java.net.URLClassLoader
|
||||
import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import javax.script.ScriptException
|
||||
|
||||
class KotlinJsr223JvmLocalScriptEngine(
|
||||
disposable: Disposable,
|
||||
@@ -44,52 +41,19 @@ class KotlinJsr223JvmLocalScriptEngine(
|
||||
scriptArgsTypes: Array<Class<*>>?
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory), KotlinJsr223JvmInvocableScriptEngine {
|
||||
|
||||
data class MessageCollectorReport(val severity: CompilerMessageSeverity, val message: String, val location: CompilerMessageLocation)
|
||||
|
||||
private val messageCollector = object : MessageCollector {
|
||||
|
||||
private val messageRenderer = MessageRenderer.WITHOUT_PATHS
|
||||
private var hasErrors = false
|
||||
private val reports = arrayListOf<MessageCollectorReport>()
|
||||
|
||||
override fun clear() {
|
||||
reports.clear()
|
||||
}
|
||||
|
||||
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) {
|
||||
hasErrors = hasErrors or severity.isError
|
||||
reports.add(MessageCollectorReport(severity, message, location))
|
||||
}
|
||||
|
||||
override fun hasErrors(): Boolean = hasErrors
|
||||
|
||||
fun resetAndThrowOnErrors() {
|
||||
try {
|
||||
if (hasErrors) {
|
||||
val firstErr = reports.firstOrNull { it.severity.isError }
|
||||
if (firstErr != null)
|
||||
throw ScriptException(messageRenderer.render(firstErr.severity, firstErr.message, firstErr.location), firstErr.location.path, firstErr.location.line, firstErr.location.column)
|
||||
else
|
||||
throw ScriptException(reports.joinToString("\n") { messageRenderer.render(it.severity, it.message, it.location) })
|
||||
}
|
||||
}
|
||||
finally {
|
||||
clear()
|
||||
hasErrors = false
|
||||
}
|
||||
}
|
||||
override val replCompiler: ReplCompiler by lazy {
|
||||
GenericReplCompiler(
|
||||
disposable,
|
||||
makeScriptDefinition(templateClasspath, templateClassName),
|
||||
makeCompilerConfiguration(),
|
||||
PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, false))
|
||||
}
|
||||
// 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) }
|
||||
|
||||
private val repl by lazy {
|
||||
GenericRepl(
|
||||
disposable,
|
||||
makeScriptDefinition(templateClasspath, templateClassName),
|
||||
makeCompilerConfiguration(),
|
||||
messageCollector,
|
||||
Thread.currentThread().contextClassLoader,
|
||||
getScriptArgs(getContext()),
|
||||
scriptArgsTypes)
|
||||
}
|
||||
override val replScriptInvoker: ReplScriptInvoker get() = localEvaluator
|
||||
|
||||
override val replEvaluator: ReplCompiledEvaluator get() = localEvaluator
|
||||
|
||||
private fun makeScriptDefinition(templateClasspath: List<File>, templateClassName: String): KotlinScriptDefinition {
|
||||
val classloader = URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), this.javaClass.classLoader)
|
||||
@@ -102,14 +66,4 @@ class KotlinJsr223JvmLocalScriptEngine(
|
||||
addJvmClasspathRoots(templateClasspath)
|
||||
put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script")
|
||||
}
|
||||
|
||||
override val replScriptInvoker: ReplScriptInvoker
|
||||
get() = repl.scriptInvoker
|
||||
|
||||
|
||||
override fun eval(codeLine: ReplCodeLine, history: List<ReplCodeLine>): ReplEvalResult {
|
||||
val evalResult = repl.eval(codeLine, history)
|
||||
messageCollector.resetAndThrowOnErrors()
|
||||
return evalResult
|
||||
}
|
||||
}
|
||||
|
||||
+1
-15
@@ -48,7 +48,7 @@ class KotlinJsr223JvmLocalScriptEngineFactory : KotlinJsr223JvmScriptEngineFacto
|
||||
class KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
KotlinJsr223JvmDaemonLocalEvalScriptEngine(
|
||||
KotlinJsr223JvmDaemonCompileScriptEngine(
|
||||
Disposer.newDisposable(),
|
||||
this,
|
||||
kotlinCompilerJar,
|
||||
@@ -59,20 +59,6 @@ class KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory : KotlinJsr223JvmScriptE
|
||||
)
|
||||
}
|
||||
|
||||
class KotlinJsr223JvmDaemonRemoteEvalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
KotlinJsr223JvmDaemonRemoteEvalScriptEngine(
|
||||
Disposer.newDisposable(),
|
||||
this,
|
||||
kotlinCompilerJar,
|
||||
scriptCompilationClasspathFromContext(),
|
||||
"kotlin.script.templates.standard.ScriptTemplateWithBindings",
|
||||
::makeSerializableArgumentsForTemplateWithBindings,
|
||||
arrayOf(Map::class.java)
|
||||
)
|
||||
}
|
||||
|
||||
private fun makeSerializableArgumentsForTemplateWithBindings(ctx: ScriptContext): Array<Any?> {
|
||||
val bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE)
|
||||
val serializableBindings = linkedMapOf<String, Any>()
|
||||
|
||||
+16
-4
@@ -38,14 +38,13 @@ import org.jetbrains.kotlin.utils.PathUtil
|
||||
import org.jetbrains.kotlin.utils.PathUtil.getResourcePathForClass
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.PrintStream
|
||||
import java.io.*
|
||||
import java.net.URI
|
||||
import java.util.jar.Manifest
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.test.*
|
||||
|
||||
|
||||
class ScriptUtilIT {
|
||||
|
||||
companion object {
|
||||
@@ -89,7 +88,14 @@ done
|
||||
|
||||
@Test
|
||||
fun testResolveStdJUnitHelloWorld() {
|
||||
Assert.assertNull(compileScript("args-junit-hello-world.kts", StandardArgsScriptTemplateWithLocalResolving::class))
|
||||
val savedErr = System.err
|
||||
try {
|
||||
System.setErr(PrintStream(NullOutputStream()))
|
||||
Assert.assertNull(compileScript("args-junit-hello-world.kts", StandardArgsScriptTemplateWithLocalResolving::class))
|
||||
}
|
||||
finally {
|
||||
System.setErr(savedErr)
|
||||
}
|
||||
|
||||
val scriptClass = compileScript("args-junit-hello-world.kts", StandardArgsScriptTemplateWithMavenResolving::class)
|
||||
if (scriptClass == null) {
|
||||
@@ -196,3 +202,9 @@ done
|
||||
return outStream.toString()
|
||||
}
|
||||
}
|
||||
|
||||
private class NullOutputStream : OutputStream() {
|
||||
override fun write(b: Int) { }
|
||||
override fun write(b: ByteArray) { }
|
||||
override fun write(b: ByteArray, off: Int, len: Int) { }
|
||||
}
|
||||
Reference in New Issue
Block a user