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

antocuni at codespeak.net antocuni at codespeak.net
Thu Nov 30 15:06:23 CET 2006


Author: antocuni
Date: Thu Nov 30 15:06:23 2006
New Revision: 35160

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:
New cli built-in: typeof, whose semantics is similar to the
corresponding C# keyword.



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Thu Nov 30 15:06:23 2006
@@ -435,3 +435,21 @@
             hop.genop('cli_setelem', [v_array, c_index, v_elem], ootype.Void)
         return v_array
 
+
+def typeof(cliClass):
+    TYPE = cliClass._INSTANCE
+    name = '%s.%s' % (TYPE._namespace, TYPE._classname)
+    return PythonNet.System.Type.GetType(name)
+
+class Entry(ExtRegistryEntry):
+    _about_ = typeof
+
+    def compute_result_annotation(self, cliClass_s):
+        from query import load_class_maybe
+        assert cliClass_s.is_constant()
+        cliType = load_class_maybe('System.Type')
+        return SomeOOInstance(cliType._INSTANCE)
+
+    def specialize_call(self, hop):
+        v_type, = hop.inputargs(*hop.args_r)
+        return hop.genop('cli_typeof', [v_type], hop.r_result.lowleveltype)

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Thu Nov 30 15:06:23 2006
@@ -210,6 +210,15 @@
         elemtype = generator.cts.lltype_to_cts(v_array.concretetype)
         generator.ilasm.opcode('stelem', elemtype)
 
+class _TypeOf(MicroInstruction):
+    def render(self, generator, op):
+        v_type, = op.args
+        assert v_type.concretetype is ootype.Void
+        cliClass = v_type.value
+        fullname = cliClass._INSTANCE._name
+        generator.ilasm.opcode('ldtoken', fullname)
+        generator.ilasm.call('class [mscorlib]System.Type class [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)')
+
 
 Call = _Call()
 CallMethod = _CallMethod()
@@ -224,3 +233,4 @@
 NewArray = _NewArray()
 GetArrayElem = _GetArrayElem()
 SetArrayElem = _SetArrayElem()
+TypeOf = _TypeOf()

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Thu Nov 30 15:06:23 2006
@@ -1,6 +1,7 @@
 from pypy.translator.cli.metavm import  Call, CallMethod, \
      IndirectCall, GetField, SetField, CastTo, OOString, DownCast, NewCustomDict,\
-     CastWeakAdrToPtr, MapException, Box, Unbox, NewArray, GetArrayElem, SetArrayElem
+     CastWeakAdrToPtr, MapException, Box, Unbox, NewArray, GetArrayElem, SetArrayElem,\
+     TypeOf
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
     New, RuntimeNew
 from pypy.translator.cli.cts import WEAKREF
@@ -39,6 +40,7 @@
     'cli_newarray':             [NewArray],
     'cli_getelem':              [GetArrayElem],
     'cli_setelem':              [SetArrayElem],
+    'cli_typeof':               [TypeOf],
     '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	Thu Nov 30 15:06:23 2006
@@ -7,7 +7,7 @@
 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
+     native_exc, new_array, init_array, typeof
 
 System = CLR.System
 Math = CLR.System.Math
@@ -111,7 +111,8 @@
         a = RPythonAnnotator()
         s = a.build_types(fn, [])
         assert isinstance(s, annmodel.SomeOOInstance)
-        assert s.ootype._name == '[mscorlib]System.Object'            
+        assert s.ootype._name == '[mscorlib]System.Object'
+
 
 class TestDotnetRtyping(CliTest):
     def _skip_pythonnet(self, msg):
@@ -274,6 +275,13 @@
         res = self.ll_to_string(self.interpret(fn, []))
         assert res.startswith("Index is less than 0")
 
+    def test_typeof(self):
+        def fn():
+            x = box(42)
+            return x.GetType() == typeof(System.Int32)
+        res = self.interpret(fn, [])
+        assert res is True
+
 class TestPythonnet(TestDotnetRtyping):
     # don't interpreter functions but execute them directly through pythonnet
     def interpret(self, f, args):



More information about the Pypy-commit mailing list