changeset 1763:b8634c8d947a

8164260: readLine does not echo characters Reviewed-by: jlaskey
author sundar
date Thu, 18 Aug 2016 16:54:27 +0530
parents 6607833b50b5
children 3a924b820d02
files src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/ShellFunctions.java
diffstat 3 files changed, 40 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java	Wed Aug 17 19:16:28 2016 +0530
+++ b/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java	Thu Aug 18 16:54:27 2016 +0530
@@ -34,6 +34,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.IOException;
+import java.io.UncheckedIOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.net.URI;
@@ -50,6 +51,7 @@
 import jdk.nashorn.internal.runtime.Property;
 import jdk.nashorn.internal.runtime.ScriptEnvironment;
 import jdk.nashorn.internal.runtime.ScriptFunction;
+import jdk.nashorn.internal.runtime.ScriptingFunctions;
 import jdk.nashorn.internal.runtime.ScriptObject;
 import jdk.nashorn.internal.runtime.ScriptRuntime;
 import jdk.nashorn.tools.Shell;
@@ -157,6 +159,15 @@
 
             global.addShellBuiltins();
 
+            // redefine readLine to use jline Console's readLine!
+            ScriptingFunctions.setReadLineHelper(str-> {
+                try {
+                    return in.readLine(str);
+                } catch (final IOException ioExp) {
+                    throw new UncheckedIOException(ioExp);
+                }
+            });
+
             if (System.getSecurityManager() == null) {
                 final Consumer<String> evaluator = str -> {
                     // could be called from different thread (GUI), we need to handle Context set/reset
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Wed Aug 17 19:16:28 2016 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Thu Aug 18 16:54:27 2016 +0530
@@ -42,6 +42,8 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
 import jdk.nashorn.internal.objects.NativeArray;
 import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
 
@@ -92,11 +94,7 @@
      * @throws IOException if an exception occurs
      */
     public static Object readLine(final Object self, final Object prompt) throws IOException {
-        if (prompt != UNDEFINED) {
-            System.out.print(JSType.toString(prompt));
-        }
-        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
-        return reader.readLine();
+        return readLine(prompt);
     }
 
     /**
@@ -219,6 +217,30 @@
         return outString;
     }
 
+    // Implementation for pluggable "readLine" functionality
+    // Used by jjs interactive mode
+
+    private static Function<String, String> readLineHelper;
+
+    public static void setReadLineHelper(Function<String, String> func) {
+        readLineHelper = Objects.requireNonNull(func);
+    }
+
+    public static Function<String, String> getReadLineHelper() {
+        return readLineHelper;
+    }
+
+    public static String readLine(final Object prompt) throws IOException {
+        final String p = (prompt != UNDEFINED)? JSType.toString(prompt) : "";
+        if (readLineHelper != null) {
+            return readLineHelper.apply(p);
+        } else {
+            System.out.print(p);
+            final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+            return reader.readLine();
+        }
+    }
+
     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
         return MH.findStatic(MethodHandles.lookup(), ScriptingFunctions.class, name, MH.type(rtype, types));
     }
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/ShellFunctions.java	Wed Aug 17 19:16:28 2016 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/ShellFunctions.java	Thu Aug 18 16:54:27 2016 +0530
@@ -34,6 +34,7 @@
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import jdk.nashorn.internal.runtime.JSType;
+import jdk.nashorn.internal.runtime.ScriptingFunctions;
 import jdk.nashorn.internal.objects.Global;
 
 /**
@@ -66,11 +67,9 @@
     public static Object input(final Object self, final Object endMarker, final Object prompt) throws IOException {
         final String endMarkerStr = (endMarker != UNDEFINED)? JSType.toString(endMarker) : "";
         final String promptStr = (prompt != UNDEFINED)? JSType.toString(prompt)  : ">> ";
-        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         final StringBuilder buf = new StringBuilder();
         while (true) {
-            System.out.print(promptStr);
-            final String line = reader.readLine();
+            final String line = ScriptingFunctions.readLine(promptStr);
             if (line == null || line.equals(endMarkerStr)) {
                 break;
             }