Java Reference
In-Depth Information
public class SharedGlobals {
public static void main(String[] args) {
// Get the Nashorn script engine using the
// --global-per_engine option
NashornScriptEngineFactory factory = new
NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine("--global-per-
engine");
try {
// Add a variable named msg to the global scope of
// the script
engine.eval("var msg = 'Hello globals'");
// Print the value of the msg value
engine.eval("print(this.msg);");
// Execute the same script, but using a new
// ScriptContext. Note that the script globals
// are shared and this script will find the
// this.msg variable created by the first
// script execution.
ScriptContext ctx = new SimpleScriptContext();
engine.eval("print(this.msg);", ctx);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Hello globals
Hello globals
Using Script Objects in Java Code
When objects and values from Nashorn cross the script-Java boundary to enter Java code,
they need to be represented as Java objects and values. Table 12-2 lists the mapping of
classes between script objects and their corresponding Java objects.
 
 
Search WWH ::




Custom Search