author | sundar |
Thu, 13 Aug 2015 19:07:27 +0530 | |
changeset 32152 | 705d4af0d4d7 |
parent 31549 | b627094c5649 |
child 32245 | 80164edf8a10 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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; |
|
27 |
||
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
28 |
import static jdk.nashorn.internal.runtime.Source.sourceFor; |
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
29 |
|
16147 | 30 |
import java.io.BufferedReader; |
31 |
import java.io.File; |
|
32 |
import java.io.FileReader; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStream; |
|
35 |
import java.io.InputStreamReader; |
|
36 |
import java.io.OutputStream; |
|
37 |
import java.io.PrintStream; |
|
38 |
import java.io.PrintWriter; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Locale; |
|
41 |
import java.util.ResourceBundle; |
|
42 |
import jdk.nashorn.api.scripting.NashornException; |
|
43 |
import jdk.nashorn.internal.codegen.Compiler; |
|
24759
31aed7d9c02a
8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents:
24751
diff
changeset
|
44 |
import jdk.nashorn.internal.codegen.Compiler.CompilationPhases; |
16233
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16230
diff
changeset
|
45 |
import jdk.nashorn.internal.ir.FunctionNode; |
17233 | 46 |
import jdk.nashorn.internal.ir.debug.ASTWriter; |
47 |
import jdk.nashorn.internal.ir.debug.PrintVisitor; |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
48 |
import jdk.nashorn.internal.objects.Global; |
16233
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16230
diff
changeset
|
49 |
import jdk.nashorn.internal.parser.Parser; |
16147 | 50 |
import jdk.nashorn.internal.runtime.Context; |
51 |
import jdk.nashorn.internal.runtime.ErrorManager; |
|
18851
bdb92c95f886
8019947: inherited property invalidation does not work with two globals in same context
sundar
parents:
17524
diff
changeset
|
52 |
import jdk.nashorn.internal.runtime.JSType; |
17244 | 53 |
import jdk.nashorn.internal.runtime.Property; |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
54 |
import jdk.nashorn.internal.runtime.ScriptEnvironment; |
16147 | 55 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
56 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
57 |
import jdk.nashorn.internal.runtime.options.Options; |
|
58 |
||
59 |
/** |
|
60 |
* Command line Shell for processing JavaScript files. |
|
61 |
*/ |
|
62 |
public class Shell { |
|
63 |
||
64 |
/** |
|
65 |
* Resource name for properties file |
|
66 |
*/ |
|
67 |
private static final String MESSAGE_RESOURCE = "jdk.nashorn.tools.resources.Shell"; |
|
68 |
/** |
|
69 |
* Shell message bundle. |
|
70 |
*/ |
|
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
31549
diff
changeset
|
71 |
protected static final ResourceBundle bundle = ResourceBundle.getBundle(MESSAGE_RESOURCE, Locale.getDefault()); |
16147 | 72 |
|
73 |
/** |
|
74 |
* Exit code for command line tool - successful |
|
75 |
*/ |
|
76 |
public static final int SUCCESS = 0; |
|
77 |
/** |
|
78 |
* Exit code for command line tool - error on command line |
|
79 |
*/ |
|
80 |
public static final int COMMANDLINE_ERROR = 100; |
|
81 |
/** |
|
82 |
* Exit code for command line tool - error compiling script |
|
83 |
*/ |
|
84 |
public static final int COMPILATION_ERROR = 101; |
|
85 |
/** |
|
86 |
* Exit code for command line tool - error during runtime |
|
87 |
*/ |
|
88 |
public static final int RUNTIME_ERROR = 102; |
|
89 |
/** |
|
90 |
* Exit code for command line tool - i/o error |
|
91 |
*/ |
|
92 |
public static final int IO_ERROR = 103; |
|
93 |
/** |
|
94 |
* Exit code for command line tool - internal error |
|
95 |
*/ |
|
96 |
public static final int INTERNAL_ERROR = 104; |
|
97 |
||
98 |
/** |
|
99 |
* Constructor |
|
100 |
*/ |
|
101 |
protected Shell() { |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Main entry point with the default input, output and error streams. |
|
106 |
* |
|
107 |
* @param args The command line arguments |
|
108 |
*/ |
|
109 |
public static void main(final String[] args) { |
|
110 |
try { |
|
29544
8151437996be
8075604: jjs exits even when non-daemon threads are still active
sundar
parents:
26503
diff
changeset
|
111 |
final int exitCode = main(System.in, System.out, System.err, args); |
8151437996be
8075604: jjs exits even when non-daemon threads are still active
sundar
parents:
26503
diff
changeset
|
112 |
if (exitCode != SUCCESS) { |
8151437996be
8075604: jjs exits even when non-daemon threads are still active
sundar
parents:
26503
diff
changeset
|
113 |
System.exit(exitCode); |
8151437996be
8075604: jjs exits even when non-daemon threads are still active
sundar
parents:
26503
diff
changeset
|
114 |
} |
16147 | 115 |
} catch (final IOException e) { |
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
116 |
System.err.println(e); //bootstrapping, Context.err may not exist |
16147 | 117 |
System.exit(IO_ERROR); |
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Starting point for executing a {@code Shell}. Starts a shell with the |
|
123 |
* given arguments and streams and lets it run until exit. |
|
124 |
* |
|
125 |
* @param in input stream for Shell |
|
126 |
* @param out output stream for Shell |
|
127 |
* @param err error stream for Shell |
|
128 |
* @param args arguments to Shell |
|
129 |
* |
|
130 |
* @return exit code |
|
131 |
* |
|
132 |
* @throws IOException if there's a problem setting up the streams |
|
133 |
*/ |
|
134 |
public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { |
|
135 |
return new Shell().run(in, out, err, args); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Run method logic. |
|
140 |
* |
|
141 |
* @param in input stream for Shell |
|
142 |
* @param out output stream for Shell |
|
143 |
* @param err error stream for Shell |
|
144 |
* @param args arguments to Shell |
|
145 |
* |
|
146 |
* @return exit code |
|
147 |
* |
|
148 |
* @throws IOException if there's a problem setting up the streams |
|
149 |
*/ |
|
16185 | 150 |
protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException { |
16147 | 151 |
final Context context = makeContext(in, out, err, args); |
152 |
if (context == null) { |
|
153 |
return COMMANDLINE_ERROR; |
|
154 |
} |
|
155 |
||
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
156 |
final Global global = context.createGlobal(); |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
157 |
final ScriptEnvironment env = context.getEnv(); |
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
158 |
final List<String> files = env.getFiles(); |
16147 | 159 |
if (files.isEmpty()) { |
16185 | 160 |
return readEvalPrint(context, global); |
16147 | 161 |
} |
162 |
||
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
163 |
if (env._compile_only) { |
16185 | 164 |
return compileScripts(context, global, files); |
16147 | 165 |
} |
166 |
||
17244 | 167 |
if (env._fx) { |
168 |
return runFXScripts(context, global, files); |
|
169 |
} |
|
17519
19c9d2553e35
8013871: mem usage histograms enabled with compiler logging level set to more specific than or equals to info when --print-mem-usage flag is used
lagergren
parents:
17518
diff
changeset
|
170 |
|
19c9d2553e35
8013871: mem usage histograms enabled with compiler logging level set to more specific than or equals to info when --print-mem-usage flag is used
lagergren
parents:
17518
diff
changeset
|
171 |
return runScripts(context, global, files); |
16147 | 172 |
} |
173 |
||
174 |
/** |
|
175 |
* Make a new Nashorn Context to compile and/or run JavaScript files. |
|
176 |
* |
|
177 |
* @param in input stream for Shell |
|
178 |
* @param out output stream for Shell |
|
179 |
* @param err error stream for Shell |
|
180 |
* @param args arguments to Shell |
|
181 |
* |
|
182 |
* @return null if there are problems with option parsing. |
|
183 |
*/ |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
184 |
private static Context makeContext(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) { |
16147 | 185 |
final PrintStream pout = out instanceof PrintStream ? (PrintStream) out : new PrintStream(out); |
186 |
final PrintStream perr = err instanceof PrintStream ? (PrintStream) err : new PrintStream(err); |
|
187 |
final PrintWriter wout = new PrintWriter(pout, true); |
|
188 |
final PrintWriter werr = new PrintWriter(perr, true); |
|
189 |
||
190 |
// Set up error handler. |
|
191 |
final ErrorManager errors = new ErrorManager(werr); |
|
192 |
// Set up options. |
|
193 |
final Options options = new Options("nashorn", werr); |
|
194 |
||
195 |
// parse options |
|
16241
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
196 |
if (args != null) { |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
197 |
try { |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
198 |
options.process(args); |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
199 |
} catch (final IllegalArgumentException e) { |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
200 |
werr.println(bundle.getString("shell.usage")); |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
201 |
options.displayHelp(e); |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
202 |
return null; |
e23ed61e75a2
8008291: Add more tests for better coverage of objects, scripting and parser packages
sundar
parents:
16233
diff
changeset
|
203 |
} |
16147 | 204 |
} |
205 |
||
206 |
// detect scripting mode by any source's first character being '#' |
|
207 |
if (!options.getBoolean("scripting")) { |
|
208 |
for (final String fileName : options.getFiles()) { |
|
209 |
final File firstFile = new File(fileName); |
|
210 |
if (firstFile.isFile()) { |
|
211 |
try (final FileReader fr = new FileReader(firstFile)) { |
|
212 |
final int firstChar = fr.read(); |
|
213 |
// starts with '# |
|
214 |
if (firstChar == '#') { |
|
215 |
options.set("scripting", true); |
|
216 |
break; |
|
217 |
} |
|
218 |
} catch (final IOException e) { |
|
219 |
// ignore this. File IO errors will be reported later anyway |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
||
16185 | 225 |
return new Context(options, errors, wout, werr, Thread.currentThread().getContextClassLoader()); |
16147 | 226 |
} |
227 |
||
228 |
/** |
|
229 |
* Compiles the given script files in the command line |
|
24719 | 230 |
* This is called only when using the --compile-only flag |
16147 | 231 |
* |
16185 | 232 |
* @param context the nashorn context |
16147 | 233 |
* @param global the global scope |
234 |
* @param files the list of script files to compile |
|
235 |
* |
|
236 |
* @return error code |
|
237 |
* @throws IOException when any script file read results in I/O error |
|
238 |
*/ |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
239 |
private static int compileScripts(final Context context, final Global global, final List<String> files) throws IOException { |
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
240 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 241 |
final boolean globalChanged = (oldGlobal != global); |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
242 |
final ScriptEnvironment env = context.getEnv(); |
16147 | 243 |
try { |
244 |
if (globalChanged) { |
|
245 |
Context.setGlobal(global); |
|
246 |
} |
|
16185 | 247 |
final ErrorManager errors = context.getErrorManager(); |
16147 | 248 |
|
249 |
// For each file on the command line. |
|
250 |
for (final String fileName : files) { |
|
26503 | 251 |
final FunctionNode functionNode = new Parser(env, sourceFor(fileName, new File(fileName)), errors, env._strict, 0, context.getLogger(Parser.class)).parse(); |
16233
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16230
diff
changeset
|
252 |
|
16147 | 253 |
if (errors.getNumberOfErrors() != 0) { |
254 |
return COMPILATION_ERROR; |
|
255 |
} |
|
16233
95d3e01c04c3
8008199: Lazy compilation and trampoline implementation
lagergren
parents:
16230
diff
changeset
|
256 |
|
26377
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
257 |
new Compiler( |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
258 |
context, |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
259 |
env, |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
260 |
null, //null - pass no code installer - this is compile only |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
261 |
functionNode.getSource(), |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
262 |
context.getErrorManager(), |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
263 |
env._strict | functionNode.isStrict()). |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
264 |
compile(functionNode, CompilationPhases.COMPILE_ALL_NO_INSTALL); |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
265 |
|
17233 | 266 |
if (env._print_ast) { |
267 |
context.getErr().println(new ASTWriter(functionNode)); |
|
268 |
} |
|
269 |
||
270 |
if (env._print_parse) { |
|
271 |
context.getErr().println(new PrintVisitor(functionNode)); |
|
272 |
} |
|
273 |
||
26377
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
274 |
if (errors.getNumberOfErrors() != 0) { |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
275 |
return COMPILATION_ERROR; |
028dad61662f
8051889: Implement block scoping in symbol assignment and scope computation
hannesw
parents:
26068
diff
changeset
|
276 |
} |
16147 | 277 |
} |
278 |
} finally { |
|
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
279 |
env.getOut().flush(); |
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
280 |
env.getErr().flush(); |
16147 | 281 |
if (globalChanged) { |
282 |
Context.setGlobal(oldGlobal); |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
return SUCCESS; |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* Runs the given JavaScript files in the command line |
|
291 |
* |
|
16185 | 292 |
* @param context the nashorn context |
16147 | 293 |
* @param global the global scope |
294 |
* @param files the list of script files to run |
|
295 |
* |
|
296 |
* @return error code |
|
297 |
* @throws IOException when any script file read results in I/O error |
|
298 |
*/ |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
299 |
private int runScripts(final Context context, final Global global, final List<String> files) throws IOException { |
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
300 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 301 |
final boolean globalChanged = (oldGlobal != global); |
302 |
try { |
|
303 |
if (globalChanged) { |
|
304 |
Context.setGlobal(global); |
|
305 |
} |
|
16185 | 306 |
final ErrorManager errors = context.getErrorManager(); |
16147 | 307 |
|
308 |
// For each file on the command line. |
|
309 |
for (final String fileName : files) { |
|
19473
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
310 |
if ("-".equals(fileName)) { |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
311 |
final int res = readEvalPrint(context, global); |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
312 |
if (res != SUCCESS) { |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
313 |
return res; |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
314 |
} |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
315 |
continue; |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
316 |
} |
8b2ee6bf9c62
8023210: jjs tools should support a mode where it will load few command line scripts and then entering into interactive shell
sundar
parents:
19459
diff
changeset
|
317 |
|
16147 | 318 |
final File file = new File(fileName); |
24206
40c6d45af73f
8040078: Avoid repeated reading of source for cached loads
hannesw
parents:
23375
diff
changeset
|
319 |
final ScriptFunction script = context.compileScript(sourceFor(fileName, file), global); |
16147 | 320 |
if (script == null || errors.getNumberOfErrors() != 0) { |
321 |
return COMPILATION_ERROR; |
|
322 |
} |
|
323 |
||
324 |
try { |
|
325 |
apply(script, global); |
|
326 |
} catch (final NashornException e) { |
|
327 |
errors.error(e.toString()); |
|
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
328 |
if (context.getEnv()._dump_on_error) { |
16147 | 329 |
e.printStackTrace(context.getErr()); |
330 |
} |
|
331 |
||
332 |
return RUNTIME_ERROR; |
|
333 |
} |
|
334 |
} |
|
335 |
} finally { |
|
336 |
context.getOut().flush(); |
|
337 |
context.getErr().flush(); |
|
338 |
if (globalChanged) { |
|
339 |
Context.setGlobal(oldGlobal); |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
return SUCCESS; |
|
344 |
} |
|
345 |
||
346 |
/** |
|
17244 | 347 |
* Runs launches "fx:bootstrap.js" with the given JavaScript files provided |
348 |
* as arguments. |
|
349 |
* |
|
350 |
* @param context the nashorn context |
|
351 |
* @param global the global scope |
|
352 |
* @param files the list of script files to provide |
|
353 |
* |
|
354 |
* @return error code |
|
355 |
* @throws IOException when any script file read results in I/O error |
|
356 |
*/ |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
357 |
private static int runFXScripts(final Context context, final Global global, final List<String> files) throws IOException { |
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
358 |
final Global oldGlobal = Context.getGlobal(); |
17244 | 359 |
final boolean globalChanged = (oldGlobal != global); |
360 |
try { |
|
361 |
if (globalChanged) { |
|
362 |
Context.setGlobal(global); |
|
363 |
} |
|
364 |
||
365 |
global.addOwnProperty("$GLOBAL", Property.NOT_ENUMERABLE, global); |
|
366 |
global.addOwnProperty("$SCRIPTS", Property.NOT_ENUMERABLE, files); |
|
367 |
context.load(global, "fx:bootstrap.js"); |
|
368 |
} catch (final NashornException e) { |
|
369 |
context.getErrorManager().error(e.toString()); |
|
370 |
if (context.getEnv()._dump_on_error) { |
|
371 |
e.printStackTrace(context.getErr()); |
|
372 |
} |
|
373 |
||
374 |
return RUNTIME_ERROR; |
|
375 |
} finally { |
|
376 |
context.getOut().flush(); |
|
377 |
context.getErr().flush(); |
|
378 |
if (globalChanged) { |
|
379 |
Context.setGlobal(oldGlobal); |
|
380 |
} |
|
381 |
} |
|
382 |
||
383 |
return SUCCESS; |
|
384 |
} |
|
385 |
||
386 |
/** |
|
16147 | 387 |
* Hook to ScriptFunction "apply". A performance metering shell may |
388 |
* introduce enter/exit timing here. |
|
389 |
* |
|
390 |
* @param target target function for apply |
|
391 |
* @param self self reference for apply |
|
392 |
* |
|
393 |
* @return result of the function apply |
|
394 |
*/ |
|
395 |
protected Object apply(final ScriptFunction target, final Object self) { |
|
396 |
return ScriptRuntime.apply(target, self); |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* read-eval-print loop for Nashorn shell. |
|
401 |
* |
|
16185 | 402 |
* @param context the nashorn context |
16147 | 403 |
* @param global global scope object to use |
404 |
* @return return code |
|
405 |
*/ |
|
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
31549
diff
changeset
|
406 |
protected int readEvalPrint(final Context context, final Global global) { |
16147 | 407 |
final String prompt = bundle.getString("shell.prompt"); |
408 |
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
|
409 |
final PrintWriter err = context.getErr(); |
|
23375
a1110f2cbe75
8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents:
19634
diff
changeset
|
410 |
final Global oldGlobal = Context.getGlobal(); |
16147 | 411 |
final boolean globalChanged = (oldGlobal != global); |
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
412 |
final ScriptEnvironment env = context.getEnv(); |
16147 | 413 |
|
414 |
try { |
|
415 |
if (globalChanged) { |
|
416 |
Context.setGlobal(global); |
|
417 |
} |
|
418 |
||
29624 | 419 |
global.addShellBuiltins(); |
16147 | 420 |
|
421 |
while (true) { |
|
422 |
err.print(prompt); |
|
423 |
err.flush(); |
|
424 |
||
425 |
String source = ""; |
|
426 |
try { |
|
427 |
source = in.readLine(); |
|
428 |
} catch (final IOException ioe) { |
|
429 |
err.println(ioe.toString()); |
|
430 |
} |
|
431 |
||
432 |
if (source == null) { |
|
433 |
break; |
|
434 |
} |
|
435 |
||
18855
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18851
diff
changeset
|
436 |
if (source.isEmpty()) { |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18851
diff
changeset
|
437 |
continue; |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18851
diff
changeset
|
438 |
} |
408663ef8f66
8020015: shared PropertyMaps should not be used without duplication
sundar
parents:
18851
diff
changeset
|
439 |
|
16147 | 440 |
try { |
31192
1e019aeea9b5
8087211: Indirect evals should be strict with -strict option
sundar
parents:
29624
diff
changeset
|
441 |
final Object res = context.eval(global, source, global, "<shell>"); |
26063
98eee061a92a
8055034: jjs exits interactive mode if exception was thrown when trying to print value of last evaluated expression
sundar
parents:
26055
diff
changeset
|
442 |
if (res != ScriptRuntime.UNDEFINED) { |
98eee061a92a
8055034: jjs exits interactive mode if exception was thrown when trying to print value of last evaluated expression
sundar
parents:
26055
diff
changeset
|
443 |
err.println(JSType.toString(res)); |
98eee061a92a
8055034: jjs exits interactive mode if exception was thrown when trying to print value of last evaluated expression
sundar
parents:
26055
diff
changeset
|
444 |
} |
16147 | 445 |
} catch (final Exception e) { |
446 |
err.println(e); |
|
16262
75513555e603
8008731: Separate configuration environment (options, error/output writer etc.) from Context
sundar
parents:
16241
diff
changeset
|
447 |
if (env._dump_on_error) { |
16147 | 448 |
e.printStackTrace(err); |
449 |
} |
|
450 |
} |
|
451 |
} |
|
452 |
} finally { |
|
453 |
if (globalChanged) { |
|
24282
2e3bd98c5664
8041697: CompiledScript slower when eval with binding
sundar
parents:
24206
diff
changeset
|
454 |
Context.setGlobal(oldGlobal); |
16147 | 455 |
} |
456 |
} |
|
457 |
||
458 |
return SUCCESS; |
|
459 |
} |
|
460 |
} |