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

The following examples show how to use jdk.nashorn.internal.runtime.Context#getErrorManager() . 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: Compiler.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a compiler for an on-demand compilation job.
 *
 * @param installer                code installer
 * @param source                   source to compile
 * @param isStrict                 is this a strict compilation
 * @param compiledFunction         compiled function, if any
 * @param types                    parameter and return value type information, if any is known
 * @param invalidatedProgramPoints invalidated program points for recompilation
 * @param typeInformationFile      descriptor of the location where type information is persisted
 * @param continuationEntryPoints  continuation entry points for restof method
 * @param runtimeScope             runtime scope for recompilation type lookup in {@code TypeEvaluator}
 * @return a new compiler
 */
public static Compiler forOnDemandCompilation(
        final CodeInstaller installer,
        final Source source,
        final boolean isStrict,
        final RecompilableScriptFunctionData compiledFunction,
        final TypeMap types,
        final Map<Integer, Type> invalidatedProgramPoints,
        final Object typeInformationFile,
        final int[] continuationEntryPoints,
        final ScriptObject runtimeScope) {
    final Context context = installer.getContext();
    return new Compiler(context, installer, source, context.getErrorManager(), isStrict, true,
            compiledFunction, types, invalidatedProgramPoints, typeInformationFile,
            continuationEntryPoints, runtimeScope);
}
 
Example 2
Source File: Compiler.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a compiler for an on-demand compilation job.
 *
 * @param installer                code installer
 * @param source                   source to compile
 * @param isStrict                 is this a strict compilation
 * @param compiledFunction         compiled function, if any
 * @param types                    parameter and return value type information, if any is known
 * @param invalidatedProgramPoints invalidated program points for recompilation
 * @param typeInformationFile      descriptor of the location where type information is persisted
 * @param continuationEntryPoints  continuation entry points for restof method
 * @param runtimeScope             runtime scope for recompilation type lookup in {@code TypeEvaluator}
 * @return a new compiler
 */
public static Compiler forOnDemandCompilation(
        final CodeInstaller installer,
        final Source source,
        final boolean isStrict,
        final RecompilableScriptFunctionData compiledFunction,
        final TypeMap types,
        final Map<Integer, Type> invalidatedProgramPoints,
        final Object typeInformationFile,
        final int[] continuationEntryPoints,
        final ScriptObject runtimeScope) {
    final Context context = installer.getContext();
    return new Compiler(context, installer, source, context.getErrorManager(), isStrict, true,
            compiledFunction, types, invalidatedProgramPoints, typeInformationFile,
            continuationEntryPoints, runtimeScope);
}
 
Example 3
Source File: Compiler.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a compiler for an on-demand compilation job.
 *
 * @param installer                code installer
 * @param source                   source to compile
 * @param isStrict                 is this a strict compilation
 * @param compiledFunction         compiled function, if any
 * @param types                    parameter and return value type information, if any is known
 * @param invalidatedProgramPoints invalidated program points for recompilation
 * @param typeInformationFile      descriptor of the location where type information is persisted
 * @param continuationEntryPoints  continuation entry points for restof method
 * @param runtimeScope             runtime scope for recompilation type lookup in {@code TypeEvaluator}
 * @return a new compiler
 */
public static Compiler forOnDemandCompilation(
        final CodeInstaller installer,
        final Source source,
        final boolean isStrict,
        final RecompilableScriptFunctionData compiledFunction,
        final TypeMap types,
        final Map<Integer, Type> invalidatedProgramPoints,
        final Object typeInformationFile,
        final int[] continuationEntryPoints,
        final ScriptObject runtimeScope) {
    final Context context = installer.getContext();
    return new Compiler(context, installer, source, context.getErrorManager(), isStrict, true,
            compiledFunction, types, invalidatedProgramPoints, typeInformationFile,
            continuationEntryPoints, runtimeScope);
}
 
Example 4
Source File: Compiler.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a compiler for an on-demand compilation job.
 *
 * @param installer                code installer
 * @param source                   source to compile
 * @param isStrict                 is this a strict compilation
 * @param compiledFunction         compiled function, if any
 * @param types                    parameter and return value type information, if any is known
 * @param invalidatedProgramPoints invalidated program points for recompilation
 * @param typeInformationFile      descriptor of the location where type information is persisted
 * @param continuationEntryPoints  continuation entry points for restof method
 * @param runtimeScope             runtime scope for recompilation type lookup in {@code TypeEvaluator}
 * @return a new compiler
 */
