/
Pipe.pm6
78 lines (67 loc) · 2.03 KB
/
Pipe.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
my class IO::Pipe is IO::Handle {
has $.proc;
has $!on-read;
has $!on-write;
has $!on-close;
has $!on-native-descriptor;
has $!eof = False;
has $!closed = False;
method TWEAK(:$!on-close!, :$enc, :$bin, :$!on-read, :$!on-write,
:$!on-native-descriptor --> Nil) {
if $bin {
die X::IO::BinaryAndEncoding.new if nqp::isconcrete($enc);
}
else {
my $encoding = Encoding::Registry.find($enc || 'utf-8');
nqp::bindattr(self, IO::Handle, '$!encoding', $encoding.name);
my $decoder := $encoding.decoder(:translate-nl);
$decoder.set-line-separators($.nl-in.list);
nqp::bindattr(self, IO::Handle, '$!decoder', $decoder);
nqp::bindattr(self, IO::Handle, '$!encoder', $encoding.encoder(:translate-nl))
}
}
method read-internal($) {
if $!on-read {
loop {
my \result = $!on-read();
if result.DEFINITE {
return result if result.elems;
}
else {
$!eof = True;
return buf8.new
}
}
}
else {
die "This pipe was opened for writing, not reading"
}
}
method eof-internal() {
$!eof
}
method write-internal($data) {
$!on-write
?? $!on-write($data)
!! die "This pipe was opened for reading, not writing"
}
method flush(IO::Handle:D: --> True) { #`(No buffering) }
method close(IO::Pipe:D:) {
$!closed = True;
$!on-close()
}
method opened(IO::Pipe:D:) {
not $!closed
}
method t(IO::Pipe:D:) {
False
}
method native-descriptor(IO::Pipe:D:) {
$!on-native-descriptor
?? $!on-native-descriptor()
!! die("This pipe does not have an associated native descriptor")
}
method IO { IO::Path }
method path { IO::Path }
}
# vim: ft=perl6 expandtab sw=4