[pypy-commit] lang-smalltalk storage: Made the different *size() methods in model module more consistent. Removed space parameter. Added a constant.

anton_gulenko noreply at buildbot.pypy.org
Fri Mar 28 14:02:25 CET 2014


Author: Anton Gulenko <anton.gulenko at googlemail.com>
Branch: storage
Changeset: r712:ab97a4252ed3
Date: 2014-03-27 20:52 +0100
http://bitbucket.org/pypy/lang-smalltalk/changeset/ab97a4252ed3/

Log:	Made the different *size() methods in model module more consistent.
	Removed space parameter. Added a constant.

diff --git a/spyvm/constants.py b/spyvm/constants.py
--- a/spyvm/constants.py
+++ b/spyvm/constants.py
@@ -59,6 +59,7 @@
 
 LITERAL_START = 1 # index of the first literal after the method header
 BYTES_PER_WORD = 4
+WORDS_IN_FLOAT = 2 # Fixed number of word-slots in a Squeak Float object
 
 # ___________________________________________________________________________
 # Special objects indices
diff --git a/spyvm/interpreter_proxy.py b/spyvm/interpreter_proxy.py
--- a/spyvm/interpreter_proxy.py
+++ b/spyvm/interpreter_proxy.py
@@ -192,13 +192,7 @@
 
 @expose_on_virtual_machine_proxy([oop], int)
 def byteSizeOf(w_object):
-    s_class = w_object.class_shadow(IProxy.space)
-    size = s_class.instsize()
-    if s_class.isvariable():
-        size += w_object.varsize(IProxy.space)
-    if not isinstance(w_object, model.W_BytesObject):
-        size *= 4
-    return size
+    return w_object.bytesize(IProxy.space)
 
 @expose_on_virtual_machine_proxy([int, oop], list)
 def fetchArrayofObject(fieldIndex, w_object):
@@ -308,7 +302,7 @@
 
 @expose_on_virtual_machine_proxy([oop], int)
 def stSizeOf(w_object):
-    return w_object.varsize(IProxy.space)
+    return w_object.varsize()
 
 @expose_on_virtual_machine_proxy([int, oop, int], oop)
 def storeIntegerofObjectwithValue(n0, w_object, a):
@@ -727,14 +721,13 @@
 # #if VM_PROXY_MINOR > 5
 @expose_on_virtual_machine_proxy([oop], bool, minor=5)
 def isArray(w_object):
+    # TODO - are ByteObjects and WordObjects not considered Arrays?
+    # What are the exact semantics of this? Should only the class Array return true?
     if not isinstance(w_object, model.W_PointersObject):
         return False
     space = IProxy.space
     s_class = w_object.class_shadow(space)
-    if s_class.instsize() == 0 and s_class.isvariable():
-        return True
-    else:
-        return False
+    return s_class.instsize() == 0 and s_class.isvariable()
 
 @expose_on_virtual_machine_proxy([], int, minor=5)
 def forceInterruptCheck():
@@ -1156,7 +1149,7 @@
 # # Class extensions for Array conversion
 # class __extend__(model.W_PointersObject):
 #     def as_c_array(self, proxy):
-#         return map(lambda x: proxy.object_to_oop(x), self.vars[self.instsize(space):])
+#         return map(lambda x: proxy.object_to_oop(x), self.vars[self.instsize():])
 
 # class __extend__(model.W_BytesObject):
 #     def as_c_array(self, proxy):
diff --git a/spyvm/model.py b/spyvm/model.py
--- a/spyvm/model.py
+++ b/spyvm/model.py
@@ -32,26 +32,35 @@
     _attrs_ = []    # no RPython-level instance variables allowed in W_Object
     _settled_ = True
     repr_classname = "W_Object"
+    bytes_per_slot = constants.BYTES_PER_WORD
     
     def size(self):
+        """Return the number of "slots" or "items" in the receiver object.
+        This means different things for different objects.
+        For ByteObject, this means the number of bytes, for WordObject the number of words,
+        for PointerObject the number of pointers (regardless if it's varsized or not).
+        """
+        return 0
+    
+    def instsize(self):
+        """Return the number of slots of the object reserved for instance variables (not number of bytes).
+        Only returns something non-zero for W_PointersObjects,
+        because all other classes in this model hierarchy represent varsized classes (except for SmallInteger)."""
+        return 0
+
+    def varsize(self):
+        """Return number of slots in the of variable-sized part (not number of bytes).
+        Not necessarily number of bytes.
+        Variable sized objects are those created with #new:."""
+        return self.size() - self.instsize()
+
+    def bytesize(self):
         """Return bytesize that conforms to Blue Book.
 
         The reported size may differ from the actual size in Spy's object
         space, as memory representation varies depending on PyPy translation."""
-        return 0
-
-    def instsize(self, space):
-        """Return the size of the object reserved for instance variables.
-        Only returns something non-zero for W_PointersObjects, W_Floats, and
-        W_LargePositiveInteger1Words"""
-        return 0
-
-    def varsize(self, space):
-        """Return bytesize of variable-sized part.
-
-        Variable sized objects are those created with #new:."""
-        return self.size()
-
+        return self.size() * self.bytes_per_slot
+        
     def getclass(self, space):
         """Return Squeak class."""
         raise NotImplementedError()
