Author: Stephan <[email protected]>
Branch: 
Changeset: r108:a66f3a08e5c2
Date: 2011-07-06 11:51 +0200
http://bitbucket.org/pypy/lang-js/changeset/a66f3a08e5c2/

Log:    consider function parameters local

diff --git a/js/astbuilder.py b/js/astbuilder.py
--- a/js/astbuilder.py
+++ b/js/astbuilder.py
@@ -268,6 +268,8 @@
     def visit_formalparameterlist(self, node):
         pos = self.get_pos(node)
         nodes = [self.dispatch(child) for child in node.children]
+        for node in nodes:
+            self.scopes.add_local(node.name)
         return operations.ArgumentList(pos, nodes)
 
     def visit_variabledeclarationlist(self, node):
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -139,6 +139,8 @@
         if self.callfunc is None: # XXX Not sure if I should raise it here
             raise JsTypeError('not a function')
         act = ActivationObject()
+        newctx = function_context(self.Scope, act, this)
+
         paramn = len(self.callfunc.params)
         for i in range(paramn):
             paramname = self.callfunc.params[i]
@@ -146,11 +148,13 @@
                 value = args[i]
             except IndexError:
                 value = w_Undefined
-            act.Put(ctx, paramname, value)
+            newctx.declare_variable(paramname)
+            newctx.assign(paramname, value)
+
         act.Put(ctx, 'this', this)
         w_Arguments = W_Arguments(self, args)
         act.Put(ctx, 'arguments', w_Arguments)
-        newctx = function_context(self.Scope, act, this)
+
         val = self.callfunc.run(ctx=newctx)
         return val
 
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
@@ -461,7 +461,7 @@
 
     def test_function_decl(self):
         self.check('function f(x, y, z) {x;}',
-                   ['DECLARE_FUNCTION f [\'x\', \'y\', \'z\'] [\n  
LOAD_VARIABLE "x"\n]'])
+                   ['DECLARE_FUNCTION f [\'x\', \'y\', \'z\'] [\n  LOAD_LOCAL 
0\n]'])
 
     def test_function_expression(self):
         self.check('var x = function() {return x}',[
@@ -547,6 +547,11 @@
         self.check('var x=1;', ['DECLARE_VAR "x"', 'LOAD_INTCONSTANT 1', 
'STORE "x"', 'POP'])
         self.check('x+=1;', ['LOAD_VARIABLE "x"','LOAD_INTCONSTANT 1', 'ADD', 
'STORE "x"', 'POP'])
 
+    def test_local_function_param(self):
+        self.check('function f(x) { return x; };', ['DECLARE_FUNCTION f 
[\'x\'] [\n  LOAD_LOCAL 0\n  RETURN\n  LOAD_UNDEFINED\n]'])
+        self.check('function f(x) { var y; return y; };', ['DECLARE_FUNCTION f 
[\'x\'] [\n  DECLARE_VAR "y"\n  LOAD_LOCAL 1\n  RETURN\n  LOAD_UNDEFINED\n]'])
+        self.check('function f(x) { return y; };', ['DECLARE_FUNCTION f 
[\'x\'] [\n  LOAD_VARIABLE "y"\n  RETURN\n  LOAD_UNDEFINED\n]'])
+
 def test_retlast_pop_removal():
     jscode = JsCode()
     jscode.emit('POP')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to