|
| 1 | +package com.neva.slinkt.script.engine |
| 2 | + |
| 3 | +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys |
| 4 | +import org.jetbrains.kotlin.cli.common.messages.MessageRenderer |
| 5 | +import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector |
| 6 | +import org.jetbrains.kotlin.cli.common.repl.* |
| 7 | +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots |
| 8 | +import org.jetbrains.kotlin.cli.jvm.config.addJvmSdkRoots |
| 9 | +import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar |
| 10 | +import org.jetbrains.kotlin.config.* |
| 11 | +import org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar |
| 12 | +import org.jetbrains.kotlin.scripting.compiler.plugin.repl.GenericReplCompiler |
| 13 | +import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinition |
| 14 | +import org.jetbrains.kotlin.scripting.resolve.KotlinScriptDefinitionFromAnnotatedTemplate |
| 15 | +import org.jetbrains.kotlin.utils.PathUtil |
| 16 | +import java.io.File |
| 17 | +import java.net.URLClassLoader |
| 18 | +import java.util.concurrent.locks.ReentrantReadWriteLock |
| 19 | +import javax.script.ScriptContext |
| 20 | +import javax.script.ScriptEngineFactory |
| 21 | +import kotlin.reflect.KClass |
| 22 | + |
| 23 | +class KotlinEngine( |
| 24 | + val classLoader: ClassLoader, |
| 25 | + factory: ScriptEngineFactory, |
| 26 | + val templateClasspath: List<File>, |
| 27 | + templateClassName: String, |
| 28 | + val getScriptArgs: (ScriptContext, Array<out KClass<out Any>>?) -> ScriptArgsWithTypes?, |
| 29 | + val scriptArgsTypes: Array<out KClass<out Any>>? |
| 30 | +) : KotlinJsr223JvmScriptEngineBase(factory), KotlinJsr223JvmInvocableScriptEngine { |
| 31 | + |
| 32 | + override val replCompiler: ReplCompiler by lazy { |
| 33 | + GenericReplCompiler( |
| 34 | + makeScriptDefinition(templateClasspath, templateClassName), |
| 35 | + makeCompilerConfiguration(), |
| 36 | + PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, false)) |
| 37 | + } |
| 38 | + // 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 |
| 39 | + private val localEvaluator by lazy { GenericReplCompilingEvaluator(replCompiler, templateClasspath, classLoader, getScriptArgs(getContext(), scriptArgsTypes)) } |
| 40 | + |
| 41 | + override val replEvaluator: ReplFullEvaluator get() = localEvaluator |
| 42 | + |
| 43 | + override val state: IReplStageState<*> get() = getCurrentState(getContext()) |
| 44 | + |
| 45 | + override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> = replEvaluator.createState(lock) |
| 46 | + |
| 47 | + override fun overrideScriptArgs(context: ScriptContext): ScriptArgsWithTypes? = getScriptArgs(context, scriptArgsTypes) |
| 48 | + |
| 49 | + private fun makeScriptDefinition(templateClasspath: List<File>, templateClassName: String): KotlinScriptDefinition { |
| 50 | + val classloader = URLClassLoader(templateClasspath.map { it.toURI().toURL() }.toTypedArray(), this.javaClass.classLoader) |
| 51 | + val cls = classloader.loadClass(templateClassName) |
| 52 | + return KotlinScriptDefinitionFromAnnotatedTemplate(cls.kotlin, emptyMap()) |
| 53 | + } |
| 54 | + |
| 55 | + private fun makeCompilerConfiguration() = CompilerConfiguration().apply { |
| 56 | + addJvmSdkRoots(PathUtil.getJdkClassesRootsFromCurrentJre()) |
| 57 | + classLoader |
| 58 | + addJvmClasspathRoots(templateClasspath) |
| 59 | + put(CLIConfigurationKeys.INTELLIJ_PLUGIN_ROOT, System.getProperty("user.dir")) //TODO tmp |
| 60 | + add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, ScriptingCompilerConfigurationComponentRegistrar()) |
| 61 | + put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script") |
| 62 | + languageVersionSettings = LanguageVersionSettingsImpl( |
| 63 | + LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE, mapOf(AnalysisFlags.skipMetadataVersionCheck to true) |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
0 commit comments