[pypy-svn] r28448 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Wed Jun 7 15:11:21 CEST 2006


Author: antocuni
Date: Wed Jun  7 15:11:20 2006
New Revision: 28448

Modified:
   pypy/dist/pypy/translator/cli/database.py
   pypy/dist/pypy/translator/cli/test/test_oo.py
Log:
- Added support for constants of type ootype.Class.

- Completed support for constants of type ootype.Instance.

- As a bonus, a failing test now passes.



Modified: pypy/dist/pypy/translator/cli/database.py
==============================================================================
--- pypy/dist/pypy/translator/cli/database.py	(original)
+++ pypy/dist/pypy/translator/cli/database.py	Wed Jun  7 15:11:20 2006
@@ -169,6 +169,8 @@
             return StringConst(db, const)
         elif isinstance(const, ootype._static_meth):
             return StaticMethodConst(db, const)
+        elif isinstance(const, ootype._class):
+            return ClassConst(db, const)
         else:
             assert False, 'Unknown constant: %s' % const
     make = staticmethod(make)
@@ -277,6 +279,29 @@
         ilasm.opcode('ldftn', signature)
         ilasm.new('instance void class %s::.ctor(object, native int)' % delegate_type)
 
+class ClassConst(AbstractConst):
+    def __init__(self, db, class_):
+        self.db = db
+        self.cts = CTS(db)
+        self.class_ = class_
+
+    def __hash__(self):
+        return hash(self.class_)
+
+    def __eq__(self, other):
+        return self.class_ == other.class_
+
+    def get_name(self):
+        return 'Class'
+
+    def get_type(self, include_class=True):
+        return self.cts.lltype_to_cts(self.class_._TYPE, include_class)
+
+    def init(self, ilasm):
+        self.cts.lltype_to_cts(self.class_._INSTANCE) # force scheduling class generation
+        classname = self.class_._INSTANCE._name
+        ilasm.opcode('ldtoken', classname)
+        ilasm.call('class [mscorlib]System.Type class [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)')
 
 class ListConst(AbstractConst):
     def __init__(self, db, list_):
@@ -323,6 +348,7 @@
 
 class InstanceConst(AbstractConst):
     def __init__(self, db, obj, static_type):
+        self.db = db
         self.cts = CTS(db)
         self.obj = obj
         if static_type is None:
@@ -347,16 +373,10 @@
         classdef = self.obj._TYPE        
         ilasm.new('instance void class %s::.ctor()' % classdef._name)
         while classdef is not None:
-            for name, (type_, default) in classdef._fields.iteritems():
-                if isinstance(type_, ootype.StaticMethod):
-                    continue
-                elif type_ is ootype.Class:
-                    value = getattr(self.obj, name)
-                    self.cts.lltype_to_cts(value._INSTANCE) # force scheduling class generation
-                    classname = value._INSTANCE._name
-                    ilasm.opcode('dup')
-                    ilasm.opcode('ldtoken', classname)
-                    ilasm.call('class [mscorlib]System.Type class [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)')
-                    ilasm.opcode('stfld class [mscorlib]System.Type %s::%s' % (classdef._name, name))
+            for name, (TYPE, default) in classdef._fields.iteritems():
+                value = getattr(self.obj, name)
+                type_ = self.cts.lltype_to_cts(TYPE)
+                ilasm.opcode('dup')
+                AbstractConst.load(self.db, TYPE, value, ilasm)
+                ilasm.opcode('stfld %s %s::%s' % (type_, classdef._name, name))
             classdef = classdef._superclass
-

Modified: pypy/dist/pypy/translator/cli/test/test_oo.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_oo.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_oo.py	Wed Jun  7 15:11:20 2006
@@ -8,21 +8,6 @@
 
         yield check, func, [int, int], (42, 13)
 
-class TestOO(CliTest):
-    def test_indirect_call(self):
-        def f():
-            return 1
-        def g():
-            return 2
-        def fn(flag):
-            if flag:
-                x = f
-            else:
-                x = g
-            return x()
-        assert self.interpret(fn, [True]) == 1
-        assert self.interpret(fn, [False]) == 2
-
 class MyClass:
     INCREMENT = 1
 
@@ -52,6 +37,35 @@
     def compute(self):
         return self.x - self.y
 
+
+class TestOO(CliTest):
+    def test_indirect_call(self):
+        def f():
+            return 1
+        def g():
+            return 2
+        def fn(flag):
+            if flag:
+                x = f
+            else:
+                x = g
+            return x()
+        assert self.interpret(fn, [True]) == 1
+        assert self.interpret(fn, [False]) == 2
+
+    def test_indirect_call_arguments(self):
+        def f(x):
+            return x+1
+        def g(x):
+            return x+2
+        def fn(flag, n):
+            if flag:
+                x = f
+            else:
+                x = g
+            return x(n)
+        assert self.interpret(fn, [True, 42]) == 43
+
 # helper functions
 def call_method(obj):
     return obj.compute()



More information about the Pypy-commit mailing list