/** * Add a callback to be invoked on shutdown. * * @param callback a callback function wrapper * @param sync whether to invoke the callback synchronously (on the main shutdown thread) or * asynchronously (on the worker's event loop thread) */ public synchronized void addShutdownHook(Scriptable callback, boolean sync) { List<Callback> hooks = shutdownHooks; if (hooks == null) { hooks = shutdownHooks = new ArrayList<Callback>(); } hooks.add(new Callback(callback, this, sync)); }
/** * Evaluate an expression from the command line. * * @param expr the JavaScript expression to evaluate * @return the return value * @throws IOException an I/O related error occurred * @throws JavaScriptException the script threw an error during compilation or execution */ public Object evaluateExpression(String expr) throws IOException, JavaScriptException { Context cx = contextFactory.enterContext(); cx.setOptimizationLevel(-1); try { Object retval; Repository repository = repositories.get(0); Scriptable parentScope = mainScope != null ? mainScope : globalScope; ModuleScope scope = new ModuleScope("<expr>", repository, parentScope, mainWorker); Resource res = new StringResource("<expr>", expr, 1); ReloadableScript script = new ReloadableScript(res, this); retval = mainWorker.evaluateScript(cx, script, scope); return retval instanceof Wrapper ? ((Wrapper) retval).unwrap() : retval; } finally { Context.exit(); } }