我正在尝试将值传递给JSR223引擎上的kotlin脚本,但是没有获取绑定。
class Script<C : ScriptContext>(val scriptClass : KClass<C>, val script : String?) {
fun execute(context : C) {
if (script != null) {
System.setProperty("idea.use.native.fs.for.win", "false")
val scriptWithContext = """
import ${scriptClass.java.canonicalName}
println(ctx)
""".trimIndent()
with(ScriptEngineManager().getEngineByExtension("kts")) {
val c = SimpleScriptContext()
c.setAttribute("ctx", context, javax.script.ScriptContext.ENGINE_SCOPE)
eval(scriptWithContext, c)
}
}
}
}
它引发异常:
Exception in thread "main" javax.script.ScriptException: error: unresolved reference: ctx
println(ctx)
^
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmScriptEngineBase.kt:65)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:31)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
...
我想念什么?