[pypy-svn] r47947 - in pypy/dist/pypy/lang/smalltalk: . test

arigo at codespeak.net arigo at codespeak.net
Thu Oct 25 17:16:59 CEST 2007


Author: arigo
Date: Thu Oct 25 17:16:59 2007
New Revision: 47947

Added:
   pypy/dist/pypy/lang/smalltalk/test/test_shadow.py   (contents, props changed)
Modified:
   pypy/dist/pypy/lang/smalltalk/classtable.py
   pypy/dist/pypy/lang/smalltalk/constants.py
   pypy/dist/pypy/lang/smalltalk/model.py
   pypy/dist/pypy/lang/smalltalk/shadow.py
   pypy/dist/pypy/lang/smalltalk/test/test_classtable.py
Log:
(toon, arigo, akuhn around)
* removed s_metaclass from the ClassShadow, where it is not really useful
* our first test for update_shadow()


Modified: pypy/dist/pypy/lang/smalltalk/classtable.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/classtable.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/classtable.py	Thu Oct 25 17:16:59 2007
@@ -4,13 +4,12 @@
 def bootstrap_class(instsize, w_superclass=None, w_metaclass=None,
                     name='?', format=shadow.POINTERS, varsized=False):
     from pypy.lang.smalltalk import model
-    w_class = model.W_PointersObject(None, 0) # a dummy placeholder for testing
+    w_class = model.W_PointersObject(w_metaclass, 0)
+                                             # a dummy placeholder for testing
     s = shadow.ClassShadow(w_class)
     s.methoddict = {}
     if w_superclass is not None:
         s.s_superclass = w_superclass.as_class_get_shadow()
-    if w_metaclass is not None:
-        s.s_metaclass = w_metaclass.as_class_get_shadow()
     s.name = name
     s.instance_size = instsize
     s.instance_kind = format
@@ -53,12 +52,11 @@
     w_Metaclass = classtable["w_Metaclass"]
     w_ProtoObjectClass.as_class_get_shadow().s_superclass = \
         w_Class.as_class_get_shadow()
-    # at this point, all classes that still lack a w_metaclass are themselves
+    # at this point, all classes that still lack a w_class are themselves
     # metaclasses
     for nm, w_cls_obj in classtable.items():
-        s = w_cls_obj.as_class_get_shadow()
-        if s.s_metaclass is None:
-            s.s_metaclass = w_Metaclass.as_class_get_shadow()
+        if w_cls_obj.w_class is None:
+            w_cls_obj.w_class = w_Metaclass
 create_classtable()
 
 def copy_in_globals_classes_known_to_the_vm():

Modified: pypy/dist/pypy/lang/smalltalk/constants.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/constants.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/constants.py	Thu Oct 25 17:16:59 2007
@@ -12,7 +12,7 @@
 CLASS_SUPERCLASS_INDEX = 0
 CLASS_METHODDICT_INDEX = 1
 CLASS_FORMAT_INDEX = 2
-CLASS_NAME_INDEX = 6
+CLASS_NAME_INDEX = 6             # in the mini.image, at least
 
 METHODDICT_VALUES_INDEX = 1
 METHODDICT_NAMES_INDEX  = 2

Modified: pypy/dist/pypy/lang/smalltalk/model.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/model.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/model.py	Thu Oct 25 17:16:59 2007
@@ -174,11 +174,13 @@
         return len(self.bytes)    
 
     def __str__(self):
-        return "".join(self.bytes)
+        return self.as_string()
 
     def __repr__(self):
-        return "<W_BytesObject %r>" % ("".join(self.bytes),)
+        return "<W_BytesObject %r>" % (self.as_string(),)
 
+    def as_string(self):
+        return "".join(self.bytes)
 
     def invariant(self):
         if not W_AbstractObjectWithClassReference.invariant(self):

Modified: pypy/dist/pypy/lang/smalltalk/shadow.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/shadow.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/shadow.py	Thu Oct 25 17:16:59 2007
@@ -37,8 +37,7 @@
     def invalidate(self):
         self.methoddict = {}
         self.s_superclass = None     # the ClassShadow of the super class
-        self.s_metaclass = None      # the ClassShadow of the meta class
-        self.name = '?' # take care when initing this, metaclasses do not have a name!
+        self.name = None
         self.invalid = True
 
     def check_for_updates(self):
@@ -62,7 +61,7 @@
         # compute the instance size (really the size, not the number of bytes)
         instsize_lo = (classformat >> 1) & 0x3F
         instsize_hi = (classformat >> (9 + 1)) & 0xC0
-        self.instance_size = instsize_lo | instsize_hi
+        self.instance_size = (instsize_lo | instsize_hi) - 1  # subtract hdr
         # decode the instSpec
         format = (classformat >> 7) & 15
         self.instance_varsized = format >= 2
