changeset 32313 | 11b284f8c4c6 |
parent 32245 | 80164edf8a10 |
child 32314 | 8f7d23d3b1ad |
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java Wed Jul 05 20:46:39 2017 +0200 +++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java Fri Aug 21 18:01:23 2015 +0530 @@ -38,6 +38,7 @@ import jdk.nashorn.internal.objects.Global; import jdk.nashorn.internal.runtime.Context; import jdk.nashorn.internal.runtime.JSType; +import jdk.nashorn.internal.runtime.Property; import jdk.nashorn.internal.runtime.ScriptEnvironment; import jdk.nashorn.internal.runtime.ScriptRuntime; import jdk.nashorn.tools.Shell; @@ -107,8 +108,29 @@ } global.addShellBuiltins(); - // expose history object for reflecting on command line history - global.put("history", new HistoryObject(in.getHistory()), false); + + if (System.getSecurityManager() == null) { + // expose history object for reflecting on command line history + global.addOwnProperty("history", Property.NOT_ENUMERABLE, new HistoryObject(in.getHistory())); + + // 'edit' command + global.addOwnProperty("edit", Property.NOT_ENUMERABLE, new EditObject(err::println, + str -> { + // could be called from different thread (GUI), we need to handle Context set/reset + final Global _oldGlobal = Context.getGlobal(); + final boolean _globalChanged = (oldGlobal != global); + if (_globalChanged) { + Context.setGlobal(global); + } + try { + evalImpl(context, global, str, err, env._dump_on_error); + } finally { + if (_globalChanged) { + Context.setGlobal(_oldGlobal); + } + } + }, in)); + } while (true) { String source = ""; @@ -128,17 +150,7 @@ continue; } - try { - final Object res = context.eval(global, source, global, "<shell>"); - if (res != ScriptRuntime.UNDEFINED) { - err.println(JSType.toString(res)); - } - } catch (final Exception e) { - err.println(e); - if (env._dump_on_error) { - e.printStackTrace(err); - } - } + evalImpl(context, global, source, err, env._dump_on_error); } } catch (final Exception e) { err.println(e); @@ -153,4 +165,19 @@ return SUCCESS; } + + private void evalImpl(final Context context, final Global global, final String source, + final PrintWriter err, final boolean doe) { + try { + final Object res = context.eval(global, source, global, "<shell>"); + if (res != ScriptRuntime.UNDEFINED) { + err.println(JSType.toString(res)); + } + } catch (final Exception e) { + err.println(e); + if (doe) { + e.printStackTrace(err); + } + } + } }