[pypy-svn] r55799 - in pypy/branch/js-refactoring-quickhacks/pypy/lang/js: . test

santagada at codespeak.net santagada at codespeak.net
Thu Jun 12 23:48:41 CEST 2008


Author: santagada
Date: Thu Jun 12 23:48:38 2008
New Revision: 55799

Modified:
   pypy/branch/js-refactoring-quickhacks/pypy/lang/js/astbuilder.py
   pypy/branch/js-refactoring-quickhacks/pypy/lang/js/interpreter.py
   pypy/branch/js-refactoring-quickhacks/pypy/lang/js/jscode.py
   pypy/branch/js-refactoring-quickhacks/pypy/lang/js/operations.py
   pypy/branch/js-refactoring-quickhacks/pypy/lang/js/test/test_interp.py
Log:
better for with support for missing parts and continue, and a with statement that works

Modified: pypy/branch/js-refactoring-quickhacks/pypy/lang/js/astbuilder.py
==============================================================================
--- pypy/branch/js-refactoring-quickhacks/pypy/lang/js/astbuilder.py	(original)
+++ pypy/branch/js-refactoring-quickhacks/pypy/lang/js/astbuilder.py	Thu Jun 12 23:48:38 2008
@@ -432,6 +432,14 @@
         else:
             target = None
         return operations.Break(pos, target)
+
+    def visit_continuestatement(self, node):
+        pos = self.get_pos(node)
+        if len(node.children) > 0:
+            target = self.dispatch(node.children[0])
+        else:
+            target = None
+        return operations.Continue(pos, target)
     
     def visit_returnstatement(self, node):
         pos = self.get_pos(node)
@@ -469,7 +477,7 @@
     
     def visit_withstatement(self, node):
         pos = self.get_pos(node)
-        identifier = self.dispatch(node.children[0])
+        withpart = self.dispatch(node.children[0])
         body = self.dispatch(node.children[1])
-        return operations.With(pos, identifier, body)
+        return operations.With(pos, withpart, body)
         

Modified: pypy/branch/js-refactoring-quickhacks/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/branch/js-refactoring-quickhacks/pypy/lang/js/interpreter.py	(original)
+++ pypy/branch/js-refactoring-quickhacks/pypy/lang/js/interpreter.py	Thu Jun 12 23:48:38 2008
@@ -748,6 +748,8 @@
         if not we_are_translated():
             # debugging
             self._code = bytecode
+            if interactive:
+                print bytecode
         if interactive:
             return bytecode.run(self.global_context, retlast=True)
         else:

Modified: pypy/branch/js-refactoring-quickhacks/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/js-refactoring-quickhacks/pypy/lang/js/jscode.py	(original)
+++ pypy/branch/js-refactoring-quickhacks/pypy/lang/js/jscode.py	Thu Jun 12 23:48:38 2008
@@ -881,11 +881,8 @@
 # ---------------- with support ---------------------
 
 class WITH_START(Opcode):
-    def __init__(self, name):
-        self.name = name
-
     def eval(self, ctx, stack):
-        ctx.push_object(ctx.resolve_identifier(ctx, self.name).ToObject(ctx))
+        ctx.push_object(stack.pop().ToObject(ctx))
 
 class WITH_END(Opcode):
     def eval(self, ctx, stack):

Modified: pypy/branch/js-refactoring-quickhacks/pypy/lang/js/operations.py
==============================================================================
--- pypy/branch/js-refactoring-quickhacks/pypy/lang/js/operations.py	(original)
+++ pypy/branch/js-refactoring-quickhacks/pypy/lang/js/operations.py	Thu Jun 12 23:48:38 2008
@@ -239,15 +239,14 @@
     def __init__(self, pos, target):
         self.pos = pos
         self.target = target
-    
+        assert target is None
+
 class Break(Unconditional):
     def emit(self, bytecode):
-        assert self.target is None
         bytecode.emit_break()    
 
 class Continue(Unconditional):
     def emit(self, bytecode):
-        assert self.target is None
         bytecode.emit_continue()
 
 class Call(Expression):
@@ -728,14 +727,14 @@
         bytecode.emit('LOAD_UNDEFINED')
 
 class With(Statement):
-    def __init__(self, pos, identifier, body):
+    def __init__(self, pos, withpart, body):
         self.pos = pos
-        assert isinstance(identifier, VariableIdentifier)
-        self.identifier = identifier.identifier
+        self.withpart = withpart
         self.body = body
 
     def emit(self, bytecode):
-        bytecode.emit('WITH_START', unicode(self.identifier))
+        self.withpart.emit(bytecode)
+        bytecode.emit('WITH_START')
         self.body.emit(bytecode)
         bytecode.emit('WITH_END')
 
@@ -766,47 +765,40 @@
         bytecode.emit('JUMP', startlabel)
         bytecode.emit_endloop_label(endlabel)    
 
