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 java.util.function.Consumer;
37 import jdk.internal.jline.console.completer.Completer;
38 import jdk.internal.jline.console.UserInterruptException;
39 import jdk.nashorn.api.scripting.NashornException;
40 import jdk.nashorn.internal.objects.Global;
41 import jdk.nashorn.internal.runtime.Context;
42 import jdk.nashorn.internal.runtime.JSType;
43 import jdk.nashorn.internal.runtime.Property;
44 import jdk.nashorn.internal.runtime.ScriptEnvironment;
45 import jdk.nashorn.internal.runtime.ScriptRuntime;
46 import jdk.nashorn.tools.Shell;
47
48 /**
49 * Interactive command line Shell for Nashorn.
50 */
51 public final class Main extends Shell {
52 private Main() {}
53
54 static final boolean DEBUG = Boolean.getBoolean("nashorn.jjs.debug");
55 static final boolean HEADLESS = GraphicsEnvironment.isHeadless();
56
57 // file where history is persisted.
58 private static final File HIST_FILE = new File(new File(System.getProperty("user.home")), ".jjs.history");
59
60 /**
61 * Main entry point with the default input, output and error streams.
62 *
63 * @param args The command line arguments
64 */
92 }
93
94
95 /**
96 * read-eval-print loop for Nashorn shell.
97 *
98 * @param context the nashorn context
99 * @param global global scope object to use
100 * @return return code
101 */
102 protected int readEvalPrint(final Context context, final Global global) {
103 final ScriptEnvironment env = context.getEnv();
104 final String prompt = bundle.getString("shell.prompt");
105 final String prompt2 = bundle.getString("shell.prompt2");
106 final PrintWriter err = context.getErr();
107 final Global oldGlobal = Context.getGlobal();
108 final boolean globalChanged = (oldGlobal != global);
109 final PropertiesHelper propsHelper = new PropertiesHelper(env._classpath);
110 final NashornCompleter completer = new NashornCompleter(context, global, this, propsHelper);
111
112 try (final Console in = new Console(System.in, System.out, HIST_FILE, completer)) {
113 if (globalChanged) {
114 Context.setGlobal(global);
115 }
116
117 global.addShellBuiltins();
118
119 if (System.getSecurityManager() == null) {
120 final Consumer<String> evaluator = str -> {
121 // could be called from different thread (GUI), we need to handle Context set/reset
122 final Global _oldGlobal = Context.getGlobal();
123 final boolean _globalChanged = (oldGlobal != global);
124 if (_globalChanged) {
125 Context.setGlobal(global);
126 }
127 try {
128 evalImpl(context, global, str, err, env._dump_on_error);
129 } finally {
130 if (_globalChanged) {
131 Context.setGlobal(_oldGlobal);
132 }
147 } catch (final IOException ioe) {
148 err.println(ioe.toString());
149 if (env._dump_on_error) {
150 ioe.printStackTrace(err);
151 }
152 return IO_ERROR;
153 } catch (final UserInterruptException ex) {
154 break;
155 }
156
157 if (source == null) {
158 break;
159 }
160
161 if (source.isEmpty()) {
162 continue;
163 }
164
165 try {
166 final Object res = context.eval(global, source, global, "<shell>");
167 if (res != ScriptRuntime.UNDEFINED) {
168 err.println(toString(res, global));
169 }
170 } catch (final Exception exp) {
171 // Is this a ECMAScript SyntaxError at last column (of the single line)?
172 // If so, it is because parser expected more input but got EOF. Try to
173 // to more lines from the user (multiline edit support).
174
175 if (completer.isSyntaxErrorAt(exp, 1, source.length())) {
176 final String fullSrc = completer.readMoreLines(source, exp, in, prompt2, err);
177
178 // check if we succeeded in getting complete code.
179 if (fullSrc != null && !fullSrc.isEmpty()) {
180 evalImpl(context, global, fullSrc, err, env._dump_on_error);
181 } // else ignore, error reported already by 'completer.readMoreLines'
182 } else {
183
184 // can't read more lines to have parseable/complete code.
185 err.println(exp);
186 if (env._dump_on_error) {
187 exp.printStackTrace(err);
201 try {
202 propsHelper.close();
203 } catch (final Exception exp) {
204 if (DEBUG) {
205 exp.printStackTrace();
206 }
207 }
208 }
209
210 return SUCCESS;
211 }
212
213 static String getMessage(final String id) {
214 return bundle.getString(id);
215 }
216
217 private void evalImpl(final Context context, final Global global, final String source,
218 final PrintWriter err, final boolean doe) {
219 try {
220 final Object res = context.eval(global, source, global, "<shell>");
221 if (res != ScriptRuntime.UNDEFINED) {
222 err.println(JSType.toString(res));
223 }
224 } catch (final Exception e) {
225 err.println(e);
226 if (doe) {
227 e.printStackTrace(err);
228 }
229 }
230 }
231 }
|
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 */
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 }
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);
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 }
|