src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java
author sundar
Thu, 28 Jun 2018 18:04:19 +0530
changeset 50878 fb7800b66c92
parent 48327 d2a837cf9ff1
child 52938 5ff7480c9e28
permissions -rw-r--r--
8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs Reviewed-by: jlaskey, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     1
/*
48327
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 47927
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     4
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    10
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    15
 * accompanied this code).
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    16
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    20
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    23
 * questions.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    24
 */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    25
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    27
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    28
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    29
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    30
import java.io.BufferedReader;
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32241
diff changeset
    31
import java.io.File;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    32
import java.io.InputStream;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    33
import java.io.InputStreamReader;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    34
import java.io.IOException;
40479
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
    35
import java.io.UncheckedIOException;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    36
import java.io.OutputStream;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    37
import java.io.PrintWriter;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    38
import java.net.URI;
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
    39
import java.security.AccessController;
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
    40
import java.security.PrivilegedAction;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    41
import java.util.concurrent.Callable;
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
    42
import java.util.function.Consumer;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    43
import java.util.function.Function;
32241
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents: 32240
diff changeset
    44
import jdk.internal.jline.console.completer.Completer;
f6228e27ad97 8133695: Implement tab-completion for identifiers
sundar
parents: 32240
diff changeset
    45
import jdk.internal.jline.console.UserInterruptException;
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    46
import jdk.nashorn.api.scripting.NashornException;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    47
import jdk.nashorn.internal.objects.Global;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    48
import jdk.nashorn.internal.objects.NativeJava;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    49
import jdk.nashorn.internal.runtime.Context;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    50
import jdk.nashorn.internal.runtime.NativeJavaPackage;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
    51
import jdk.nashorn.internal.runtime.Property;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    52
import jdk.nashorn.internal.runtime.ScriptEnvironment;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
    53
import jdk.nashorn.internal.runtime.ScriptFunction;
40479
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
    54
import jdk.nashorn.internal.runtime.ScriptingFunctions;
34845
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
    55
import jdk.nashorn.internal.runtime.ScriptObject;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    56
import jdk.nashorn.internal.runtime.ScriptRuntime;
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
    57
import jdk.nashorn.internal.runtime.Source;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    58
import jdk.nashorn.tools.Shell;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    59
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    60
/**
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    61
 * Interactive command line Shell for Nashorn.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    62
 */
50878
fb7800b66c92 8204492: Add deprecation annotation to Nashorn APIs and warning to nashorn, jjs
sundar
parents: 48327
diff changeset
    63
