Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Carl Mäsak authored July 06, 2010

Showing 1 changed file with 56 additions and 0 deletions. Show diff stats Hide diff stats

  1. 56  src/core/IO.pm
56  src/core/IO.pm
@@ -66,6 +66,62 @@ class IO is Cool {
66 66
         self.print(@items, "\n");
67 67
     }
68 68
 
  69
+    multi method read(Int $bytes) {
  70
+        my $pio = $!PIO;
  71
+        my @bytes = Q:PIR {
  72
+            .local int nbytes, byte
  73
+            .local pmc bytebuffer, it, result
  74
+            .local pmc pio
  75
+            pio = find_lex '$pio'
  76
+            pio = deref_unless_object pio
  77
+            $P0 = find_lex '$bytes'
  78
+            nbytes = $P0
  79
+            $S0 = pio.'read'(nbytes)
  80
+            bytebuffer = new ['ByteBuffer']
  81
+            bytebuffer = $S0
  82
+
  83
+            result = new ['Parcel']
  84
+            it = iter bytebuffer
  85
+          bytes_loop:
  86
+            unless it goto done
  87
+            byte = shift it
  88
+            push result, byte
  89
+            goto bytes_loop
  90
+          done:
  91
+            %r = result
  92
+        };
  93
+        return Buf.new(@bytes);
  94
+    }
  95
+
  96
+    multi method write(Buf $buf) {
  97
+        my @contents = $buf.contents;
  98
+        my $pio = $!PIO;
  99
+        Q:PIR {
  100
+            $P0 = find_lex '@contents'
  101
+
  102
+            .local pmc bb
  103
+            .local string s
  104
+            bb = new ['ByteBuffer']
  105
+            .local pmc it
  106
+            .local int i
  107
+            it = iter $P0
  108
+            i = 0
  109
+          loop:
  110
+            unless it goto done
  111
+            $P1 = shift it
  112
+            $I1 = $P1
  113
+            bb[i] = $I1
  114
+            inc i
  115
+            goto loop
  116
+          done:
  117
+            s = bb.'get_string_as'(binary:"")
  118
+            .local pmc pio
  119
+            pio = find_lex '$pio'
  120
+            pio = deref_unless_object pio
  121
+            pio.'print'(s)
  122
+        };
  123
+    }
  124
+
69 125
     multi method getc() {
70 126
         my $c = $!PIO.read(1);
71 127
         fail if $c eq '';

0 notes on commit 692aa15

Please sign in to comment.
Something went wrong with that request. Please try again.