-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Pipe.pm6
78 lines (67 loc) · 2.07 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 {
X::IO::BinaryAndEncoding.new.throw 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($) {
if $!on-read {
loop {
my \result = $!on-read();
if result.DEFINITE {
return result if result.elems;
}
else {
$!eof = True;
return nqp::create(buf8.^pun)
}
}
}
else {
X::AdHoc.new( payload => "This pipe was opened for writing, not reading" ).throw
}
}
method EOF() {
$!eof
}
method WRITE($data) {
$!on-write
?? $!on-write($data)
!! X::AdHoc.new( payload => "This pipe was opened for reading, not writing").throw
}
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: expandtab shiftwidth=4