[pypy-svn] r53308 - pypy/branch/js-refactoring/pypy/lang/js

fijal at codespeak.net fijal at codespeak.net
Fri Apr 4 04:15:12 CEST 2008


Author: fijal
Date: Fri Apr  4 04:15:10 2008
New Revision: 53308

Modified:
   pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py
   pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
   pypy/branch/js-refactoring/pypy/lang/js/jscode.py
   pypy/branch/js-refactoring/pypy/lang/js/operations.py
Log:
* basic support for methods
* pass few trivial tests


Modified: pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/astbuilder.py	Fri Apr  4 04:15:10 2008
@@ -14,8 +14,8 @@
         '*': operations.Mult,
         '/': operations.Division,
         '%': operations.Mod,
-        #'^': operations.BitwiseXor,
-        #'|': operations.BitwiseOr,
+        '^': operations.BitwiseXor,
+        '|': operations.BitwiseOr,
         '&': operations.BitwiseAnd,
         '&&': operations.And,
         '||': operations.Or,
@@ -27,9 +27,9 @@
         '>=': operations.Ge,
         '<': operations.Lt,
         '<=': operations.Le,
-        #'>>': operations.Rsh,
-        #'>>>': operations.Ursh,
-        #'<<': operations.Lsh,
+        '>>': operations.Rsh,
+        '>>>': operations.Ursh,
+        '<<': operations.Lsh,
         '.': operations.MemberDot,
         '[': operations.Member,
         ',': operations.Comma,

Modified: pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/interpreter.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/interpreter.py	Fri Apr  4 04:15:10 2008
@@ -250,10 +250,10 @@
     def Call(self, ctx, args=[], this=None):
         tam = len(args)
         if tam >= 1:
-            fbody  = args[tam-1].GetValue().ToString(ctx)
+            fbody  = args[tam-1].ToString(ctx)
             argslist = []
             for i in range(tam-1):
-                argslist.append(args[i].GetValue().ToString(ctx))
+                argslist.append(args[i].ToString(ctx))
             fargs = ','.join(argslist)
             functioncode = "function (%s) {%s}"%(fargs, fbody)
         else:

Modified: pypy/branch/js-refactoring/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/jscode.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/jscode.py	Fri Apr  4 04:15:10 2008
@@ -8,6 +8,7 @@
 from pypy.lang.js.baseop import plus, sub, compare, AbstractEC, StrictEC,\
      compare_e, increment, commonnew, mult, division, uminus, mod
 from pypy.rlib.jit import hint
+from pypy.rlib.rarithmetic import intmask
 
 class AlreadyRun(Exception):
     pass
@@ -399,6 +400,34 @@
     def operation(ctx, op1, op2):
         return W_IntNumber(op1&op2)
 
+class BITXOR(BaseBinaryBitwiseOp):
+    @staticmethod
+    def operation(ctx, op1, op2):
+        return W_IntNumber(op1^op2)
+
+class BITOR(BaseBinaryBitwiseOp):
+    @staticmethod
+    def operation(ctx, op1, op2):
+        return W_IntNumber(op1|op2)
+
+class URSH(BaseBinaryBitwiseOp):
+    def eval(self, ctx, stack):
+        op2 = stack.pop().ToUInt32()
+        op1 = stack.pop().ToUInt32()
+        stack.append(W_IntNumber(op1 >> (op2 & 0x1F)))
+
+class RSH(BaseBinaryBitwiseOp):
+    def eval(self, ctx, stack):
+        op2 = stack.pop().ToUInt32()
+        op1 = stack.pop().ToInt32()
+        stack.append(W_IntNumber(op1 >> intmask(op2 & 0x1F)))
+
+class LSH(BaseBinaryBitwiseOp):
+    def eval(self, ctx, stack):
+        op2 = stack.pop().ToUInt32()
+        op1 = stack.pop().ToInt32()
+        stack.append(W_IntNumber(op1 << intmask(op2 & 0x1F)))
+
 class MUL(BaseBinaryOperation):
     @staticmethod
     def operation(ctx, op1, op2):
@@ -517,12 +546,38 @@
         stack.append(result)
         return result
 
+class BaseAssignBitOper(BaseStore):
+    def process(self, ctx, name, stack):
+        right = stack.pop().ToInt32()
+        left = ctx.resolve_identifier(name).ToInt32()
+        result = self.operation(ctx, left, right)
+        stack.append(result)
+        return result
+
 class STORE_ADD(BaseAssignOper):
     operation = staticmethod(ADD.operation)
 
 class STORE_SUB(BaseAssignOper):
     operation = staticmethod(SUB.operation)
 
