Main.java revision 1372:7cbb2b5867fd
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.tools.jjs;
27
28import java.io.BufferedReader;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.io.IOException;
32import java.io.OutputStream;
33import java.io.PrintWriter;
34import java.util.prefs.Preferences;
35import jdk.nashorn.internal.objects.Global;
36import jdk.nashorn.internal.runtime.Context;
37import jdk.nashorn.internal.runtime.ErrorManager;
38import jdk.nashorn.internal.runtime.JSType;
39import jdk.nashorn.internal.runtime.ScriptEnvironment;
40import jdk.nashorn.internal.runtime.ScriptRuntime;
41import jdk.nashorn.tools.Shell;
42import jdk.internal.jline.console.UserInterruptException;
43
44/**
45 * Interactive command line Shell for Nashorn.
46 */
47public final class Main extends Shell {
48    private Main() {}
49
50    static final Preferences PREFS = Preferences.userRoot().node("tool/jjs");
51
52    /**
53     * Main entry point with the default input, output and error streams.
54     *
55     * @param args The command line arguments
56     */
57    public static void main(final String[] args) {
58        try {
59            final int exitCode = main(System.in, System.out, System.err, args);
60            if (exitCode != SUCCESS) {
61                System.exit(exitCode);
62            }
63        } catch (final IOException e) {
64            System.err.println(e); //bootstrapping, Context.err may not exist
65            System.exit(IO_ERROR);
66        }
67    }
68
69    /**
70     * Starting point for executing a {@code Shell}. Starts a shell with the
71     * given arguments and streams and lets it run until exit.
72     *
73     * @param in input stream for Shell
74     * @param out output stream for Shell
75     * @param err error stream for Shell
76     * @param args arguments to Shell
77     *
78     * @return exit code
79     *
80     * @throws IOException if there's a problem setting up the streams
81     */
82    public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
83        return new Main().run(in, out, err, args);
84    }
85
86    /**
87     * read-eval-print loop for Nashorn shell.
88     *
89     * @param context the nashorn context
90     * @param global  global scope object to use
91     * @return return code
92     */
93    protected int readEvalPrint(final Context context, final Global global) {
94        final ScriptEnvironment env = context.getEnv();
95        final String prompt = bundle.getString("shell.prompt");
96        final PrintWriter err = context.getErr();
97        final Global oldGlobal = Context.getGlobal();
98        final boolean globalChanged = (oldGlobal != global);
99
100        try (final Console in = new Console(System.in, System.out, PREFS)) {
101            if (globalChanged) {
102                Context.setGlobal(global);
103            }
104
105            global.addShellBuiltins();
106
107            while (true) {
108                String source = "";
109                try {
110                    source = in.readLine(prompt);
111                } catch (final IOException ioe) {
112                    err.println(ioe.toString());
113                    if (env._dump_on_error) {
114                        ioe.printStackTrace(err);
115                    }
116                    return IO_ERROR;
117                } catch (final UserInterruptException ex) {
118                    break;
119                }
120
121                if (source.isEmpty()) {
122                    continue;
123                }
124
125                try {
126                    final Object res = context.eval(global, source, global, "<shell>");
127                    if (res != ScriptRuntime.UNDEFINED) {
128                        err.println(JSType.toString(res));
129                    }
130                } catch (final Exception e) {
131                    err.println(e);
132                    if (env._dump_on_error) {
133                        e.printStackTrace(err);
134                    }
135                }
136            }
137        } catch (final Exception e) {
138            err.println(e);
139            if (env._dump_on_error) {
140                e.printStackTrace(err);
141            }
142        } finally {
143            if (globalChanged) {
144                Context.setGlobal(oldGlobal);
145            }
146        }
147
148        return SUCCESS;
149    }
150}
151