java - Reading updated variables after evaluating a script -
i testing jsr 223 (scripting) code works rhino see how works nashorn. 1 area of difference in handling of updates variables passed in via bindings
argument engine.eval()
. in rhino, can use method pass in dynamic variable bindings when evaluating script , read out updated values of these variables after executing script. in nashorn, however, not work - values of variables in bindings object still present initial values after executing script.
to illustrate testng test case:
@test public void shouldsupportreadingvariablesfrombindings() throws exception { // given scriptengine engine = new scriptenginemanager().getenginebyname("javascript"); bindings vars = new simplebindings(); vars.put("state", 1); // when engine.eval("state = 2", vars); number result = (number) vars.get("state"); // assertequals(result.intvalue(), 2); }
with rhino (apple jdk 1.6.0_65 on mac os x 10.9.3) test passes. nashorn (oracle jdk 1.8.0_b132) fails. i've tried variety of different approaches read variable afterwards - calling engine.get()
, engine.getbindings(scriptcontext.engine_scope).get()
, engine.getcontext().getbindings(...).get()
etc. result null
.
i can work if call engine.put("state", 1)
, read afterwards engine.get("state")
, seems bit messy compared passing in bindings argument.
reading jsr-223 spec, cannot find wording supports usage, engine-specific? there other way accomplish want in nashorn?
edit: 1 approach works wrap each variable in atomicreference
, call explicit .set(..)
methods on in javascript. ugly, possibly workable.
if pass bindings created scriptengine.createbindings, nashorn store updated value in bindings. (i tested.)
according the openjdk wiki , this openjdk bug, behavior difference between rhino , nashorn expected. nashorn not intended 100% drop-in compatible rhino.
Comments
Post a Comment