List of usage examples for jdk.nashorn.internal.runtime Context Context
public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader)
From source file:com.mongodb.jvm.test.TestNashorn.java
License:Apache License
private static void run(String script) { PrintWriter out = new PrintWriter(System.out, true); PrintWriter err = new PrintWriter(System.err, true); Options options = new Options("nashorn", err); options.set("print.no.newline", true); ErrorManager errors = new ErrorManager(err); Context context = new Context(options, errors, out, err, Thread.currentThread().getContextClassLoader()); ScriptObject globalScope = context.createGlobal(); Context.setGlobal(globalScope); ScriptFunction fn = context.compileScript(Source.sourceFor(TestNashorn.class.getCanonicalName(), script), globalScope);/*from w ww .j a v a 2s. c o m*/ ScriptRuntime.apply(fn, globalScope); }
From source file:com.threecrickets.scripturian.adapter.NashornAdapter.java
License:LGPL
/** * Constructor.//from w w w . jav a2s.c o m * * @throws LanguageAdapterException * In case of an initialization error */ public NashornAdapter() throws LanguageAdapterException { super("Nashorn", Version.version(), "JavaScript", new NashornScriptEngineFactory().getLanguageVersion(), Arrays.asList("js", "javascript", "nashorn"), "js", Arrays.asList("javascript", "js", "nashorn"), "nashorn"); try { System.setProperty("nashorn.persistent.code.cache", getCacheDir().getCanonicalPath()); } catch (IOException x) { throw new LanguageAdapterException(NashornAdapter.class, "Could not access cache directory: " + getCacheDir(), x); } PrintWriter out = new PrintWriter(new ExecutionContextWriter(), true); PrintWriter err = new PrintWriter(new ExecutionContextErrorWriter(), true); // See: jdk.nashorn.internal.runtime.ScriptEnvironment Options options = new Options("nashorn", err); options.set("print.no.newline", true); options.set("persistent.code.cache", true); ErrorManager errors = new ErrorManager(err); context = new Context(options, errors, out, err, getClass().getClassLoader()); }
From source file:io.stallion.plugins.javascript.JavascriptShell.java
License:Open Source License
private static Context makeContext(InputStream in, OutputStream out, OutputStream err, String[] args) { PrintStream pout = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out); PrintStream perr = err instanceof PrintStream ? (PrintStream) err : new PrintStream(err); PrintWriter wout = new PrintWriter(pout, true); PrintWriter werr = new PrintWriter(perr, true); ErrorManager errors = new ErrorManager(werr); Options options = new Options("nashorn", werr); if (args != null) { try {/*from w w w . j av a 2s . c o m*/ options.process(args); } catch (IllegalArgumentException var27) { werr.println(bundle.getString("shell.usage")); options.displayHelp(var27); return null; } } return new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); }
From source file:z.zee.Z.java
License:Open Source License
/** * Make a new Nashorn Context to compile and/or run JavaScript files. * * @param in input stream for Z/*from w w w.jav a2 s .co m*/ * @param out output stream for Z * @param err error stream for Z * @param args arguments to Z * * @return null if there are problems with option parsing. */ @SuppressWarnings("resource") private static Context makeContext(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) { final PrintStream pout = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out); final PrintStream perr = err instanceof PrintStream ? (PrintStream) err : new PrintStream(err); final PrintWriter wout = new PrintWriter(pout, true); final PrintWriter werr = new PrintWriter(perr, true); // Set up error handler. final ErrorManager errors = new ErrorManager(werr); // Set up options. final Options options = new Options("nashorn", werr); // parse options if (args != null) { try { options.process(args); } catch (final IllegalArgumentException e) { werr.println(bundle.getString("shell.usage")); options.displayHelp(e); return null; } } // detect scripting mode by any source's first character being '#' scripting = options.getBoolean("scripting"); if (!scripting) { for (final String fileName : options.getFiles()) { final File firstFile = new File(fileName); if (firstFile.isFile()) { try (final FileReader fr = new FileReader(firstFile)) { final int firstChar = fr.read(); // starts with '# if (firstChar == '#') { options.set("scripting", true); break; } } catch (final IOException e) { // ignore this. File IO errors will be reported later anyway } } } } return new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); }