@Deprecated(since="11", forRemoval=true)
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    64
public final class Main extends Shell {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    65
    private Main() {}
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    66
34845
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
    67
    private static final String DOC_PROPERTY_NAME = "__doc__";
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
    68
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    69
    static final boolean DEBUG = Boolean.getBoolean("nashorn.jjs.debug");
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
    70
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32241
diff changeset
    71
    // file where history is persisted.
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32241
diff changeset
    72
    private static final File HIST_FILE = new File(new File(System.getProperty("user.home")), ".jjs.history");
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    73
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    74
    /**
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    75
     * Main entry point with the default input, output and error streams.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    76
     *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    77
     * @param args The command line arguments
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    78
     */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    79
    public static void main(final String[] args) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    80
        try {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    81
            final int exitCode = main(System.in, System.out, System.err, args);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    82
            if (exitCode != SUCCESS) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    83
                System.exit(exitCode);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    84
            }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    85
        } catch (final IOException e) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    86
            System.err.println(e); //bootstrapping, Context.err may not exist
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    87
            System.exit(IO_ERROR);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    88
        }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    89
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    90
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    91
    /**
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    92
     * Starting point for executing a {@code Shell}. Starts a shell with the
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    93
     * given arguments and streams and lets it run until exit.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    94
     *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    95
     * @param in input stream for Shell
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    96
     * @param out output stream for Shell
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    97
     * @param err error stream for Shell
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    98
     * @param args arguments to Shell
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    99
     *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   100
     * @return exit code
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   101
     *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   102
     * @throws IOException if there's a problem setting up the streams
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   103
     */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   104
    public static int main(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   105
        return new Main().run(in, out, err, args);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   106
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   107
32245
80164edf8a10 8133872: Expression completion should work on contexts where an expression is accepted
sundar
parents: 32242
diff changeset
   108
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   109
    /**
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   110
     * read-eval-print loop for Nashorn shell.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   111
     *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   112
     * @param context the nashorn context
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   113
     * @param global  global scope object to use
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   114
     * @return return code
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   115
     */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   116
    protected int readEvalPrint(final Context context, final Global global) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   117
        final ScriptEnvironment env = context.getEnv();
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   118
        final String prompt = bundle.getString("shell.prompt");
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   119
        final String prompt2 = bundle.getString("shell.prompt2");
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   120
        final PrintWriter err = context.getErr();
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   121
        final Global oldGlobal = Context.getGlobal();
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   122
        final boolean globalChanged = (oldGlobal != global);
38897
b92825d7708c 8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents: 34845
diff changeset
   123
        final PropertiesHelper propsHelper = new PropertiesHelper(context);
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   124
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   125
        if (globalChanged) {
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   126
            Context.setGlobal(global);
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   127
        }
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   128
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   129
        // jjs.js is read and evaluated. The result of the evaluation is an "exports" object. This is done
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   130
        // to avoid polluting javascript global scope. These are internal funtions are retrieved from the
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   131
        // 'exports' object and used from here.
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   132
        final ScriptObject jjsObj = (ScriptObject)context.eval(global, readJJSScript(), global, "<jjs.js>");
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   133
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   134
        final boolean isHeadless = (boolean) ScriptRuntime.apply((ScriptFunction) jjsObj.get("isHeadless"), null);
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   135
        final ScriptFunction fileChooserFunc = isHeadless? null : (ScriptFunction) jjsObj.get("chooseFile");
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   136
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   137
        final NashornCompleter completer = new NashornCompleter(context, global, this, propsHelper, fileChooserFunc);
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   138
        final ScriptFunction browseFunc = isHeadless? null : (ScriptFunction) jjsObj.get("browse");
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   139
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   140
        final ScriptFunction javadoc = (ScriptFunction) jjsObj.get("javadoc");
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
   141
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   142
        try (final Console in = new Console(System.in, System.out, HIST_FILE, completer,
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   143
                str -> {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   144
                    try {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   145
                        final Object res = context.eval(global, str, global, "<shell>");
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   146
                        if (res != null && res != UNDEFINED) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   147
                            // Special case Java types: show the javadoc for the class.
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   148
                            if (!isHeadless && NativeJava.isType(UNDEFINED, res)) {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   149
                                final String typeName = NativeJava.typeName(UNDEFINED, res).toString();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   150
                                final String url = typeName.replace('.', '/').replace('$', '.') + ".html";
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   151
                                openBrowserForJavadoc(browseFunc, url);
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   152
                            } else if (!isHeadless && res instanceof NativeJavaPackage) {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   153
                                final String pkgName = ((NativeJavaPackage)res).getName();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   154
                                final String url = pkgName.replace('.', '/') + "/package-summary.html";
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   155
                                openBrowserForJavadoc(browseFunc, url);
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   156
                            } else if (NativeJava.isJavaMethod(UNDEFINED, res)) {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   157
                                ScriptRuntime.apply(javadoc, UNDEFINED, res);
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   158
                                return ""; // javadoc function already prints javadoc
34845
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   159
                            } else if (res instanceof ScriptObject) {
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   160
                                final ScriptObject sobj = (ScriptObject)res;
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   161
                                if (sobj.has(DOC_PROPERTY_NAME)) {
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   162
                                    return toString(sobj.get(DOC_PROPERTY_NAME), global);
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   163
                                } else if (sobj instanceof ScriptFunction) {
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   164
                                    return ((ScriptFunction)sobj).getDocumentation();
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   165
                                }
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   166
                            }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   167
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   168
                            // FIXME: better than toString for other cases?
34845
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   169
                            return toString(res, global);
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   170
                        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   171
                     } catch (Exception ignored) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   172
                     }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   173
                     return null;
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   174
                })) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   175
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   176
            global.addShellBuiltins();
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   177
40479
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   178
            // redefine readLine to use jline Console's readLine!
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   179
            ScriptingFunctions.setReadLineHelper(str-> {
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   180
                try {
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   181
                    return in.readLine(str);
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   182
                } catch (final IOException ioExp) {
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   183
                    throw new UncheckedIOException(ioExp);
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   184
                }
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   185
            });
