[pypy-svn] r53035 - in pypy/branch/jit-hotpath/pypy/rpython/ootypesystem: . test

antocuni at codespeak.net antocuni at codespeak.net
Fri Mar 28 10:06:14 CET 2008


Author: antocuni
Date: Fri Mar 28 10:06:14 2008
New Revision: 53035

Modified:
   pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ootype.py
   pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/test/test_ootype.py
Log:
introduce a new type ootype.Object, which every non-primitive type can
be converted to.  Backends can be easily map it to System.Object or
java.lang.Object etc.



Modified: pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ootype.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ootype.py	(original)
+++ pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/ootype.py	Fri Mar 28 10:06:14 2008
@@ -43,6 +43,16 @@
         raise TypeError("%r object is not hashable" % self.__class__.__name__)
 
 
+# warning: the name Object is rebount at the end of file
+class Object(OOType):
+    """
+    A type which everything can be casted to.
+    """
+
+    def _defl(self):
+        return self._null
+
+
 class Class(OOType):
 
     def _defl(self):
@@ -691,12 +701,52 @@
     
 # ____________________________________________________________
 
+class _object(object):
+
+    def __init__(self, obj):
+        self._TYPE = Object
+        self.obj = obj or None  # null obj ==> None
+
+    def __nonzero__(self):
+        return self.obj is not None
+
+    def __eq__(self, other):
+        if not isinstance(other, _object):
+            raise TypeError("comparing an _object with %r" % other)
+        return self.obj == other.obj
+
+    def __ne__(self, other):
+        return not (self == other)
+
+    def __hash__(self):
+        return hash(self.obj)
+
+    def _cast_to_object(self):
+        return self
+
+    def _cast_to(self, EXPECTED_TYPE):
+        if self.obj is None:
+            return null(EXPECTED_TYPE)
+        elif isinstance(EXPECTED_TYPE, Instance):
+            return oodowncast(EXPECTED_TYPE, self.obj)
+        elif isinstance(EXPECTED_TYPE, SpecializableType):
+            T = typeOf(self.obj)
+            if T != EXPECTED_TYPE:
+                raise RuntimeError("Invalid cast: %s --> %s" % (T, EXPECTED_TYPE))
+            return self.obj
+        else:
+            assert False, 'to be implemented'
+
+
 class _class(object):
     _TYPE = Class
 
     def __init__(self, INSTANCE):
         self._INSTANCE = INSTANCE
 
+    def _cast_to_object(self):
+        return _object(self)
+
 nullruntimeclass = _class(None)
 
 class _instance(object):
@@ -766,6 +816,9 @@
         else:
             return 0   # for all null instances
 
+    def _cast_to_object(self):
+        return _object(ooupcast(ROOT, self))
+
 
 def _null_mixin(klass):
     class mixin(object):
@@ -876,6 +929,9 @@
     def _identityhash(self):
         return self._inst._identityhash()
 
+    def _cast_to_object(self):
+        return _object(ooupcast(ROOT, self))
+
 if STATICNESS:
     instance_impl = _view
 else:
@@ -938,6 +994,9 @@
    def __hash__(self):
        return hash(frozendict(self.__dict__))
 
+   def _cast_to_object(self):
+       return _object(self)
+
 
 class _static_meth(_callable):
    allowed_types = (StaticMethod,)
@@ -976,6 +1035,9 @@
         callb, checked_args = self.meth._checkargs(args)
         return callb(self.inst, *checked_args)
 
+    def _cast_to_object(self):
+        return _object(self)
+
 
 class _meth(_callable):
     _bound_class = _bound_meth
@@ -1101,6 +1163,8 @@
 
         return object.__getattribute__(self, name)
 
+    def _cast_to_object(self):
+        return _object(self)
 
 class _string(_builtin_type):
 
@@ -1482,6 +1546,9 @@
     def __ne__(self, other):
         return not (self == other)
 
+    def _cast_to_object(self):
+        return _object(self)
+
 class _null_record(_null_mixin(_record), _record):
 
     def __init__(self, RECORD):
@@ -1572,6 +1639,15 @@
 def oodowncast(INSTANCE, instance):
     return instance._downcast(INSTANCE)
 
+def cast_to_object(whatever):
+    TYPE = typeOf(whatever)
+    assert isinstance(TYPE, OOType)
+    return whatever._cast_to_object()
+
+def cast_from_object(EXPECTED_TYPE, obj):
+    assert typeOf(obj) is Object
+    return obj._cast_to(EXPECTED_TYPE)
+
 def ooidentityhash(inst):
     assert isinstance(typeOf(inst), (Instance, Record))
     return inst._identityhash()
@@ -1640,6 +1716,11 @@
     ref.ll_set(obj)
     return ref
 
+
+Object = Object()
+NULL = _object(None)
+Object._null = NULL
+
 ROOT = Instance('Root', None, _is_root=True)
 String = String()
 Unicode = Unicode()

Modified: pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/test/test_ootype.py
==============================================================================
--- pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/test/test_ootype.py	(original)
+++ pypy/branch/jit-hotpath/pypy/rpython/ootypesystem/test/test_ootype.py	Fri Mar 28 10:06:14 2008
@@ -535,3 +535,34 @@
     assert len(TYPE_A._lookup_graphs('ofoo')) == 2
     assert len(TYPE_B._lookup_graphs('ofoo')) == 1
     assert len(TYPE_C._lookup_graphs('ofoo')) == 1
+
+def test_cast_object_instance():
+    A = Instance("Foo", ROOT)
+    a = new(A)
+    obj = cast_to_object(a)
+    assert typeOf(obj) is Object
+    assert cast_from_object(A, obj) == a
+    a2 = cast_from_object(ROOT, obj)
+    assert typeOf(a2) is ROOT
+    assert a2 == ooupcast(ROOT, a)
+
+def test_cast_object_record():
+    R = Record({'x': Signed})
+    r = new(R)
+    r.x = 42
+    obj = cast_to_object(r)
+    assert typeOf(obj) is Object
+    r2 = cast_from_object(R, obj)
+    assert typeOf(r2) is R
+    assert r == r2
+
+def test_cast_object_null():
+    A = Instance("Foo", ROOT)
+    B = Record({'x': Signed})
+    a = null(A)
+    b = null(B)
+    obj1 = cast_to_object(a)
+    obj2 = cast_to_object(b)
+    assert obj1 == obj2
+    assert cast_from_object(A, obj1) == a
+    assert cast_from_object(B, obj2) == b


More information about the pypy-svn mailing list