[pypy-commit] lang-js default: implemented Array.prototype.push as in ECMA 15.4.4.7

stepahn noreply at buildbot.pypy.org
Fri May 13 13:45:13 CEST 2011


Author: Stephan <stephan at stzal.com>
Branch: 
Changeset: r41:252c34d0986c
Date: 2011-05-08 19:45 +0200
http://bitbucket.org/pypy/lang-js/changeset/252c34d0986c/

Log:	implemented Array.prototype.push as in ECMA 15.4.4.7

diff --git a/js/interpreter.py b/js/interpreter.py
--- a/js/interpreter.py
+++ b/js/interpreter.py
@@ -562,6 +562,16 @@
 
         return W_String(common_join(ctx, this, sep))
 
+class W_ArrayPush(W_NewBuiltin):
+    def Call(self, ctx, args=[], this=None):
+        n = this.Get(ctx, 'length').ToUInt32(ctx)
+        for arg in args:
+            this.Put(ctx, str(n), arg)
+            n += 1
+        j = W_IntNumber(n)
+        this.Put(ctx, 'length', j);
+        return j
+
 class W_ArrayReverse(W_NewBuiltin):
     length = 0
     def Call(self, ctx, args=[], this=None):
@@ -808,6 +818,7 @@
             'join': W_ArrayJoin(ctx),
             'reverse': W_ArrayReverse(ctx),
             'sort': W_ArraySort(ctx),
+            'push': W_ArrayPush(ctx),
         })
 
         w_Array.Put(ctx, 'prototype', w_ArrPrototype, flags = allon)
diff --git a/js/test/test_array.py b/js/test/test_array.py
new file mode 100644
--- /dev/null
+++ b/js/test/test_array.py
@@ -0,0 +1,7 @@
+from js.test.test_interp import assertv, assertp
+
+def test_array_push():
+    yield assertv, "var x = []; x.push(42); x.length;", 1
+    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'


More information about the pypy-commit mailing list