26 package jdk.nashorn.tools;
27
28 import static jdk.nashorn.internal.runtime.Source.sourceFor;
29
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;
44 import jdk.nashorn.internal.codegen.Compiler.CompilationPhases;
45 import jdk.nashorn.internal.ir.FunctionNode;
46 import jdk.nashorn.internal.ir.debug.ASTWriter;
47 import jdk.nashorn.internal.ir.debug.PrintVisitor;
48 import jdk.nashorn.internal.objects.Global;
49 import jdk.nashorn.internal.parser.Parser;
50 import jdk.nashorn.internal.runtime.Context;
51 import jdk.nashorn.internal.runtime.ErrorManager;
52 import jdk.nashorn.internal.runtime.JSType;
53 import jdk.nashorn.internal.runtime.Property;
54 import jdk.nashorn.internal.runtime.ScriptEnvironment;
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
378 if (globalChanged) {
379 Context.setGlobal(oldGlobal);
380 }
381 }
382
383 return SUCCESS;
384 }
385
386 /**
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 *
402 * @param context the nashorn context
403 * @param global global scope object to use
404 * @return return code
405 */
406 protected int readEvalPrint(final Context context, final Global global) {
407 final String prompt = bundle.getString("shell.prompt");
408 final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
409 final PrintWriter err = context.getErr();
410 final Global oldGlobal = Context.getGlobal();
411 final boolean globalChanged = (oldGlobal != global);
412 final ScriptEnvironment env = context.getEnv();
413
414 try {
415 if (globalChanged) {
416 Context.setGlobal(global);
417 }
|
26 package jdk.nashorn.tools;
27
28 import static jdk.nashorn.internal.runtime.Source.sourceFor;
29
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;
44 import jdk.nashorn.internal.codegen.Compiler.CompilationPhases;
45 import jdk.nashorn.internal.ir.FunctionNode;
46 import jdk.nashorn.internal.ir.Expression;
47 import jdk.nashorn.internal.ir.debug.ASTWriter;
48 import jdk.nashorn.internal.ir.debug.PrintVisitor;
49 import jdk.nashorn.internal.objects.Global;
50 import jdk.nashorn.internal.parser.Parser;
51 import jdk.nashorn.internal.runtime.Context;
52 import jdk.nashorn.internal.runtime.ErrorManager;
53 import jdk.nashorn.internal.runtime.JSType;
54 import jdk.nashorn.internal.runtime.Property;
55 import jdk.nashorn.internal.runtime.ScriptEnvironment;
56 import jdk.nashorn.internal.runtime.ScriptFunction;
57 import jdk.nashorn.internal.runtime.ScriptRuntime;
58 import jdk.nashorn.internal.runtime.options.Options;
59
60 /**
61 * Command line Shell for processing JavaScript files.
62 */
63 public class Shell {
64
65 /**
66 * Resource name for properties file
379 if (globalChanged) {
380 Context.setGlobal(oldGlobal);
381 }
382 }
383
384 return SUCCESS;
385 }
386
387 /**
388 * Hook to ScriptFunction "apply". A performance metering shell may
389 * introduce enter/exit timing here.
390 *
391 * @param target target function for apply
392 * @param self self reference for apply
393 *
394 * @return result of the function apply
395 */
396 protected Object apply(final ScriptFunction target, final Object self) {
397 return ScriptRuntime.apply(target, self);
398 }
399
400 /**
401 * Parse potentially partial code and keep track of the start of last expression.
402 * This 'partial' parsing support is meant to be used for code-completion.
403 *
404 * @param context the nashorn context
405 * @param code code that is to be parsed
406 * @return the start index of the last expression parsed in the (incomplete) code.
407 */
408 protected int getLastExpressionStart(final Context context, final String code) {
409 final int[] exprStart = { -1 };
410
411 final Parser p = new Parser(context.getEnv(), sourceFor("<partial_code>", code),new Context.ThrowErrorManager()) {
412 @Override
413 protected Expression expression() {
414 exprStart[0] = this.start;
415 return super.expression();
416 }
417
418 @Override
419 protected Expression assignmentExpression(final boolean noIn) {
420 exprStart[0] = this.start;
421 return super.expression();
422 }
423 };
424
425 try {
426 p.parse();
427 } catch (final Exception ignored) {
428 // throw any parser exception, but we are partial parsing anyway
429 }
430
431 return exprStart[0];
432 }
433
434
435 /**
436 * read-eval-print loop for Nashorn shell.
437 *
438 * @param context the nashorn context
439 * @param global global scope object to use
440 * @return return code
441 */
442 protected int readEvalPrint(final Context context, final Global global) {
443 final String prompt = bundle.getString("shell.prompt");
444 final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
445 final PrintWriter err = context.getErr();
446 final Global oldGlobal = Context.getGlobal();
447 final boolean globalChanged = (oldGlobal != global);
448 final ScriptEnvironment env = context.getEnv();
449
450 try {
451 if (globalChanged) {
452 Context.setGlobal(global);
453 }
|