Author: Stephan <[email protected]>
Branch: 
Changeset: r120:c69e55100fd6
Date: 2011-09-05 14:01 +0200
http://bitbucket.org/pypy/lang-js/changeset/c69e55100fd6/

Log:    use scope information in function context

diff --git a/js/astbuilder.py b/js/astbuilder.py
--- a/js/astbuilder.py
+++ b/js/astbuilder.py
@@ -344,10 +344,13 @@
             p = []
         else:
             p = [pident.get_literal() for pident in parameters.nodes]
-        funcobj = operations.FunctionStatement(pos, identifier, p, 
functionbody)
+        funcobj = operations.FunctionStatement(pos, identifier, p, 
functionbody, self.scopes.current_scope())
+        self.scopes.end_scope()
         if declaration:
-            self.funclists[-1][identifier.get_literal()] = funcobj
-        self.scopes.end_scope()
+            n = identifier.get_literal()
+            # XXX functions are local variables too
+            self.scopes.add_local(n)
+            self.funclists[-1][n] = funcobj
         return funcobj
 
     def visit_functiondeclaration(self, node):
diff --git a/js/jsexecution_context.py b/js/jsexecution_context.py
--- a/js/jsexecution_context.py
+++ b/js/jsexecution_context.py
@@ -139,3 +139,10 @@
         if self.ctx_obj.HasProperty(identifier):
             return self.ctx_obj.Get(ctx, identifier);
         return ExecutionContext.resolve_identifier(self, ctx, identifier)
+
+class FunctionContext(ExecutionContext):
+    def __init__(self, parent, func):
+        ExecutionContext.__init__(self, parent)
+        if func.scope:
+            from js.utils import mapdict_with_map
+            self.values = mapdict_with_map(func.scope.local_variables)
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -169,11 +169,11 @@
         from js.jsobj import W_Root
         assert isinstance(this, W_Root)
 
-        from js.jsexecution_context import ActivationContext, ExecutionContext
+        from js.jsexecution_context import ActivationContext, FunctionContext
 
         w_Arguments = W_Arguments(self, args)
         act = ActivationContext(self.ctx, this, w_Arguments)
-        newctx = ExecutionContext(act)
+        newctx = FunctionContext(act, self.callfunc)
 
         paramn = len(self.callfunc.params)
         for i in range(paramn):
diff --git a/js/operations.py b/js/operations.py
--- a/js/operations.py
+++ b/js/operations.py
@@ -313,7 +313,7 @@
         bytecode.emit('LOAD_MEMBER')
 
 class FunctionStatement(Statement):
-    def __init__(self, pos, name, params, body):
+    def __init__(self, pos, name, params, body, scope):
         self.pos = pos
         if name is None:
             self.name = None
@@ -321,9 +321,11 @@
             self.name = name.get_literal()
         self.body = body
         self.params = params
+        self.scope = scope
 
     def emit(self, bytecode):
         code = JsCode()
+        code.scope = self.scope
         if self.body is not None:
             self.body.emit(code)
         funcobj = code.make_js_function(self.name, self.params)
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
@@ -907,3 +907,6 @@
     }
     f();
     """, 12
+
+def test_empty_function_with_params():
+    assertv("x = function(x) { }; x(); false", False)
diff --git a/js/utils.py b/js/utils.py
--- a/js/utils.py
+++ b/js/utils.py
@@ -93,3 +93,11 @@
 
     def setindex(self, idx, value):
         self.values[idx] = value
+
+def mapdict_with_map(m):
+    assert isinstance(m, Map)
+    indexes = m.indexes
+    md = MapDict(len(indexes))
+    md.indexes = indexes
+    md.next_index = m.next_index
+    return md
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to