Java Code Examples for jdk.nashorn.internal.runtime.Context#compileScript()

The following examples show how to use jdk.nashorn.internal.runtime.Context#compileScript() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void compileErrorTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());
    try {
        final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
        if (script != null) {
            fail("Invalid script compiled without errors");
        }
        if (errors.getNumberOfErrors() != 1) {
            fail("Wrong number of errors: " + errors.getNumberOfErrors());
        }
    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 2
Source File: ContextTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void compileErrorTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());
    try {
        final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
        if (script != null) {
            fail("Invalid script compiled without errors");
        }
        if (errors.getNumberOfErrors() != 1) {
            fail("Wrong number of errors: " + errors.getNumberOfErrors());
        }
    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 3
Source File: ContextTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void compileErrorTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());
    try {
        final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
        if (script != null) {
            fail("Invalid script compiled without errors");
        }
        if (errors.getNumberOfErrors() != 1) {
            fail("Wrong number of errors: " + errors.getNumberOfErrors());
        }
    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 4
Source File: ContextTest.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void compileErrorTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());
    try {
        final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
        if (script != null) {
            fail("Invalid script compiled without errors");
        }
        if (errors.getNumberOfErrors() != 1) {
            fail("Wrong number of errors: " + errors.getNumberOfErrors());
        }
    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 5
Source File: ContextTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void compileErrorTest() {
    final Options options = new Options("");
    final ErrorManager errors = new ErrorManager();
    final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
    final Global oldGlobal = Context.getGlobal();
    Context.setGlobal(cx.createGlobal());
    try {
        final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
        if (script != null) {
            fail("Invalid script compiled without errors");
        }
        if (errors.getNumberOfErrors() != 1) {
            fail("Wrong number of errors: " + errors.getNumberOfErrors());
        }
    } finally {
        Context.setGlobal(oldGlobal);
    }
}
 
Example 6
Source File: ContextTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 7
Source File: Shell.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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(sourceFor(fileName, file), 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;
}
 
Example 8
Source File: Shell.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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(sourceFor(fileName, file), 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;
}
 
Example 9
Source File: ContextTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 10
Source File: Shell.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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(sourceFor(fileName, file), 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;
}
 
Example 11
Source File: ContextTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 12
Source File: ContextTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 13
Source File: Shell.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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(sourceFor(fileName, file), global);
            if (script == null || errors.getNumberOfErrors() != 0) {
                if (context.getEnv()._parse_only && !errors.hasErrors()) {
                    continue; // No error, continue to consume all files in list
                }
                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;
}
 
Example 14
Source File: Shell.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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(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;
}
 
Example 15
Source File: ContextTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 16
Source File: Shell.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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(sourceFor(fileName, file), 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;
}
 
Example 17
Source File: ContextTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 18
Source File: ContextTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static Object eval(final Context cx, final String name, final String code) {
    final Source source = sourceFor(name, code);
    final ScriptObject global = Context.getGlobal();
    final ScriptFunction func = cx.compileScript(source, global);
    return func != null ? ScriptRuntime.apply(func, global) : null;
}
 
Example 19
Source File: Shell.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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(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;
}
 
Example 20
Source File: Shell.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Runs the given JavaScript files in the command line
 *
 * @param context the nashorn context
 * @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(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;
}