Skip to content

Commit

Permalink
[IO] implemented IO.read and IO.write
Browse files Browse the repository at this point in the history
There's still some mis-encoding somewhere, because a few tests still fail in
t/spec/S16-filehandles/io.t.
  • Loading branch information
Carl Masak committed Jul 9, 2010
1 parent 2d0a573 commit 692aa15
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/core/IO.pm
Expand Up @@ -66,6 +66,62 @@ class IO is Cool {
self.print(@items, "\n");
}

multi method read(Int $bytes) {
my $pio = $!PIO;
my @bytes = Q:PIR {
.local int nbytes, byte
.local pmc bytebuffer, it, result
.local pmc pio
pio = find_lex '$pio'
pio = deref_unless_object pio
$P0 = find_lex '$bytes'
nbytes = $P0
$S0 = pio.'read'(nbytes)
bytebuffer = new ['ByteBuffer']
bytebuffer = $S0
result = new ['Parcel']
it = iter bytebuffer
bytes_loop:
unless it goto done
byte = shift it
push result, byte
goto bytes_loop
done:
%r = result
};
return Buf.new(@bytes);
}

multi method write(Buf $buf) {
my @contents = $buf.contents;
my $pio = $!PIO;
Q:PIR {
$P0 = find_lex '@contents'
.local pmc bb
.local string s
bb = new ['ByteBuffer']
.local pmc it
.local int i
it = iter $P0
i = 0
loop:
unless it goto done
$P1 = shift it
$I1 = $P1
bb[i] = $I1
inc i
goto loop
done:
s = bb.'get_string_as'(binary:"")
.local pmc pio
pio = find_lex '$pio'
pio = deref_unless_object pio
pio.'print'(s)
};
}

multi method getc() {
my $c = $!PIO.read(1);
fail if $c eq '';
Expand Down

0 comments on commit 692aa15

Please sign in to comment.