[pypy-svn] r34037 - in pypy/dist/pypy/lang/js: . test

stephan at codespeak.net stephan at codespeak.net
Wed Nov 1 16:16:11 CET 2006


Author: stephan
Date: Wed Nov  1 16:16:10 2006
New Revision: 34037

Modified:
   pypy/dist/pypy/lang/js/astgen.py
   pypy/dist/pypy/lang/js/interpreter.py
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/test/test_interp.py
Log:
(stephan, santagada) Basic function parameters working

Modified: pypy/dist/pypy/lang/js/astgen.py
==============================================================================
--- pypy/dist/pypy/lang/js/astgen.py	(original)
+++ pypy/dist/pypy/lang/js/astgen.py	Wed Nov  1 16:16:10 2006
@@ -135,7 +135,7 @@
     elif tp == 'FUNCTION':
         scope = scope_manager.enter_scope()
         body = from_dict(d['body'])
-        f = Function(d['params'], body, scope)
+        f = Function(d['params'].split(','), body, scope)
         scope_manager.leave_scope()
         return f
     elif tp == 'RETURN':

Modified: pypy/dist/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/dist/pypy/lang/js/interpreter.py	(original)
+++ pypy/dist/pypy/lang/js/interpreter.py	Wed Nov  1 16:16:10 2006
@@ -17,6 +17,61 @@
         scope_manager.set_variable(self.identifier.name, val)
         return val
 
+class __extend__(Call):
+    def call(self, context=None):
+        name = self.identifier.get_literal()
+        if name == 'print':
+            writer(",".join([i.ToString() for i in self.arglist.call(context)]))
+        else:
+            #        #import pdb;pdb.set_trace()
+        
+#        #
+#        retval = self.body.call()
+#        #scope_manager.current_scope = backup_scope
+#        return retval
+            backup_scope = scope_manager.current_scope
+            w_obj = scope_manager.get_variable(name)
+            scope_manager.current_scope = w_obj.function.scope
+            
+            retval = w_obj.Call(context=context, args=[i for i in self.arglist.call(context)])
+            scope_manager.current_scope = backup_scope
+            return retval
+
+class __extend__(Dot):
+    def call(self, context=None):
+        w_obj = self.left.call(context).GetValue().ToObject()
+        name = self.right.get_literal()
+        return w_obj.Get(name)
+
+class __extend__(Function):
+    def call(self, context=None):
+       w_obj = W_Object({}, function=self)
+       return w_obj
+
+class __extend__(Identifier):
+    def call(self, context=None):
+        if self.initialiser is not None:
+            scope_manager.set_variable(self.name, self.initialiser.call(context))
+        try:
+            return context.access(self.name)
+        except NameError:
+            return scope_manager.get_variable(self.name)
+    
+    def get_literal(self):
+        return self.name
+
+class __extend__(Index):
+    def call(self, context=None):
+        w_obj = self.left.call(context).GetValue()
+        w_member = self.expr.call(context).GetValue()
+        w_obj = w_obj.ToObject()
+        name = w_member.ToString()
+        return w_obj.Get(name)
+
+class __extend__(List):
+    def call(self, context=None):
+        return [node.call(context) for node in self.nodes]
+
 class __extend__(Number):
     def call(self, context):
         return W_Number(self.num)
@@ -25,20 +80,17 @@
         # XXX Think about a shortcut later
         return str(W_Number(self.num))
 
-class __extend__(Dot):
+class __extend__(ObjectInit):
     def call(self, context=None):
-        w_obj = self.left.call(context).GetValue().ToObject()
-        name = self.right.get_literal()
-        return w_obj.Get(name)
-
-#class __extend__(Function):
-#    def call(self, context=None):
-#        #import pdb;pdb.set_trace()
-#        #backup_scope = scope_manager.current_scope
-#        #scope_manager.current_scope = self.scope
-#        retval = self.body.call()
-#        #scope_manager.current_scope = backup_scope
-#        return retval
+        w_obj = W_Object({})
+        for property in self.properties:
+            name = property.name.get_literal()
+            w_expr = property.value.call(context).GetValue()
+            w_obj.Put(name, w_expr)
+        return w_obj
+        #dict_w = {}
+        #for property in self.properties:
+        #    dict_w[property.name
 
 class __extend__(Plus):
     def call(self, context=None):
@@ -58,51 +110,28 @@
             return W_Number(num_left + num_right)
         #return self.left.call(context).add(self.right.call(context))
 
-class __extend__(Semicolon):
-    def call(self, context=None):
-        self.expr.call(context)
-
-class __extend__(Identifier):
-    def call(self, context=None):
-        if self.initialiser is not None:
-            scope_manager.set_variable(self.name, self.initialiser.call(context))
-        return scope_manager.get_variable(self.name)
-    
-    def get_literal(self):
-        return self.name
-
 class __extend__(Script):
