Example usage for jdk.nashorn.internal.runtime Context compileScript

List of usage examples for jdk.nashorn.internal.runtime Context compileScript

Introduction

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

Prototype

public ScriptFunction compileScript(final Source source, final ScriptObject scope) 

Source Link

Document

Compile a top level script.

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);//from w  w  w . j  a  va2  s.co  m
    ScriptRuntime.apply(fn, globalScope);
}

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. jav  a2 s .c om*/
 * @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:z.zee.Z.java

License:Open Source License

/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context//www . j a v a 2s.com
 * @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;
}