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

antocuni at codespeak.net antocuni at codespeak.net
Wed Feb 13 18:46:14 CET 2008


Author: antocuni
Date: Wed Feb 13 18:46:13 2008
New Revision: 51448

Modified:
   pypy/dist/pypy/translator/cli/dotnet.py
   pypy/dist/pypy/translator/cli/metavm.py
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
Log:
fieldinfo_for_const is a new function for getting the FieldInfo
instance corresponding to the static field of the Constants clas where
the constant resides.



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Wed Feb 13 18:46:13 2008
@@ -600,6 +600,22 @@
         v_inst = hop.inputarg(hop.args_r[0], arg=0)
         return hop.genop('oodowncast', [v_inst], resulttype = hop.r_result.lowleveltype)
 
+
+def fieldinfo_for_const(const):
+    assert False, 'It works only after translation'
+
+class Entry(ExtRegistryEntry):
+    _about_ = fieldinfo_for_const
+
+    def compute_result_annotation(self, s_const):
+        assert s_const.is_constant()
+        return SomeOOInstance(CLR.System.Reflection.FieldInfo._INSTANCE)
+
+    def specialize_call(self, hop):
+        llvalue = hop.args_v[0].value
+        c_llvalue = hop.inputconst(ootype.Void, llvalue)
+        return hop.genop('cli_fieldinfo_for_const', [c_llvalue], resulttype = hop.r_result.lowleveltype)
+
 from pypy.translator.cli.query import CliNamespace
 CLR = CliNamespace(None)
 CLR._buildtree()

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Wed Feb 13 18:46:13 2008
@@ -238,6 +238,18 @@
         generator.load(op.args[2])
         generator.ilasm.store_static_field(cts_type, desc)
 
+class _FieldInfoForConst(MicroInstruction):
+    def render(self, generator, op):
+        from pypy.translator.cli.constant import CONST_CLASS
+        llvalue = op.args[0].value
+        constgen = generator.db.constant_generator
+        const = constgen.record_const(llvalue)
+        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)
+
+
 OOTYPE_TO_MNEMONIC = {
     ootype.Signed: 'i4',
     ootype.SignedLongLong: 'i8',
@@ -266,4 +278,5 @@
 EventHandler = _EventHandler()
 GetStaticField = _GetStaticField()
 SetStaticField = _SetStaticField()
+FieldInfoForConst = _FieldInfoForConst()
 CastPrimitive = _CastPrimitive()

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Wed Feb 13 18:46:13 2008
@@ -1,7 +1,8 @@
 from pypy.translator.cli.metavm import  Call, CallMethod, \
      IndirectCall, GetField, SetField, DownCast, NewCustomDict,\
      MapException, Box, Unbox, NewArray, GetArrayElem, SetArrayElem,\
-     TypeOf, CastPrimitive, EventHandler, GetStaticField, SetStaticField
+     TypeOf, CastPrimitive, EventHandler, GetStaticField, SetStaticField,\
+     FieldInfoForConst
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
     New, RuntimeNew, CastTo, PushPrimitive, OOString, OOUnicode
 from pypy.translator.cli.cts import WEAKREF
@@ -45,6 +46,7 @@
     'cli_eventhandler':         [EventHandler],
     'cli_getstaticfield':       [GetStaticField],
     'cli_setstaticfield':       [SetStaticField],
+    'cli_fieldinfo_for_const':  [FieldInfoForConst],
     'oois':                     'ceq',
     'oononnull':                [PushAllArgs, 'ldnull', 'ceq']+Not,
     'instanceof':               [CastTo, 'ldnull', 'cgt.un'],

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	Wed Feb 13 18:46:13 2008
@@ -7,7 +7,8 @@
 from pypy.translator.cli.test.runtest import CliTest
 from pypy.translator.cli.dotnet import SomeCliClass, SomeCliStaticMethod,\
      NativeInstance, CLR, box, unbox, OverloadingResolver, NativeException,\
-     native_exc, new_array, init_array, typeof, eventhandler, clidowncast
+     native_exc, new_array, init_array, typeof, eventhandler, clidowncast,\
+     fieldinfo_for_const
 
 System = CLR.System
 ArrayList = CLR.System.Collections.ArrayList
@@ -521,6 +522,21 @@
             return f
         self.interpret(fn, [])
 
+    def test_fieldinfo_for_const(self):
+        A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
+        const = ootype.new(A)
+        const.xx = 42
+        def fn():
+            fieldinfo = fieldinfo_for_const(const)
+            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
@@ -539,3 +555,6 @@
             return t.get_Name()
         res = self.interpret(fn, [])
         assert res == 'DelegateType_int__int_2'
+
+    def test_fieldinfo_for_const(self):
+        pass # it makes sense only during translation



More information about the Pypy-commit mailing list