"nashorn" val engine = new ScriptEngineManager() .getEngineByName(ENGINE_NAME) .asInstanceOf[ScriptEngine with Invocable] val fName = "func" val f = s"function $fName(a, b) { return a + b; };" engine.asInstanceOf[Compilable].compile(f).eval() val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] } val result = engine.invokeFunction(fName, argments:_*) println(s"result => $result") }
engine = new ScriptEngineManager() .getEngineByName(ENGINE_NAME) .asInstanceOf[ScriptEngine with Invocable] val fName = "func" val f = s"function $fName(a, b) { return a + b; };" engine.asInstanceOf[Compilable].compile(f).eval() val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] } val result = engine.invokeFunction(fName, argments:_*) println(s"result => $result") } Preparation Example https://goo.gl/1KrXoo
engine = new ScriptEngineManager() .getEngineByName(ENGINE_NAME) .asInstanceOf[ScriptEngine with Invocable] val fName = "func" val f = s"function $fName(a, b) { return a + b; };" engine.asInstanceOf[Compilable].compile(f).eval() val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] } val result = engine.invokeFunction(fName, argments:_*) println(s"result => $result") } Compilation Example https://goo.gl/1KrXoo
engine = new ScriptEngineManager() .getEngineByName(ENGINE_NAME) .asInstanceOf[ScriptEngine with Invocable] val fName = "func" val f = s"function $fName(a, b) { return a + b; };" engine.asInstanceOf[Compilable].compile(f).eval() val argments = Seq(1, 2) map { _.asInstanceOf[AnyRef] } val result = engine.invokeFunction(fName, argments:_*) println(s"result => $result") } Execution Example https://goo.gl/1KrXoo
in JavaScript • Java.Type(“<package.className>”) • https://docs.oracle.com/javase/jp/8/docs/technotes/guides/scripting/nashorn/api.html • Enable casting a result of execution • asInstanceOf[T] • https://gist.github.com/petitviolet/4c446066da25c150a0eb50b39b4522d3