+def common_forvar(bytecode, obj, body, itername):
+    obj.emit(bytecode)
+    bytecode.emit('LOAD_ITERATOR')
+    precond = bytecode.emit_startloop_label()
+    finish = bytecode.prealocate_endloop_label()
+    bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish)
+    bytecode.emit('NEXT_ITERATOR', itername)
+    body.emit(bytecode)
+    bytecode.emit('JUMP', precond)
+    bytecode.emit_endloop_label(finish)
+    bytecode.emit('POP')
+
 class ForVarIn(Statement):
     def __init__(self, pos, vardecl, lobject, body):
         self.pos = pos
         assert isinstance(vardecl, VariableDeclaration)
-        self.iteratorname = vardecl.identifier
+        self.iteratorname = unicode(vardecl.identifier)
         self.object = lobject
         self.body = body
 
-    
     def emit(self, bytecode):
-        bytecode.emit('DECLARE_VAR', unicode(self.iteratorname))
-        self.object.emit(bytecode)
-        bytecode.emit('LOAD_ITERATOR')
-        precond = bytecode.emit_startloop_label()
-        finish = bytecode.prealocate_endloop_label()
-        bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish)
-        bytecode.emit('NEXT_ITERATOR', unicode(self.iteratorname))
-        self.body.emit(bytecode)
-        bytecode.emit('JUMP', precond)
-        bytecode.emit_endloop_label(finish)
-        bytecode.emit('POP')    
+        bytecode.emit('DECLARE_VAR', self.iteratorname)
+        common_forvar(bytecode, self.object, self.body, self.iteratorname)
 
 class ForIn(Statement):
     def __init__(self, pos, name, lobject, body):
         self.pos = pos
         #assert isinstance(iterator, Node)
-        self.iteratorname = name
+        self.iteratorname = unicode(name)
         self.object = lobject
         self.body = body
 
     def emit(self, bytecode):
-        self.object.emit(bytecode)
-        bytecode.emit('LOAD_ITERATOR')
-        precond = bytecode.emit_startloop_label()
-        finish = bytecode.prealocate_endloop_label()
-        bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish)
-        bytecode.emit('NEXT_ITERATOR', unicode(self.iteratorname))
-        self.body.emit(bytecode)
-        bytecode.emit('JUMP', precond)
-        bytecode.emit_endloop_label(finish)
-        bytecode.emit('POP')
+        common_forvar(bytecode, self.object, self.body, self.iteratorname)
 
 class For(Statement):
     def __init__(self, pos, setup, condition, update, body):
@@ -817,16 +809,23 @@
         self.body = body
 
     def emit(self, bytecode):
-        self.setup.emit(bytecode)
-        if isinstance(self.setup, Expression):
-            bytecode.emit('POP')
+        if self.setup is not None:
+            self.setup.emit(bytecode)
+            if isinstance(self.setup, Expression):
+                bytecode.emit('POP')
+        
+        firstep = bytecode.prealocate_label()
+        bytecode.emit('JUMP', firstep)
         precond = bytecode.emit_startloop_label()
         finish = bytecode.prealocate_endloop_label()
-        self.condition.emit(bytecode)
-        bytecode.emit('JUMP_IF_FALSE', finish)
+        if self.update is not None:
+            self.update.emit(bytecode)
+            bytecode.emit('POP')
+        bytecode.emit_label(firstep)
+        if self.condition is not None:
+            self.condition.emit(bytecode)
+            bytecode.emit('JUMP_IF_FALSE', finish)
         self.body.emit(bytecode)
-        self.update.emit(bytecode)
-        bytecode.emit('POP')
         bytecode.emit('JUMP', precond)
         bytecode.emit_endloop_label(finish)
     

Modified: pypy/branch/js-refactoring-quickhacks/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/branch/js-refactoring-quickhacks/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/branch/js-refactoring-quickhacks/pypy/lang/js/test/test_interp.py	Thu Jun 12 23:48:38 2008
@@ -312,13 +312,23 @@
     """, ["true", "false"])
 
 def test_for():
-    assertp("""
+    yield assertp, """
     i = 0;
     for (i; i<3; i++) {
         print(i);
     }
     print(i);
-    """, ["0","1","2","3"])
+    """, ["0","1","2","3"]
+    
+    yield assertp, """
+    i = 0;
+    for (;;) {
+        if(i >= 3) break;
+        print(i);
+        i++
+    }
+    print(i);
+    """, ["0","1","2","3"]
 
 def test_eval():
     yield assertp, """
@@ -342,6 +352,15 @@
     }
     print('out');""", "out")
 
+def test_continue():
+    assertp("""
+    for(x = 0; x < 3; x++) {
+        print(x);
+        continue;
+        print('error');
+    }
+    print('out');""", ["0","1","2","out"])
+
 def test_typeof():
     assertv("""
     var x = 3;
@@ -502,7 +521,7 @@
     yield assertv, "2 !== 2;", False
 
 def test_with():
-    assertp("""
+    yield assertp, """
     var mock = {x:2};
     var x=4;
     print(x);
@@ -517,7 +536,13 @@
         print(y);
     }
     print(x);
-    """, ['4', '2', '3', '4'])
+    """, ['4', '2', '3', '4']
+    
+    yield assertp, """
+    with(new Array(1,2,3)) {
+        print(join('.'))
+    }
+    """, "1.2.3"
 
 def test_bitops():
     yield assertv, "2 ^ 2;", 0



More information about the Pypy-commit mailing list