public class RhinoEngine extends Object implements org.mozilla.javascript.tools.debugger.ScopeProvider
| Constructor and Description |
|---|
RhinoEngine(RingoConfig config,
Map<String,Object> globals)
Create a RhinoEngine with the given configuration.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addModuleLoader(String extension,
Object value) |
void |
addShutdownHook(org.mozilla.javascript.Scriptable callback,
boolean sync)
Add a callback to be invoked on shutdown.
|
void |
addToClasspath(Trackable path) |
Object |
asJavaObject(Object object)
Get a wrapper for an object that exposes it as Java object to JavaScript.
|
Object |
asJavaString(Object object)
Get a wrapper for a string that exposes the java.lang.String methods to JavaScript
This is useful for accessing strings as java.lang.String without the cost of
creating a new instance.
|
RhinoEngine |
createSandbox(RingoConfig config,
Map<String,Object> globals)
Create a sandboxed scripting engine with the same install directory as this and the
given module paths, global properties, class shutter and sealing
|
void |
defineHostClass(Class<org.mozilla.javascript.Scriptable> clazz)
Define a Javascript host object implemented by the given class.
|
protected void |
enterAsyncTask() |
Object |
evaluateExpression(String expr)
Evaluate an expression from the command line.
|
protected void |
exitAsyncTask() |
Repository |
findRepository(String path,
Repository localPath)
Search for a repository in the local path, or the main repository path.
|
Resource |
findResource(String path,
ModuleLoader[] loaders,
Repository localRoot)
Search for a resource in a local path, or the main repository path.
|
List<Resource> |
findResources(String path,
boolean recursive)
Get a list of all child resources for the given path relative to
our script repository.
|
Object[] |
getArguments()
Get the script arguments as object array suitable for use with Context.newArray().
|
String |
getCharset() |
RingoClassLoader |
getClassLoader() |
List<String> |
getCommandLineArguments()
Get the list of command line arguments
|
RingoConfig |
getConfig() |
RingoContextFactory |
getContextFactory() |
RingoWorker |
getCurrentWorker(org.mozilla.javascript.Scriptable obj)
Get the worker associated with the current thread, or the given scope or function argument if provided.
|
static RhinoEngine |
getEngine(org.mozilla.javascript.Scriptable scope)
Get the currently active RhinoEngine instance.
|
List<ScriptError> |
getMainErrors()
Get the list of errors encountered by the main worker.
|
String |
getMainModule()
Get the name of the main script as module name, if any
|
ModuleScope |
getMainModuleScope()
Get the main scrip's module scope, if any
|
RingoWorker |
getMainWorker()
Get the main worker running the main script.
|
ModuleLoader |
getModuleLoader(Resource resource) |
int |
getOptimizationLevel()
Get the current Rhino optimization level
|
Repository |
getParentRepository(org.mozilla.javascript.Scriptable scope)
Get the repository associated with the scope or one of its prototypes
|
List<Repository> |
getRepositories()
Get the engine's module search path as a list of repositories
|
Repository |
getRingoHome()
Get the our installation directory.
|
org.mozilla.javascript.Scriptable |
getScope()
Get the engine's global shared scope
|
ReloadableScript |
getScript(String moduleName)
Resolves a type name to a script file within our script directory
and returns a Scriptable evaluated to the file.
|
ReloadableScript |
getScript(String moduleName,
Repository localPath)
Resolves a type name to a script file within our script directory
and returns a Scriptable evaluated to the file.
|
org.mozilla.javascript.Scriptable |
getShellScope(RingoWorker worker)
Return a shell scope for interactive evaluation
|
RingoWorker |
getWorker()
Get a new
RingoWorker. |
org.mozilla.javascript.WrapFactory |
getWrapFactory()
Get the engine's WrapFactory.
|
protected void |
initArguments(Object[] args)
Initialize and normalize the global variables and arguments on a thread scope.
|
Object |
invoke(Object module,
String method,
Object... args)
Invoke a javascript function.
|
protected boolean |
isPolicyEnabled() |
org.mozilla.javascript.Scriptable |
loadModule(org.mozilla.javascript.Context cx,
String moduleName,
org.mozilla.javascript.Scriptable loadingScope)
Load a Javascript module into a module scope.
|
protected Resource |
loadPackage(String moduleName,
Repository localPath)
Resolves a module id to a package resource.
|
static String |
normalizePath(String path) |
void |
removeModuleLoader(String extension) |
Trackable |
resolve(String path,
Repository localRoot)
Try to resolve path to a resource or repository relative to a local path,
or the engine's repository path.
|
Object |
runScript(Object scriptResource,
String... scriptArgs)
Invoke a script from the command line.
|
protected RingoWorker |
setCurrentWorker(RingoWorker worker)
Associate a worker with the current worker and return the worker
that was previously associated with it, or null.
|
void |
setOptimizationLevel(int level)
Set Rhino optimization level
|
void |
waitForAsyncTasks()
Wait until all daemon threads running in this engine have terminated.
|
static Object |
wrapArgument(Object value,
org.mozilla.javascript.Scriptable scope)
Prepare a single property or argument value for use within rhino.
|
public RhinoEngine(RingoConfig config, Map<String,Object> globals) throws Exception
globals
is not null, its contents are added as properties on the global object.config - the configuration used to initialize the engine.globals - an optional map of global propertiesException - if the engine can't be createdpublic void defineHostClass(Class<org.mozilla.javascript.Scriptable> clazz) throws InvocationTargetException, InstantiationException, IllegalAccessException
clazz - The Java class implementing the host object.IllegalAccessException - if access is not available
to a reflected class memberInstantiationException - if unable to instantiate
the named classInvocationTargetException - if an exception is thrown
during execution of methods of the named classpublic Object runScript(Object scriptResource, String... scriptArgs) throws IOException, org.mozilla.javascript.JavaScriptException
scriptResource - the script resource of pathscriptArgs - an array of command line argumentsIOException - an I/O related error occurredorg.mozilla.javascript.JavaScriptException - the script threw an error during
compilation or executionpublic Object evaluateExpression(String expr) throws IOException, org.mozilla.javascript.JavaScriptException
expr - the JavaScript expression to evaluateIOException - an I/O related error occurredorg.mozilla.javascript.JavaScriptException - the script threw an error during
compilation or executionpublic Object invoke(Object module, String method, Object... args) throws IOException, NoSuchMethodException, ExecutionException, InterruptedException
module - the module name or object, or null for the main modulemethod - the method name to call in the scriptargs - the arguments to pass to the methodNoSuchMethodException - the method is not definedIOException - an I/O related error occurredExecutionExceptionInterruptedExceptionprotected RingoWorker setCurrentWorker(RingoWorker worker)
worker - the new worker associated with the current threadpublic RingoWorker getCurrentWorker(org.mozilla.javascript.Scriptable obj)
IllegalStateException is thrown if no worker could be found or if different workers are
associated with the current thread and the argument object.obj - a scope or function objectIllegalStateException - if no worker could be found, or if different workers are associates with
the current thread and the argument objectpublic RingoWorker getMainWorker()
public RingoWorker getWorker()
RingoWorker.public void addShutdownHook(org.mozilla.javascript.Scriptable callback,
boolean sync)
callback - a callback function wrappersync - whether to invoke the callback synchronously (on the main
shutdown thread) or asynchronously (on the worker's event
loop thread)public List<ScriptError> getMainErrors()
public org.mozilla.javascript.Scriptable getShellScope(RingoWorker worker) throws IOException
IOException - an I/O related exception occurredpublic org.mozilla.javascript.Scriptable getScope()
getScope in interface org.mozilla.javascript.tools.debugger.ScopeProviderprotected void initArguments(Object[] args)
args - the argumentspublic static Object wrapArgument(Object value, org.mozilla.javascript.Scriptable scope)
value - the property or argument valuescope - the scopepublic int getOptimizationLevel()
public void setOptimizationLevel(int level)
level - the new optimization levelpublic ReloadableScript getScript(String moduleName) throws org.mozilla.javascript.JavaScriptException, IOException
moduleName - the name of the module to loadorg.mozilla.javascript.JavaScriptException - if an error occurred evaluating the script fileIOException - if an error occurred reading the script filepublic ReloadableScript getScript(String moduleName, Repository localPath) throws org.mozilla.javascript.JavaScriptException, IOException
moduleName - the name of the module to loadlocalPath - the path of the resource issuing this callorg.mozilla.javascript.JavaScriptException - if an error occurred evaluating the script fileIOException - if an error occurred reading the script fileprotected Resource loadPackage(String moduleName, Repository localPath) throws IOException
moduleName - the name of the package to loadlocalPath - the path of the resource issuing this callIOException - an unrecoverable I/O exception occurred while
reading the packagepublic org.mozilla.javascript.Scriptable loadModule(org.mozilla.javascript.Context cx,
String moduleName,
org.mozilla.javascript.Scriptable loadingScope)
throws IOException
cx - the current contextmoduleName - the module nameloadingScope - the scope requesting the moduleIOException - indicates that in input/output related error occurredpublic String getMainModule()
public ModuleScope getMainModuleScope()
public Object[] getArguments()
public String getCharset()
public static RhinoEngine getEngine(org.mozilla.javascript.Scriptable scope)
scope - the global scope or a top level module scopepublic RhinoEngine createSandbox(RingoConfig config, Map<String,Object> globals) throws Exception
config - the sandbox configurationglobals - a map of predefined global properties, may be nullFileNotFoundException - if any part of the module paths does not existExceptionprotected boolean isPolicyEnabled()
public void waitForAsyncTasks()
throws InterruptedException
InterruptedException - if the current thread has been interruptedprotected void enterAsyncTask()
protected void exitAsyncTask()
public List<String> getCommandLineArguments()
public List<Repository> getRepositories()
public Repository getRingoHome()
public Repository getParentRepository(org.mozilla.javascript.Scriptable scope)
scope - the scope to get the repository frompublic List<Resource> findResources(String path, boolean recursive) throws IOException
path - the repository pathrecursive - whether to include nested resourcesIOExceptionpublic Trackable resolve(String path, Repository localRoot) throws IOException
path - the resource namelocalRoot - a repository to look firstIOException - if an I/O error occurredpublic Resource findResource(String path, ModuleLoader[] loaders, Repository localRoot) throws IOException
path - the resource nameloaders - optional list of module loaderslocalRoot - a repository to look firstIOException - if an I/O error occurredpublic Repository findRepository(String path, Repository localPath) throws IOException
path - the repository namelocalPath - a repository to look firstIOException - if an I/O error occurredpublic ModuleLoader getModuleLoader(Resource resource)
public void removeModuleLoader(String extension)
public void addToClasspath(Trackable path) throws MalformedURLException
MalformedURLExceptionpublic RingoContextFactory getContextFactory()
public RingoClassLoader getClassLoader()
public RingoConfig getConfig()
public Object asJavaString(Object object)
object - an objectpublic Object asJavaObject(Object object)
object - an objectpublic org.mozilla.javascript.WrapFactory getWrapFactory()
Copyright © 2016. All rights reserved.