@@ -279,6 +288,7 @@
     """Large positive integer for exactly 1 word"""
     _attrs_ = ["value", "_exposed_size"]
     repr_classname = "W_LargePositiveInteger1Word"
+    bytes_per_slot = 1
     
     def __init__(self, value, size=4):
         self.value = intmask(value)
@@ -348,10 +358,10 @@
         new_value = self.value & r_uint(~(0xff << skew))
         new_value |= r_uint(byte << skew)
         self.value = intmask(new_value)
-
+    
     def size(self):
         return self._exposed_size
-
+        
     def invariant(self):
         return isinstance(self.value, int)
 
@@ -450,7 +460,7 @@
         self.value = float_unpack(r, 8)
 
     def size(self):
-        return 2
+        return constants.WORDS_IN_FLOAT
 
 @signature.finishsigs
 class W_AbstractObjectWithClassReference(W_AbstractObjectWithIdentityHash):
@@ -603,7 +613,7 @@
             shadow_info = self.shadow.__repr__()
             if self.shadow.provides_getname:
                 name = self._get_shadow().getname()
-        return '(%s) len=%d %s' % (shadow_info, self.size(), name)
+        return '(%s) len=%d [%s]' % (shadow_info, self.size(), name)
         
     def fetch_all(self, space):
         return [self.fetch(space, i) for i in range(self.size())]
@@ -623,11 +633,11 @@
         
     def at0(self, space, index0):
         # To test, at0 = in varsize part
-        return self.fetch(space, index0+self.instsize(space))
+        return self.fetch(space, index0 + self.instsize())
 
     def atput0(self, space, index0, w_value):
         # To test, at0 = in varsize part
-        self.store(space, index0 + self.instsize(space), w_value)
+        self.store(space, index0 + self.instsize(), w_value)
 
     def fetch(self, space, n0):
         return self._get_shadow().fetch(n0)
@@ -635,16 +645,11 @@
     def store(self, space, n0, w_value):
         return self._get_shadow().store(n0, w_value)
 
-    def varsize(self, space):
-        return self.size() - self.instsize(space)
-
-    def instsize(self, space):
-        return self.class_shadow(space).instsize()
-
     def size(self):
-        if not self.shadow:
-            return 0
         return self._get_shadow().size()
+        
+    def instsize(self):
+        return self.class_shadow(self.space()).instsize()
 
     def store_shadow(self, shadow):
         self.shadow = shadow
@@ -746,6 +751,7 @@
     _attrs_ = ['bytes', 'c_bytes', '_size']
     _immutable_fields_ = ['_size', 'bytes[*]?']
     repr_classname = 'W_BytesObject'
+    bytes_per_slot = 1
     
     def __init__(self, space, w_class, size):
         W_AbstractObjectWithClassReference.__init__(self, space, w_class)
@@ -1032,7 +1038,7 @@
 
     def size(self):
         return self._realsize
-
+        
     def invariant(self):
         return False
 
@@ -1245,9 +1251,14 @@
                 hasattr(self, 'primitive') and
                 self.primitive is not None)
 
-    def size(self):
+    def bytesize(self, space):
+        # This is very special: words and bytes are mixed here.
         return self.headersize() + self.getliteralsize() + len(self.bytes)
 
+    def size(self):
+        # One word for the header.
+        return 1 + self.literalsize + len(self.bytes)
+    
     def gettempsize(self):
         return self.tempsize
 
diff --git a/spyvm/primitives.py b/spyvm/primitives.py
--- a/spyvm/primitives.py
+++ b/spyvm/primitives.py
@@ -15,7 +15,7 @@
         raise PrimitiveFailedError()
 
 def assert_valid_index(space, n0, w_obj):
-    if not 0 <= n0 < w_obj.varsize(space):
+    if not 0 <= n0 < w_obj.varsize():
         raise PrimitiveFailedError()
     # return the index, since from here on the annotator knows that
     # n0 cannot be negative
@@ -406,7 +406,7 @@
 def func(interp, s_frame, w_obj):
     if not w_obj.class_shadow(interp.space).isvariable():
         raise PrimitiveFailedError()
-    return interp.space.wrap_int(w_obj.varsize(interp.space))
+    return interp.space.wrap_int(w_obj.varsize())
 
 @expose_primitive(STRING_AT, unwrap_spec=[object, index1_0])
 def func(interp, s_frame, w_obj, n0):
@@ -765,8 +765,8 @@
     # might be different (e.g. Symbol and ByteString)
     if w_rcvr.__class__ is not w_replacement.__class__:
         raise PrimitiveFailedError
-    if (w_rcvr.size() - w_rcvr.instsize(interp.space) <= stop
-            or w_replacement.size() - w_replacement.instsize(interp.space) <= repStart + (stop - start)):
+    if (w_rcvr.size() - w_rcvr.instsize() <= stop
+            or w_replacement.size() - w_replacement.instsize() <= repStart + (stop - start)):
         raise PrimitiveFailedError()
     repOff = repStart - start
     for i0 in range(start, stop + 1):


More information about the pypy-commit mailing list