Author: Stephan <[email protected]>
Branch: 
Changeset: r161:7bed36dad633
Date: 2011-12-21 13:07 +0100
http://bitbucket.org/pypy/lang-js/changeset/7bed36dad633/

Log:    added Array.reverse builtin

diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -767,10 +767,16 @@
 
     from js.jsobj import W_ArrayConstructor, W__Array
     w_Array = W_ArrayConstructor()
+    w_Global.Put('Array', w_Array)
 
     # 15.4.4
     w_ArrayPrototype = W__Array()
+
     w_ArrayPrototype._prototype_ = W__Object._prototype_
+    w_ArrayPrototype.Put('__proto__', w_ArrayPrototype._prototype_)
+
+    # 15.4.3.1
+    W__Array._prototype_ = w_ArrayPrototype
 
     # 15.4.4.1
     w_ArrayPrototype.Put('constructor', w_Array)
@@ -784,26 +790,9 @@
     put_native_function(w_ArrayPrototype, 'pop', array_builtins.pop)
     # 15.4.4.7
     put_native_function(w_ArrayPrototype, 'push', array_builtins.push)
+    # 15.4.4.8
+    put_native_function(w_ArrayPrototype, 'reverse', array_builtins.reverse)
 
-    # 15.4.3.1
-    W__Array._prototype_ = w_ArrayPrototype
-
-    #put_values(w_ArrPrototype, {
-        #'constructor': w_FncPrototype,
-        #'__proto__': w_ArrPrototype,
-        #'toString': W_ArrayToString(),
-        #'join': W_ArrayJoin(),
-        #'reverse': W_ArrayReverse(),
-        #'sort': W_ArraySort(),
-        #'push': W_ArrayPush(),
-        #'pop': W_ArrayPop(),
-    #})
-
-    #w_Array._prototype_ = w_FunctionPrototype
-    #w_Array.Put('__proto__', w_FunctionPrototype, flags = allon)
-
-    #w_Array.Put('length', _w(1), flags = allon)
-    w_Global.Put('Array', w_Array)
 
 
     #Math
diff --git a/js/builtins_array.py b/js/builtins_array.py
--- a/js/builtins_array.py
+++ b/js/builtins_array.py
@@ -76,28 +76,35 @@
         o.Put('length', _w(indx))
         return element
 
-#class W_ArrayReverse(W_NewBuiltin):
-    #length = 0
-    #def Call(self, args=[], this=None):
-        #r2 = this.Get('length').ToUInt32()
-        #k = r_uint(0)
-        #r3 = r_uint(math.floor( float(r2)/2.0 ))
-        #if r3 == k:
-            #return this
+# 15.4.4.8
+def reverse(this, *args):
+    o = this.ToObject()
+    length = o.Get('lenght').ToUInt32()
 
-        #while k < r3:
-            #r6 = r2 - k - 1
-            #r7 = str(k)
-            #r8 = str(r6)
+    import math
+    middle = math.floor(lenght/2)
 
-            #r9 = this.Get(r7)
-            #r10 = this.Get(r8)
+    lower = 0
+    while lower != middle:
+        upper = lenght - lower - 1
+        lowerP = str(lower)
+        upperP = str(upper)
+        lowerValue = o.Get(lowerP)
+        upperValue = o.Get(upperP)
+        lowerExists = o.HasProperty(lowerP)
+        upperExists = o.HasProperty(upperP)
 
-            #this.Put(r7, r10)
-            #this.Put(r8, r9)
-            #k += 1
+        if lowerExists is True and upperExists is True:
+            o.Put(lowerP, upperValue)
+            o.Put(upperP, lowerValue)
+        elif lowerExists is False and upperExists is True:
+            o.Put(lowerP, upperValue)
+            o.Delete(upperP)
+        elif lowerExists is True and upperExists is False:
+            o.Delete(lowerP)
+            o.Put(upperP, lowerValue)
 
-        #return this
+        lower = lower + 1
 
 #class W_ArraySort(W_NewBuiltin):
     #length = 1
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to