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:
+93
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.jvmhost.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
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.jvmhost.jsr223
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.*
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import javax.script.Bindings
|
||||
import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import kotlin.script.experimental.api.ScriptCompilationConfiguration
|
||||
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
|
||||
import kotlin.script.experimental.jvm.baseClassLoader
|
||||
import kotlin.script.experimental.jvm.jvm
|
||||
import kotlin.script.experimental.jvmhost.repl.JvmReplCompiler
|
||||
import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluator
|
||||
import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluatorState
|
||||
|
||||
// TODO: reimplement without legacy REPL infrastructure
|
||||
|
||||
class KotlinJsr223ScriptEngineImpl(
|
||||
factory: ScriptEngineFactory,
|
||||
val compilationConfiguration: ScriptCompilationConfiguration,
|
||||
val evaluationConfiguration: ScriptEvaluationConfiguration
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory), KotlinJsr223InvocableScriptEngine {
|
||||
|
||||
override val replCompiler: ReplCompiler by lazy {
|
||||
JvmReplCompiler(compilationConfiguration)
|
||||
}
|
||||
private val localEvaluator by lazy {
|
||||
GenericReplCompilingEvaluatorBase(replCompiler, JvmReplEvaluator(evaluationConfiguration))
|
||||
}
|
||||
|
||||
override val replEvaluator: ReplFullEvaluator get() = localEvaluator
|
||||
|
||||
val state: IReplStageState<*> get() = getCurrentState(getContext())
|
||||
|
||||
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> = replEvaluator.createState(lock)
|
||||
|
||||
override fun overrideScriptArgs(context: ScriptContext): ScriptArgsWithTypes? =
|
||||
ScriptArgsWithTypes(arrayOf(context.getBindings(ScriptContext.ENGINE_SCOPE).orEmpty()), arrayOf(Bindings::class))
|
||||
|
||||
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]!!
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user