How can I get the results of executed javascript code from groovy? I tried the following, but I always get back the string "world". I would have expected an object or map.
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
def manager = new ScriptEngineManager()
manager.getEngineByName("JavaScript").eval("""
{hello: name}
""", [name:'world'] as SimpleBindings)
Easy!
You could map the object to a variable, and return that...
import javax.script.*
def bindings = [name:'world']
def response = new ScriptEngineManager()
.getEngineByName('javascript')
.eval("var r = {hello:name}; r;", bindings as SimpleBindings)
println response.hello // -> world
Or you could keep track of a response
Map object, and update that...
import javax.script.*
def bindings = [name:'world',response:[:]]
new ScriptEngineManager()
.getEngineByName('javascript')
.eval("var r = {hello:name}; response.data = r;", bindings as SimpleBindings)
println bindings.response.data.hello // -> world
Groovy version: 2.4.5
Java version: 1.8.0_60