Kotlin JSR223 Thread safe and createBinding issue


#1

I am trying to use Kotlin JSR 223 in multi-threaded environment. My assumption is same CompiledScript instance can be used from multiple threads at same time and create new binding when invoking compiledscript.eval function.

Below code throws exception:

CompiledScript compileScript1 = ((KotlinJsr223JvmLocalScriptEngine) scriptEngine).compile(“bindings[“x”] as Int + 5”);
Bindings bindings1 = ((KotlinJsr223JvmLocalScriptEngine) scriptEngine).createBindings();
bindings1.put(“x”, 3);
System.out.println("Eval result : " + compileScript1.eval(bindings1));

CompiledScript compileScript2 = ((KotlinJsr223JvmLocalScriptEngine) scriptEngine).compile(“bindings[“y”] as Int + 5”);
Bindings bindings2 = ((KotlinJsr223JvmLocalScriptEngine) scriptEngine).createBindings();
bindings2.put(“x”, 8);
bindings2.put(“y”, 5);
System.out.println("Eval result : " + compileScript2.eval(bindings2)); // Throw happens here …

throws

`Exception in thread "main" javax.script.ScriptException: java.lang.NoSuchMethodException: Line_2.<init>(javax.script.Bindings)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:90)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase$CompiledKotlinScript.eval(KotlinJsr223JvmScriptEngineBase.kt:103)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
at com.opusconsulting.sandbox.script.kotlin.ReportError.main(ReportError.java:46)`

Caused by: java.lang.NoSuchMethodException: Line_2.(javax.script.Bindings)
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at org.jetbrains.kotlin.cli.common.repl.GenericReplEvaluator.eval(GenericReplEvaluator.kt:88)
at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluator.eval(GenericReplCompilingEvaluator.kt:78)
at org.jetbrains.kotlin.cli.common.repl.ReplEvalAction$DefaultImpls.eval$default(ReplApi.kt:120)
at org.jetbrains.kotlin.cli.common.repl.KotlinJsr223JvmScriptEngineBase.eval(KotlinJsr223JvmScriptEngineBase.kt:87)
… 3 more`

Note: If i use same binding instance for both eval, it works fine.


#2

@saravanan77 - I am stuck with the same issue. were you able to solve it?