Author: Stephan <step...@stzal.com>
Branch: 
Changeset: r62:0e61f2a8c76a
Date: 2011-05-17 18:44 +0200
http://bitbucket.org/pypy/lang-js/changeset/0e61f2a8c76a/

Log:    allow switch without default

diff --git a/js/astbuilder.py b/js/astbuilder.py
--- a/js/astbuilder.py
+++ b/js/astbuilder.py
@@ -391,7 +391,10 @@
     def visit_caseblock(self, node):
         pos = self.get_pos(node)
         caseclauses = self.dispatch(node.children[0])
-        defaultblock = self.dispatch(node.children[1])
+        if len(node.children) > 1:
+            defaultblock = self.dispatch(node.children[1])
+        else:
+            defaultblock = None
         return operations.Cases(pos, caseclauses, defaultblock)
 
     def visit_caseclauses(self, node):
diff --git a/js/operations.py b/js/operations.py
--- a/js/operations.py
+++ b/js/operations.py
@@ -384,7 +384,10 @@
             clause.block.emit(bytecode)
             bytecode.emit('JUMP', end_of_switch)
             bytecode.emit('LABEL', next_clause)
-        self.default_clause.emit(bytecode)
+        if self.default_clause is not None:
+            self.default_clause.emit(bytecode)
+        else:
+            bytecode.unpop_or_undefined()
         bytecode.emit('LABEL', end_of_switch)
         bytecode.emit('POP')
 
diff --git a/js/test/test_interp.py b/js/test/test_interp.py
--- a/js/test/test_interp.py
+++ b/js/test/test_interp.py
@@ -695,3 +695,21 @@
     yield assertv, switch_test_code(1), 1
     yield assertv, switch_test_code(2), 2
     yield assertv, switch_test_code(3), 42
+
+def switch_no_default_test_code(x):
+    return """
+    function f(x) {
+      switch(x) {
+        case 1:
+            return 2;
+            break;
+      }
+      return 42;
+    };
+
+    f(%(x)s);
+    """ % {'x': x}
+
+def test_switch_no_default():
+    yield assertv, switch_no_default_test_code(0), 42
+    yield assertv, switch_no_default_test_code(1), 2
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to