- 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 theCompilableandInvocableinterfaces, allowing for efficient precompilation and repeated execution of scripts.- Since:
- 1.8u40
- See Also:
- NashornScriptEngineFactory
 
- 
- 
Field SummaryFields Modifier and Type Field Description static StringNASHORN_GLOBALKey used to associate Nashorn global object mirror with arbitrary Bindings instance.- 
Fields inherited from class javax.script.AbstractScriptEnginecontext
 - 
Fields inherited from interface javax.script.ScriptEngineARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
 
- 
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description CompiledScriptcompile(Reader reader)Compiles the script (source read fromReader) for later execution.CompiledScriptcompile(String str)Compiles the script (source represented as aString) for later execution.BindingscreateBindings()Returns an uninitializedBindings.Objecteval(Reader reader, ScriptContext ctxt)Same aseval(String, ScriptContext)where the source of the script is read from aReader.Objecteval(String script, ScriptContext ctxt)Causes the immediate execution of the script whose source is the String passed as the first argument.ScriptEngineFactorygetFactory()Returns aScriptEngineFactoryfor the class to which thisScriptEnginebelongs.<T> TgetInterface(Class<T> clazz)Returns an implementation of an interface using functions compiled in the interpreter.<T> TgetInterface(Object thiz, Class<T> clazz)Returns an implementation of an interface using member functions of a scripting object compiled in the interpreter.ObjectinvokeFunction(String name, Object... args)Used to call top-level procedures and functions defined in scripts.ObjectinvokeMethod(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.AbstractScriptEngineeval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext
 
- 
 
- 
- 
- 
Field Detail- 
NASHORN_GLOBALpublic static final String NASHORN_GLOBAL Key used to associate Nashorn global object mirror with arbitrary Bindings instance.- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
evalpublic Object eval(Reader reader, ScriptContext ctxt) throws ScriptException Description copied from interface:ScriptEngineSame aseval(String, ScriptContext)where the source of the script is read from aReader.- Specified by:
- evalin interface- ScriptEngine
- Parameters:
- reader- The source of the script to be executed by the script engine.
- ctxt- The- ScriptContextpassed 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 throw- ScriptExceptionwrappers for checked Exceptions thrown by underlying scripting implementations.
 
 - 
evalpublic Object eval(String script, ScriptContext ctxt) throws ScriptException Description copied from interface:ScriptEngineCauses 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:
- evalin interface- ScriptEngine
- Parameters:
- script- The script to be executed by the script engine.
- ctxt- A- ScriptContextexposing sets of attributes in different scopes. The meanings of the scopes- ScriptContext.GLOBAL_SCOPE, and- ScriptContext.ENGINE_SCOPEare defined in the specification.
 The- ENGINE_SCOPE- Bindingsof the- ScriptContextcontains 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 throw- ScriptExceptionwrappers for checked Exceptions thrown by underlying scripting implementations.
 
 - 
getFactorypublic ScriptEngineFactory getFactory() Description copied from interface:ScriptEngineReturns aScriptEngineFactoryfor the class to which thisScriptEnginebelongs.- Specified by:
- getFactoryin interface- ScriptEngine
- Returns:
- The ScriptEngineFactory
 
 - 
createBindingspublic Bindings createBindings() Description copied from interface:ScriptEngineReturns an uninitializedBindings.- Specified by:
- createBindingsin interface- ScriptEngine
- Returns:
- A Bindingsthat can be used to replace the state of thisScriptEngine.
 
 - 
compilepublic CompiledScript compile(Reader reader) throws ScriptException Description copied from interface:CompilableCompiles 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:
- compilein interface- Compilable
- Parameters:
- reader- The reader from which the script source is obtained.
- Returns:
- An instance of a subclass of CompiledScriptto be executed later using one of itsevalmethods ofCompiledScript.
- Throws:
- ScriptException- if compilation fails.
 
 - 
compilepublic CompiledScript compile(String str) throws ScriptException Description copied from interface:CompilableCompiles the script (source represented as aString) for later execution.- Specified by:
- compilein interface- Compilable
- Parameters:
- str- The source of the script, represented as a- String.
- Returns:
- An instance of a subclass of CompiledScriptto be executed later using one of theevalmethods ofCompiledScript.
- Throws:
- ScriptException- if compilation fails.
 
 - 
invokeFunctionpublic Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException Description copied from interface:InvocableUsed to call top-level procedures and functions defined in scripts.- Specified by:
- invokeFunctionin interface- Invocable
- Parameters:
- name- of the procedure or function to call
- args- 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.
 
 - 
invokeMethodpublic Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException Description copied from interface:InvocableCalls a method on a script object compiled during a previous script execution, which is retained in the state of theScriptEngine.- Specified by:
- invokeMethodin interface- Invocable
- 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.
 
 - 
getInterfacepublic <T> T getInterface(Class<T> clazz) Description copied from interface:InvocableReturns an implementation of an interface using functions compiled in the interpreter. The methods of the interface may be implemented using theinvokeFunctionmethod.- Specified by:
- getInterfacein interface- Invocable
- Type Parameters:
- T- the type of the interface to return
- Parameters:
- clazz- The- Classobject 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 ScriptEnginecannot be found matching the ones in the requested interface.
 
 - 
getInterfacepublic <T> T getInterface(Object thiz, Class<T> clazz) Description copied from interface:InvocableReturns 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 theinvokeMethodmethod.- Specified by:
- getInterfacein interface- Invocable
- 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- The- Classobject 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 ScriptEnginecannot be found matching the ones in the requested interface.
 
 
- 
 
-