Refactor default JSR-223 engine implementation:
- rename classes for clarity - sort out dependencies - move common pars to jvm-host jar - switch to configurations only customization
This commit is contained in:
@@ -12,12 +12,14 @@ dependencies {
|
||||
compile(project(":kotlin-scripting-jvm"))
|
||||
compile(project(":kotlin-scripting-jvm-host"))
|
||||
compile(project(":kotlin-scripting-compiler"))
|
||||
compileOnly(project(":compiler:cli"))
|
||||
compileOnly(project(":compiler:cli-common"))
|
||||
compileOnly(project(":kotlin-reflect-api"))
|
||||
compileOnly(intellijCoreDep())
|
||||
runtime(project(":kotlin-compiler"))
|
||||
runtime(project(":kotlin-reflect"))
|
||||
testCompile(commonDep("junit"))
|
||||
testCompileOnly(project(":compiler:cli"))
|
||||
testCompileOnly(project(":core:util.runtime"))
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
kotlin.script.experimental.jsr223.KotlinJsr223JvmScriptEngineFactory
|
||||
kotlin.script.experimental.jsr223.KotlinJsr223DefaultScriptEngineFactory
|
||||
|
||||
|
||||
+1
-2
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.cli.common.repl.KOTLIN_SCRIPT_STATE_BINDINGS_KEY
|
||||
import javax.script.Bindings
|
||||
import javax.script.ScriptEngine
|
||||
import kotlin.script.experimental.annotations.KotlinScript
|
||||
import kotlin.script.templates.ScriptTemplateDefinition
|
||||
import kotlin.script.templates.standard.ScriptTemplateWithBindings
|
||||
|
||||
@Suppress("unused")
|
||||
@@ -47,4 +46,4 @@ abstract class KotlinJsr223DefaultScript(val jsr223Bindings: Bindings) : ScriptT
|
||||
}
|
||||
|
||||
fun createBindings(): Bindings = withMyEngine { it.createBindings() }
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.jsr223
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase
|
||||
import javax.script.ScriptEngine
|
||||
import kotlin.script.experimental.api.ScriptCompilationConfiguration
|
||||
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
|
||||
import kotlin.script.experimental.jvm.jvm
|
||||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
|
||||
import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate
|
||||
import kotlin.script.experimental.jvmhost.jsr223.KotlinJsr223ScriptEngineImpl
|
||||
|
||||
class KotlinJsr223DefaultScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
private val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<KotlinJsr223DefaultScript>()
|
||||
private val evaluationConfiguration = createJvmEvaluationConfigurationFromTemplate<KotlinJsr223DefaultScript>()
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
KotlinJsr223ScriptEngineImpl(
|
||||
this,
|
||||
ScriptCompilationConfiguration(compilationConfiguration) {
|
||||
jvm {
|
||||
dependenciesFromCurrentContext(wholeClasspath = true)
|
||||
}
|
||||
},
|
||||
evaluationConfiguration
|
||||
)
|
||||
}
|
||||
|
||||
-93
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.jsr223
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.InvokeWrapper
|
||||
import org.jetbrains.kotlin.cli.common.repl.renderReplStackTrace
|
||||
import org.jetbrains.kotlin.utils.tryCreateCallableMapping
|
||||
import java.lang.reflect.Proxy
|
||||
import javax.script.Invocable
|
||||
import javax.script.ScriptException
|
||||
import kotlin.reflect.KFunction
|
||||
import kotlin.reflect.KParameter
|
||||
import kotlin.reflect.full.functions
|
||||
import kotlin.reflect.full.safeCast
|
||||
|
||||
interface KotlinJsr223InvocableScriptEngine : Invocable {
|
||||
|
||||
val invokeWrapper: InvokeWrapper?
|
||||
|
||||
val backwardInstancesHistory: Sequence<Any>
|
||||
|
||||
val baseClassLoader: ClassLoader
|
||||
|
||||
fun instancesForInvokeSearch(requestedReceiver: Any) =
|
||||
sequenceOf(requestedReceiver) + backwardInstancesHistory.filterNot { it == requestedReceiver }
|
||||
|
||||
override fun invokeFunction(name: String?, vararg args: Any?): Any? {
|
||||
if (name == null) throw java.lang.NullPointerException("function name cannot be null")
|
||||
return invokeImpl(backwardInstancesHistory, name, args)
|
||||
}
|
||||
|
||||
override fun invokeMethod(thiz: Any?, name: String?, vararg args: Any?): Any? {
|
||||
if (name == null) throw java.lang.NullPointerException("method name cannot be null")
|
||||
if (thiz == null) throw IllegalArgumentException("cannot invoke method on the null object")
|
||||
return invokeImpl(instancesForInvokeSearch(thiz), name, args)
|
||||
}
|
||||
|
||||
private fun invokeImpl(possibleReceivers: Sequence<Any>, name: String, args: Array<out Any?>): Any? {
|
||||
// TODO: cache the method lookups?
|
||||
|
||||
val (fn, mapping) = possibleReceivers.mapNotNull { instance ->
|
||||
val candidates = instance::class.functions.filter { it.name == name }
|
||||
candidates.findMapping(listOf(instance) + args)
|
||||
}.filterNotNull().firstOrNull() ?: throw NoSuchMethodException("no suitable function '$name' found")
|
||||
|
||||
val res = try {
|
||||
if (invokeWrapper != null) {
|
||||
invokeWrapper!!.invoke {
|
||||
fn.callBy(mapping)
|
||||
}
|
||||
} else {
|
||||
fn.callBy(mapping)
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
// ignore everything in the stack trace until this constructor call
|
||||
throw ScriptException(renderReplStackTrace(e.cause!!, startFromMethodName = fn.name))
|
||||
}
|
||||
return if (fn.returnType.classifier == Unit::class) Unit else res
|
||||
}
|
||||
|
||||
|
||||
override fun <T : Any> getInterface(clasz: Class<T>?): T? {
|
||||
return proxyInterface(backwardInstancesHistory, clasz)
|
||||
}
|
||||
|
||||
override fun <T : Any> getInterface(thiz: Any?, clasz: Class<T>?): T? {
|
||||
if (thiz == null) throw IllegalArgumentException("object cannot be null")
|
||||
return proxyInterface(instancesForInvokeSearch(thiz), clasz)
|
||||
}
|
||||
|
||||
private fun <T : Any> proxyInterface(possibleReceivers: Sequence<Any>, clasz: Class<T>?): T? {
|
||||
if (clasz == null) throw IllegalArgumentException("class object cannot be null")
|
||||
if (!clasz.isInterface) throw IllegalArgumentException("expecting interface")
|
||||
|
||||
// TODO: cache the method lookups?
|
||||
|
||||
val proxy = Proxy.newProxyInstance(baseClassLoader, arrayOf(clasz)) { _, method, args ->
|
||||
invokeImpl(possibleReceivers, method.name, args ?: emptyArray())
|
||||
}
|
||||
return clasz.kotlin.safeCast(proxy)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Iterable<KFunction<*>>.findMapping(args: List<Any?>): Pair<KFunction<*>, Map<KParameter, Any?>>? {
|
||||
for (fn in this) {
|
||||
val mapping = tryCreateCallableMapping(fn, args)
|
||||
if (mapping != null) return fn to mapping
|
||||
}
|
||||
return null
|
||||
}
|
||||
-61
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.jsr223
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.*
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
|
||||
import kotlin.script.experimental.jvm.baseClassLoader
|
||||
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
|
||||
import kotlin.script.experimental.jvm.jvm
|
||||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
|
||||
import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate
|
||||
import kotlin.script.experimental.jvmhost.repl.JvmReplCompiler
|
||||
import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluator
|
||||
import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluatorState
|
||||
|
||||
class KotlinJsr223ScriptEngine(
|
||||
factory: ScriptEngineFactory,
|
||||
val getScriptArgs: (ScriptContext, Array<out KClass<out Any>>?) -> ScriptArgsWithTypes?,
|
||||
val scriptArgsTypes: Array<out KClass<out Any>>?
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory), KotlinJsr223InvocableScriptEngine {
|
||||
|
||||
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<KotlinJsr223DefaultScript> {
|
||||
jvm {
|
||||
dependenciesFromCurrentContext(wholeClasspath = true)
|
||||
}
|
||||
}
|
||||
|
||||
val evaluationConfiguration = createJvmEvaluationConfigurationFromTemplate<KotlinJsr223DefaultScript>()
|
||||
|
||||
override val replCompiler: ReplCompiler by lazy {
|
||||
JvmReplCompiler(compilationConfiguration)
|
||||
}
|
||||
private val localEvaluator by lazy {
|
||||
GenericReplCompilingEvaluatorBase(replCompiler, JvmReplEvaluator(evaluationConfiguration))
|
||||
}
|
||||
|
||||
override val replEvaluator: ReplFullEvaluator get() = localEvaluator
|
||||
|
||||
internal val state: IReplStageState<*> get() = getCurrentState(getContext())
|
||||
|
||||
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> = replEvaluator.createState(lock)
|
||||
|
||||
override fun overrideScriptArgs(context: ScriptContext): ScriptArgsWithTypes? = getScriptArgs(context, scriptArgsTypes)
|
||||
|
||||
override val invokeWrapper: InvokeWrapper?
|
||||
get() = null
|
||||
|
||||
override val backwardInstancesHistory: Sequence<Any>
|
||||
get() = getCurrentState(getContext()).asState(JvmReplEvaluatorState::class.java).history.asReversed().asSequence().map { it.item }
|
||||
|
||||
override val baseClassLoader: ClassLoader
|
||||
get() = evaluationConfiguration[ScriptEvaluationConfiguration.jvm.baseClassLoader]!!
|
||||
}
|
||||
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
// could be used externally in javax.script.ScriptEngineFactory META-INF file
|
||||
|
||||
package kotlin.script.experimental.jsr223
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineFactoryBase
|
||||
import org.jetbrains.kotlin.cli.common.repl.ScriptArgsWithTypes
|
||||
import javax.script.Bindings
|
||||
import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngine
|
||||
|
||||
class KotlinJsr223JvmScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
KotlinJsr223ScriptEngine(
|
||||
this,
|
||||
{ ctx, types -> ScriptArgsWithTypes(arrayOf(ctx.getBindings(ScriptContext.ENGINE_SCOPE)), types ?: emptyArray()) },
|
||||
arrayOf(Bindings::class)
|
||||
)
|
||||
}
|
||||
|
||||
+5
-5
@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.config.KotlinCompilerVersion
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import javax.script.*
|
||||
import kotlin.script.experimental.jsr223.KotlinJsr223ScriptEngine
|
||||
import kotlin.script.experimental.jvmhost.jsr223.KotlinJsr223ScriptEngineImpl
|
||||
|
||||
class KotlinJsr223ScriptEngineIT {
|
||||
|
||||
@@ -44,7 +44,7 @@ class KotlinJsr223ScriptEngineIT {
|
||||
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
|
||||
Assert.assertNotNull(factory)
|
||||
val engine = factory!!.scriptEngine
|
||||
Assert.assertNotNull(engine as? KotlinJsr223ScriptEngine)
|
||||
Assert.assertNotNull(engine as? KotlinJsr223ScriptEngineImpl)
|
||||
Assert.assertSame(factory, engine!!.factory)
|
||||
val bindings = engine.createBindings()
|
||||
Assert.assertTrue(bindings is SimpleBindings)
|
||||
@@ -89,7 +89,7 @@ class KotlinJsr223ScriptEngineIT {
|
||||
fun testEngineRepeatWithReset() {
|
||||
val code = "open class A {}\n" +
|
||||
"class B : A() {}"
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223ScriptEngine
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223ScriptEngineImpl
|
||||
|
||||
val res1 = engine.eval(code)
|
||||
Assert.assertNull(res1)
|
||||
@@ -127,7 +127,7 @@ obj
|
||||
|
||||
@Test
|
||||
fun testSimpleCompilable() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223ScriptEngine
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223ScriptEngineImpl
|
||||
val comp1 = engine.compile("val x = 3")
|
||||
val comp2 = engine.compile("x + 2")
|
||||
val res1 = comp1.eval()
|
||||
@@ -138,7 +138,7 @@ obj
|
||||
|
||||
@Test
|
||||
fun testMultipleCompilable() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223ScriptEngine
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223ScriptEngineImpl
|
||||
val compiled1 = engine.compile("""listOf(1,2,3).joinToString(",")""")
|
||||
val compiled2 = engine.compile("""val x = bindings["boundValue"] as Int + bindings["z"] as Int""")
|
||||
val compiled3 = engine.compile("""x""")
|
||||
|
||||
Reference in New Issue
Block a user