+class STORE_MUL(BaseAssignOper):
+    operation = staticmethod(MUL.operation)
+
+class STORE_DIV(BaseAssignOper):
+    operation = staticmethod(DIV.operation)
+
+class STORE_MOD(BaseAssignOper):
+    operation = staticmethod(MOD.operation)
+
+class STORE_BITAND(BaseAssignBitOper):
+    operation = staticmethod(BITAND.operation)
+
+class STORE_BITOR(BaseAssignBitOper):
+    operation = staticmethod(BITOR.operation)
+
+class STORE_BITXOR(BaseAssignBitOper):
+    operation = staticmethod(BITXOR.operation)
+
 class STORE_POSTINCR(BaseStore):
     def process(self, ctx, name, stack):
         value = ctx.resolve_identifier(name)
@@ -645,6 +700,21 @@
             raise ThrowException(W_String('it is not a function'))
         stack.append(res)
 
+class CALL_METHOD(Opcode):
+    def eval(self, ctx, stack):
+        method = stack.pop()
+        what = stack.pop().ToObject(ctx)
+        args = stack.pop()
+        r1 = what.Get(method.ToString())
+        if not isinstance(r1, W_PrimitiveObject):
+            raise ThrowException(W_String("it is not a callable"))
+        try:
+            res = r1.Call(ctx=ctx, args=args.tolist(), this=what)
+        except JsTypeError:
+            raise ThrowException(W_String('it is not a function'))
+        stack.append(res)
+        
+
 class DUP(Opcode):
     def eval(self, ctx, stack):
         stack.append(stack[-1])

Modified: pypy/branch/js-refactoring/pypy/lang/js/operations.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/operations.py	(original)
+++ pypy/branch/js-refactoring/pypy/lang/js/operations.py	Fri Apr  4 04:15:10 2008
@@ -134,6 +134,10 @@
     '/=' : 'DIV',
     '++' : 'INCR',
     '--' : 'DECR',
+    '%=' : 'MOD',
+    '&=' : 'BITAND',
+    '|=' : 'BITOR',
+    '^=' : 'BITXOR',
     }
 
 class SimpleIncrement(Expression):
@@ -252,24 +256,14 @@
         for node in self.nodes:
             node.emit(bytecode)
     
-BitwiseAnd = create_binary_op('BITAND')    
+BitwiseAnd = create_binary_op('BITAND')
+BitwiseXor = create_binary_op('BITXOR')
+BitwiseOr = create_binary_op('BITOR')
 
 #class BitwiseNot(UnaryOp):
 #    def eval(self, ctx):
 #        op1 = self.expr.eval(ctx).GetValue().ToInt32()
 #        return W_IntNumber(~op1)
-    
-
-# class BitwiseOr(BinaryOp):
-#     def decision(self, ctx, op1, op2):
-#         return W_IntNumber(op1|op2)
-    
-
-
-# class BitwiseXor(BinaryOp):
-#     def decision(self, ctx, op1, op2):
-#         return W_IntNumber(op1^op2)
-    
 
 class Unconditional(Statement):
     def __init__(self, pos, target):
@@ -294,8 +288,17 @@
     
     def emit(self, bytecode):
         self.args.emit(bytecode)
-        self.left.emit(bytecode)
-        bytecode.emit('CALL')
+        left = self.left
+        if isinstance(left, MemberDot):
+            left.left.emit(bytecode)
+            # XXX optimise
+            bytecode.emit('LOAD_STRINGCONSTANT', left.name)
+            bytecode.emit('CALL_METHOD')
+        elif isinstance(left, Member):
+            raise NotImplementedError
+        else:
+            left.emit(bytecode)
+            bytecode.emit('CALL')
 
 Comma = create_binary_op('COMMA')    
 
@@ -443,23 +446,9 @@
 #
 ##############################################################################
 
-# class Ursh(BinaryOp):
-#     def decision(self, ctx, op1, op2):
-#         a = op1.ToUInt32()
-#         b = op2.ToUInt32()
-#         return W_IntNumber(a >> (b & 0x1F))
-
-# class Rsh(BinaryOp):
-#     def decision(self, ctx, op1, op2):
-#         a = op1.ToInt32()
-#         b = op2.ToUInt32()
-#         return W_IntNumber(a >> intmask(b & 0x1F))
-
-# class Lsh(BinaryOp):
-#     def decision(self, ctx, op1, op2):
-#         a = op1.ToInt32()
-#         b = op2.ToUInt32()
-#         return W_IntNumber(a << intmask(b & 0x1F))
+Ursh = create_binary_op('URSH')
+Rsh = create_binary_op('RSH')
+Lsh = create_binary_op('LSH')
 
 ##############################################################################
 #
@@ -720,7 +709,7 @@
     def get_literal(self):
         return self.identifier
 
-class VariableDeclList(Expression):
+class VariableDeclList(Statement):
     def __init__(self, pos, nodes):
         self.pos = pos
         self.nodes = nodes
@@ -839,7 +828,8 @@
 
     def emit(self, bytecode):
         self.setup.emit(bytecode)
-        bytecode.emit('POP')
+        if isinstance(self.setup, Expression):
+            bytecode.emit('POP')
         precond = bytecode.emit_startloop_label()
         finish = bytecode.prealocate_endloop_label()
         self.condition.emit(bytecode)



More information about the Pypy-commit mailing list