Skip to content

Commit 756f91b

Browse files
committed
Get stdin and readline interactive using new IO.
1 parent 35ebaea commit 756f91b

File tree

3 files changed

+110
-24
lines changed

3 files changed

+110
-24
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.perl6.nqp.io;
2+
3+
import org.perl6.nqp.runtime.ThreadContext;
4+
5+
public interface IIOInteractive {
6+
public String readlineInteractive(ThreadContext tc, String prompt);
7+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.perl6.nqp.io;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
import java.io.OutputStreamWriter;
9+
import java.nio.ByteBuffer;
10+
import java.nio.CharBuffer;
11+
import java.nio.channels.FileChannel;
12+
import java.nio.charset.Charset;
13+
import java.nio.file.Path;
14+
import java.nio.file.StandardOpenOption;
15+
import java.util.ArrayList;
16+
17+
import jline.ConsoleReader;
18+
19+
import org.perl6.nqp.runtime.ExceptionHandling;
20+
import org.perl6.nqp.runtime.ThreadContext;
21+
22+
public class StandardReadHandle implements IIOClosable, IIOEncodable, IIOSyncReadable, IIOInteractive {
23+
private InputStream is;
24+
private BufferedReader br;
25+
private ConsoleReader cr;
26+
private boolean eof = false;
27+
private Charset cs;
28+
29+
public StandardReadHandle(ThreadContext tc, InputStream is) {
30+
this.is = is;
31+
setEncoding(tc, Charset.forName("UTF-8"));
32+
}
33+
34+
public void close(ThreadContext tc) {
35+
try {
36+
is.close();
37+
} catch (IOException e) {
38+
throw ExceptionHandling.dieInternal(tc, e);
39+
}
40+
}
41+
42+
public void setEncoding(ThreadContext tc, Charset cs) {
43+
this.cs = cs;
44+
}
45+
46+
public synchronized String slurp(ThreadContext tc) {
47+
try {
48+
if (br == null)
49+
br = new BufferedReader(new InputStreamReader(is, cs));
50+
StringBuffer data = new StringBuffer();
51+
char[] buf = new char[4096];
52+
int read = 0;
53+
while((read = br.read(buf)) != -1)
54+
data.append(String.valueOf(buf, 0, read));
55+
eof = true;
56+
return data.toString();
57+
} catch (IOException e) {
58+
throw ExceptionHandling.dieInternal(tc, e);
59+
}
60+
}
61+
62+
public synchronized String readline(ThreadContext tc) {
63+
try {
64+
if (br == null)
65+
br = new BufferedReader(new InputStreamReader(is, cs));
66+
String line = br.readLine();
67+
if (line == null) {
68+
eof = true;
69+
line = "";
70+
}
71+
return line;
72+
} catch (IOException e) {
73+
throw ExceptionHandling.dieInternal(tc, e);
74+
}
75+
}
76+
77+
public synchronized String readlineInteractive(ThreadContext tc, String prompt) {
78+
try {
79+
if (cr == null)
80+
cr = new ConsoleReader(is, new OutputStreamWriter(tc.gc.out));
81+
String line = cr.readLine(prompt);
82+
if (line == null) {
83+
eof = true;
84+
line = "";
85+
}
86+
return line;
87+
} catch (IOException e) {
88+
throw ExceptionHandling.dieInternal(tc, e);
89+
}
90+
}
91+
92+
public boolean eof(ThreadContext tc) {
93+
return eof;
94+
}
95+
}

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
import java.lang.invoke.MethodHandle;
44
import java.lang.invoke.MethodHandles;
55
import java.lang.invoke.MethodType;
6-
import java.io.BufferedReader;
76
import java.io.File;
8-
import java.io.FileInputStream;
97
import java.io.FileNotFoundException;
108
import java.io.FileOutputStream;
119
import java.io.IOException;
12-
import java.io.InputStreamReader;
13-
import java.io.OutputStreamWriter;
1410
import java.io.UnsupportedEncodingException;
1511
import java.math.BigDecimal;
1612
import java.math.BigInteger;
@@ -38,14 +34,14 @@
3834
import java.util.Set;
3935
import java.util.concurrent.TimeUnit;
4036

41-
import jline.ConsoleReader;
42-
4337
import org.perl6.nqp.io.FileHandle;
4438
import org.perl6.nqp.io.IIOClosable;
4539
import org.perl6.nqp.io.IIOEncodable;
40+
import org.perl6.nqp.io.IIOInteractive;
4641
import org.perl6.nqp.io.IIOSeekable;
4742
import org.perl6.nqp.io.IIOSyncReadable;
4843
import org.perl6.nqp.io.IIOSyncWritable;
44+
import org.perl6.nqp.io.StandardReadHandle;
4945
import org.perl6.nqp.io.StandardWriteHandle;
5046
import org.perl6.nqp.jast2bc.JASTToJVMBytecode;
5147
import org.perl6.nqp.sixmodel.BoolificationSpec;
@@ -334,7 +330,7 @@ public static long fileislink(String path, ThreadContext tc) {
334330
public static SixModelObject getstdin(ThreadContext tc) {
335331
SixModelObject IOType = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.ioType;
336332
IOHandleInstance h = (IOHandleInstance)IOType.st.REPR.allocate(tc, IOType.st);
337-
h.is = tc.gc.in;
333+
h.handle = new StandardReadHandle(tc, tc.gc.in);
338334
return h;
339335
}
340336

@@ -446,26 +442,14 @@ public static String readlinefh(SixModelObject obj, ThreadContext tc) {
446442
}
447443
}
448444

449-
/* We don't have proper readline support yet. */
450445
public static String readlineintfh(SixModelObject obj, String prompt, ThreadContext tc) {
451446
if (obj instanceof IOHandleInstance) {
452447
IOHandleInstance h = (IOHandleInstance)obj;
453-
if (h.is == null)
454-
die_s("File handle is not opened for read", tc);
455-
try {
456-
if (h.cr == null) {
457-
h.cr = new ConsoleReader(h.is, new OutputStreamWriter(tc.gc.out));
458-
}
459-
String line = h.cr.readLine(prompt);
460-
if (line == null) {
461-
h.eof = true;
462-
}
463-
return line;
464-
}
465-
catch (IOException e) {
466-
die_s(e.getMessage(), tc);
467-
return null; /* Unreachable */
468-
}
448+
if (h.handle instanceof IIOInteractive)
449+
return ((IIOInteractive)h.handle).readlineInteractive(tc, prompt);
450+
else
451+
throw ExceptionHandling.dieInternal(tc,
452+
"This handle does not support readline interactive");
469453
}
470454
else {
471455
throw ExceptionHandling.dieInternal(tc,

0 commit comments

Comments
 (0)