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

santagada at codespeak.net santagada at codespeak.net
Tue Mar 20 04:01:17 CET 2007


Author: santagada
Date: Tue Mar 20 04:01:15 2007
New Revision: 40805

Modified:
   pypy/dist/pypy/lang/js/jsobj.py
   pypy/dist/pypy/lang/js/operations.py
   pypy/dist/pypy/lang/js/test/test_interp.py
   pypy/dist/pypy/lang/js/test/test_parser.py
Log:
made arrays work a lot more like the javascript ones (but they can be optimzed a lot)

Modified: pypy/dist/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/dist/pypy/lang/js/jsobj.py	(original)
+++ pypy/dist/pypy/lang/js/jsobj.py	Tue Mar 20 04:01:15 2007
@@ -292,38 +292,42 @@
         toString = W_Builtin(array_str_builtin)
         self.Put('toString', toString, de=True)
         self.Put('length', W_Number(0))
-        self.array = []
+        self.length = r_uint(0)
         self.set_builtin_call(arraycallbi)
 
     def Put(self, P, V, dd=False,
             ro=False, de=False, it=False):
-        try:
-            x = int(P)
-            # print "puting", V, 'in', x
-            if x > self.Get('length').ToNumber() - 1:
-                currsize = len(self.array)
-                self.propdict['length'].value = W_Number(x+1)
-                print x+1
-                for i in range(x-(currsize-1)):
-                    self.array.append(w_Undefined)
-            self.array[x]= V
-                    
-        except ValueError:
-            W_Builtin.Put(self, P, V, dd=dd, ro=ro, de=de, it=it)
+        
+        if not self.CanPut(P): return
+        if P in self.propdict:
+            if P == 'length':
+                try:
+                    res = V.ToUInt32()
+                    if V.ToNumber() < 0:
+                        raise RangeError()
+                    self.propdict['length'].value = W_Number(res)
+                    self.length = res
+                    return
+                except ValueError:
+                    raise RangeError('invalid array length')
+            else:
+                self.propdict[P].value = V
+        else:
+            self.propdict[P] = Property(P, V,
+            dd = dd, ro = ro, it = it)
 
-    def Get(self, P):
         try:
-            x = int(P)
-            if x > (len(self.array)-1):
-                return w_Undefined
-            else:
-                # print "returning", self.array[x], 'in', x
-                return self.array[x]
+            index = r_uint(float(P))
         except ValueError:
-            return W_PrimitiveObject.Get(self, P)
+            return
+        if index < self.length:
+            return
+        self.length = index+1
+        self.propdict['length'].value = W_Number(index+1)
+        return
     
     def ToString(self):
-        return ','.join([item.ToString() for item in self.array])
+        return ','.join([self.Get(str(index)).ToString() for index in range(self.length)])
 
 def array_str_builtin(ctx, args, this):
     return W_String(this.ToString())
@@ -439,6 +443,7 @@
         self.scope = []
         self.this = None
         self.variable = None
+        self.debug = False
         self.property = Property('',w_Undefined) #Attribute flags for new vars
     
     def assign(self, name, value):

Modified: pypy/dist/pypy/lang/js/operations.py
==============================================================================
--- pypy/dist/pypy/lang/js/operations.py	(original)
+++ pypy/dist/pypy/lang/js/operations.py	Tue Mar 20 04:01:15 2007
@@ -968,6 +968,10 @@
     def execute(self, ctx):
         for var in self.nodes:
             var.execute(ctx)
+        return W_String(self.nodes[-1].get_literal())
+
+    def eval(self, ctx):
+        return self.execute(ctx)
 
 class Void(UnaryOp):
     opcode = 'VOID'

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	Tue Mar 20 04:01:15 2007
@@ -503,3 +503,9 @@
         print(2 & 3)
         print(2 | 3)
         """, ['0', '2', '3'])
+
+    def test_for_strange(self):
+        self.assert_prints("""
+        for (var arg = "", i = 0; i < 2; i++) { print(i)}
+        """, ['0', '1'])
+

Modified: pypy/dist/pypy/lang/js/test/test_parser.py
==============================================================================
--- pypy/dist/pypy/lang/js/test/test_parser.py	(original)
+++ pypy/dist/pypy/lang/js/test/test_parser.py	Tue Mar 20 04:01:15 2007
@@ -6,5 +6,5 @@
 
 
 def test_quoting():
-    read_js_output('x = "\'"')
+    read_js_output("x = '\\''")
     read_js_output('x = "\\\\"')
\ No newline at end of file



More information about the Pypy-commit mailing list