Permalink
Browse files

Add tests, (re-)fix the clone VTABLE.

  • Loading branch information...
nolanlum authored and cotto committed Jan 1, 2011
1 parent b235875 commit 476cb11d5fe0530ba2d73f58227061785a4d2694
Showing with 42 additions and 13 deletions.
  1. +14 −12 src/pmc/filehandle.pmc
  2. +28 −1 t/pmc/filehandle.t
View
@@ -126,26 +126,28 @@ Create a copy of the filehandle.
data_struct->file_pos = old_struct->file_pos;
data_struct->last_pos = old_struct->last_pos;
- /* The buffer is special; we have to check to make sure we're setting the right mode. */
+ /* Duplicate the file handle. */
+ data_struct->os_handle = (PIOHANDLE)Parrot_dup(old_struct->os_handle);
+
+ /* We need to correctly allocate a buffer, so we check for the buffering mode. */
if (data_struct->flags & PIO_F_LINEBUF)
- Parrot_io_setlinebuf(INTERP, SELF);
- else if (data_struct->flags & PIO_F_BLKBUF)
- Parrot_io_setbuf(INTERP, SELF, PIO_UNBOUND);
+ Parrot_io_setbuf(INTERP, copy, PIO_LINEBUFSIZE);
else
- Parrot_io_setbuf(INTERP, SELF, old_struct->buffer_size);
+ Parrot_io_setbuf(INTERP, copy, old_struct->buffer_size);
- /* This shouldn't be needed due to setbuf calls above but just in case. */
- data_struct->buffer_flags = old_struct->buffer_flags;
+ /* Transfer only read/write flags on the buffer. */
+ data_struct->buffer_flags &= ~(PIO_BF_READBUF & PIO_BF_WRITEBUF);
+ data_struct->buffer_flags |= old_struct->buffer_flags &
+ (PIO_BF_READBUF & PIO_BF_WRITEBUF);
/* Then after we've set the buffer mode we can memcpy the data over. */
- if (data_struct->buffer_size > 0)
+ if (old_struct->buffer_size > 0)
memcpy(data_struct->buffer_start, old_struct->buffer_start,
old_struct->buffer_end - old_struct->buffer_start);
- data_struct->buffer_next = old_struct->buffer_next;
-
- /* Finally duplicate the file handle. */
- data_struct->os_handle = (PIOHANDLE)Parrot_dup(old_struct->os_handle);
+ /* This is an offset, so we must do math to get a new offset. */
+ data_struct->buffer_next = data_struct->buffer_start +
+ (old_struct->buffer_next - old_struct->buffer_start);
return copy;
}
View
@@ -6,7 +6,7 @@ use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More;
-use Parrot::Test tests => 24;
+use Parrot::Test tests => 25;
use Parrot::Test::Util 'create_tempfile';
use Parrot::Test::Util 'create_tempfile';
@@ -576,6 +576,33 @@ CODE
ok 1 - $S0 = $P0.mode() # get read mode
OUT
+pir_output_is( <<'CODE', <<'OUTPUT', "clone preserves all attributes of filehandle" );
+.sub main
+ .local pmc fh,fh_clone
+ .local string line1, line2
+
+ fh = new ['FileHandle']
+ fh.'open'('README')
+
+ line1 = fh.'readline'()
+
+ fh_clone = clone fh
+ line2 = fh_clone.'readline'()
+
+ $I0 = line1 == line2
+ say $I0
+
+ fh_clone.'seek'(0, 0)
+ line2 = fh_clone.'readline'()
+
+ $I0 = line1 == line2
+ say $I0
+.end
+CODE
+0
+1
+OUTPUT
+
pir_output_is( <<"CODE", <<"OUTPUT", "readall - closed filehandle" );
.sub main :main
\$S0 = <<"EOS"

0 comments on commit 476cb11

Please sign in to comment.