- java.lang.Object
-
- javax.script.AbstractScriptEngine
-
- jdk.nashorn.api.scripting.NashornScriptEngine
-
- All Implemented Interfaces:
Compilable
,Invocable
,ScriptEngine
public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable
JSR-223 compliant script engine for Nashorn. Instances are not created directly, but rather returned throughNashornScriptEngineFactory.getScriptEngine()
. Note that this engine implements theCompilable
andInvocable
interfaces, allowing for efficient precompilation and repeated execution of scripts.- Since:
- 1.8u40
- See Also:
NashornScriptEngineFactory
-
-
Field Summary
Fields Modifier and Type Field Description static String
NASHORN_GLOBAL
Key used to associate Nashorn global object mirror with arbitrary Bindings instance.-
Fields inherited from class javax.script.AbstractScriptEngine
context
-
Fields inherited from interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompiledScript
compile(Reader reader)
Compiles the script (source read fromReader
) for later execution.CompiledScript
compile(String str)
Compiles the script (source represented as aString
) for later execution.Bindings
createBindings()
Returns an uninitializedBindings
.Object
eval(Reader reader, ScriptContext ctxt)
Same aseval(String, ScriptContext)
where the source of the script is read from aReader
.Object
eval(String script, ScriptContext ctxt)
Causes the immediate execution of the script whose source is the String passed as the first argument.ScriptEngineFactory
getFactory()
Returns aScriptEngineFactory
for the class to which thisScriptEngine
belongs.<T> T
getInterface(Class<T> clazz)
Returns an implementation of an interface using functions compiled in the interpreter.<T> T
getInterface(Object thiz, Class<T> clazz)
Returns an implementation of an interface using member functions of a scripting object compiled in the interpreter.Object
invokeFunction(String name, Object... args)
Used to call top-level procedures and functions defined in scripts.Object
invokeMethod(Object thiz, String name, Object... args)
Calls a method on a script object compiled during a previous script execution, which is retained in the state of theScriptEngine
.-
Methods inherited from class javax.script.AbstractScriptEngine
eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext
-
-
-
-
Field Detail
-
NASHORN_GLOBAL
public static final String NASHORN_GLOBAL
Key used to associate Nashorn global object mirror with arbitrary Bindings instance.- See Also:
- Constant Field Values
-
-
Method Detail
-
eval
public Object eval(Reader reader, ScriptContext ctxt) throws ScriptException
Description copied from interface:ScriptEngine
Same aseval(String, ScriptContext)
where the source of the script is read from aReader
.- Specified by:
eval
in interfaceScriptEngine
- Parameters:
reader
- The source of the script to be executed by the script engine.ctxt
- TheScriptContext
passed to the script engine.- Returns:
- The value returned from the execution of the script.
- Throws:
ScriptException
- if an error occurs in script. ScriptEngines should create and throwScriptException
wrappers for checked Exceptions thrown by underlying scripting implementations.
-
eval
public Object eval(String script, ScriptContext ctxt) throws ScriptException
Description copied from interface:ScriptEngine
Causes the immediate execution of the script whose source is the String passed as the first argument. The script may be reparsed or recompiled before execution. State left in the engine from previous executions, including variable values and compiled procedures may be visible during this execution.- Specified by:
eval
in interfaceScriptEngine
- Parameters:
script
- The script to be executed by the script engine.ctxt
- AScriptContext
exposing sets of attributes in different scopes. The meanings of the scopesScriptContext.GLOBAL_SCOPE
, andScriptContext.ENGINE_SCOPE
are defined in the specification.
TheENGINE_SCOPE
Bindings
of theScriptContext
contains the bindings of scripting variables to application objects to be used during this script execution.- Returns:
- The value returned from the execution of the script.
- Throws:
ScriptException
- if an error occurs in script. ScriptEngines should create and throwScriptException
wrappers for checked Exceptions thrown by underlying scripting implementations.
-
getFactory
public ScriptEngineFactory getFactory()
Description copied from interface:ScriptEngine
Returns aScriptEngineFactory
for the class to which thisScriptEngine
belongs.- Specified by:
getFactory
in interfaceScriptEngine
- Returns:
- The
ScriptEngineFactory
-
createBindings
public Bindings createBindings()
Description copied from interface:ScriptEngine
Returns an uninitializedBindings
.- Specified by:
createBindings
in interfaceScriptEngine
- Returns:
- A
Bindings
that can be used to replace the state of thisScriptEngine
.
-
compile
public CompiledScript compile(Reader reader) throws ScriptException
Description copied from interface:Compilable
Compiles the script (source read fromReader
) for later execution. Functionality is identical tocompile(String)
other than the way in which the source is passed.- Specified by:
compile
in interfaceCompilable
- Parameters:
reader
- The reader from which the script source is obtained.- Returns:
- An instance of a subclass of
CompiledScript
to be executed later using one of itseval
methods ofCompiledScript
. - Throws:
ScriptException
- if compilation fails.
-
compile
public CompiledScript compile(String str) throws ScriptException
Description copied from interface:Compilable
Compiles the script (source represented as aString
) for later execution.- Specified by:
compile
in interfaceCompilable
- Parameters:
str
- The source of the script, represented as aString
.- Returns:
- An instance of a subclass of
CompiledScript
to be executed later using one of theeval
methods ofCompiledScript
. - Throws:
ScriptException
- if compilation fails.
-
invokeFunction
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException
Description copied from interface:Invocable
Used to call top-level procedures and functions defined in scripts.- Specified by:
invokeFunction
in interfaceInvocable
- Parameters:
name
- of the procedure or function to callargs
- Arguments to pass to the procedure or function- Returns:
- The value returned by the procedure or function
- Throws:
ScriptException
- if an error occurs during invocation of the method.NoSuchMethodException
- if method with given name or matching argument types cannot be found.
-
invokeMethod
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException
Description copied from interface:Invocable
Calls a method on a script object compiled during a previous script execution, which is retained in the state of theScriptEngine
.- Specified by:
invokeMethod
in interfaceInvocable
- Parameters:
thiz
- If the procedure is a member of a class defined in the script and thiz is an instance of that class returned by a previous execution or invocation, the named method is called through that instance.name
- The name of the procedure to be called.args
- Arguments to pass to the procedure. The rules for converting the arguments to scripting variables are implementation-specific.- Returns:
- The value returned by the procedure. The rules for converting the scripting variable returned by the script method to a Java Object are implementation-specific.
- Throws:
ScriptException
- if an error occurs during invocation of the method.NoSuchMethodException
- if method with given name or matching argument types cannot be found.
-
getInterface
public <T> T getInterface(Class<T> clazz)
Description copied from interface:Invocable
Returns an implementation of an interface using functions compiled in the interpreter. The methods of the interface may be implemented using theinvokeFunction
method.- Specified by:
getInterface
in interfaceInvocable
- Type Parameters:
T
- the type of the interface to return- Parameters:
clazz
- TheClass
object of the interface to return.- Returns:
- An instance of requested interface - null if the requested interface is unavailable,
i. e. if compiled functions in the
ScriptEngine
cannot be found matching the ones in the requested interface.
-
getInterface
public <T> T getInterface(Object thiz, Class<T> clazz)
Description copied from interface:Invocable
Returns an implementation of an interface using member functions of a scripting object compiled in the interpreter. The methods of the interface may be implemented using theinvokeMethod
method.- Specified by:
getInterface
in interfaceInvocable
- Type Parameters:
T
- the type of the interface to return- Parameters:
thiz
- The scripting object whose member functions are used to implement the methods of the interface.clazz
- TheClass
object of the interface to return.- Returns:
- An instance of requested interface - null if the requested interface is unavailable,
i. e. if compiled methods in the
ScriptEngine
cannot be found matching the ones in the requested interface.
-
-