-    def call(self, context=None):
-        new_context = ExecutionContext(context)
+    def call(self, context=None, args=None, this=None, params=None):
+        if params == None:
+            params = []
+        ncontext = ExecutionContext(context)
+        for i, item in enumerate(params):
+            try:
+                temp = args[i]
+            except IndexError:
+                temp = w_Undefined
+            ncontext.assign(item, temp)
+
         try:
             for node in self.nodes:
-                node.call(new_context)
+                node.call(ncontext)
         except ExecutionReturned, e:
             return e.value
         return w_Undefined
 
-class __extend__(Call):
-    def call(self, context=None):
-        name = self.identifier.get_literal()
-        if name == 'print':
-            writer(",".join([i.ToString() for i in self.arglist.call(context)]))
-        else:
-            #        #import pdb;pdb.set_trace()
-        
-#        #
-#        retval = self.body.call()
-#        #scope_manager.current_scope = backup_scope
-#        return retval
-            backup_scope = scope_manager.current_scope
-            w_obj = scope_manager.get_variable(name)
-            scope_manager.current_scope = w_obj.function.scope
-            retval = w_obj.Call()
-            scope_manager.current_scope = backup_scope
-            return retval
-
-class __extend__(List):
+class __extend__(Semicolon):
     def call(self, context=None):
-        return [node.call(context) for node in self.nodes]
+        self.expr.call(context)
 
 class __extend__(String):
     def call(self, context=None):
@@ -111,31 +140,6 @@
     def get_literal(self):
         return self.strval
 
-class __extend__(ObjectInit):
-    def call(self, context=None):
-        w_obj = W_Object({})
-        for property in self.properties:
-            name = property.name.get_literal()
-            w_expr = property.value.call(context).GetValue()
-            w_obj.Put(name, w_expr)
-        return w_obj
-        #dict_w = {}
-        #for property in self.properties:
-        #    dict_w[property.name
-
-class __extend__(Index):
-    def call(self, context=None):
-        w_obj = self.left.call(context).GetValue()
-        w_member = self.expr.call(context).GetValue()
-        w_obj = w_obj.ToObject()
-        name = w_member.ToString()
-        return w_obj.Get(name)
-
-class __extend__(Function):
-    def call(self, context=None):
-       w_obj = W_Object({}, function=self)
-       return w_obj
-
 class __extend__(Return):
     def call(self, context=None):
         raise ExecutionReturned(self.expr.call(context))

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Wed Nov  1 16:16:10 2006
@@ -2,6 +2,9 @@
 class SeePage(NotImplementedError):
     pass
 
+INFDEF = 1e300 * 1e300
+NaN    = INFDEF/INFDEF
+
 class W_Root(object):
     def GetValue(self):
         return self
@@ -27,7 +30,7 @@
     
     def ToNumber(self):
         # XXX make NaN
-        return 0
+        return NaN
 
 class W_Null(W_Root):
     def __str__(self):
@@ -65,6 +68,9 @@
 
     def __str__(self):
         # XXX: more attention
+        # cough, cough
+        if str(self.floatval) == str(NaN):
+            return 'NaN'
         if float(int(self.floatval)) == self.floatval:
             return str(int(self.floatval))
         return str(self.floatval)
@@ -90,9 +96,11 @@
         self.function = function
         #self.class_ = None
 
-    def Call(self, this=None):
+    def Call(self, context=None, args=None, this=None):
         if self.function:
-            return self.function.body.call()
+            return self.function.body.call(context=context, 
+                                           args=args, this=this, 
+                                           params= self.function.params)
         else:
             raise SeePage(33)
     

Modified: pypy/dist/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_interp.py	Wed Nov  1 16:16:10 2006
@@ -76,3 +76,39 @@
         var z = 2;
         print(x(), y, p);
         """), ["5,3,0"])
+
+    def test_function_args(self):
+        self.assert_prints(parse_d("""
+        x = function (t,r) {
+               return t+r;
+        };
+        print(x(2,3));
+        """), ["5"])
+
+    def test_function_less_args(self):
+        self.assert_prints(parse_d("""
+        x = function (t, r) {
+                return t + r;
+        };
+        print(x(2));
+        """), ["NaN"])
+
+    def test_function_more_args(self):
+        self.assert_prints(parse_d("""
+        x = function (t, r) {
+                return t + r;
+        };
+        print(x(2,3,4));
+        """), ["5"])
+
+    def test_function_arguments(self):
+        self.assert_prints(parse_d("""
+        x = function () {
+                var r = arguments[0];
+                var t = arguments[1];
+                return t + r;
+        };
+        print(x(2,3));
+        """), ["5"])
+
+



More information about the Pypy-commit mailing list