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 26 package jdk.nashorn.tools.jjs; 27 28 import java.awt.GraphicsEnvironment; 29 import java.io.BufferedReader; 30 import java.io.File; 31 import java.io.InputStream; 32 import java.io.InputStreamReader; 33 import java.io.IOException; 34 import java.io.OutputStream; 35 import java.io.PrintWriter; 36 import jdk.internal.jline.console.completer.Completer; 37 import jdk.internal.jline.console.UserInterruptException; 38 import jdk.nashorn.api.scripting.NashornException; 39 import jdk.nashorn.internal.objects.Global; 40 import jdk.nashorn.internal.runtime.Context; 41 import jdk.nashorn.internal.runtime.JSType; 42 import jdk.nashorn.internal.runtime.Property; 43 import jdk.nashorn.internal.runtime.ScriptEnvironment; 44 import jdk.nashorn.internal.runtime.ScriptRuntime; 45 import jdk.nashorn.tools.Shell; 46 47 /** 48 * Interactive command line Shell for Nashorn. 49 */ 50 public final class Main extends Shell { 51 private Main() {} 52 53 static final boolean DEBUG = Boolean.getBoolean("nashorn.jjs.debug"); 54 static final boolean HEADLESS = GraphicsEnvironment.isHeadless(); 55 56 // file where history is persisted. 57 private static final File HIST_FILE = new File(new File(System.getProperty("user.home")), ".jjs.history"); 58 59 /** 60 * Main entry point with the default input, output and error streams. 61 * 62 * @param args The command line arguments 63 */ 64 public static void main(final String[] args) { 65 try { 66 final int exitCode = main(System.in, System.out, System.err, args); 67 if (exitCode != SUCCESS) { 68 System.exit(exitCode); 69 } 70 } catch (final IOException e) { 71 System.err.println(e); //bootstrapping, Context.err may not exist 72 System.exit(IO_ERROR); 73 } 74 } 75 76 /** 77 * Starting point for executing a {@code Shell}. Starts a shell with the 78 * given arguments and streams and lets it run until exit. 79 * 80 * @param in input stream for Shell 81 * @param out output stream for Shell 82 * @param err error stream for Shell 83 * @param args arguments to Shell 84 * 85 * @return exit code 86 * 87 * @throws IOException if there's a problem setting up the streams 88 */ 89 public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { 90 return new Main().run(in, out, err, args); 91 } 92 93 94 /** 95 * read-eval-print loop for Nashorn shell. 96 * 97 * @param context the nashorn context 98 * @param global global scope object to use 99 * @return return code 100 */ 101 protected int readEvalPrint(final Context context, final Global global) { 102 final ScriptEnvironment env = context.getEnv(); 103 final String prompt = bundle.getString("shell.prompt"); 104 final String prompt2 = bundle.getString("shell.prompt2"); 105 final PrintWriter err = context.getErr(); 106 final Global oldGlobal = Context.getGlobal(); 107 final boolean globalChanged = (oldGlobal != global); 108 final PropertiesHelper propsHelper = new PropertiesHelper(env._classpath); 109 final NashornCompleter completer = new NashornCompleter(context, global, this, propsHelper); 110 111 try (final Console in = new Console(System.in, System.out, HIST_FILE, completer)) { 112 if (globalChanged) { 113 Context.setGlobal(global); 114 } 115 116 global.addShellBuiltins(); 117 118 if (System.getSecurityManager() == null) { 119 // expose history object for reflecting on command line history 120 global.addOwnProperty("history", Property.NOT_ENUMERABLE, new HistoryObject(in.getHistory())); 121 122 // 'edit' command 123 global.addOwnProperty("edit", Property.NOT_ENUMERABLE, new EditObject(err::println, 124 str -> { 125 // could be called from different thread (GUI), we need to handle Context set/reset 126 final Global _oldGlobal = Context.getGlobal(); 127 final boolean _globalChanged = (oldGlobal != global); 128 if (_globalChanged) { 129 Context.setGlobal(global); 130 } 131 try { 132 evalImpl(context, global, str, err, env._dump_on_error); 133 } finally { 134 if (_globalChanged) { 135 Context.setGlobal(_oldGlobal); 136 } 137 } 138 }, in)); 139 } 140 141 while (true) { 142 String source = ""; 143 try { 144 source = in.readLine(prompt); 145 } catch (final IOException ioe) { 146 err.println(ioe.toString()); 147 if (env._dump_on_error) { 148 ioe.printStackTrace(err); 149 } 150 return IO_ERROR; 151 } catch (final UserInterruptException ex) { 152 break; 153 } 154 155 if (source.isEmpty()) { 156 continue; 157 } 158 159 try { 160 final Object res = context.eval(global, source, global, "<shell>"); 161 if (res != ScriptRuntime.UNDEFINED) { 162 err.println(JSType.toString(res)); 163 } 164 } catch (final Exception exp) { 165 // Is this a ECMAScript SyntaxError at last column (of the single line)? 166 // If so, it is because parser expected more input but got EOF. Try to 167 // to more lines from the user (multiline edit support). 168 169 if (completer.isSyntaxErrorAt(exp, 1, source.length())) { 170 final String fullSrc = completer.readMoreLines(source, exp, in, prompt2, err); 171 172 // check if we succeeded in getting complete code. 173 if (fullSrc != null && !fullSrc.isEmpty()) { 174 evalImpl(context, global, fullSrc, err, env._dump_on_error); 175 } // else ignore, error reported already by 'completer.readMoreLines' 176 } else { 177 178 // can't read more lines to have parseable/complete code. 179 err.println(exp); 180 if (env._dump_on_error) { 181 exp.printStackTrace(err); 182 } 183 } 184 } 185 } 186 } catch (final Exception e) { 187 err.println(e); 188 if (env._dump_on_error) { 189 e.printStackTrace(err); 190 } 191 } finally { 192 if (globalChanged) { 193 Context.setGlobal(oldGlobal); 194 } 195 try { 196 propsHelper.close(); 197 } catch (final Exception exp) { 198 if (DEBUG) { 199 exp.printStackTrace(); 200 } 201 } 202 } 203 204 return SUCCESS; 205 } 206 207 static String getMessage(final String id) { 208 return bundle.getString(id); 209 } 210 211 private void evalImpl(final Context context, final Global global, final String source, 212 final PrintWriter err, final boolean doe) { 213 try { 214 final Object res = context.eval(global, source, global, "<shell>"); 215 if (res != ScriptRuntime.UNDEFINED) { 216 err.println(JSType.toString(res)); 217 } 218 } catch (final Exception e) { 219 err.println(e); 220 if (doe) { 221 e.printStackTrace(err); 222 } 223 } 224 } 225 }