Main.java revision 1377:23f843804b05
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.internal.jline.console.completer.Completer; 36import jdk.internal.jline.console.UserInterruptException; 37import jdk.nashorn.api.scripting.NashornException; 38import jdk.nashorn.internal.objects.Global; 39import jdk.nashorn.internal.runtime.Context; 40import jdk.nashorn.internal.runtime.JSType; 41import jdk.nashorn.internal.runtime.ScriptEnvironment; 42import jdk.nashorn.internal.runtime.ScriptRuntime; 43import jdk.nashorn.tools.Shell; 44 45/** 46 * Interactive command line Shell for Nashorn. 47 */ 48public final class Main extends Shell { 49 private Main() {} 50 51 static final Preferences PREFS = Preferences.userRoot().node("tool/jjs"); 52 53 /** 54 * Main entry point with the default input, output and error streams. 55 * 56 * @param args The command line arguments 57 */ 58 public static void main(final String[] args) { 59 try { 60 final int exitCode = main(System.in, System.out, System.err, args); 61 if (exitCode != SUCCESS) { 62 System.exit(exitCode); 63 } 64 } catch (final IOException e) { 65 System.err.println(e); //bootstrapping, Context.err may not exist 66 System.exit(IO_ERROR); 67 } 68 } 69 70 /** 71 * Starting point for executing a {@code Shell}. Starts a shell with the 72 * given arguments and streams and lets it run until exit. 73 * 74 * @param in input stream for Shell 75 * @param out output stream for Shell 76 * @param err error stream for Shell 77 * @param args arguments to Shell 78 * 79 * @return exit code 80 * 81 * @throws IOException if there's a problem setting up the streams 82 */ 83 public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { 84 return new Main().run(in, out, err, args); 85 } 86 87 /** 88 * read-eval-print loop for Nashorn shell. 89 * 90 * @param context the nashorn context 91 * @param global global scope object to use 92 * @return return code 93 */ 94 protected int readEvalPrint(final Context context, final Global global) { 95 final ScriptEnvironment env = context.getEnv(); 96 final String prompt = bundle.getString("shell.prompt"); 97 final PrintWriter err = context.getErr(); 98 final Global oldGlobal = Context.getGlobal(); 99 final boolean globalChanged = (oldGlobal != global); 100 final Completer completer = new NashornCompleter(context, global); 101 102 try (final Console in = new Console(System.in, System.out, PREFS, completer)) { 103 if (globalChanged) { 104 Context.setGlobal(global); 105 } 106 107 global.addShellBuiltins(); 108 109 while (true) { 110 String source = ""; 111 try { 112 source = in.readLine(prompt); 113 } catch (final IOException ioe) { 114 err.println(ioe.toString()); 115 if (env._dump_on_error) { 116 ioe.printStackTrace(err); 117 } 118 return IO_ERROR; 119 } catch (final UserInterruptException ex) { 120 break; 121 } 122 123 if (source.isEmpty()) { 124 continue; 125 } 126 127 try { 128 final Object res = context.eval(global, source, global, "<shell>"); 129 if (res != ScriptRuntime.UNDEFINED) { 130 err.println(JSType.toString(res)); 131 } 132 } catch (final Exception e) { 133 err.println(e); 134 if (env._dump_on_error) { 135 e.printStackTrace(err); 136 } 137 } 138 } 139 } catch (final Exception e) { 140 err.println(e); 141 if (env._dump_on_error) { 142 e.printStackTrace(err); 143 } 144 } finally { 145 if (globalChanged) { 146 Context.setGlobal(oldGlobal); 147 } 148 } 149 150 return SUCCESS; 151 } 152} 153