Author: allison
Date: Thu Oct  4 14:09:52 2007
New Revision: 21848

Modified:
   branches/pdd15oo/src/oo.c
   branches/pdd15oo/src/pmc/class.pmc
   branches/pdd15oo/src/pmc/namespace.pmc
   branches/pdd15oo/t/oo/new.t

Log:
[pdd15oo] Allow string arrays as keys for creating and instantiating classes.


Modified: branches/pdd15oo/src/oo.c
==============================================================================
--- branches/pdd15oo/src/oo.c   (original)
+++ branches/pdd15oo/src/oo.c   Thu Oct  4 14:09:52 2007
@@ -134,7 +134,8 @@
         classobj = VTABLE_get_class(interp, key);
     }
     else if (VTABLE_isa(interp, key, CONST_STRING(interp, "String"))
-            || VTABLE_isa(interp, key, CONST_STRING(interp, "Key"))) {
+          || VTABLE_isa(interp, key, CONST_STRING(interp, "Key"))
+          || VTABLE_isa(interp, key, CONST_STRING(interp, 
"ResizableStringArray"))) {
         PMC *hll_ns = VTABLE_get_pmc_keyed_int(interp, interp->HLL_namespace,
                 CONTEXT(interp->ctx)->current_HLL);
         PMC *ns = Parrot_get_namespace_keyed(interp, hll_ns, key);

Modified: branches/pdd15oo/src/pmc/class.pmc
==============================================================================
--- branches/pdd15oo/src/pmc/class.pmc  (original)
+++ branches/pdd15oo/src/pmc/class.pmc  Thu Oct  4 14:09:52 2007
@@ -521,6 +521,7 @@
             case enum_class_String:
             case enum_class_Key:
             case enum_class_NameSpace:
+            case enum_class_ResizableStringArray:
                 arg = pmc_new(interp, enum_class_Hash);
                 VTABLE_set_pmc_keyed_str(interp, arg, name_str, init_data);
                 break;
@@ -531,11 +532,9 @@
 
             /* slow attempt to determine init_data type */
             default:
-                if (VTABLE_isa(interp, init_data, CONST_STRING(interp, 
"String"))) {
-                    arg = pmc_new(interp, enum_class_Hash);
-                    VTABLE_set_pmc_keyed_str(interp, arg, name_str, init_data);
-                }
-                else if (VTABLE_isa(interp, init_data, CONST_STRING(interp, 
"Key"))) {
+                if (VTABLE_isa(interp, init_data, CONST_STRING(interp, 
"String"))
+                 || VTABLE_isa(interp, init_data, CONST_STRING(interp, "Key"))
+                 || VTABLE_isa(interp, init_data, CONST_STRING(interp, 
"ResizableStringArray"))) {
                     arg = pmc_new(interp, enum_class_Hash);
                     VTABLE_set_pmc_keyed_str(interp, arg, name_str, init_data);
                 }
@@ -544,8 +543,8 @@
                     arg = init_data;
                 }
                 else {
-                    arg = pmc_new(interp, enum_class_Hash);
-                    VTABLE_set_pmc_keyed_str(interp, arg, name_str, init_data);
+                    real_exception(INTERP, NULL, E_NameError,
+                            "Invalid class name key in init_pmc for Class");
                 }
                 break;
         }

Modified: branches/pdd15oo/src/pmc/namespace.pmc
==============================================================================
--- branches/pdd15oo/src/pmc/namespace.pmc      (original)
+++ branches/pdd15oo/src/pmc/namespace.pmc      Thu Oct  4 14:09:52 2007
@@ -318,6 +318,21 @@
             }
         }
 
+        if (key->vtable->base_type == enum_class_ResizableStringArray) {
+            INTVAL elements = VTABLE_elements(interp, key);
+            INTVAL i;
+            for (i = 0; i < elements; ++i)  {
+                part = VTABLE_get_string_keyed_int(interp, key, i);
+
+                if ((i + 1) >= elements) { /* Last entry in the array */
+                    Parrot_set_global(INTERP, ns, part, value);
+                    return;
+                }
+
+                ns = Parrot_make_namespace_keyed_str(INTERP, ns, part);
+            }
+        }
+
         real_exception(INTERP, NULL, E_NameError,
             "Invalid namespace key in set_pmc_keyed");
     }
@@ -428,6 +443,23 @@
             return ns;
         }
 
