Commits

Amaury Forgeot d'Arc committed 64577a6

Finally found a way to add methods to controller classes

  • Participants
  • Parent commits 78f7cb0
  • Branches sepcomp2

Comments (0)

Files changed (2)

pypy/rpython/controllerentry.py

         from pypy.rpython.rcontrollerentry import rtypedelegate
         return rtypedelegate(self.new, hop, revealargs=[], revealresult=True)
 
+    def bound_method_controller(self, attr):
+        class BoundMethod(object): pass
+        class BoundMethodController(Controller):
+            knowntype = BoundMethod
+            def call(_self, obj, *args):
+                return getattr(self, 'method_' + attr)(obj, *args)
+        return BoundMethodController()
+    bound_method_controller._annspecialcase_ = 'specialize:memo'
+
     def getattr(self, obj, attr):
+        if hasattr(self, 'method_' + attr):
+            return self.bound_method_controller(attr).box(obj)
         return getattr(self, 'get_' + attr)(obj)
     getattr._annspecialcase_ = 'specialize:arg(0, 2)'
 

pypy/rpython/test/test_controllerentry.py

     def set_foo(self, obj, value):
         value.append(obj)
 
+    def method_compute(self, obj, value):
+        return obj + value
+
     def getitem(self, obj, key):
         return obj + key
 
     assert ''.join(res.item0.chars) == "4_bar"
     assert ''.join(res.item1.chars) == "4_foo"
     assert ''.join(res.item2.chars) == "4_baz"
+
+def fun4(a):
+    c = C(a)
+    return c.compute('bar')
+
+def test_boundmethods_annotate():
+    a = RPythonAnnotator()
+    s = a.build_types(fun4, [a.bookkeeper.immutablevalue("5")])
+    assert s.const == "5_bar"
+
+def test_boundmethods_specialize():
+    res = interpret(fun4, ["5"])
+    assert ''.join(res.chars) == "5_bar"