< prev index next >

src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java

Print this page




  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 }


  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     // This override exists precisely give access to this method to NashornCompleter!
 158     @Override
 159     protected int getLastExpressionStart(final Context context, final String code) {
 160         return super.getLastExpressionStart(context, code);
 161     }
 162 }
< prev index next >