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
66 */
67 private static final String MESSAGE_RESOURCE = "jdk.nashorn.tools.resources.Shell";
68 /**
69 * Shell message bundle.
70 */
71 protected static final ResourceBundle bundle = ResourceBundle.getBundle(MESSAGE_RESOURCE, Locale.getDefault());
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
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 implements PartialParser {
64
65 /**
66 * Resource name for properties file
67 */
68 private static final String MESSAGE_RESOURCE = "jdk.nashorn.tools.resources.Shell";
69 /**
70 * Shell message bundle.
71 */
72 protected static final ResourceBundle bundle = ResourceBundle.getBundle(MESSAGE_RESOURCE, Locale.getDefault());
73
74 /**
75 * Exit code for command line tool - successful
76 */
77 public static final int SUCCESS = 0;
78 /**
79 * Exit code for command line tool - error on command line
80 */
81 public static final int COMMANDLINE_ERROR = 100;
82 /**
83 * Exit code for command line tool - error compiling script
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 @Override
409 public final int getLastExpressionStart(final Context context, final String code) {
410 final int[] exprStart = { -1 };
411
412 final Parser p = new Parser(context.getEnv(), sourceFor("<partial_code>", code),new Context.ThrowErrorManager()) {
413 @Override
414 protected Expression expression() {
415 exprStart[0] = this.start;
416 return super.expression();
417 }
418
419 @Override
420 protected Expression assignmentExpression(final boolean noIn) {
421 exprStart[0] = this.start;
422 return super.expression();
423 }
424 };
425
426 try {
427 p.parse();
428 } catch (final Exception ignored) {
429 // throw any parser exception, but we are partial parsing anyway
430 }
431
432 return exprStart[0];
433 }
434
435
436 /**
437 * read-eval-print loop for Nashorn shell.
438 *
439 * @param context the nashorn context
440 * @param global global scope object to use
441 * @return return code
442 */
443 protected int readEvalPrint(final Context context, final Global global) {
444 final String prompt = bundle.getString("shell.prompt");
445 final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
446 final PrintWriter err = context.getErr();
447 final Global oldGlobal = Context.getGlobal();
448 final boolean globalChanged = (oldGlobal != global);
449 final ScriptEnvironment env = context.getEnv();
450
451 try {
452 if (globalChanged) {
453 Context.setGlobal(global);
454 }
|