e64d53b67d12 8164260: readLine does not echo characters
sundar
parents: 38897
diff changeset
   186
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   187
            if (System.getSecurityManager() == null) {
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   188
                final Consumer<String> evaluator = str -> {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   189
                    // could be called from different thread (GUI), we need to handle Context set/reset
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   190
                    final Global _oldGlobal = Context.getGlobal();
40576
50b33dfee46a 8164748: Edit pad crashes when calling function
sundar
parents: 40479
diff changeset
   191
                    final boolean _globalChanged = (_oldGlobal != global);
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   192
                    if (_globalChanged) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   193
                        Context.setGlobal(global);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   194
                    }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   195
                    try {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   196
                        evalImpl(context, global, str, err, env._dump_on_error);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   197
                    } finally {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   198
                        if (_globalChanged) {
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   199
                            Context.setGlobal(_oldGlobal);
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   200
                        }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   201
                    }
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   202
                };
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   203
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   204
                // expose history object for reflecting on command line history
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   205
                global.addOwnProperty("history", Property.NOT_ENUMERABLE, new HistoryObject(in.getHistory(), err, evaluator));
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   206
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   207
                // 'edit' command
32324
b021cc36fd65 8134562: jjs history object should have methods to save/load history to/from given file and also allow reexecution of commands by a call
sundar
parents: 32320
diff changeset
   208
                global.addOwnProperty("edit", Property.NOT_ENUMERABLE, new EditObject(in, err::println, evaluator));
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   209
            }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   210
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   211
            while (true) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   212
                String source = "";
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   213
                try {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   214
                    source = in.readLine(prompt);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   215
                } catch (final IOException ioe) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   216
                    err.println(ioe.toString());
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   217
                    if (env._dump_on_error) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   218
                        ioe.printStackTrace(err);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   219
                    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   220
                    return IO_ERROR;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   221
                } catch (final UserInterruptException ex) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   222
                    break;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   223
                }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   224
32896
1d1ff2cf5846 8138910: Ctrl-D causes jjs to crash with NPE
sundar
parents: 32324
diff changeset
   225
                if (source == null) {
1d1ff2cf5846 8138910: Ctrl-D causes jjs to crash with NPE
sundar
parents: 32324
diff changeset
   226
                    break;
1d1ff2cf5846 8138910: Ctrl-D causes jjs to crash with NPE
sundar
parents: 32324
diff changeset
   227
                }
1d1ff2cf5846 8138910: Ctrl-D causes jjs to crash with NPE
sundar
parents: 32324
diff changeset
   228
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   229
                if (source.isEmpty()) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   230
                    continue;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   231
                }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   232
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   233
                try {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   234
                    final Object res = context.eval(global, source, global, "<shell>");
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   235
                    if (res != UNDEFINED) {
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 32896
diff changeset
   236
                        err.println(toString(res, global));
32317
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   237
                    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   238
                } catch (final Exception exp) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   239
                    // Is this a ECMAScript SyntaxError at last column (of the single line)?
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   240
                    // If so, it is because parser expected more input but got EOF. Try to
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   241
                    // to more lines from the user (multiline edit support).
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   242
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   243
                    if (completer.isSyntaxErrorAt(exp, 1, source.length())) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   244
                        final String fullSrc = completer.readMoreLines(source, exp, in, prompt2, err);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   245
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   246
                        // check if we succeeded in getting complete code.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   247
                        if (fullSrc != null && !fullSrc.isEmpty()) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   248
                            evalImpl(context, global, fullSrc, err, env._dump_on_error);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   249
                        } // else ignore, error reported already by 'completer.readMoreLines'
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   250
                    } else {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   251
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   252
                        // can't read more lines to have parseable/complete code.
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   253
                        err.println(exp);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   254
                        if (env._dump_on_error) {
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   255
                            exp.printStackTrace(err);
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   256
                        }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   257
                    }
