Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[IO] implemented IO.read and IO.write

There's still some mis-encoding somewhere, because a few tests still fail in
t/spec/S16-filehandles/io.t.
  • Loading branch information...
commit 692aa15f2538858028934b8e26910199cc5fdc53 1 parent 2d0a573
@masak masak authored
Showing with 56 additions and 0 deletions.
  1. +56 −0 src/core/IO.pm
View
56 src/core/IO.pm
@@ -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 '';
Please sign in to comment.
Something went wrong with that request. Please try again.