Leopold Toetsch wrote:
Am Mittwoch, 20. Dezember 2006 05:59 schrieb Will Coleda:
Are Hash and Array supposed to have different results on unset keys?

The .Undefs returned by Arrays are IMHO and unfortunate leftover of he early 
PerlArrays. We've managed to change the return result of hashes to .Null, so 
this should be possible with arrays too.
Changing the PMCs ain't hard. It's changing all the code that is getting an undef back and testing for it that's the issue.

Patch attached that cleans up the code to return PMCNULL instead of undef anyway. I don't really want to apply this until I hear a design decision though, and preferably have time to fix some of the code in the repository that depends on the difference (or have other people say they are willing to help in that).

Thanks,

Jonathan

Index: src/pmc/resizablepmcarray.pmc
===================================================================
--- src/pmc/resizablepmcarray.pmc       (revision 16208)
+++ src/pmc/resizablepmcarray.pmc       (working copy)
@@ -205,8 +205,6 @@
         if (key >= PMC_int_val(SELF))
             DYNSELF.set_integer_native(key+1);
         data = PMC_data(SELF);
-        if (data[key] == PMCNULL)
-            data[key] = pmc_new(INTERP, enum_class_Undef);
         return data[key];
     }
 
Index: src/pmc/array.pmc
===================================================================
--- src/pmc/array.pmc   (revision 16208)
+++ src/pmc/array.pmc   (working copy)
@@ -22,25 +22,9 @@
 
 /*
 
-=item C<static PMC* undef(Interp* interp)>
-
-Returns a C<Undef> PMC.
-
-=cut
-
-*/
-
-static PMC* undef(Interp* interp)
-{
-    return pmc_new(interp, enum_class_Undef);
-}
-
-/*
-
 =item C<static PMC* retval(Interp *interp, void *ret)>
 
-Processes C<*ret>, returning the appropriate PMC, or raising an
-exception if necessary.
+Processes C<*ret>, returning PMCNULL if needed.
 
 =cut
 
@@ -48,20 +32,7 @@
 
 static PMC* retval(Interp *interp, void *ret)
 {
-    PMC *value;
-    if (ret == 0)
-    real_exception(interp, NULL, E_IndexError,
-        "Array index out of bounds!");
-    /* XXX getting non existent value, exception or undef?
-     * current is for perlarray */
-    if (ret == (void*) -1)
-        value = undef(interp);
-    else {
-        value = *(PMC**) ret;
-        if (value == NULL)  /* XXX same here */
-            value = undef(interp);
-    }
-    return value;
+    return ret == 0 || ret == (void*) -1 ? PMCNULL : (PMC*)ret;
 }
 
 /*
@@ -88,7 +59,7 @@
             "Array index out of bounds!");
     /* assign into a sparse or not yet set value */
     if (ret == (void*) -1 || *(PMC**)ret == 0) {
-        value = undef(interp);
+        value = PMCNULL;
         list_assign(interp, list, key, value, enum_type_PMC);
     }
     else
@@ -348,7 +319,8 @@
         if (nextkey == NULL) return SELF.get_integer_keyed_int(ix);
 
         box = SELF.get_pmc_keyed_int(ix);
-        if (box == NULL) box = undef(INTERP);
+        if (box == NULL)
+            box = PMCNULL;
         return VTABLE_get_integer_keyed(INTERP, box, nextkey);
     }
 
@@ -391,7 +363,8 @@
         if (nextkey == NULL) return SELF.get_number_keyed_int(ix);
 
         box = SELF.get_pmc_keyed_int(ix);
-        if (box == NULL) box = undef(INTERP);
+        if (box == NULL)
+            box = PMCNULL;
         return VTABLE_get_number_keyed(INTERP, box, nextkey);
     }
 
@@ -433,7 +406,8 @@
         if (nextkey == NULL) return SELF.get_string_keyed_int(ix);
 
         box = SELF.get_pmc_keyed_int(ix);
-        if (box == NULL) box = undef(INTERP);
+        if (box == NULL)
+            box = PMCNULL;
         return VTABLE_get_string_keyed(INTERP, box, nextkey);
     }
 
@@ -473,7 +447,8 @@
         if (nextkey == NULL) return SELF.get_pmc_keyed_int(ix);
 
         box = SELF.get_pmc_keyed_int(ix);
-        if (box == NULL) box = undef(INTERP);
+        if (box == NULL)
+            box = PMCNULL;
         return VTABLE_get_pmc_keyed(INTERP, box, nextkey);
     }
 
@@ -774,7 +749,7 @@
 */
 
     void unshift_integer (INTVAL value) {
-        PMC * val = undef(INTERP);
+        PMC * val = PMCNULL;
         list_unshift(INTERP, (List*)PMC_data(SELF), val, enum_type_PMC);
         VTABLE_set_integer_native(INTERP, val, value);
     }
@@ -791,7 +766,7 @@
 */
 
     void unshift_float (FLOATVAL value) {
-        PMC * val = undef(INTERP);
+        PMC * val = PMCNULL;
         list_unshift(INTERP, (List*)PMC_data(SELF), val, enum_type_PMC);
         VTABLE_set_number_native(INTERP, val, value);
     }
@@ -808,7 +783,7 @@
 */
 
     void unshift_string (STRING *value) {
-        PMC * val = undef(INTERP);
+        PMC * val = PMCNULL;
         list_unshift(INTERP, (List*)PMC_data(SELF), val, enum_type_PMC);
         VTABLE_set_string_native(INTERP, val, value);
     }
@@ -1277,10 +1252,6 @@
 
 F<src/list.c>, F<include/parrot/list.h>
 
-=head1 TODO
-
-Create global immutable undef object.
-
 =cut
 
 */

Reply via email to