public final class NashornScriptEngine extends AbstractScriptEngine implements Compilable, Invocable
NashornScriptEngineFactory.getScriptEngine()
. Note that this engine implements the Compilable
and
Invocable
interfaces, allowing for efficient precompilation and repeated execution of scripts.NashornScriptEngineFactory
Modifier and Type | Field and Description |
---|---|
static String |
NASHORN_GLOBAL
Key used to associate Nashorn global object mirror with arbitrary Bindings instance.
|
context
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
Modifier and Type | Method and Description |
---|---|
CompiledScript |
compile(Reader reader)
Compiles the script (source read from
Reader ) for
later execution. |
CompiledScript |
compile(String str)
Compiles the script (source represented as a
String ) for
later execution. |
Bindings |
createBindings()
Returns an uninitialized
Bindings . |
Object |
eval(Reader reader,
ScriptContext ctxt)
Same as
eval(String, ScriptContext) where the source of the script
is read from a Reader . |
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 a
ScriptEngineFactory for the class to which this ScriptEngine 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 the
ScriptEngine . |
eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext
public static final String NASHORN_GLOBAL
public Object eval(Reader reader, ScriptContext ctxt) throws ScriptException
javax.script.ScriptEngine
eval(String, ScriptContext)
where the source of the script
is read from a Reader
.eval
in interface ScriptEngine
reader
- The source of the script to be executed by the script engine.ctxt
- The ScriptContext
passed to the script engine.ScriptException
- if an error occurs in script.public Object eval(String script, ScriptContext ctxt) throws ScriptException
javax.script.ScriptEngine
eval
in interface ScriptEngine
script
- The script to be executed by the script engine.ctxt
- A ScriptContext
exposing sets of attributes in
different scopes. The meanings of the scopes ScriptContext.GLOBAL_SCOPE
,
and ScriptContext.ENGINE_SCOPE
are defined in the specification.
ENGINE_SCOPE
Bindings
of the ScriptContext
contains the
bindings of scripting variables to application objects to be used during this
script execution.ScriptException
- if an error occurs in script. ScriptEngines should create and throw
ScriptException
wrappers for checked Exceptions thrown by underlying scripting
implementations.public ScriptEngineFactory getFactory()
javax.script.ScriptEngine
ScriptEngineFactory
for the class to which this ScriptEngine
belongs.getFactory
in interface ScriptEngine
ScriptEngineFactory
public Bindings createBindings()
javax.script.ScriptEngine
Bindings
.createBindings
in interface ScriptEngine
Bindings
that can be used to replace the state of this ScriptEngine
.public CompiledScript compile(Reader reader) throws ScriptException
javax.script.Compilable
Reader
) for
later execution. Functionality is identical to
compile(String)
other than the way in which the source is
passed.compile
in interface Compilable
reader
- The reader from which the script source is obtained.CompiledScript
to be executed
later using one of its eval
methods of CompiledScript
.ScriptException
- if compilation fails.public CompiledScript compile(String str) throws ScriptException
javax.script.Compilable
String
) for
later execution.compile
in interface Compilable
str
- The source of the script, represented as a String
.CompiledScript
to be executed later using one
of the eval
methods of CompiledScript
.ScriptException
- if compilation fails.public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException
javax.script.Invocable
invokeFunction
in interface Invocable
name
- of the procedure or function to callargs
- Arguments to pass to the procedure or functionScriptException
- if an error occurs during invocation of the method.NoSuchMethodException
- if method with given name or matching argument types cannot be found.public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException
javax.script.Invocable
ScriptEngine
.invokeMethod
in interface Invocable
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.ScriptException
- if an error occurs during invocation of the method.NoSuchMethodException
- if method with given name or matching argument types cannot be found.public <T> T getInterface(Class<T> clazz)
javax.script.Invocable
invokeFunction
method.getInterface
in interface Invocable
T
- the type of the interface to returnclazz
- The Class
object of the interface to return.ScriptEngine
cannot be found matching
the ones in the requested interface.public <T> T getInterface(Object thiz, Class<T> clazz)
javax.script.Invocable
invokeMethod
method.getInterface
in interface Invocable
T
- the type of the interface to returnthiz
- The scripting object whose member functions are used to implement the methods of the interface.clazz
- The Class
object of the interface to return.ScriptEngine
cannot be found matching
the ones in the requested interface.
Copyright © 2014, 2015, Oracle and/or its affiliates. All rights reserved.