[pypy-commit] lang-js default: implemented [].shift

andrewsmedina noreply at buildbot.pypy.org
Wed May 22 11:30:51 CEST 2013


Author: Andrews Medina <andrewsmedina at gmail.com>
Branch: 
Changeset: r390:601cbe26b7b1
Date: 2013-05-20 00:11 -0300
http://bitbucket.org/pypy/lang-js/changeset/601cbe26b7b1/

Log:	implemented [].shift

diff --git a/js/builtins/array.py b/js/builtins/array.py
--- a/js/builtins/array.py
+++ b/js/builtins/array.py
@@ -46,6 +46,8 @@
 
     put_native_function(w_ArrayPrototype, u'lastIndexOf', last_index_of)
 
+    put_native_function(w_ArrayPrototype, u'shift', shift)
+
 
 # 15.4.4.7
 @w_return
@@ -136,6 +138,25 @@
         return element
 
 
+ at w_return
+def shift(this, args):
+    o = this.ToObject()
+    l = o.get(u'length').ToUInt32()
+
+    if l == 0:
+        o.put(u'length', _w(0))
+        return newundefined()
+    else:
+        new_length = l - 1
+        element = o.get(u"0")
+        for i in xrange(0, new_length):
+            indx = unicode(str(i))
+            next_indx = unicode(str(i + 1))
+            o.put(indx, o.get(next_indx))
+        o.put(u'length', _w(new_length))
+        return element
+
+
 # 15.4.4.8
 @w_return
 def reverse(this, args):
diff --git a/test/test_array.py b/test/test_array.py
--- a/test/test_array.py
+++ b/test/test_array.py
@@ -1,6 +1,12 @@
 from test.test_interp import assertv, assertp
 
 
+def test_array_shift(capsys):
+    assertp("var a = [2, 5, 9, 2]; print(a.shift());", "2", capsys)
+    assertp("var a = [2, 5, 9, 2]; a.shift(); print(a);", "5,9,2", capsys)
+    assertp("var a = [2, 5, 9, 2]; a.shift(); print(a.length);", "3", capsys)
+
+
 def test_array_last_index_of(capsys):
     assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(2));", "3", capsys)
     assertp("var a = [2, 5, 9, 2]; print(a.lastIndexOf(7));", "-1", capsys)


More information about the pypy-commit mailing list