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

antocuni at codespeak.net antocuni at codespeak.net
Thu Feb 14 10:03:41 CET 2008


Author: antocuni
Date: Thu Feb 14 10:03:39 2008
New Revision: 51478

Modified:
   pypy/dist/pypy/translator/cli/constant.py
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
make the result of fieldinfo_for_const working also as a pbc



Modified: pypy/dist/pypy/translator/cli/constant.py
==============================================================================
--- pypy/dist/pypy/translator/cli/constant.py	(original)
+++ pypy/dist/pypy/translator/cli/constant.py	Thu Feb 14 10:03:39 2008
@@ -28,7 +28,7 @@
 from pypy.translator.oosupport.constant import \
      push_constant, WeakRefConst, StaticMethodConst, CustomDictConst, \
      ListConst, ClassConst, InstanceConst, RecordConst, DictConst, \
-     BaseConstantGenerator
+     BaseConstantGenerator, AbstractConst
 from pypy.translator.cli.ilgenerator import CLIBaseGenerator
 from pypy.rpython.ootypesystem import ootype
 from pypy.translator.cli.comparer import EqualityComparer
@@ -84,6 +84,14 @@
         type = self.cts.lltype_to_cts(EXPECTED_TYPE)
         gen.ilasm.opcode('castclass', type)
 
+    def _create_complex_const(self, value):
+        from pypy.translator.cli.dotnet import _fieldinfo
+        if isinstance(value, _fieldinfo):
+            uniq = self.db.unique()
+            return CLIFieldInfoConst(self.db, value.llvalue, uniq)
+        else:
+            return BaseConstantGenerator._create_complex_const(self, value)
+
 class FieldConstGenerator(CLIBaseConstGenerator):
     pass
 
@@ -408,3 +416,26 @@
             gen.ilasm.call_method('void %s::ll_set(object)' % self.get_type(), True)
             return True
     
+
+class CLIFieldInfoConst(AbstractConst):
+    def __init__(self, db, llvalue, count):
+        AbstractConst.__init__(self, db, llvalue, count)
+        self.name = 'FieldInfo__%d' % count
+    
+    def create_pointer(self, generator):
+        constgen = generator.db.constant_generator
+        const = constgen.record_const(self.value)
+        generator.ilasm.opcode('ldtoken', CONST_CLASS)
+        generator.ilasm.call('class [mscorlib]System.Type class [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)')
+        generator.ilasm.opcode('ldstr', '"%s"' % const.name)
+        generator.ilasm.call_method('class [mscorlib]System.Reflection.FieldInfo class [mscorlib]System.Type::GetField(string)', virtual=True)
+
+    def get_type(self):
+        from pypy.translator.cli.cts import CliClassType
+        return CliClassType('mscorlib', 'System.Reflection.FieldInfo')
+
+    def initialize_data(self, constgen, gen):
+        pass
+
+    def record_dependencies(self):
+        self.db.constant_generator.record_const(self.value)

Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Thu Feb 14 10:03:39 2008
@@ -600,9 +600,13 @@
         v_inst = hop.inputarg(hop.args_r[0], arg=0)
         return hop.genop('oodowncast', [v_inst], resulttype = hop.r_result.lowleveltype)
 
+class _fieldinfo(object):
+    def __init__(self, llvalue):
+        self._TYPE = CLR.System.Reflection.FieldInfo._INSTANCE
+        self.llvalue = llvalue
 
 def fieldinfo_for_const(const):
-    assert False, 'It works only after translation'
+    return _fieldinfo(const)
 
 class Entry(ExtRegistryEntry):
     _about_ = fieldinfo_for_const
@@ -616,6 +620,13 @@
         c_llvalue = hop.inputconst(ootype.Void, llvalue)
         return hop.genop('cli_fieldinfo_for_const', [c_llvalue], resulttype = hop.r_result.lowleveltype)
 
+
+class Entry(ExtRegistryEntry):
+    _type_ = _fieldinfo
+
+    def compute_annotation(self):
+        return SomeOOInstance(CLR.System.Reflection.FieldInfo._INSTANCE)
+
 from pypy.translator.cli.query import CliNamespace
 CLR = CliNamespace(None)
 CLR._buildtree()

Modified: pypy/dist/pypy/translator/cli/test/test_dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_dotnet.py	Thu Feb 14 10:03:39 2008
@@ -537,6 +537,20 @@
         res = self.interpret(fn, [])
         assert res == 42
 
+    def test_fieldinfo_for_const_pbc(self):
+        A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
+        const = ootype.new(A)
+        fieldinfo = fieldinfo_for_const(const)
+        def fn():
+            const.xx = 42
+            obj = fieldinfo.GetValue(None)
+            # get the 'xx' field by using reflection
+            t = obj.GetType()
+            x_info = t.GetField('xx')
+            x_value = x_info.GetValue(obj)
+            return unbox(x_value, ootype.Signed)
+        res = self.interpret(fn, [])
+        assert res == 42
 
 class TestPythonnet(TestDotnetRtyping):
     # don't interpreter functions but execute them directly through pythonnet
@@ -558,3 +572,6 @@
 
     def test_fieldinfo_for_const(self):
         pass # it makes sense only during translation
+
+    def test_fieldinfo_for_const_pbc(self):
+        pass # it makes sense only during translation



More information about the Pypy-commit mailing list