Kotlin / kotlin-script-examples

Examples of Kotlin Scripts and usages of the Kotlin Scripting API
Apache License 2.0
295 stars 42 forks source link

Can't bind to engine #4

Open npedot opened 3 years ago

npedot commented 3 years ago

import org.junit.Test import javax.script.ScriptEngineManager import kotlin.test.assertEquals

class ScriptTest {

@Test
fun testSimpleEval() {
    val engine = ScriptEngineManager().getEngineByExtension("kts")!!
    val res = engine.eval("2+4")
    assertEquals(6,res)
}

@Test
fun testSimpleBinding() {
    val engine = ScriptEngineManager().getEngineByExtension("kts")!!
    engine.put("hello","world")
    val res = engine.eval("hello")
    assertEquals("world",res)
}

}

I've got

javax.script.ScriptException: Unresolved reference: hello at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.compileAndEval(KotlinJsr223JvmScriptEngineBase.kt:65) at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:31)

ligee commented 3 years ago

What version of the Kotlin libs you're yousing? Have you tried the examples and tests from this repo 1:1? There is a test - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/jsr223/jsr223-simple/src/test/kotlin/org/jetbrains/kotlin/script/examples/jvm/jsr223/simple/test/simpleJsr223Test.kt#L29 - with exactly this functionality here, and it works for me.