Author: allison
Date: Sun Jun 17 16:13:52 2007
New Revision: 19072

Modified:
   trunk/src/pmc/class.pmc
   trunk/src/pmc/object.pmc

Log:
[pdd15] Implement 'isa' for Class and Object PMCs.


Modified: trunk/src/pmc/class.pmc
==============================================================================
--- trunk/src/pmc/class.pmc     (original)
+++ trunk/src/pmc/class.pmc     Sun Jun 17 16:13:52 2007
@@ -853,6 +853,45 @@
         return copy;
     }
 
+/*
+
+=item C<INTVAL isa(STRING *classname)>
+
+Returns whether the class is or inherits from C<*classname>.
+
+=cut
+
+*/
+
+    INTVAL isa(STRING *classname) {
+        Parrot_Class *_class = PARROT_CLASS(SELF);
+        const INTVAL num_classes = VTABLE_elements(interp, _class->parents);
+        STRING *name;
+        INTVAL i;
+
+        if (SUPER(classname))
+            return 1;
+
+        /* Check if the passed name is the same as the stored short name. */
+        if (string_equal(interp, classname, _class->name) == 0)
+            return 1;
+
+        /* Check if the passed name is the same as the fully qualified name. */
+        if (string_equal(interp, classname, VTABLE_get_string(interp, SELF)) 
== 0)
+            return 1;
+
+        /* Iterate over all the parents and check if they respond true
+         * for 'isa' on the classname.
+         */
+        for (i = 0; i < num_classes; i++) {
+            PMC *cur_class = VTABLE_get_pmc_keyed_int(interp, _class->parents, 
i);
+            if (VTABLE_isa(interp, cur_class, classname))
+                return 1;
+        }
+
+        return 0;
+    }
+
     /* **********************************************************************
      * Below here are non-vtable methods that eventually will go in a role
      * that is composed into here to optionally give a nice interface from

Modified: trunk/src/pmc/object.pmc
==============================================================================
--- trunk/src/pmc/object.pmc    (original)
+++ trunk/src/pmc/object.pmc    Sun Jun 17 16:13:52 2007
@@ -240,6 +240,28 @@
         return !PMC_IS_NULL(method);
     }
 
+/*
+
+=item C<INTVAL isa(STRING *classname)>
+
+Returns whether the class is or inherits from C<*classname>.
+
+=cut
+
+*/
+    INTVAL isa(STRING *classname)
+    {
+        PMC *class;
+        INTVAL isa;
+
+        if (SUPER(classname))
+            return 1;
+
+        class = VTABLE_get_class(interp, SELF);
+        isa = VTABLE_isa(interp, class, classname);
+        return isa;
+    }
+
 }
 
 /*

Reply via email to