public static Compiler forOnDemandCompilation(
        final CodeInstaller installer,
        final Source source,
        final boolean isStrict,
        final RecompilableScriptFunctionData compiledFunction,
        final TypeMap types,
        final Map<Integer, Type> invalidatedProgramPoints,
        final Object typeInformationFile,
        final int[] continuationEntryPoints,
        final ScriptObject runtimeScope) {
    final Context context = installer.getContext();
    return new Compiler(context, installer, source, context.getErrorManager(), isStrict, true,
            compiledFunction, types, invalidatedProgramPoints, typeInformationFile,
            continuationEntryPoints, runtimeScope);
}
 
Example 5
Source File: Shell.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the given script files in the command line
 *
 * @param context the nashorn context
 * @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, new Source(fileName, new File(fileName)), errors).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            if (env._print_ast) {
                context.getErr().println(new ASTWriter(functionNode));
            }

            if (env._print_parse) {
                context.getErr().println(new PrintVisitor(functionNode));
            }

            //null - pass no code installer - this is compile only
            new Compiler(env).compile(functionNode);
        }
    } finally {
        env.getOut().flush();
        env.getErr().flush();
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }
    }

    return SUCCESS;
}
 
Example 6
Source File: Shell.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the given script files in the command line
 * This is called only when using the --compile-only flag
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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, 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, 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;
}
 
Example 7
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 8
Source File: Shell.java    From TencentKona-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 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: 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 10
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 11
Source File: Shell.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the given script files in the command line
 * This is called only when using the --compile-only flag
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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, sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            Compiler.forNoInstallerCompilation(
                   context,
                   functionNode.getSource(),
                   env._strict | functionNode.isStrict()).
                   compile(functionNode, 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;
}
 
Example 12
Source File: Shell.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the given script files in the command line
 * This is called only when using the --compile-only flag
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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, sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            Compiler.forNoInstallerCompilation(
                   context,
                   functionNode.getSource(),
                   env._strict | functionNode.isStrict()).
                   compile(functionNode, 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;
}
 
Example 13
Source File: Shell.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the given script files in the command line
 * This is called only when using the --compile-only flag
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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, sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            Compiler.forNoInstallerCompilation(
                   context,
                   functionNode.getSource(),
                   env._strict | functionNode.isStrict()).
                   compile(functionNode, 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;
}
 
Example 14
Source File: Shell.java    From openjdk-jdk8u-backup 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 15
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 16
Source File: Shell.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the given script files in the command line
 * This is called only when using the --compile-only flag
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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, sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            Compiler.forNoInstallerCompilation(
                   context,
                   functionNode.getSource(),
                   env._strict | functionNode.isStrict()).
                   compile(functionNode, 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;
}
 
Example 17
Source File: Shell.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compiles the given script files in the command line
 * This is called only when using the --compile-only flag
 *
 * @param context the nashorn context
 * @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 Global global, final List<String> files) throws IOException {
    final Global 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, sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            Compiler.forNoInstallerCompilation(
                   context,
                   functionNode.getSource(),
                   env._strict | functionNode.isStrict()).
                   compile(functionNode, 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;
}
 
Example 18
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 19
Source File: Compiler.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a compiler without a code installer. It can only be used to compile code, not install the
 * generated classes and as such it is useful only for implementation of {@code --compile-only} command
 * line option.
 * @param context  the current context
 * @param source   source to compile
 * @param isStrict is this a strict compilation
 * @return a new compiler
 */
public static Compiler forNoInstallerCompilation(
        final Context context,
        final Source source,
        final boolean isStrict) {
    return new Compiler(context, null, source, context.getErrorManager(), isStrict);
}
 
Example 20
Source File: Compiler.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a compiler without a code installer. It can only be used to compile code, not install the
 * generated classes and as such it is useful only for implementation of {@code --compile-only} command
 * line option.
 * @param context  the current context
 * @param source   source to compile
 * @param isStrict is this a strict compilation
 * @return a new compiler
 */
public static Compiler forNoInstallerCompilation(
        final Context context,
        final Source source,
        final boolean isStrict) {
    return new Compiler(context, null, source, context.getErrorManager(), isStrict);
}