[pypy-commit] lang-js default: implemented Array.prototype.pop as in ECMA-262 15.4.4.6

stepahn noreply at buildbot.pypy.org
Wed May 18 11:14:46 CEST 2011


Author: Stephan <stephan at stzal.com>
Branch: 
Changeset: r55:8fe2e6977835
Date: 2011-05-12 11:22 +0200
http://bitbucket.org/pypy/lang-js/changeset/8fe2e6977835/

Log:	implemented Array.prototype.pop as in ECMA-262 15.4.4.6

diff --git a/js/interpreter.py b/js/interpreter.py
--- a/js/interpreter.py
+++ b/js/interpreter.py
@@ -574,6 +574,19 @@
         this.Put(ctx, 'length', j);
         return j
 
+class W_ArrayPop(W_NewBuiltin):
+    def Call(self, ctx, args=[], this=None):
+        len = this.Get(ctx, 'length').ToUInt32(ctx)
+        if(len == 0):
+            return w_Undefined
+        else:
+            indx = len-1
+            indxstr = str(indx)
+            element = this.Get(ctx, indxstr)
+            this.Delete(indxstr)
+            this.Put(ctx, 'length', W_IntNumber(indx))
+            return element
+
 class W_ArrayReverse(W_NewBuiltin):
     length = 0
     def Call(self, ctx, args=[], this=None):
@@ -821,6 +834,7 @@
             'reverse': W_ArrayReverse(ctx),
             'sort': W_ArraySort(ctx),
             'push': W_ArrayPush(ctx),
+            'pop': W_ArrayPop(ctx),
         })
 
         w_Array.Put(ctx, 'prototype', w_ArrPrototype, flags = allon)
diff --git a/js/test/test_array.py b/js/test/test_array.py
--- a/js/test/test_array.py
+++ b/js/test/test_array.py
@@ -5,3 +5,9 @@
     yield assertv, "var x = []; x.push(42); x[0];", 42
     yield assertv, "var x = [1,2,3]; x.push(42); x[3];", 42
     yield assertp, "var x = []; x.push(4); x.push(3); x.push(2); x.push(1); print(x)", '4,3,2,1'
+
+def test_array_pop():
+    yield assertv, "var x = [4,3,2,1]; x.pop(); x.length;", 3
+    yield assertv, "var x = [4,3,2,1]; x.pop();", 1
+    yield assertv, "var x = [4,3,2,1]; x.pop(); x.pop(); x.pop(); x.pop();", 4
+    yield assertv, "var x = [4,3,2,1]; x.pop(); x.pop(); x.pop(); x.pop(); x.length", 0


More information about the pypy-commit mailing list