Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
switch Buf from ByteBuffer to binary string storage
the serializer knows how serialize strings, but not ByteBuffer; should fix #114500
  • Loading branch information
moritz committed Oct 14, 2012
1 parent edb767a commit 837d0f8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
43 changes: 19 additions & 24 deletions src/core/Buf.pm
Expand Up @@ -7,9 +7,10 @@ my class X::Buf::Pack { ... };
my class X::Buf::Pack::NonASCII { ... };

my class Buf does Positional {
has Mu $!buffer;
has str $!buffer;
my int $binary_encoding = pir::find_encoding__Is('binary');
method BUILD() {
$!buffer := pir::new__Ps('ByteBuffer');
$!buffer = pir::trans_encoding__Ssi('', $binary_encoding);
1;
}
method new(*@codes) {
Expand All @@ -19,25 +20,26 @@ my class Buf does Positional {
}
method !set_codes(@codes) {
my int $bytes = @codes.elems;
pir::set__vPI($!buffer, $bytes);
my $rsa := pir::new__Ps('ResizableStringArray');
my int $i = 0;
while $i < $bytes {
nqp::bindpos_i($!buffer, $i, nqp::unbox_i(@codes[$i]));
nqp::bindpos_s($rsa, $i, nqp::chr(nqp::unbox_i(@codes[$i])));
$i = $i + 1;
}
$!buffer = nqp::join('', $rsa);
self;
}

method at_pos(Buf:D: Int:D $idx) {
nqp::p6box_i(nqp::atpos_i($!buffer, nqp::unbox_i($idx)));
nqp::p6box_i(nqp::ord(nqp::substr($!buffer, nqp::unbox_i($idx), 1)));
}

multi method Bool(Buf:D:) {
nqp::p6bool(nqp::elems($!buffer));
nqp::p6bool(nqp::chars($!buffer));
}

method elems(Buf:D:) {
nqp::p6box_i(nqp::elems($!buffer));
nqp::p6box_i(nqp::chars($!buffer));
}
method bytes(Buf:D:) { self.elems }
method chars() { X::Buf::AsStr.new(method => 'chars').throw }
Expand All @@ -49,10 +51,10 @@ my class Buf does Positional {

method list() {
my @l;
my int $bytes = nqp::elems($!buffer);
my int $bytes = nqp::chars($!buffer);
my int $i = 0;
while $i < $bytes {
@l[$i] = nqp::p6box_i(nqp::atpos_i($!buffer, $i));
@l[$i] = nqp::p6box_i(nqp::ord(nqp::substr($!buffer, $i, 1)));
$i = $i + 1;
}
@l;
Expand All @@ -63,20 +65,15 @@ my class Buf does Positional {
}

method decode(Str:D $encoding = 'utf8') {
nqp::p6box_s $!buffer.get_string(
nqp::unbox_s PARROT_ENCODING($encoding)
);
my $bb := pir::new__Ps('ByteBuffer');
pir::set__vPs($bb, $!buffer);
nqp::p6box_s($bb.get_string(PARROT_ENCODING($encoding)));
}

method subbuf(Buf:D: $from = 0, $len = self.elems) {
my $ret := nqp::create(self);
my $buf := pir::new__Ps('ByteBuffer');
nqp::bindattr($ret, Buf, '$!buffer', $buf);
pir::set__vPs($buf,
nqp::substr($!buffer.get_string('binary'),
nqp::unbox_i($from.Int),
nqp::unbox_i($len.Int)
)
nqp::bindattr_s($ret, Buf, '$!buffer',
nqp::substr($!buffer, nqp::unbox_i($from), nqp::unbox_i($len))
);
$ret;
}
Expand Down Expand Up @@ -160,12 +157,10 @@ multi prefix:<~^>(Buf:D $a) {
}
multi infix:<~>(Buf:D $a, Buf:D $b) {
my Buf $r := nqp::create(Buf);
my Mu $br := pir::new__Ps('ByteBuffer');

my Mu $ba := nqp::getattr(nqp::p6decont($a), Buf, '$!buffer');
my Mu $bb := nqp::getattr(nqp::p6decont($b), Buf, '$!buffer');
pir::set__vPs($br, nqp::concat_s($ba.get_string('binary'), $bb.get_string('binary')));
nqp::bindattr($r, Buf, '$!buffer', $br);
my str $ba = nqp::getattr_s(nqp::p6decont($a), Buf, '$!buffer');
my str $bb = nqp::getattr_s(nqp::p6decont($b), Buf, '$!buffer');
nqp::bindattr_s($r, Buf, '$!buffer', nqp::concat_s($ba, $bb));
$r;
}
multi sub infix:<~&>(Buf:D $a, Buf:D $b) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/IO.pm
Expand Up @@ -149,7 +149,7 @@ class IO does IO::FileTestable {
method read(IO:D: Cool:D $bytes as Int) {
my Mu $parrot_buffer := $!PIO.read_bytes(nqp::unbox_i($bytes));
my $buf := nqp::create(Buf);
nqp::bindattr($buf, Buf, '$!buffer', $parrot_buffer);
nqp::bindattr_s($buf, Buf, '$!buffer', $parrot_buffer.get_string('binary'));
$buf;
}
# first arguemnt should probably be an enum
Expand All @@ -166,14 +166,14 @@ class IO does IO::FileTestable {
}

method write(IO:D: Buf:D $buf) {
my Mu $b := nqp::getattr(
my str $b = nqp::getattr_s(
nqp::p6decont($buf),
Buf,
'$!buffer'
);
my str $encoding = $!PIO.encoding;
$!PIO.encoding('binary');
$!PIO.print($b.get_string('binary'));
$!PIO.print($b);
$!PIO.encoding($encoding) unless $encoding eq 'binary';
True;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/IO/Socket.pm
Expand Up @@ -27,8 +27,8 @@ my role IO::Socket {
fail('Socket not available') unless $!PIO;
my $buf := nqp::create(Buf);
my Mu $parrot_buf := pir::new__PS('ByteBuffer');
pir::set__vPS($parrot_buf, $!PIO.read(nqp::unbox_i($bufsize)));
nqp::bindattr($buf, Buf, '$!buffer', $parrot_buf);
nqp::bindattr_s($buf, Buf, '$!buffer',
$parrot_buf.get_string('binary'));
$buf;
}

Expand Down
12 changes: 6 additions & 6 deletions src/core/Str.pm
Expand Up @@ -744,12 +744,12 @@ my class Str does Stringy {

method encode(Str:D $encoding = 'utf8') {
my $buf := Buf.new;
pir::set__vPs(nqp::getattr($buf, Buf, '$!buffer'),
pir::trans_encoding__Ssi(
nqp::unbox_s(self),
pir::find_encoding__Is(nqp::unbox_s(PARROT_ENCODING($encoding)))
)
);
my $bb := pir::new__Ps('ByteBuffer');
pir::set__vPS($bb, pir::trans_encoding__SSI(
nqp::unbox_s(self),
pir::find_encoding__IS(nqp::unbox_s(PARROT_ENCODING($encoding)))
));
nqp::bindattr_s($buf, Buf, '$!buffer', $bb.get_string('binary'));
$buf;
}

Expand Down

3 comments on commit 837d0f8

@ronaldxs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You noted this commit as fixing RT #114500. It may do that but it breaks LWP::Simple. If I do a git checkout of the previous rakudo commit then the LWP::Simple test t/get-perl6-org.t works. If I then checkout your commit here and run the same test it dies with a "could not parse headers" error.

If I run tcpdump to see the packets like:

sudo tcpdump -A -s 4096 -i eth0 host perl6.org >/tmp/perl6_org_tcpdump.txt &

Then I see that the request is going out and the page is coming back, but from what my debugging could tell, on line 197 of LWP/Simple.pm a socket read is done that comes up empty so:

my Buf $resp = $sock.read(2 * 1024); # on line 197

$resp.bytes is 0

If I don't hear back for a while, or you ask me, I guess I will eventually file an RT.

Thanks.

@moritz
Copy link
Member Author

@moritz moritz commented on 837d0f8 Oct 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW the LWP::Simple tests have been broken for me (with that very same error message) long before this commit.

@ronaldxs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have seen LWP::Simple break with that error message for older commits when using a version of parrot that does not match (is newer) than the nqp/tools/build/PARROT_REVISION appropriate for the commit. Otherwise, for me, when the parrot version matches and I clean out the rakudo install directory, using the previous commit reliably makes the test pass and then forwarding to this commit makes it fail.

Please sign in to comment.