From 43037373d78b6c029e0612b7427e8f48b6a4fd62 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 22 Sep 2016 11:36:19 +0200 Subject: [PATCH] Implement params to pass args to generic and remote repls to support script templates with params --- .../common/repl/GenericReplCompiledEvaluator.kt | 14 +++++++++++--- .../jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt | 1 + .../kotlin/daemon/client/KotlinRemoteReplClient.kt | 10 +++++++++- .../kotlin/daemon/common/CompileService.kt | 2 ++ .../jetbrains/kotlin/daemon/CompileServiceImpl.kt | 4 +++- .../kotlin/daemon/KotlinRemoteReplService.kt | 6 ++++-- 6 files changed, 30 insertions(+), 7 deletions(-) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompiledEvaluator.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompiledEvaluator.kt index 103abbce83b..c969b2272c6 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompiledEvaluator.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplCompiledEvaluator.kt @@ -23,7 +23,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock import kotlin.concurrent.read import kotlin.concurrent.write -open class GenericReplCompiledEvaluator(baseClasspath: Iterable, baseClassloader: ClassLoader?) : ReplCompiledEvaluator { +open class GenericReplCompiledEvaluator(baseClasspath: Iterable, baseClassloader: ClassLoader?, val scriptArgs: Array? = null, val scriptArgsTypes: Array>? = null) : ReplCompiledEvaluator { private var classLoader: org.jetbrains.kotlin.cli.common.repl.ReplClassLoader = org.jetbrains.kotlin.cli.common.repl.ReplClassLoader(URLClassLoader(baseClasspath.map { it.toURI().toURL() }.toTypedArray(), baseClassloader)) @@ -53,8 +53,16 @@ open class GenericReplCompiledEvaluator(baseClasspath: Iterable, baseClass val scriptClass = classLoaderLock.read { classLoader.loadClass("Line${codeLine.no}") } - val constructorParams = compiledLoadedClassesHistory.map { it.second.klass }.toTypedArray() - val constructorArgs = compiledLoadedClassesHistory.map { it.second.instance }.toTypedArray() + val constructorParams: Array> = + (compiledLoadedClassesHistory.map { it.second.klass } + + (scriptArgs?.asIterable() + ?.mapIndexed { i, it -> + it?.javaClass ?: if (i < (scriptArgsTypes?.size ?: 0)) scriptArgsTypes!![i] else Any::class.java + } + ?: emptyList() + ) + ).toTypedArray() + val constructorArgs: Array = (compiledLoadedClassesHistory.map { it.second.instance } + (scriptArgs?.asIterable() ?: emptyList())).toTypedArray() val scriptInstanceConstructor = scriptClass.getConstructor(*constructorParams) val scriptInstance = diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt index b89cad5ee4b..ee4b51fe545 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/GenericRepl.kt @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.idea.KotlinLanguage +import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.parsing.KotlinParserDefinition import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.script.KotlinScriptDefinition 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/KotlinRemoteReplClient.kt index c19e7ea3547..73e74ec899d 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/KotlinRemoteReplClient.kt @@ -35,7 +35,9 @@ open class KotlinRemoteReplClientBase( targetPlatform: CompileService.TargetPlatform, templateClasspath: List, templateClassName: String, - compilerMessagesOutputStream: OutputStream, + scriptArgs: Array? = null, + scriptArgsTypes: Array>? = null, + compilerMessagesOutputStream: OutputStream = System.err, evalOutputStream: OutputStream? = null, evalErrorStream: OutputStream? = null, evalInputStream: InputStream? = null, @@ -49,6 +51,8 @@ open class KotlinRemoteReplClientBase( CompilerCallbackServicesFacadeServer(port = port), templateClasspath, templateClassName, + scriptArgs, + scriptArgsTypes, RemoteOutputStreamServer(compilerMessagesOutputStream, port), evalOutputStream?.let { RemoteOutputStreamServer(it, port) }, evalErrorStream?.let { RemoteOutputStreamServer(it, port) }, @@ -104,6 +108,8 @@ class KotlinRemoteReplEvaluator( targetPlatform: CompileService.TargetPlatform, templateClasspath: List, templateClassName: String, + scriptArgs: Array?, + scriptArgsTypes: Array>?, compilerMessagesOutputStream: OutputStream, evalOutputStream: OutputStream?, evalErrorStream: OutputStream?, @@ -117,6 +123,8 @@ class KotlinRemoteReplEvaluator( targetPlatform = targetPlatform, templateClasspath = templateClasspath, templateClassName = templateClassName, + scriptArgs = scriptArgs, + scriptArgsTypes = scriptArgsTypes, compilerMessagesOutputStream = compilerMessagesOutputStream, evalOutputStream = evalOutputStream, evalErrorStream = evalErrorStream, diff --git a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt index 6da0e9fb333..810bd5b8bf9 100644 --- a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt +++ b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt @@ -132,6 +132,8 @@ interface CompileService : Remote { servicesFacade: CompilerCallbackServicesFacade, templateClasspath: List, templateClassName: String, + scriptArgs: Array?, + scriptArgsTypes: Array>?, compilerMessagesOutputStream: RemoteOutputStream, evalOutputStream: RemoteOutputStream?, evalErrorStream: RemoteOutputStream?, diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt index 0bfecc50523..03324fa9032 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt @@ -273,6 +273,8 @@ class CompileServiceImpl( servicesFacade: CompilerCallbackServicesFacade, templateClasspath: List, templateClassName: String, + scriptArgs: Array?, + scriptArgsTypes: Array>?, compilerMessagesOutputStream: RemoteOutputStream, evalOutputStream: RemoteOutputStream?, evalErrorStream: RemoteOutputStream?, @@ -283,7 +285,7 @@ class CompileServiceImpl( CompileService.CallResult.Error("Sorry, only JVM target platform is supported now") else { val disposable = Disposer.newDisposable() - val repl = KotlinJvmReplService(disposable, templateClasspath, templateClassName, compilerMessagesOutputStream, evalOutputStream, evalErrorStream, evalInputStream, operationsTracer) + val repl = KotlinJvmReplService(disposable, templateClasspath, templateClassName, scriptArgs, scriptArgsTypes, compilerMessagesOutputStream, evalOutputStream, evalErrorStream, evalInputStream, operationsTracer) val sessionId = leaseSessionImpl(ClientOrSessionProxy(aliveFlagPath, repl, disposable)) CompileService.CallResult.Good(sessionId) diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt index 7a122b55b01..98f351f9e26 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinRemoteReplService.kt @@ -42,6 +42,8 @@ open class KotlinJvmReplService( disposable: Disposable, templateClasspath: List, templateClassName: String, + scriptArgs: Array?, + scriptArgsTypes: Array>?, compilerOutputStreamProxy: RemoteOutputStream, evalOutputStream: RemoteOutputStream?, evalErrorStream: RemoteOutputStream?, @@ -115,8 +117,8 @@ open class KotlinJvmReplService( private val compiledEvaluator : GenericReplCompiledEvaluator by lazy { if (evalOutputStream == null && evalErrorStream == null && evalInputStream == null) - GenericReplCompiledEvaluator(configuration.jvmClasspathRoots, null) - else object : GenericReplCompiledEvaluator(configuration.jvmClasspathRoots, null) { + GenericReplCompiledEvaluator(configuration.jvmClasspathRoots, null, scriptArgs, scriptArgsTypes) + else object : GenericReplCompiledEvaluator(configuration.jvmClasspathRoots, null, scriptArgs, scriptArgsTypes) { val out = PrintStream(BufferedOutputStream(RemoteOutputStreamClient(evalOutputStream!!, DummyProfiler()), REMOTE_STREAM_BUFFER_SIZE)) val err = PrintStream(BufferedOutputStream(RemoteOutputStreamClient(evalErrorStream!!, DummyProfiler()), REMOTE_STREAM_BUFFER_SIZE)) val `in` = BufferedInputStream(RemoteInputStreamClient(evalInputStream!!, DummyProfiler()), REMOTE_STREAM_BUFFER_SIZE)