+        if (key->vtable->base_type == enum_class_ResizableStringArray) {
+            INTVAL elements = VTABLE_elements(interp, key);
+            INTVAL i;
+            for (i = 0; i < elements; ++i)  {
+                part = VTABLE_get_string_keyed_int(interp, key, i);
+
+                if ((i + 1) >= elements) /* Last entry in the array */
+                    return VTABLE_get_pointer_keyed_str(INTERP, ns, part);
+
+                ns = Parrot_get_namespace_keyed_str(INTERP, ns, part);
+
+                if (PMC_IS_NULL(ns))
+                    return PMCNULL;
+            }
+            return ns;
+        }
+
         real_exception(INTERP, NULL, E_NameError,
             "Invalid namespace key in get_pointer_keyed");
     }

Modified: branches/pdd15oo/t/oo/new.t
==============================================================================
--- branches/pdd15oo/t/oo/new.t (original)
+++ branches/pdd15oo/t/oo/new.t Thu Oct  4 14:09:52 2007
@@ -6,7 +6,7 @@
 use warnings;
 use lib qw( . lib ../lib ../../lib );
 use Test::More;
-use Parrot::Test tests => 18;
+use Parrot::Test tests => 21;
 
 =head1 NAME
 
@@ -224,10 +224,6 @@
     $S1 = typeof $P2
     say $S1
 
-    $I3 = isa $P2, 'Bar'
-    print $I3
-    print "\n"
-
     $I3 = isa $P2, ['Foo';'Bar']
     print $I3
     print "\n"
@@ -242,7 +238,6 @@
 Foo;Bar
 1
 1
-1
 OUT
 
 pir_output_is(
@@ -280,6 +275,71 @@
 1
 OUT
 
+pir_output_is( <<'CODE', <<'OUT', 'create and instantiate from array of names' 
);
+.sub main :main
+    $P0 = split " ", "Foo Bar"
+    $P1 = newclass $P0
+    $S1 = typeof $P1
+    say $S1
+
+    $I3 = isa $P1, "Class"
+    print $I3
+    print "\n"
+
+    $P2 = new $P0
+
+    $S1 = typeof $P2
+    say $S1
+
+    $I3 = isa $P2, ['Foo';'Bar']
+    print $I3
+    print "\n"
+
+    $I3 = isa $P2, "Object"
+    print $I3
+    print "\n"
+.end
+CODE
+Class
+1
+Foo;Bar
+1
+1
+OUT
+
+pir_error_output_like( <<'CODE', <<'OUT', 'only string arrays work for 
creating classes' );
+.sub main :main
+    $P0 = new 'ResizablePMCArray'
+    $P10 = new 'String'
+    $P10 = 'Foo'
+    $P11 = new 'String'
+    $P11 = 'Bar'
+
+    $P1 = newclass $P0
+    $S1 = typeof $P1
+    say $S1
+
+    $I3 = isa $P1, "Class"
+    print $I3
+    print "\n"
+
+    $P2 = new $P0
+
+    $S1 = typeof $P2
+    say $S1
+
+    $I3 = isa $P2, ['Foo';'Bar']
+    print $I3
+    print "\n"
+
+    $I3 = isa $P2, "Object"
+    print $I3
+    print "\n"
+.end
+CODE
+/Invalid class name key/
+OUT
+
 pir_output_is( <<'CODE', <<'OUT', 'instantiate from class object with init' );
 .sub main :main
     $P1 = newclass "Foo"
@@ -411,6 +471,39 @@
 data for Foo
 OUT
 
+pir_output_is( <<'CODE', <<'OUT', 'instantiate from array of names with init' 
);
+.sub main :main
+    $P0 = split " ", "Foo Bar"
+    $P1 = newclass $P0
+    addattribute $P1, 'data'
+    $P3 = new 'Hash'
+    $P4 = new 'String'
+    $P4 = "data for Foo;Bar\n"
+    $P3['data'] = $P4
+
+    $P2 = new $P0, $P3
+
+    $S1 = typeof $P2
+    say $S1
+
+    $I3 = isa $P2, ["Foo";"Bar"]
+    print $I3
+    print "\n"
+
+    $I3 = isa $P2, "Object"
+    print $I3
+    print "\n"
+
+    $P5 = getattribute $P2, 'data'
+    print $P5
+.end
+CODE
+Foo;Bar
+1
+1
+data for Foo;Bar
+OUT
+
 pir_output_is( <<'CODE', <<'OUT', 'instantiate from key name with init', todo 
=> 'init keyed' );
 .sub main :main
     $P1 = newclass ['Foo';'Bar']

Reply via email to