I have to port a software using Rhino1.7R4 and its org.mozilla.javascript package to use the javax.script package and its ScriptEngine (Rhino in Java 6 & 7, Nashorn in Java 8).
The main problem is to stack scopes (Bindings). Using the Rhino jar, I do:
Scriptable scope ...
Scriptable newScope = javascriptContext.initStandardObjects();
newScope.setParentScope(scope);
So
- if a variable is defined without var, it's a global variable (root scope)
- if a variable is defined with var, it's a local variable (current scope)
- if a variable is accessed or modified, engine lookup in its current scope, and parent, and grand parent ... and the global scope
This is the JS standard behavior.
How can I do the same as setParentScope with javax.script API ?