Skip to content

Commit 3847738

Browse files
committed
implement nqp::closefh_i on jvm
1 parent e600d5c commit 3847738

File tree

6 files changed

+92
-29
lines changed

6 files changed

+92
-29
lines changed

src/vm/jvm/QAST/Compiler.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,7 @@ QAST::OperationsJAST.map_classlib_core_op('readallfh', $TYPE_OPS, 'readallfh', [
20082008
QAST::OperationsJAST.map_classlib_core_op('getcfh', $TYPE_OPS, 'getcfh', [$RT_OBJ], $RT_STR, :tc);
20092009
QAST::OperationsJAST.map_classlib_core_op('eoffh', $TYPE_OPS, 'eoffh', [$RT_OBJ], $RT_INT, :tc);
20102010
QAST::OperationsJAST.map_classlib_core_op('closefh', $TYPE_OPS, 'closefh', [$RT_OBJ], $RT_OBJ, :tc);
2011+
QAST::OperationsJAST.map_classlib_core_op('closefh_i', $TYPE_OPS, 'closefhi', [$RT_OBJ], $RT_INT, :tc);
20112012

20122013
QAST::OperationsJAST.map_classlib_core_op('chmod', $TYPE_OPS, 'chmod', [$RT_STR, $RT_INT], $RT_INT, :tc);
20132014
QAST::OperationsJAST.map_classlib_core_op('unlink', $TYPE_OPS, 'unlink', [$RT_STR], $RT_INT, :tc);
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 IIOExitable {
6+
public int exitValue(ThreadContext tc) throws IllegalThreadStateException;
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.perl6.nqp.io;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
import java.nio.ByteBuffer;
8+
import java.nio.channels.ByteChannel;
9+
import java.nio.channels.Channels;
10+
import java.nio.channels.ReadableByteChannel;
11+
import java.nio.channels.WritableByteChannel;
12+
import java.nio.charset.Charset;
13+
import java.util.Map;
14+
15+
import org.perl6.nqp.runtime.ExceptionHandling;
16+
import org.perl6.nqp.runtime.ThreadContext;
17+
18+
public class ProcessChannel implements ByteChannel {
19+
protected WritableByteChannel stdin;
20+
protected ReadableByteChannel stdout;
21+
protected Process process;
22+
23+
public ProcessChannel(Process process, OutputStream stdin, InputStream stdout) {
24+
this.stdin = Channels.newChannel(stdin);
25+
this.stdout = Channels.newChannel(stdout);
26+
this.process = process;
27+
}
28+
29+
public int read(ByteBuffer dst) throws IOException {
30+
return stdout.read(dst);
31+
}
32+
33+
public boolean isOpen() {
34+
return stdin.isOpen();
35+
}
36+
37+
public void close() throws IOException {
38+
stdin.close();
39+
stdout.close();
40+
}
41+
42+
public int exitValue() throws IllegalThreadStateException {
43+
return process.exitValue();
44+
}
45+
46+
public int write(ByteBuffer src) throws IOException {
47+
return stdin.write(src);
48+
}
49+
}

src/vm/jvm/runtime/org/perl6/nqp/io/ProcessHandle.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ProcessHandle(ThreadContext tc, String cmd, String dir, Map<String, Strin
3737

3838
try {
3939
process = pb.start();
40-
chan = new ProcessChannel(process.getOutputStream(), process.getInputStream());
40+
chan = new ProcessChannel(process, process.getOutputStream(), process.getInputStream());
4141
setEncoding(tc, Charset.forName("UTF-8"));
4242
} catch (IOException e) {
4343
throw ExceptionHandling.dieInternal(tc, e);
@@ -47,31 +47,4 @@ public ProcessHandle(ThreadContext tc, String cmd, String dir, Map<String, Strin
4747
public void flush(ThreadContext tc) {
4848
// Not provided.
4949
}
50-
51-
static class ProcessChannel implements ByteChannel {
52-
protected WritableByteChannel stdin;
53-
protected ReadableByteChannel stdout;
54-
55-
public ProcessChannel(OutputStream stdin, InputStream stdout) {
56-
this.stdin = Channels.newChannel(stdin);
57-
this.stdout = Channels.newChannel(stdout);
58-
}
59-
60-
public int read(ByteBuffer dst) throws IOException {
61-
return stdout.read(dst);
62-
}
63-
64-
public boolean isOpen() {
65-
return stdin.isOpen();
66-
}
67-
68-
public void close() throws IOException {
69-
stdin.close();
70-
stdout.close();
71-
}
72-
73-
public int write(ByteBuffer src) throws IOException {
74-
return stdin.write(src);
75-
}
76-
}
7750
}

src/vm/jvm/runtime/org/perl6/nqp/io/SyncHandle.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.perl6.nqp.runtime.ThreadContext;
1616

1717
public abstract class SyncHandle implements IIOClosable, IIOEncodable,
18-
IIOSyncReadable, IIOSyncWritable, IIOLineSeparable {
18+
IIOSyncReadable, IIOSyncWritable, IIOLineSeparable, IIOExitable {
1919

2020
protected ByteChannel chan;
2121
protected CharsetEncoder enc;
@@ -32,6 +32,19 @@ public void close(ThreadContext tc) {
3232
}
3333
}
3434

35+
public int exitValue(ThreadContext tc) throws IllegalThreadStateException {
36+
try {
37+
if (chan instanceof ProcessChannel) {
38+
return ((ProcessChannel)chan).exitValue();
39+
}
40+
else
41+
throw ExceptionHandling.dieInternal(tc,
42+
"This channel does not support exitValue");
43+
} catch (IllegalThreadStateException e) {
44+
throw ExceptionHandling.dieInternal(tc, e);
45+
}
46+
}
47+
3548
public void setEncoding(ThreadContext tc, Charset cs) {
3649
enc = cs.newEncoder();
3750
dec = cs.newDecoder();

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
import org.perl6.nqp.io.IIOCancelable;
4545
import org.perl6.nqp.io.IIOClosable;
4646
import org.perl6.nqp.io.IIOEncodable;
47+
import org.perl6.nqp.io.IIOExitable;
4748
import org.perl6.nqp.io.IIOInteractive;
4849
import org.perl6.nqp.io.IIOLineSeparable;
4950
import org.perl6.nqp.io.IIOSeekable;
5051
import org.perl6.nqp.io.IIOSyncReadable;
5152
import org.perl6.nqp.io.IIOSyncWritable;
5253
import org.perl6.nqp.io.ProcessHandle;
54+
import org.perl6.nqp.io.ProcessChannel;
5355
import org.perl6.nqp.io.ServerSocketHandle;
5456
import org.perl6.nqp.io.SocketHandle;
5557
import org.perl6.nqp.io.StandardReadHandle;
@@ -755,6 +757,24 @@ public static SixModelObject closefh(SixModelObject obj, ThreadContext tc) {
755757
return obj;
756758
}
757759

760+
public static long closefhi(SixModelObject obj, ThreadContext tc) {
761+
if (obj instanceof IOHandleInstance) {
762+
IOHandleInstance h = (IOHandleInstance)obj;
763+
if (h.handle instanceof IIOClosable
764+
&& h.handle instanceof IIOExitable) {
765+
((IIOClosable)h.handle).close(tc);
766+
return (long)((IIOExitable)h.handle).exitValue(tc);
767+
}
768+
else
769+
throw ExceptionHandling.dieInternal(tc,
770+
"This handle does not support close or exitValue");
771+
}
772+
else {
773+
die_s("closefhi requires an object with the IOHandle REPR", tc);
774+
}
775+
return -1;
776+
}
777+
758778
public static Set<PosixFilePermission> modeToPosixFilePermission(long mode) {
759779
Set<PosixFilePermission> perms = EnumSet.noneOf(PosixFilePermission.class);
760780
if ((mode & 0001) != 0) perms.add(PosixFilePermission.OTHERS_EXECUTE);

0 commit comments

Comments
 (0)