Author: Stephan <[email protected]>
Branch: 
Changeset: r46:7fffd7ed0a93
Date: 2011-05-12 17:15 +0200
http://bitbucket.org/pypy/lang-js/changeset/7fffd7ed0a93/

Log:    allways keep keep last element on stack and return it

diff --git a/js/jscode.py b/js/jscode.py
--- a/js/jscode.py
+++ b/js/jscode.py
@@ -99,8 +99,14 @@
         return opcode
 
     def make_js_function(self, name='__dont_care__', params=None):
+        if self.opcodes and isinstance(self.opcodes[-1], POP):
+            self.opcodes.pop()
+        else:
+            self.emit('LOAD_UNDEFINED')
+
         if self.has_labels:
             self.remove_labels()
+
         return JsFunction(name, params, self.opcodes)
 
     def remove_labels(self):
@@ -143,11 +149,6 @@
             return e.value
 
     def run_bytecode(self, ctx, stack, check_stack=True, retlast=False):
-        popped = False
-        if retlast and self.opcodes and isinstance(self.opcodes[-1], POP):
-            self.opcodes.pop()
-            popped = True
-
         i = 0
         to_pop = 0
         try:
@@ -179,15 +180,9 @@
             for i in range(to_pop):
                 ctx.pop_object()
 
-        if retlast:
-            if popped:
-                assert len(stack) == 1
-                return stack[0]
-            else:
-                assert not stack
-                return w_Undefined
         if check_stack:
-            assert not stack
+            assert len(stack) == 1
+        return stack[0]
 
 class Opcode(object):
     def __init__(self):
diff --git a/js/test/test_parser.py b/js/test/test_parser.py
--- a/js/test/test_parser.py
+++ b/js/test/test_parser.py
@@ -457,12 +457,12 @@
 
     def test_function_decl(self):
         self.check('function f(x, y, z) {x;}',
-                   ['DECLARE_FUNCTION f [\'x\', \'y\', \'z\'] [\n  
LOAD_VARIABLE "x"\n  POP\n]'])
+                   ['DECLARE_FUNCTION f [\'x\', \'y\', \'z\'] [\n  
LOAD_VARIABLE "x"\n]'])
 
     def test_function_expression(self):
         self.check('var x = function() {return x}',[
             'DECLARE_VAR "x"',
-            'DECLARE_FUNCTION [] [\n  LOAD_VARIABLE "x"\n  RETURN\n]',
+            'DECLARE_FUNCTION [] [\n  LOAD_VARIABLE "x"\n  RETURN\n  
LOAD_UNDEFINED\n]',
             'LOAD_FUNCTION',
             'STORE "x"',
             'POP'])
@@ -510,6 +510,31 @@
                     'STORE_MEMBER_SUB',
                     'POP'])
 
+
+def test_retlast_pop_removal():
+    jscode = JsCode()
+    jscode.emit('POP')
+    jsfunc = jscode.make_js_function()
+    assert not jsfunc.opcodes
+
+    jscode = JsCode()
+    jscode.emit('POP')
+    jscode.emit('LABEL', 0)
+    jsfunc = jscode.make_js_function()
+    assert_bytecode_list_eql(jsfunc.opcodes, ['POP', 'LOAD_UNDEFINED'])
+
+
+
+def test_retlast_undefined_addition():
+    jscode = JsCode()
+    jsfunc = jscode.make_js_function()
+    assert_bytecode_list_eql(jsfunc.opcodes, ['LOAD_UNDEFINED'])
+
+    jscode = JsCode()
+    jscode.emit('LOAD_INTCONSTANT', 1)
+    jsfunc = jscode.make_js_function()
+    assert_bytecode_list_eql(jsfunc.opcodes, ['LOAD_INTCONSTANT 1', 
'LOAD_UNDEFINED'])
+
 from js.jsparser import parse
 
 def test_simplesemicolon():
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to