[svn:parrot] r45272 - in trunk: src/pmc t/pmc

whiteknight at svn.parrot.org whiteknight at svn.parrot.org
Mon Mar 29 12:14:30 UTC 2010


Author: whiteknight
Date: Mon Mar 29 12:14:29 2010
New Revision: 45272
URL: https://trac.parrot.org/parrot/changeset/45272

Log:
add clear_method_cache and get_method_cache methods to the Class PMC. Add tests to verify their behavior. This should satisfy the request in TT #1497.

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

Modified: trunk/src/pmc/class.pmc
==============================================================================
--- trunk/src/pmc/class.pmc	Mon Mar 29 06:53:06 2010	(r45271)
+++ trunk/src/pmc/class.pmc	Mon Mar 29 12:14:29 2010	(r45272)
@@ -2007,6 +2007,23 @@
         RETURN(INTVAL does);
     }
 
+    METHOD clear_method_cache() {
+        Parrot_Class_attributes * const attrs = PARROT_OBJECT(SELF);
+        PMC * const cache = attrs->meth_cache;
+        if (cache)
+            attrs->meth_cache = PMCNULL;
+    }
+
+    METHOD get_method_cache() {
+        Parrot_Class_attributes * const attrs = PARROT_OBJECT(SELF);
+        PMC * cache = attrs->meth_cache;
+        if (!cache) {
+            cache = Parrot_pmc_new(INTERP, enum_class_Hash);
+            attrs->meth_cache = cache;
+        }
+        RETURN(PMC *cache);
+    }
+
 } /* END pmclass */
 
 /*

Modified: trunk/t/pmc/class.t
==============================================================================
--- trunk/t/pmc/class.t	Mon Mar 29 06:53:06 2010	(r45271)
+++ trunk/t/pmc/class.t	Mon Mar 29 12:14:29 2010	(r45272)
@@ -17,7 +17,7 @@
 =cut
 
 
-.const int TESTS = 63
+.const int TESTS = 73
 
 
 .sub 'main' :main
@@ -46,6 +46,7 @@
      'does'()
      'more does'()
      'anon_inherit'()
+     'method_cache_tt1497'()
 .end
 
 
@@ -602,6 +603,88 @@
     ok(1, 'inheritance of two different anonymous classes works')
 .end
 
+.sub 'method_cache_tt1497'
+    $P0 = new ["tt1497_Object"]
+
+    $P1 = find_method $P0, "foo"
+    $I0 = isnull $P1
+    is($I0, 0, "can find foo. Sanity")
+    $I0 = $P0.$P1()
+    is($I0, 1, "found the correct foo")
+
+    $P9 = box 2
+    setattribute $P0, "state", $P9
+
+    $P1 = find_method $P0, "foo"
+    $I0 = isnull $P1
+    is($I0, 0, "can find foo. Sanity")
+    $I0 = $P0.$P1()
+    is($I0, 1, "we've cached the old foo")
+
+    $P2 = get_class "tt1497_Object"
+    $P2.'clear_method_cache'()
+
+    $P1 = find_method $P0, "foo"
+    $I0 = isnull $P1
+    is($I0, 0, "can find foo. Sanity")
+    $I0 = $P0.$P1()
+    is($I0, 2, "cleared cache, can find the next foo")
+
+    $P3 = $P2.'get_method_cache'()
+    $P1 = $P3["foo"]
+    $I0 = isnull $P1
+    is($I0, 0, "can find foo in method cache")
+    $I0 = $P0.$P1()
+    is($I0, 2, "cleared cache, can find the next foo")
+
+    $P9 = box 1
+    setattribute $P0, "state", $P9
+
+    $P3 = $P2.'get_method_cache'()
+    $P1 = $P3["foo"]
+    $I0 = isnull $P1
+    is($I0, 0, "can find foo in method cache")
+    $I0 = $P0.$P1()
+    is($I0, 2, "cleared cache, can find the next foo")
+.end
+
+.namespace ["tt1497_Object"]
+
+.sub '__tt1497_init' :anon :load :init
+    $P0 = newclass "tt1497_Object"
+    addattribute $P0, "state"
+.end
+
+.sub 'foo1'
+    .return(1)
+.end
+
+.sub 'foo2'
+    .return(2)
+.end
+
+.sub 'find_method' :vtable
+    .param string name
+    $P0 = getattribute self, "state"
+    unless null $P0 goto have_state
+    $P0 = box 1
+    setattribute self, "state", $P0
+  have_state:
+    if $P0 == 1 goto getfoo1
+    if $P0 == 2 goto getfoo2
+    $P0 = null
+    goto return_meth
+  getfoo1:
+    .const 'Sub' foo1 = "foo1"
+    $P0 = foo1
+    goto return_meth
+  getfoo2:
+    .const 'Sub' foo2 = "foo2"
+    $P0 = foo2
+  return_meth:
+    .return($P0)
+.end
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100


More information about the parrot-commits mailing list