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

antocuni at codespeak.net antocuni at codespeak.net
Thu Oct 26 17:25:36 CEST 2006


Author: antocuni
Date: Thu Oct 26 17:25:33 2006
New Revision: 33777

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/query.py
   pypy/dist/pypy/translator/cli/src/query.cs
   pypy/dist/pypy/translator/cli/test/test_dotnet.py
   pypy/dist/pypy/translator/cli/test/test_query.py
Log:
Initial array support: so far you can't create new arrays but only
manipulate existing ones, and you can only get an item, not set.



Modified: pypy/dist/pypy/translator/cli/dotnet.py
==============================================================================
--- pypy/dist/pypy/translator/cli/dotnet.py	(original)
+++ pypy/dist/pypy/translator/cli/dotnet.py	Thu Oct 26 17:25:33 2006
@@ -1,14 +1,17 @@
+from pypy.annotation.pairtype import pair, pairtype
+from pypy.annotation.model import SomeObject, SomeOOInstance, SomeInteger, s_ImpossibleValue
 from pypy.rpython.error import TyperError
 from pypy.rpython.extregistry import ExtRegistryEntry
+from pypy.rpython.rmodel import Repr
+from pypy.rpython.rint import IntegerRepr
+from pypy.rpython.ootypesystem.rootype import OOInstanceRepr
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.ootypesystem.ootype import meth, overload, Meth, StaticMethod
-from pypy.annotation import model as annmodel
-from pypy.rpython.rmodel import Repr
 from pypy.translator.cli.support import PythonNet
 
 ## Annotation model
 
-class SomeCliClass(annmodel.SomeObject):
+class SomeCliClass(SomeObject):
     def getattr(self, s_attr):
         assert self.is_constant()
         assert s_attr.is_constant()
@@ -16,7 +19,7 @@
 
     def simple_call(self, *s_args):
         assert self.is_constant()
-        return annmodel.SomeOOInstance(self.const._INSTANCE)
+        return SomeOOInstance(self.const._INSTANCE)
 
     def rtyper_makerepr(self, rtyper):
         return CliClassRepr(self.const)
@@ -25,7 +28,7 @@
         return self.__class__, self.const
 
 
-class SomeCliStaticMethod(annmodel.SomeObject):
+class SomeCliStaticMethod(SomeObject):
     def __init__(self, cli_class, meth_name):
         self.cli_class = cli_class
         self.meth_name = meth_name
@@ -39,6 +42,12 @@
     def rtyper_makekey(self):
         return self.__class__, self.cli_class, self.meth_name
 
+class __extend__(pairtype(SomeOOInstance, SomeInteger)):
+    def getitem((ooinst, index)):
+        if ooinst.ootype._isArray:
+            return SomeOOInstance(ooinst.ootype._ELEMENT)
+        return s_ImpossibleValue
+    
 
 ## Rtyper model
 
@@ -78,6 +87,15 @@
         cDesc = hop.inputconst(ootype.Void, desc)
         return hop.genop("direct_call", [cDesc] + vlist, resulttype=resulttype)
 
+class __extend__(pairtype(OOInstanceRepr, IntegerRepr)):
+
+    def rtype_getitem((r_inst, r_int), hop):
+        if not r_inst.lowleveltype._isArray:
+            raise TyperError("getitem() on a non-array instance")
+        v_array, v_index = hop.inputargs(r_inst, ootype.Signed)
+        hop.exception_is_here()
+        return hop.genop('cli_getelem', [v_array, v_index], hop.r_result.lowleveltype)
+
 
 ## OOType model
 
@@ -219,7 +237,7 @@
     _about_ = box
 
     def compute_result_annotation(self, x_s):
-        return annmodel.SomeOOInstance(CLR.System.Object._INSTANCE)
+        return SomeOOInstance(CLR.System.Object._INSTANCE)
 
     def specialize_call(self, hop):
         v_obj, = hop.inputargs(*hop.args_r)
@@ -235,7 +253,7 @@
     _about_ = unbox
 
     def compute_result_annotation(self, x_s, type_s):
-        assert isinstance(x_s, annmodel.SomeOOInstance)
+        assert isinstance(x_s, SomeOOInstance)
         assert x_s.ootype == CLR.System.Object._INSTANCE
         assert type_s.is_constant()
         TYPE = type_s.const

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Thu Oct 26 17:25:33 2006
@@ -172,6 +172,13 @@
         generator.load(v_obj)
         generator.ilasm.opcode('unbox.any', boxtype)
 
+class _GetArrayElem(MicroInstruction):
+    def render(self, generator, op):
+        generator.load(op.args[0])
+        generator.load(op.args[1])
+        rettype = generator.cts.lltype_to_cts(op.result.concretetype)
+        generator.ilasm.opcode('ldelem', rettype)
+
 Call = _Call()
 CallMethod = _CallMethod()
 IndirectCall = _IndirectCall()
@@ -182,3 +189,4 @@
 CastWeakAdrToPtr = _CastWeakAdrToPtr()
 Box = _Box()
 Unbox = _Unbox()
+GetArrayElem = _GetArrayElem()

Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Thu Oct 26 17:25:33 2006
@@ -1,6 +1,6 @@
 from pypy.translator.cli.metavm import  Call, CallMethod, RuntimeNew, \
      IndirectCall, GetField, SetField, CastTo, OOString, DownCast, NewCustomDict,\
