28 import java.io.BufferedReader; 29 import java.io.File; 30 import java.io.InputStream; 31 import java.io.InputStreamReader; 32 import java.io.IOException; 33 import java.io.OutputStream; 34 import java.io.PrintWriter; 35 import jdk.internal.jline.console.completer.Completer; 36 import jdk.internal.jline.console.UserInterruptException; 37 import jdk.nashorn.api.scripting.NashornException; 38 import jdk.nashorn.internal.objects.Global; 39 import jdk.nashorn.internal.runtime.Context; 40 import jdk.nashorn.internal.runtime.JSType; 41 import jdk.nashorn.internal.runtime.ScriptEnvironment; 42 import jdk.nashorn.internal.runtime.ScriptRuntime; 43 import jdk.nashorn.tools.Shell; 44 45 /** 46 * Interactive command line Shell for Nashorn. 47 */ 48 public final class Main extends Shell { 49 private Main() {} 50 51 // file where history is persisted. 52 private static final File HIST_FILE = new File(new File(System.getProperty("user.home")), ".jjs.history"); 53 54 /** 55 * Main entry point with the default input, output and error streams. 56 * 57 * @param args The command line arguments 58 */ 59 public static void main(final String[] args) { 60 try { 61 final int exitCode = main(System.in, System.out, System.err, args); 62 if (exitCode != SUCCESS) { 63 System.exit(exitCode); 64 } 65 } catch (final IOException e) { 66 System.err.println(e); //bootstrapping, Context.err may not exist 67 System.exit(IO_ERROR); 68 } 69 } 70 71 /** 72 * Starting point for executing a {@code Shell}. Starts a shell with the 73 * given arguments and streams and lets it run until exit. 74 * 75 * @param in input stream for Shell 76 * @param out output stream for Shell 77 * @param err error stream for Shell 78 * @param args arguments to Shell 79 * 80 * @return exit code 81 * 82 * @throws IOException if there's a problem setting up the streams 83 */ 84 public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { 85 return new Main().run(in, out, err, args); 86 } 87 88 /** 89 * read-eval-print loop for Nashorn shell. 90 * 91 * @param context the nashorn context 92 * @param global global scope object to use 93 * @return return code 94 */ 95 protected int readEvalPrint(final Context context, final Global global) { 96 final ScriptEnvironment env = context.getEnv(); 97 final String prompt = bundle.getString("shell.prompt"); 98 final PrintWriter err = context.getErr(); 99 final Global oldGlobal = Context.getGlobal(); 100 final boolean globalChanged = (oldGlobal != global); 101 final Completer completer = new NashornCompleter(context, global); 102 103 try (final Console in = new Console(System.in, System.out, HIST_FILE, completer)) { 104 if (globalChanged) { 105 Context.setGlobal(global); 106 } 107 108 global.addShellBuiltins(); 109 // expose history object for reflecting on command line history 110 global.put("history", new HistoryObject(in.getHistory()), false); 111 112 while (true) { 113 String source = ""; 114 try { 115 source = in.readLine(prompt); 116 } catch (final IOException ioe) { 117 err.println(ioe.toString()); 118 if (env._dump_on_error) { 119 ioe.printStackTrace(err); 120 } 121 return IO_ERROR; 134 } 135 } catch (final Exception e) { 136 err.println(e); 137 if (env._dump_on_error) { 138 e.printStackTrace(err); 139 } 140 } 141 } 142 } catch (final Exception e) { 143 err.println(e); 144 if (env._dump_on_error) { 145 e.printStackTrace(err); 146 } 147 } finally { 148 if (globalChanged) { 149 Context.setGlobal(oldGlobal); 150 } 151 } 152 153 return SUCCESS; 154 } 155 } | 28 import java.io.BufferedReader; 29 import java.io.File; 30 import java.io.InputStream; 31 import java.io.InputStreamReader; 32 import java.io.IOException; 33 import java.io.OutputStream; 34 import java.io.PrintWriter; 35 import jdk.internal.jline.console.completer.Completer; 36 import jdk.internal.jline.console.UserInterruptException; 37 import jdk.nashorn.api.scripting.NashornException; 38 import jdk.nashorn.internal.objects.Global; 39 import jdk.nashorn.internal.runtime.Context; 40 import jdk.nashorn.internal.runtime.JSType; 41 import jdk.nashorn.internal.runtime.ScriptEnvironment; 42 import jdk.nashorn.internal.runtime.ScriptRuntime; 43 import jdk.nashorn.tools.Shell; 44 45 /** 46 * Interactive command line Shell for Nashorn. 47 */ 48 public final class Main extends Shell implements PartialParser { 49 private Main() {} 50 51 // file where history is persisted. 52 private static final File HIST_FILE = new File(new File(System.getProperty("user.home")), ".jjs.history"); 53 54 /** 55 * Main entry point with the default input, output and error streams. 56 * 57 * @param args The command line arguments 58 */ 59 public static void main(final String[] args) { 60 try { 61 final int exitCode = main(System.in, System.out, System.err, args); 62 if (exitCode != SUCCESS) { 63 System.exit(exitCode); 64 } 65 } catch (final IOException e) { 66 System.err.println(e); //bootstrapping, Context.err may not exist 67 System.exit(IO_ERROR); 68 } 69 } 70 71 /** 72 * Starting point for executing a {@code Shell}. Starts a shell with the 73 * given arguments and streams and lets it run until exit. 74 * 75 * @param in input stream for Shell 76 * @param out output stream for Shell 77 * @param err error stream for Shell 78 * @param args arguments to Shell 79 * 80 * @return exit code 81 * 82 * @throws IOException if there's a problem setting up the streams 83 */ 84 public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { 85 return new Main().run(in, out, err, args); 86 } 87 88 89 /** 90 * read-eval-print loop for Nashorn shell. 91 * 92 * @param context the nashorn context 93 * @param global global scope object to use 94 * @return return code 95 */ 96 protected int readEvalPrint(final Context context, final Global global) { 97 final ScriptEnvironment env = context.getEnv(); 98 final String prompt = bundle.getString("shell.prompt"); 99 final PrintWriter err = context.getErr(); 100 final Global oldGlobal = Context.getGlobal(); 101 final boolean globalChanged = (oldGlobal != global); 102 final Completer completer = new NashornCompleter(context, global, this); 103 104 try (final Console in = new Console(System.in, System.out, HIST_FILE, completer)) { 105 if (globalChanged) { 106 Context.setGlobal(global); 107 } 108 109 global.addShellBuiltins(); 110 // expose history object for reflecting on command line history 111 global.put("history", new HistoryObject(in.getHistory()), false); 112 113 while (true) { 114 String source = ""; 115 try { 116 source = in.readLine(prompt); 117 } catch (final IOException ioe) { 118 err.println(ioe.toString()); 119 if (env._dump_on_error) { 120 ioe.printStackTrace(err); 121 } 122 return IO_ERROR; 135 } 136 } catch (final Exception e) { 137 err.println(e); 138 if (env._dump_on_error) { 139 e.printStackTrace(err); 140 } 141 } 142 } 143 } catch (final Exception e) { 144 err.println(e); 145 if (env._dump_on_error) { 146 e.printStackTrace(err); 147 } 148 } finally { 149 if (globalChanged) { 150 Context.setGlobal(oldGlobal); 151 } 152 } 153 154 return SUCCESS; 155 } 156 157 // implement PartialParser method by invoking superclass' protected method! 158 @Override 159 public int getLastExpressionStart(final Context context, final String code) { 160 return super.getLastExpressionStart(context, code); 161 } 162 } |