Author: jrieks
Date: Fri Apr 15 04:41:58 2005
New Revision: 7840

Modified:
   trunk/include/parrot/pobj.h
   trunk/io/io.c
   trunk/t/pmc/io.t
Log:
introduced PMC_data0(pmc):
- it returns 0 if no pmc_ext is present
  (instead of assertion failed like PMC_data)
- it is allowed as rvalue only (can not be assigned to)

added TODO test for IO ops with wrong PMCs


Modified: trunk/include/parrot/pobj.h
==============================================================================
--- trunk/include/parrot/pobj.h (original)
+++ trunk/include/parrot/pobj.h Fri Apr 15 04:41:58 2005
@@ -194,8 +194,11 @@
 #endif /* NDEBUG */
 #if PMC_DATA_IN_EXT
 #  define PMC_data(pmc)       PMC_ext_checked(pmc)->data
+#  define PMC_data0(pmc)      ((pmc)->pmc_ext ? pmc->pmc_ext->data : 0)
 #else
 #  define PMC_data(pmc)       (pmc)->data
+/* do not allow PMC_data2 as lvalue */
+#  define PMC_data0(pmc)      (1 ? (pmc)->data : 0)
 #endif /* PMC_DATA_IN_EXT */
 #define PMC_metadata(pmc)     PMC_ext_checked(pmc)->_metadata
 #define PMC_next_for_GC(pmc)  PMC_ext_checked(pmc)->_next_for_GC

Modified: trunk/io/io.c
==============================================================================
--- trunk/io/io.c       (original)
+++ trunk/io/io.c       Fri Apr 15 04:41:58 2005
@@ -216,7 +216,7 @@
 void
 PIO_destroy(theINTERP, PMC *pmc)
 {
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data2(pmc);
     UNUSED(interpreter);
 
     if(!io)
@@ -556,7 +556,7 @@
 PIO_peek(theINTERP, PMC *pmc, STRING **buffer)
 {
     ParrotIOLayer *l = pmc->cache.struct_val;
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return -1;
     return PIO_peek_down(interpreter, l, io, buffer);
@@ -587,9 +587,10 @@
 PIO_pioctl(theINTERP, PMC *pmc, INTVAL cmd, INTVAL arg)
 {
 
-    ParrotIO * io = PMC_data(pmc);
-    ParrotIOBuf * b = &io->b;
+    ParrotIO * io = PMC_data0(pmc);
+    ParrotIOBuf * b;
     if(!io) return -1;
+    b = &io->b;
 
     switch(cmd) {
        case PIOCTL_CMDSETRECSEP: io->recsep = arg;
@@ -638,7 +639,7 @@
 PIO_setbuf(theINTERP, PMC *pmc, size_t bufsize)
 {
     ParrotIOLayer *layer = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return -1;
     PIO_flush(interpreter, pmc);
@@ -661,7 +662,7 @@
 PIO_setlinebuf(theINTERP, PMC *pmc)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return -1;
 
@@ -757,7 +758,7 @@
 {
     INTVAL res;
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return -1;
     PIO_flush(interpreter, pmc); /* XXX boe: is this neccessary here? */
@@ -782,7 +783,7 @@
 PIO_flush(theINTERP, PMC *pmc)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return;
 
@@ -811,7 +812,7 @@
 {
     STRING *res = NULL;
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
 
     res = PIO_make_io_string(interpreter, &res, len );
 
@@ -828,7 +829,7 @@
 PIO_read(theINTERP, PMC *pmc, void *buffer, size_t len)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     STRING *res = new_string_header(interpreter,
             PObj_bufstart_external_FLAG|PObj_external_FLAG);
     if (!io)
@@ -854,7 +855,7 @@
 PIO_write(theINTERP, PMC *pmc, const void *buffer, size_t len)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     STRING fake;
     union {
         const void * __c_ptr;
@@ -891,7 +892,7 @@
 PIO_seek(theINTERP, PMC *pmc, PIOOFF_T offset, INTVAL w)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return -1;
 
@@ -913,7 +914,7 @@
 PIO_tell(theINTERP, PMC *pmc)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     if(!io)
         return -1;
 
@@ -935,7 +936,7 @@
 INTVAL
 PIO_eof(theINTERP, PMC *pmc)
 {
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
 
     UNUSED(interpreter);
 
@@ -982,7 +983,9 @@
 PIO_putps(theINTERP, PMC *pmc, STRING *s)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
+    assert((int)l != 0xdeadbeef);
+    assert(io != 0);
     return PIO_write_down(interpreter, l, io, s);
 }
 
@@ -1097,7 +1100,7 @@
 PIOHANDLE
 PIO_getfd(theINTERP, PMC *pmc)
 {
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
 
     UNUSED(interpreter);
 
@@ -1292,7 +1295,7 @@
 PIO_poll(theINTERP, PMC *pmc, INTVAL which, INTVAL sec, INTVAL usec)
 {
     ParrotIOLayer *l = PMC_struct_val(pmc);
-    ParrotIO *io = PMC_data(pmc);
+    ParrotIO *io = PMC_data0(pmc);
     return PIO_poll_down(interpreter, l, io, which, sec, usec);
 }
 

Modified: trunk/t/pmc/io.t
==============================================================================
--- trunk/t/pmc/io.t    (original)
+++ trunk/t/pmc/io.t    Fri Apr 15 04:41:58 2005
@@ -16,7 +16,7 @@
 
 =cut
 
-use Parrot::Test tests => 30;
+use Parrot::Test tests => 31;
 use Test::More;
 
 sub file_content_is {
@@ -29,6 +29,58 @@
     close FOO;
 }
 
+TODO: {
+local $TODO = "IO on some invalid types";
+
+pir_output_is(<<'CODE', <<'OUTPUT', "IO on some invalid types");
+.sub main
+    $P0 = null
+    test($P0, "Undef")
+    new $P0, .Integer
+    test($P0, "null")
+    new $P0, .Undef
+    test($P0, "Integer")
+    new $P0, .String
+    test($P0, "String")
+.end
+.sub test
+    .param pmc io
+    .param string name
+    
+    print name
+    read $S0, io, 1
+    length $I0, $S0
+    if $I0 == 0 goto ok1
+    print " not"
+ok1:
+    print " ok 1\n"
+
+    print name
+    # what should happen here?
+    close io
+    print " ok 2\n"
+
+    print name
+    # what should happen here?
+    print io, "not"
+    print " ok 3\n"
+.end
+CODE
+Undef ok 1
+Undef ok 2
+Undef ok 3
+null ok 1
+null ok 2
+null ok 3
+Integer ok 1
+Integer ok 2
+Integer ok 3
+String ok 1
+String ok 2
+String ok 3
+OUTPUT
+};
+
 output_is(<<'CODE', <<'OUTPUT', "open/close");
        open P0, "temp.file", ">"
        print P0, "a line\n"

Reply via email to