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

niko at codespeak.net niko at codespeak.net
Wed Oct 24 12:52:37 CEST 2007


Author: niko
Date: Wed Oct 24 12:52:37 2007
New Revision: 47813

Modified:
   pypy/dist/pypy/lang/smalltalk/fakeimage.py
   pypy/dist/pypy/lang/smalltalk/primitives.py
   pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
Log:
implement OBJECT_AT and OBJECT_AT_PUT.  
add constants for known slot indices into fakeimage.py.
add simple tests for OBJECT_AT, OBJECT_AT_PUT



Modified: pypy/dist/pypy/lang/smalltalk/fakeimage.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/fakeimage.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/fakeimage.py	Wed Oct 24 12:52:37 2007
@@ -21,7 +21,7 @@
 
 def ord_w_char(w_c):
     assert w_c.w_class is ct.w_Character
-    w_ord = w_c.getnamedvar(0)
+    w_ord = w_c.getnamedvar(CHARACTER_VALUE_INDEX)
     assert w_ord.w_class is ct.w_SmallInteger
     assert isinstance(w_ord, model.W_SmallInteger)
     return w_ord.value
@@ -33,13 +33,23 @@
         return w_false
 
 # ___________________________________________________________________________
+# Slot Names
+
+CHARACTER_VALUE_INDEX = 0        # Page 630 of the blue book
+
+STREAM_ARRAY_INDEX = 0           # Page 631 of the blue book
+STREAM_INDEX_INDEX = 1
+STREAM_READ_LIMIT_INDEX = 2
+STREAM_WRITE_LIMIT_INDEX = 3
+
+# ___________________________________________________________________________
 # Global Data
 
 def make_char_table():
     global CharacterTable
     def bld_char(i):
         w_cinst = ct.w_Character.new()
-        w_cinst.setnamedvar(0, small_int(i))
+        w_cinst.setnamedvar(CHARACTER_VALUE_INDEX, small_int(i))
         return w_cinst
     CharacterTable = [bld_char(i) for i in range(256)]
 make_char_table()

Modified: pypy/dist/pypy/lang/smalltalk/primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/primitives.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/primitives.py	Wed Oct 24 12:52:37 2007
@@ -112,6 +112,8 @@
 SIZE = 62
 STRING_AT = 63
 STRING_AT_PUT = 64
+OBJECT_AT = 68
+OBJECT_AT_PUT = 69
 
 def common_at(stack):
     [w_idx, w_obj] = stack
@@ -164,6 +166,25 @@
     w_obj.setbyte(idx, fimg.ord_w_char(w_val))
     return w_val
 
+ at primitive(OBJECT_AT)
+ at stack(2)
+def func(stack):
+    [w_idx, w_rcvr] = stack
+    idx = unwrap_int(w_idx)
+    if idx < 0 or idx >= w_rcvr.w_class.instvarsize:
+        raise PrimitiveFailedError()
+    return w_rcvr.getnamedvar(idx)
+
+ at primitive(OBJECT_AT_PUT)
+ at stack(3)
+def func(stack):
+    [w_val, w_idx, w_rcvr] = stack
+    idx = unwrap_int(w_idx)
+    if idx < 0 or idx >= w_rcvr.w_class.instvarsize:
+        raise PrimitiveFailedError()
+    w_rcvr.setnamedvar(idx, w_val)
+    return w_val
+
 # ___________________________________________________________________________
 # Boolean Primitives
 
@@ -190,27 +211,23 @@
     NOTEQUAL: operator.ne
     }
 for (code,op) in bool_ops.items():
-    def func(frame, op=op): # n.b. capture op value
-        w_v1 = frame.peek(1)
-        w_v2 = frame.peek(0)
+    @primitive(code)
+    @stack(2)
+    def func(stack, op=op): # n.b. capture op value
+        [w_v2, w_v1] = stack
         v1 = unwrap_int(w_v1)
         v2 = unwrap_int(w_v2)
         res = op(v1, v2)
-        
         w_res = fimg.wrap_bool(res)
-        frame.pop_n(2)
         return w_res
-    prim_table[code] = func
 
 for (code,op) in bool_ops.items():
-    def func(frame, op=op): # n.b. capture op value
-        w_v1 = frame.peek(1)
-        w_v2 = frame.peek(0)
+    @primitive(code+_FLOAT_OFFSET)
+    @stack(2)
+    def func(stack, op=op): # n.b. capture op value
+        [w_v2, w_v1] = stack
         v1 = unwrap_float(w_v1)
         v2 = unwrap_float(w_v2)
         res = op(v1, v2)
-        
         w_res = fimg.wrap_bool(res)
-        frame.pop_n(2)
         return w_res
-    prim_table[code+_FLOAT_OFFSET] = func

Modified: pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_primitives.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_primitives.py	Wed Oct 24 12:52:37 2007
@@ -79,6 +79,29 @@
     for i in range(len(exp)):
         assert prim(p.STRING_AT, [test_str, i]) == wrap(exp[i])
 
+def test_object_at():
+    w_v = prim(p.OBJECT_AT, ["q", fimg.CHARACTER_VALUE_INDEX])
+    assert w_v.value == ord("q")
+
+def test_invalid_object_at():
+    prim_fails(p.OBJECT_AT, ["q", fimg.CHARACTER_VALUE_INDEX+1])
+    
+def test_object_at_put():
+    w_obj = model.W_Class(None, None, 1, format=model.POINTERS).new()
+    assert prim(p.OBJECT_AT_PUT, [w_obj, 0, "q"]) is wrap("q")
+    assert prim(p.OBJECT_AT, [w_obj, 0]) is wrap("q")
+
+def test_invalid_object_at_put():
+    w_obj = model.W_Class(None, None, 1, format=model.POINTERS).new()
+    prim_fails(p.OBJECT_AT, [w_obj, 1, 1])
+    
+def test_string_at_put():
+    test_str = wrap("foobar")
+    assert prim(p.STRING_AT_PUT, [test_str, 3, "c"]) == wrap("c")
+    exp = "foocar"
+    for i in range(len(exp)):
+        assert prim(p.STRING_AT, [test_str, i]) == wrap(exp[i])
+
 def test_boolean():
     assert prim(p.LESSTHAN, [1,2]) == fimg.w_true
     assert prim(p.GREATERTHAN, [3,4]) == fimg.w_false



More information about the Pypy-commit mailing list