- Loading...
Nashorn implements javax.script [http://docs.oracle.com/javase/7/docs/api/javax/script/package-frame.html] API. This page has specific info regarding nashorn script engine as well as nashorn specific scripting extensions under jdk.nashorn.api.scripting package.
Basic script engine usage document is [http://download.java.net/jdk8/docs/technotes/guides/scripting/nashorn/]
In this page, whereever "context" is mentioned it denotes javax.script.ScriptContext instance being used by script engine to evaluate scripts. "engine" represents a javax.script.ScriptEngine (a nashorn engine instance).
Nashorn script engine allows customization options via a System property called "nashorn.args". By default, nashorn script engine sets -scripting and -doe options. If you want to override, you can specify -Dnashorn.args=<nashorn options> in your Java command line. You can check out the list of options available by using the nashorn command line shell tool "jjs" which is available under $JDK_HOME/bin directory.
A ScriptContext contains one or more Bindings each associated each jsr223 "scope". By default there are two scopes, namely ENGINE_SCOPE and GLOBAL_SCOPE. When nashorn engine is created it creates a default context. The default context's ENGINE_SCOPE is wrapped instance of ECMAScript "global" scope - which is the "this" in top level expressions. So, you can access ECMAScript top-level objects like "Object", "Math", "RegExp", "undefined" from this scope object. GLOBAL_SCOPE is a javax.script.SimpleBindings instance.This the user can fill with name, value pairs from the java code.
Bindings b = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
System.out.println(b.get("Object")); // gets ECMAScript "Object" constructor
System.out.println(b.get("undefined")); // ECMAScript 'undefined' value