[pypy-svn] r39324 - in pypy/dist/pypy/lang/js: . test test/ecma

santagada at codespeak.net santagada at codespeak.net
Thu Feb 22 23:04:49 CET 2007


Author: santagada
Date: Thu Feb 22 23:04:46 2007
New Revision: 39324

Modified:
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/test/ecma/conftest.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
new operations and the for-in statement


Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Thu Feb 22 23:04:46 2007
@@ -62,6 +62,7 @@
 class UnaryOp(Expression):
     def from_tree(self, t):
         self.expr = get_obj(t, '0')
+        self.postfix = bool(get_string(t, 'postfix'))
 
 class BinaryOp(Expression):
     def from_tree(self, t):
@@ -247,15 +248,27 @@
         v3 = self.right.eval(ctx).GetValue()
         op = self.value
         if op == "=":
-            v1.PutValue(v3, ctx)
+            val = v3
         elif op == "*":
-            v1.PutValue(Mult().mathop(ctx, v1.GetValue(), v3), ctx)
+            val = Mult().mathop(ctx, v1.GetValue(), v3)
         elif op == "+":
-            v1.PutValue(Plus().mathop(ctx, v1.GetValue(), v3), ctx)
+            val = Plus().mathop(ctx, v1.GetValue(), v3)
+        elif op == "/":
+            val = Div().mathop(ctx, v1.GetValue(), v3)
+        elif op == "%":
+            val = Mod().mathop(ctx, v1.GetValue(), v3)
+        elif op == "&":
+            val = BitwiseAnd().mathop(ctx, v1.GetValue(), v3)
+        elif op == "|":
+            val = BitwiseOR().mathop(ctx, v1.GetValue(), v3)
+        elif op == "^":
+            val = BitwiseXOR().mathop(ctx, v1.GetValue(), v3)
         else:
             print op
             raise NotImplementedError()
-        return v3
+        
+        v1.PutValue(val, ctx)
+        return val
 
 class Block(Statement):
     opcode = 'BLOCK'
@@ -281,6 +294,14 @@
     def decision(self, ctx, op1, op2):
         return W_Number(op1&op2)
 
+class BitwiseNot(UnaryOp):
+    opcode = 'BITWISE_NOT'
+
+    def eval(self, ctx):
+        op1 = self.expr.eval(ctx).GetValue().ToInt32()
+        return W_Number(~op1)
+
+
 class BitwiseOR(BinaryBitwiseOp):
     opcode = 'BITWISE_OR'
     
@@ -562,7 +583,11 @@
         x = val.ToNumber()
         resl = Plus().mathop(ctx, W_Number(x), W_Number(1))
         thing.PutValue(resl, ctx)
-        return val
+        if self.postfix:
+            return val
+        else:
+            return resl
+        
 
 class Decrement(UnaryOp):
     opcode = 'DECREMENT'
@@ -573,7 +598,10 @@
         x = val.ToNumber()
         resl = Plus().mathop(ctx, W_Number(x), W_Number(-1))
         thing.PutValue(resl, ctx)
-        return val
+        if self.postfix:
+            return val
+        else:
+            return resl
 
 
 class Index(BinaryOp):
@@ -861,13 +889,34 @@
         self.expr.eval(ctx)
         return w_Undefined
 
-class While(Statement):
-    opcode = 'WHILE'
-    
+class WhileBase(Statement):
     def from_tree(self, t):
         self.condition = get_obj(t, 'condition')
         self.body = get_obj(t, 'body')
 
+class Do(WhileBase):
+    opcode = 'DO'
+    
+    def execute(self, ctx):
+        try:
+            self.body.execute(ctx)
+        except ExecutionReturned, e:
+            if e.type == 'break':
+                return
+            elif e.type == 'continue':
+                pass
+        while self.condition.eval(ctx).ToBoolean():
+            try:
+                self.body.execute(ctx)
+            except ExecutionReturned, e:
+                if e.type == 'break':
+                    break
+                elif e.type == 'continue':
+                    continue
+    
+class While(WhileBase):
+    opcode = 'WHILE'
+    
     def execute(self, ctx):
         while self.condition.eval(ctx).ToBoolean():
             try:
@@ -878,6 +927,30 @@
                 elif e.type == 'continue':
                     continue
 
