我一直在遇到Kotlins JSR-223实现ScriptEngine
的奇怪问题(kotlin-scripting-jsr223)
基本上,我有一个条件,我解析并编译成一个CompiledScript:
fun parse(clazz: KClass<out GenericEvent>, condition: String): String {
val simpleName = clazz.simpleName
return """
import ${clazz.java.name}
val condition: $simpleName.() -> Boolean = { ${trimQuotes(condition)} }
fun check(event: $simpleName) = condition.invoke(event)
""".trimIndent()
}
然后我使用(engine as Compilable).compile(code)
将结果字符串编译成CompiledScript
。
现在,每当我尝试在很短的时间内多次eval()
这些CompiledScript
时:
listener.compiledScript.eval()
return@Callable (engine as Invocable).invokeFunction("check", event)
似乎重用了以前使用过的旧参数。首先,我认为这是一个线程安全问题(因为它看起来很像一个),但是使用互斥锁或单线程执行器来编译脚本并没有改变这种行为。
完整代码:
override suspend fun onEvent(e: GenericEvent) {
val events = listeners[e.javaClass] ?: return
events.forEach { listener ->
try {
val event = e.javaClass.cast(e)
if (listener.compiledScript != null) {
val result = executor.submit(Callable { // executor = Executors.newSingleThreadExecutor()
println(Thread.currentThread().id) // confirms it's always the same thread
listener.compiledScript.eval()
return@Callable (engine as Invocable).invokeFunction("check", event)
}).get()
if (result is Boolean && result)
listener.method.callSuspend(listener.instance, event)
} else {
listener.method.callSuspend(listener.instance, event)
}
} catch (throwable: Throwable) {
log.error("An error occurred while evaluating listener condition!", throwable)
}
}
}
一开始,这是预期的
Condition: message.contentRaw == "first"
Is actually: first
,但它创建了奇怪的,但总是相同的模式。
Condition: message.contentRaw == "first"
Is actually: second
Condition: message.contentRaw == "second"
Is actually: second
Condition: message.contentRaw == "first"
Is actually: second
Condition: message.contentRaw == "second"
Is actually: second
Condition: message.contentRaw == "first"
Is actually: second
Condition: message.contentRaw == "second"
Is actually: second
(如果条件不为真,则不应打印这些消息)
我之前做了一些调试,我在编译的代码中打印了条件和实际事件,它确实重用了check()中前面的事件参数,尽管我实际上使用了一个完全不同的参数。
通过创建新的SimpleScriptContext
,将其传递给CompiledScript
的eval()
方法,然后在调用invokeFunction()
之前使用新创建的上下文调用ScriptEngine#setContext
来修复它
疯狂的东西!