Fix state conversion, fix tests
This commit is contained in:
+6
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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 },
|
||||
|
||||
+4
-3
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user