+class ForIn(Statement):
+    opcode = 'FOR_IN'
+    
+    def from_tree(self, t):
+        self.object = get_obj(t, 'object')
+        self.body = get_obj(t, 'body')
+        self.iterator = get_obj(t, 'iterator')
+
+    def execute(self, ctx):
+        obj = self.object.eval(ctx).GetValue().ToObject()
+        for prop in obj.propdict.values():
+            if prop.de:
+                continue
+            iterator = self.iterator.eval(ctx)
+            print prop.name
+            iterator.PutValue(prop.value, ctx)
+            try:
+                result = self.body.execute(ctx)
+            except ExecutionReturned, e:
+                if e.type == 'break':
+                    break
+                elif e.type == 'continue':
+                    continue
+    
 class For(Statement):
     opcode = 'FOR'
 
@@ -980,3 +1053,16 @@
     else:
         raise NotImplementedError("Dont know how to handle %s" % opcode)
 
+def wrap_arguments(pyargs):
+    "receives a list of arguments and wrap then in their js equivalents"
+    res = []
+    for arg in pyargs:
+        if isinstance(arg, W_Root)
+            res.append(arg)
+        elif isinstance(arg, str):
+            res.append(W_String(arg))
+        elif isinstance(arg, int) or isinstance(arg, float) or isinstance(arg, long):
+            res.append(W_Number(arg))
+        elif isinstance(arg, bool):
+            res.append(W_Boolean(arg))
+        elif isinstance(arg, )

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Thu Feb 22 23:04:46 2007
@@ -127,7 +127,7 @@
                  Value=w_Undefined, callfunc=None):
         self.propdict = {}
         self.propdict['prototype'] = Property('prototype', w_Undefined,
-                                              dd=True)
+                                              dd=True, de=True)
         self.Prototype = Prototype
         self.Class = Class
         self.callfunc = callfunc
@@ -242,7 +242,7 @@
                  Value=w_Undefined, callfunc=None):
         W_PrimitiveObject.__init__(self, ctx, Prototype,
                                    Class, Value, callfunc)
-        self.propdict['toString'] = Property('toString', W_Builtin(str_builtin))
+        self.propdict['toString'] = Property('toString', W_Builtin(str_builtin), de=True)
 
 
 class W_Builtin(W_PrimitiveObject):
@@ -286,12 +286,13 @@
                  Value=w_Undefined, callfunc=None):
         W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, callfunc)
         toString = W_Builtin(array_str_builtin)
-        self.Put('toString', toString)
+        self.Put('toString', toString, de=True)
         self.Put('length', W_Number(0))
         self.array = []
         self.set_builtin_call(arraycallbi)
-    
-    def Put(self, P, V):
+
+    def Put(self, P, V, dd=False,
+            ro=False, de=False, it=False):
         try:
             x = int(P)
             # print "puting", V, 'in', x
@@ -303,11 +304,7 @@
             self.array[x]= V
                     
         except ValueError:
-            if not self.CanPut(P): return
-            if P in self.propdict:
-                self.propdict[P].value = V
-            else:
-                self.propdict[P] = Property(P, V)
+            super(W_Array, self).Put(P, V, dd, ro, de, it)
 
     def Get(self, P):
         try:
@@ -500,6 +497,7 @@
         if self.base is None:
             base = ctx.scope[-1]
         base.Put(self.property_name, w)
+        return w
 
     def GetBase(self):
         return self.base

Modified: pypy/dist/pypy/lang/js/test/ecma/conftest.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/ecma/conftest.py	(original)
+++ pypy/dist/pypy/lang/js/test/ecma/conftest.py	Thu Feb 22 23:04:46 2007
@@ -2,7 +2,7 @@
 from pypy.lang.js.interpreter import *
 from pypy.lang.js.jsobj import W_Array, JsBaseExcept
 from pypy.lang.js.jsparser import JsSyntaxError
-from py.__.test.outcome import Failed
+from py.__.test.outcome import Failed, ExceptionFailure
 import pypy.lang.js as js
 
 js.jsobj.DEBUG = True
@@ -77,7 +77,7 @@
         r3 = ctx.resolve_identifier('run_test').GetValue()
         result = r3.Call(ctx=ctx, args=[W_Number(self.number),]).ToNumber()
         if result == 0:
-            py.test.fail()
+            raise Failed(msg="Results don't match")
         elif result == -1:
             py.test.skip()
 
@@ -85,5 +85,5 @@
     def _getpathlineno(self):
         return self.parent.parent.fspath, 0 
 
-
-Directory = JSDirectory
+if py.test.config.option.ecma:
+    Directory = JSDirectory

Modified: pypy/dist/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_interp.py	Thu Feb 22 23:04:46 2007
@@ -462,3 +462,11 @@
         delete x.y
         print(x.y)
         """, ['undefined'])
+
+    def test_forin(self):
+        self.assert_prints("""
+        var x = {a:5}
+        for(y in x){
+            print(y)
+        }
+        """, ['5',])



More information about the Pypy-commit mailing list