Example usage for jdk.nashorn.internal.runtime ErrorManager ErrorManager

List of usage examples for jdk.nashorn.internal.runtime ErrorManager ErrorManager

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime ErrorManager ErrorManager.

Prototype

public ErrorManager(final PrintWriter writer) 

Source Link

Document

Constructor.

Usage

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);/* www.j av a2s  .  co  m*/
    ScriptRuntime.apply(fn, globalScope);
}

From source file:com.threecrickets.scripturian.adapter.NashornAdapter.java

License:LGPL

/**
 * Constructor./*from  w  ww.  j a v a 2 s.c om*/
 * 
 * @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 ww w.  ja  va 2 s  .co  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:org.kihara.util.JavascriptEngine.java

License:Open Source License

/**
 * Make a new Nashorn Context to compile and/or run JavaScript files.
 *
 * @param i input stream for Shell/*from  ww  w .jav  a 2 s. co m*/
 * @param o output stream for Shell
 * @param e error stream for Shell
 *
 * @return null if there are problems with option parsing.
 */
@SuppressWarnings("resource")
private static Context makeContext(final InputStream i, final OutputStream o, final OutputStream e) {
    final PrintWriter out = new PrintWriter(o instanceof PrintStream ? (PrintStream) o : new PrintStream(o),
            true);
    final PrintWriter err = new PrintWriter(e instanceof PrintStream ? (PrintStream) e : new PrintStream(e),
            true);

    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final ErrorManager errors = new ErrorManager(err);
    final Options options = new Options("nashorn", err);

    options.set("scripting", true);
    options.set("language", "es6");
    options.set("optimistic.types", true);
    options.set("lazy.compilation", true);

    final ClassFilter filter = (name) -> {
        if (Arrays.stream(prohibitedClasses).parallel().filter(name::equals).findAny().isPresent()) {
            System.err.println("Access Manager: Denied item " + name + ".");
            return false;
        } else
            return true;
    };

    return new Context(options, errors, out, err, loader, filter);
}

From source file:org.netbeans.es.perftest.nashorn.NashornParser.java

License:Open Source License

@Override
public boolean parse(File file, ParserOptions options) throws IOException {
    final PrintWriter err = options.isPrintError() ? options.getProgressWriter()
            : new PrintWriter(new OutputStreamWriter(new ByteArrayOutputStream()));
    final ScriptEnvironment env = new ScriptEnvironment(
            new jdk.nashorn.internal.runtime.options.Options(file.getAbsolutePath()), err, err);
    final Source src = Source.sourceFor(file.getName(), file);
    final ErrorManager em = new ErrorManager(err);
    final Parser p = new Parser(env, src, em);
    final FunctionNode node = p.parse();
    return !em.hasErrors();
}

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// w w w .j  av a  2 s  .c  om
 * @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());
}