-     CastWeakAdrToPtr, MapException, Box, Unbox
+     CastWeakAdrToPtr, MapException, Box, Unbox, GetArrayElem
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult, InstructionList,\
     New
 from pypy.translator.cli.cts import WEAKREF
@@ -36,6 +36,7 @@
     'oodowncast':               [DownCast],
     'clibox':                   [Box],
     'cliunbox':                 [Unbox],
+    'cli_getelem':              [GetArrayElem],
     'oois':                     'ceq',
     'oononnull':                [PushAllArgs, 'ldnull', 'ceq']+Not,
     'instanceof':               [CastTo, 'ldnull', 'cgt.un'],

Modified: pypy/dist/pypy/translator/cli/query.py
==============================================================================
--- pypy/dist/pypy/translator/cli/query.py	(original)
+++ pypy/dist/pypy/translator/cli/query.py	Thu Oct 26 17:25:33 2006
@@ -113,6 +113,10 @@
         load_class_maybe(self.BaseType)
         BASETYPE = ClassCache[self.BaseType]._INSTANCE
         TYPE = NativeInstance('[mscorlib]', namespace, name, BASETYPE, {}, {})
+        TYPE._isArray = self.IsArray
+        if self.IsArray:
+            load_class_maybe(self.ElementType)
+            TYPE._ELEMENT = ClassCache[self.ElementType]._INSTANCE
         Class = CliClass(TYPE, {})
         OOTypeCache[self.OOType] = TYPE
         ClassCache[self.FullName] = Class

Modified: pypy/dist/pypy/translator/cli/src/query.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/query.cs	(original)
+++ pypy/dist/pypy/translator/cli/src/query.cs	Thu Oct 26 17:25:33 2006
@@ -35,6 +35,9 @@
         Console.WriteLine("FullName = '{0}'", t.FullName);
         Console.WriteLine("BaseType = '{0}'", GetBaseType(t));
         Console.WriteLine("OOType = '{0}'", GetOOType(t));
+        Console.WriteLine("IsArray = {0}", t.IsArray);
+        if (t.IsArray)
+            Console.WriteLine("ElementType = '{0}'", t.GetElementType().FullName);
         PrintMethods("StaticMethods", t.GetMethods(BindingFlags.Static|BindingFlags.Public|BindingFlags.DeclaredOnly));
         PrintMethods("Methods", t.GetMethods(BindingFlags.Instance|BindingFlags.Public|BindingFlags.DeclaredOnly));
         PendingTypes.Remove(t);
@@ -53,7 +56,9 @@
 
     private static string GetOOType(Type t)
     {
-        if (t == typeof(void))
+        if (t == null)
+            return "";
+        else if (t == typeof(void))
             return "ootype.Void";
         else if (t == typeof(int))
             return "ootype.Signed";

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 Oct 26 17:25:33 2006
@@ -9,7 +9,6 @@
 System = CLR.System
 Math = CLR.System.Math
 ArrayList = CLR.System.Collections.ArrayList
-StringBuilder = CLR.System.Text.StringBuilder
 
 class TestDotnetAnnotation(object):
 
@@ -72,6 +71,25 @@
         s = a.build_types(fn, [])
         assert isinstance(s, annmodel.SomeInteger)
 
+    def test_array(self):
+        def fn():
+            x = ArrayList()
+            return x.ToArray()
+        a = RPythonAnnotator()
+        s = a.build_types(fn, [])
+        assert isinstance(s, annmodel.SomeOOInstance)
+        assert s.ootype._isArray
+        assert s.ootype._ELEMENT._name == '[mscorlib]System.Object'
+
+    def test_array_getitem(self):
+        def fn():
+            x = ArrayList().ToArray()
+            return x[0]
+        a = RPythonAnnotator()
+        s = a.build_types(fn, [])
+        assert isinstance(s, annmodel.SomeOOInstance)
+        assert s.ootype._name == '[mscorlib]System.Object'
+
 class TestDotnetRtyping(CliTest):
     def _skip_pythonnet(self):
         pass
@@ -99,6 +117,7 @@
         assert self.interpret(fn, []) == 2
 
     def test_tostring(self):
+        StringBuilder = CLR.System.Text.StringBuilder
         def fn():
             x = StringBuilder()
             x.Append("foo").Append("bar")
@@ -140,6 +159,14 @@
                 return 43
         assert self.interpret(fn, []) == 42
 
+    def test_array(self):
+        def fn():
+            x = ArrayList()
+            x.Add(box(42))
+            array = x.ToArray()
+            return unbox(array[0], ootype.Signed)
+        assert self.interpret(fn, []) == 42
+
 
 class TestPythonnet(TestDotnetRtyping):
     # don't interpreter functions but execute them directly through pythonnet

Modified: pypy/dist/pypy/translator/cli/test/test_query.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_query.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_query.py	Thu Oct 26 17:25:33 2006
@@ -24,6 +24,12 @@
     assert 'Equals' in Object._static_methods
     assert 'ToString' in Object._INSTANCE._methods
 
+def test_array():
+    query.load_class_or_namespace('System.Object[]')
+    cls = query.ClassCache['System.Object[]']
+    assert cls._INSTANCE._isArray
+    assert cls._INSTANCE._ELEMENT is CLR.System.Object._INSTANCE
+
 def test_savedesc():
     from pypy.tool.udir import udir
     CLR.System.Object # force System.Object to be loaded



More information about the Pypy-commit mailing list