@@ -84,6 +83,11 @@
             self.instance_kind = COMPILED_METHOD
         else:
             raise ClassShadowError("unknown format %d" % (format,))
+        # read the name
+        if w_self.size() > constants.CLASS_NAME_INDEX:
+            w_name = w_self.fetch(constants.CLASS_NAME_INDEX)
+            if isinstance(w_name, model.W_BytesObject):
+                self.name = w_name.as_string()
         # XXX read the methoddict
         # ...
 
@@ -97,8 +101,6 @@
             self.s_superclass = None
         else:
             self.s_superclass = w_superclass.as_class_get_shadow()
-        # read s_metaclass
-        self.s_metaclass = w_self.shadow_of_my_class()
 
     def new(self, extrasize=0):
         w_cls = self.w_self

Modified: pypy/dist/pypy/lang/smalltalk/test/test_classtable.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_classtable.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_classtable.py	Thu Oct 25 17:16:59 2007
@@ -1,26 +1,24 @@
 from pypy.lang.smalltalk import classtable
 
-def ismetaclass(shadow):
-    # Heuristic to detect if this is the shadow of a metaclass. Don't use
-    # apart from in this test file, because classtable['w_Metaclass'] is
+def ismetaclass(w_cls):
+    # Heuristic to detect if this is a metaclass. Don't use apart
+    # from in this test file, because classtable['w_Metaclass'] is
     # bogus after loading an image.
-    return shadow.s_metaclass.w_self is classtable.classtable['w_Metaclass']
+    return w_cls.w_class is classtable.classtable['w_Metaclass']
 
 def test_every_class_is_an_instance_of_a_metaclass():
     for (nm, w_cls) in classtable.classtable.items():
-        shadow = w_cls.as_class_get_shadow()
-        assert ismetaclass(shadow) or ismetaclass(shadow.s_metaclass)
+        assert ismetaclass(w_cls) or ismetaclass(w_cls.w_class)
 
 def test_every_metaclass_inherits_from_class_and_behavior():
     s_Class = classtable.classtable['w_Class'].as_class_get_shadow()
     s_Behavior = classtable.classtable['w_Behavior'].as_class_get_shadow()
     for (nm, w_cls) in classtable.classtable.items():
-        shadow = w_cls.as_class_get_shadow()
-        if ismetaclass(shadow):
+        if ismetaclass(w_cls):
+            shadow = w_cls.as_class_get_shadow()
             assert shadow.inherits_from(s_Class)
     assert s_Class.inherits_from(s_Behavior)
 
 def test_metaclass_of_metaclass_is_an_instance_of_metaclass():
-    s_Metaclass = classtable.classtable['w_Metaclass'].as_class_get_shadow()
-    assert s_Metaclass.s_metaclass.s_metaclass is s_Metaclass
-
+    w_Metaclass = classtable.classtable['w_Metaclass']
+    assert w_Metaclass.w_class.w_class is w_Metaclass

Added: pypy/dist/pypy/lang/smalltalk/test/test_shadow.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lang/smalltalk/test/test_shadow.py	Thu Oct 25 17:16:59 2007
@@ -0,0 +1,28 @@
+from pypy.lang.smalltalk import model, shadow, classtable, constants, objtable
+
+w_Object = classtable.classtable['w_Object']
+w_Metaclass  = classtable.classtable['w_Metaclass']
+
+def build_smalltalk_class(name, format, w_superclass=w_Object,
+                          w_classofclass=None):
+    if w_classofclass is None:
+        w_classofclass = build_smalltalk_class(None, 0x94,
+                                               w_superclass.w_class,
+                                               w_Metaclass)
+    size = constants.CLASS_NAME_INDEX + 1
+    w_class = model.W_PointersObject(w_classofclass, size)
+    w_class.store(constants.CLASS_SUPERCLASS_INDEX, w_superclass)
+    #w_class.store(constants.CLASS_METHODDICT_INDEX, ...)
+    w_class.store(constants.CLASS_FORMAT_INDEX, objtable.wrap_int(format))
+    if name is not None:
+        w_class.store(constants.CLASS_NAME_INDEX, objtable.wrap_string(name))
+    return w_class
+
+def test_empty_class():
+    w_class = build_smalltalk_class("Empty", 0x2)
+    classshadow = w_class.as_class_get_shadow()
+    assert classshadow.instance_kind == shadow.POINTERS
+    assert not classshadow.isvariable()
+    assert classshadow.instsize() == 0
+    assert classshadow.name == "Empty"
+    assert classshadow.s_superclass is w_Object.as_class_get_shadow()



More information about the Pypy-commit mailing list