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