Fix state conversion, fix tests

This commit is contained in:
Ilya Chernikov
2017-02-06 17:18:42 +01:00
parent c5bc58ad32
commit 7b2ea001c1
12 changed files with 83 additions and 91 deletions
@@ -74,6 +74,11 @@ open class AggregatedReplStageState<T1, T2>(val state1: IReplStageState<T1>, val
{
override val history: IReplStageHistory<Pair<T1, T2>> = AggregatedReplStateHistory(state1.history, state2.history, lock)
override fun <StateT : IReplStageState<*>> asState(): StateT = (state1 as? StateT) ?: (state2 as StateT) ?: super.asState()
override fun <StateT : IReplStageState<*>> asState(target: Class<out StateT>): StateT =
when {
target.isAssignableFrom(state1::class.java) -> state1 as StateT
target.isAssignableFrom(state2::class.java) -> state2 as StateT
else -> super.asState(target)
}
}
@@ -32,7 +32,7 @@ class GenericReplCompilingEvaluator(val compiler: ReplCompiler,
override fun compileAndEval(state: IReplStageState<*>, codeLine: ReplCodeLine, scriptArgs: ScriptArgsWithTypes?, invokeWrapper: InvokeWrapper?): ReplEvalResult {
return state.lock.write {
val aggregatedState = state.asState<AggregatedReplStageState<*, *>>()
val aggregatedState = state.asState(AggregatedReplStageState::class.java)
val compiled = compiler.compile(state, codeLine)
when (compiled) {
is ReplCompileResult.Error -> ReplEvalResult.Error.CompileTime(compiled.message, compiled.location)
@@ -47,7 +47,7 @@ open class GenericReplEvaluator(val baseClasspath: Iterable<File>,
scriptArgs: ScriptArgsWithTypes?,
invokeWrapper: InvokeWrapper?): ReplEvalResult {
state.lock.write {
val evalState = state.asState<GenericReplEvaluatorState>()
val evalState = state.asState(GenericReplEvaluatorState::class.java)
// val verifyHistory = compileResult.state.dropLast(1)
// val defaultHistoryActor = HistoryActions(
// effectiveHistory = evalState.history.map { it.item },
@@ -29,10 +29,11 @@ import kotlin.reflect.full.safeCast
@Suppress("unused") // used externally (kotlin.script.utils)
interface KotlinJsr223JvmInvocableScriptEngine : Invocable {
val replScriptEvaluator: ReplEvaluatorExposedInternalHistory
val state: IReplStageState<*> // The Invokable interface do not allow Context/Bindings substitution, so state is supplied via property
private fun prioritizedHistory(receiverClass: KClass<*>?, receiverInstance: Any?): List<EvalClassWithInstanceAndLoader> {
return replScriptEvaluator.lastEvaluatedScripts.map { it.second }.filter { it.instance != null }.reversed().ensureNotEmpty("no script ").let { history ->
val evalState = state.asState(GenericReplEvaluatorState::class.java)
return evalState.history.map { it.item }.filter { it.instance != null }.reversed().ensureNotEmpty("no script ").let { history ->
if (receiverInstance != null) {
val receiverKlass = receiverClass ?: receiverInstance.javaClass.kotlin
val receiverInHistory = history.find { it.instance == receiverInstance } ?:
@@ -94,7 +95,7 @@ interface KotlinJsr223JvmInvocableScriptEngine : Invocable {
}
private fun <T : Any> proxyInterface(thiz: Any?, clasz: Class<T>?): T? {
replScriptEvaluator.lastEvaluatedScripts.ensureNotEmpty("no script")
if (state.history.size == 0) throw IllegalStateException("no script")
val priority = prioritizedHistory(thiz?.javaClass?.kotlin, thiz)
if (clasz == null) throw IllegalArgumentException("class object cannot be null")
@@ -77,7 +77,9 @@ interface IReplStageState<T> {
val lock: ReentrantReadWriteLock
fun <StateT : IReplStageState<*>> asState(): StateT = (this as? StateT) ?: throw IllegalArgumentException("$this is not an expected instance of IReplStageState")
fun <StateT : IReplStageState<*>> asState(target: Class<out StateT>): StateT =
if (target.isAssignableFrom(this::class.java)) this as StateT
else throw IllegalArgumentException("$this is not an expected instance of IReplStageState")
}