List of usage examples for jdk.nashorn.internal.runtime Context getErr
public PrintWriter getErr()
From source file:io.stallion.plugins.javascript.JavascriptShell.java
License:Open Source License
private static int readEvalPrint(InputStream input, OutputStream out, Context context, Global global) throws IOException, ScriptException { JsPluginSettings pluginSettings = new JsPluginSettings(); //context.eval(global, ""); //Global global = context.createGlobal(); //ScriptEnvironment env = context.getEnv(); String prompt = bundle.getString("shell.prompt"); //BufferedReader in = new BufferedReader(new InputStreamReader(input)); ConsoleReader in = new ConsoleReader(); PrintWriter err = context.getErr(); Global oldGlobal = Context.getGlobal(); boolean globalChanged = oldGlobal != global; ScriptEnvironment env = context.getEnv(); if (DB.available()) { global.put("DB", DB.instance(), false); }//from w w w . j a v a 2 s . c o m try { if (globalChanged) { Context.setGlobal(global); } global.addShellBuiltins(); // Load builtins global.put("javaToJsHelpers", new JavaToJsHelpers(Sandbox.allPermissions()), true); SandboxedContext ctx = new SandboxedContext(Settings.instance().getTargetFolder() + "/js", Sandbox.allPermissions(), pluginSettings); global.put("myContext", ctx, true); String stallionSharedJs = IOUtils .toString(JavascriptShell.class.getResource("/jslib/stallion_shared.js"), UTF8); context.eval(global, "load(" + JSON.stringify( map(val("script", stallionSharedJs), val("name", "stallion_shared.js"))) + ");", global, "<shellboot>"); global.put("stallionClassLoader", new UnrestrictedJsClassLoader(), true); while (true) { String source; do { //err.print(prompt); //err.flush(); source = ""; try { source = in.readLine(prompt); } catch (IOException var14) { err.println(var14.toString()); } if (source == null) { return 0; } } while (source.isEmpty()); try { Object e = context.eval(global, source, global, "<shell>"); if (e != ScriptRuntime.UNDEFINED) { err.println(JSType.toString(e)); } } catch (Exception var15) { err.println(var15); if (env._dump_on_error) { var15.printStackTrace(err); } } } } finally { if (globalChanged) { Context.setGlobal(oldGlobal); } } }
From source file:org.kihara.util.JavascriptEngine.java
License:Open Source License
/** * Compiles the given script files in the command line * * @param context the nashorn context//from w ww. j av a2 s . c o m * @param global the global scope * @param files the list of script files to compile * * @return error code * @throws IOException when any script file read results in I/O error */ private static int compileScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); final ScriptEnvironment env = context.getEnv(); try { if (globalChanged) { Context.setGlobal(global); } final ErrorManager errors = context.getErrorManager(); // For each file on the command line. for (final String fileName : files) { final FunctionNode functionNode = new Parser(env, Source.sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse(); if (errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } new Compiler(context, env, null, //null - pass no code installer - this is compile only functionNode.getSource(), context.getErrorManager(), env._strict | functionNode.isStrict()) .compile(functionNode, Compiler.CompilationPhases.COMPILE_ALL_NO_INSTALL); //*/ /* Compiler.forNoInstallerCompilation(context, functionNode.getSource(), env._strict | functionNode.isStrict()). compile(functionNode, Compiler.CompilationPhases.COMPILE_ALL_NO_INSTALL); //*/ if (env._print_ast) { context.getErr().println(new ASTWriter(functionNode)); } if (env._print_parse) { context.getErr().println(new PrintVisitor(functionNode)); } if (errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } } } finally { env.getOut().flush(); env.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
From source file:org.kihara.util.JavascriptEngine.java
License:Open Source License
/** * Runs the given JavaScript files in the command line * * @param context the nashorn context//from w ww . ja v a2 s . co m * @param global the global scope * @param files the list of script files to run * * @return error code * @throws IOException when any script file read results in I/O error */ private int runScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } final ErrorManager errors = context.getErrorManager(); // For each file on the command line. for (final String fileName : files) { if ("-".equals(fileName)) { final int res = readEvalPrint(context, global); if (res != SUCCESS) { return res; } continue; } final File file = new File(fileName); final ScriptFunction script = context .compileScript(Source.sourceFor(fileName, file.toURI().toURL()), global); if (script == null || errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } try { apply(script, global); } catch (final NashornException e) { errors.error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } } } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
From source file:org.kihara.util.JavascriptEngine.java
License:Open Source License
/** * Runs launches "fx:bootstrap.js" with the given JavaScript files provided * as arguments./*w w w.j av a2 s .co m*/ * * @param context the nashorn context * @param global the global scope * @param files the list of script files to provide * * @return error code * @throws IOException when any script file read results in I/O error */ private static int runFXScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } global.addOwnProperty("$GLOBAL", Property.NOT_ENUMERABLE, global); global.addOwnProperty("$SCRIPTS", Property.NOT_ENUMERABLE, files); context.load(global, "fx:bootstrap.js"); } catch (final NashornException e) { context.getErrorManager().error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
From source file:org.kihara.util.JavascriptEngine.java
License:Open Source License
/** * read-eval-print loop for Nashorn shell. * * @param context the nashorn context//from ww w. j a v a 2 s. c o m * @param global global scope object to use * @return return code */ @SuppressWarnings("resource") private static int readEvalPrint(final Context context, final ScriptObject global) { final String prompt = "> ";//bundle.getString("shell.prompt"); final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); final PrintWriter err = context.getErr(); final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); final ScriptEnvironment env = context.getEnv(); try { if (globalChanged) { Context.setGlobal(global); } // initialize with "shell.js" script try { final Source source = Source.sourceFor("<shell.js>", JavascriptEngine.SHELL_INIT_JS); context.eval(global, source.getString(), global, "<shell.js>", false, false); // custom scripts context.eval(global, script.get(), global, "<shell.js>", false, false); } catch (final Exception e) { err.println(e); if (env._dump_on_error) { e.printStackTrace(err); } return INTERNAL_ERROR; } while (true) { err.print(prompt); err.flush(); String source = ""; try { source = in.readLine(); } catch (final IOException ioe) { err.println(ioe.toString()); } if (source == null) { break; } if (source.isEmpty()) { continue; } Object res; try { res = context.eval(global, source, global, "<shell>", env._strict, false); } catch (final Exception e) { err.println(e); if (env._dump_on_error) { e.printStackTrace(err); } continue; } if (res != ScriptRuntime.UNDEFINED) { err.println(JSType.toString(res)); } } } finally { if (globalChanged) { Context.setGlobal(global); } } return SUCCESS; }
From source file:z.zee.Z.java
License:Open Source License
/** * Run method logic.//w w w. j a v a2s.c om * * @param in input stream for Z * @param out output stream for Z * @param err error stream for Z * @param args arguments to Z * * @return exit code * * @throws IOException if there's a problem setting up the streams */ protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { final Context context = makeContext(in, out, err, args); if (context == null) { return COMMANDLINE_ERROR; } final ScriptObject global = context.createGlobal(); final ScriptEnvironment env = context.getEnv(); final List<String> files = env.getFiles(); if (env.getArguments().isEmpty() && !scripting) { context.getErr().println(bundle.getString("shell.usage")); return COMPILATION_ERROR; } if (files.isEmpty()) { return readEvalPrint(context, global); } if (env._compile_only) { return compileScripts(context, global, files); } return runScripts(context, global, files); }
From source file:z.zee.Z.java
License:Open Source License
/** * Runs the given JavaScript files in the command line * * @param context the nashorn context//from w ww . j a v a2s. co m * @param global the global scope * @param files the list of script files to run * * @return error code * @throws IOException when any script file read results in I/O error */ private int runScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } final ErrorManager errors = context.getErrorManager(); // For each file on the command line. for (final String fileName : files) { final File file = new File(fileName); final ScriptFunction script = context.compileScript(new Source(fileName, file.toURI().toURL()), global); if (script == null || errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } try { apply(script, global); } catch (final NashornException e) { errors.error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } } } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
From source file:z.zee.Z.java
License:Open Source License
/** * read-eval-print loop for Nashorn shell. * * @param context the nashorn context/*w w w.jav a 2 s. c o m*/ * @param global global scope object to use * @return return code */ @SuppressWarnings("resource") private static int readEvalPrint(final Context context, final ScriptObject global) { final String prompt = bundle.getString("shell.prompt"); final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); final PrintWriter err = context.getErr(); final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); final ScriptEnvironment env = context.getEnv(); try { if (globalChanged) { Context.setGlobal(global); } // initialize with "z.js" script try { final Source source = new Source("<z.js>", Z.class.getResource("resources/z.js")); context.eval(global, source.getString(), global, "<z.js>", false); } catch (final Exception e) { err.println(e); if (env._dump_on_error) { e.printStackTrace(err); } return INTERNAL_ERROR; } while (true) { err.print(prompt); err.flush(); String source = ""; try { source = in.readLine(); } catch (final IOException ioe) { err.println(ioe.toString()); } if (source == null) { break; } Object res; try { res = context.eval(global, source, global, "<shell>", env._strict); } catch (final Exception e) { err.println(e); if (env._dump_on_error) { e.printStackTrace(err); } continue; } if (res != null && res != ScriptRuntime.UNDEFINED) { err.println(ScriptRuntime.safeToString(res)); } } } finally { if (globalChanged) { Context.setGlobal(global); } } return SUCCESS; }