2b653e4e7d65 8134279: jjs should support multiple line input to complete incomplete code
sundar
parents: 32314
diff changeset
   258
                }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   259
            }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   260
        } catch (final Exception e) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   261
            err.println(e);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   262
            if (env._dump_on_error) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   263
                e.printStackTrace(err);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   264
            }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   265
        } finally {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   266
            if (globalChanged) {
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   267
                Context.setGlobal(oldGlobal);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   268
            }
32314
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   269
            try {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   270
                propsHelper.close();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   271
            } catch (final Exception exp) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   272
                if (DEBUG) {
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   273
                    exp.printStackTrace();
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   274
                }
8f7d23d3b1ad 8134255: Implement tab-completion for java package prefixes and package names
sundar
parents: 32313
diff changeset
   275
            }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   276
        }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   277
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   278
        return SUCCESS;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   279
    }
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   280
32320
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32317
diff changeset
   281
    static String getMessage(final String id) {
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32317
diff changeset
   282
        return bundle.getString(id);
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32317
diff changeset
   283
    }
3e408f16c1c9 8134397: Features that require AWT, swing should handle headless mode properly
sundar
parents: 32317
diff changeset
   284
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   285
    private void evalImpl(final Context context, final Global global, final String source,
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   286
            final PrintWriter err, final boolean doe) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   287
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   288
            final Object res = context.eval(global, source, global, "<shell>");
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   289
            if (res != UNDEFINED) {
34845
48ffe69dacef 8146253: jjs should look for "doc string" property to print documentation on shift-tab
sundar
parents: 34735
diff changeset
   290
                err.println(toString(res, global));
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   291
            }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   292
        } catch (final Exception e) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   293
            err.println(e);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   294
            if (doe) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   295
                e.printStackTrace(err);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   296
            }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   297
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32245
diff changeset
   298
    }
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   299
47927
a6b2d28c8094 8191810: jjs should avoid hard coded javadoc base url for shift-tab documentation feature
sundar
parents: 47704
diff changeset
   300
    private static String JAVADOC_BASE = "https://docs.oracle.com/javase/%d/docs/api/";
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   301
    private static void openBrowserForJavadoc(ScriptFunction browse, String relativeUrl) {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   302
        try {
48327
d2a837cf9ff1 8192833: JEP 322: Time-Based Release Versioning
mr
parents: 47927
diff changeset
   303
            final URI uri = new URI(String.format(JAVADOC_BASE, Runtime.version().feature()) + relativeUrl);
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
   304
            ScriptRuntime.apply(browse, null, uri);
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   305
        } catch (Exception ignored) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   306
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 33690
diff changeset
   307
    }
47704
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   308
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   309
    private static String readJJSScript() {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   310
        return AccessController.doPrivileged(
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   311
            new PrivilegedAction<String>() {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   312
                @Override
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   313
                public String run() {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   314
                    try {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   315
                        final InputStream resStream = Main.class.getResourceAsStream("resources/jjs.js");
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   316
                        if (resStream == null) {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   317
                            throw new RuntimeException("resources/jjs.js is missing!");
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   318
                        }
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   319
                        return new String(Source.readFully(resStream));
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   320
                    } catch (final IOException exp) {
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   321
                        throw new RuntimeException(exp);
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   322
                    }
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   323
                }
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   324
            });
38aa08d2ec6c 8190795: jjs should show javadoc for java methods on shift-tab
sundar
parents: 47492
diff changeset
   325
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   326
}