Commits

Andrews Medina committed 477e94f

fixed [].sort() and obj.put behaviour.

Comments (0)

Files changed (4)

js/builtins/array.py

     # 15.4.4.11
     put_native_function(w_ArrayPrototype, u'sort', sort)
 
+    put_native_function(w_ArrayPrototype, u'forEach', for_each)
+
 
 # 15.4.4.7
 @w_return
         lower = lower + 1
 
 
+@w_return
+def for_each(this):
+    obj = this
+    length = this.get(u'length').ToUInt32()
+    return this
+
+
 # 15.4.4.11
 @w_return
 def sort(this, args):
-    obj = this
+    obj = this.ToObject()
     length = this.get(u'length').ToUInt32()
 
     comparefn = get_arg(args, 0)
 
         own_desc = self.get_own_property(p)
         if is_data_descriptor(own_desc) is True:
-            value_desc = PropertyDescriptor(value=v)
+            value_desc = PropertyDescriptor(value=v, writable=True)
             self.define_own_property(p, value_desc, throw)
             return
 

test/test_array.py

 from test.test_interp import assertv, assertp
 
 
+def test_sort(capsys):
+    assertp("var x = [5,2]; print(x.sort());", '2,5', capsys)
+    assertp("var x = [1,2,3]; print(x.sort());", '1,2,3', capsys)
+    assertp("var x = [4,3,2,1]; print(x.sort());", '1,2,3,4', capsys)
+
+
 def test_array_push(capsys):
     assertv("var x = []; x.push(42); x.length;", 1)
     assertv("var x = []; x.push(42); x[0];", 42)

test/test_w_array.py

 from js.object_space import _w
 
 
+def test_array_put_change_index():
+    a = W__Array()
+    a.put(u'0', 42)
+    assert a.get(u'0') == 42
+    a.put(u'0', 43)
+    assert a.get(u'0') == 43
+
+
 def test_array_get():
     a = W__Array()
     a._set_prop(u'23', DataProperty(42, True, True, True))