1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/for-of/array-iterator-generic.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,35 @@ 1.4 +// Array.prototype.iterator is generic. 1.5 +// That is, it can be applied to arraylike objects and strings, not just arrays. 1.6 + 1.7 +load(libdir + "asserts.js"); 1.8 +load(libdir + "iteration.js"); 1.9 + 1.10 +function test(obj) { 1.11 + var it = Array.prototype[std_iterator].call(obj); 1.12 + var ki = Array.prototype.keys.call(obj); 1.13 + var ei = Array.prototype.entries.call(obj); 1.14 + for (var i = 0; i < (obj.length >>> 0); i++) { 1.15 + assertIteratorNext(it, obj[i]); 1.16 + assertIteratorNext(ki, i); 1.17 + assertIteratorNext(ei, [i, obj[i]]); 1.18 + } 1.19 + assertIteratorDone(it, undefined); 1.20 + assertIteratorDone(ki, undefined); 1.21 + assertIteratorDone(ei, undefined); 1.22 +} 1.23 + 1.24 +test({length: 0}); 1.25 +test({length: 0, 0: 'x', 1: 'y'}); 1.26 +test({length: 2, 0: 'x', 1: 'y'}); 1.27 +test(Object.create(['x', 'y', 'z'])); 1.28 +test(Object.create({length: 2, 0: 'x', 1: 'y'})); 1.29 +test(""); 1.30 +test("ponies"); 1.31 + 1.32 +// Perverse length values. 1.33 +test({length: 0x1f00000000}); 1.34 +test({length: -0xfffffffe, 0: 'a', 1: 'b'}); 1.35 +test({length: "011", 9: 9, 10: 10, 11: 11}); 1.36 +test({length: -0}); 1.37 +test({length: 2.7, 0: 0, 1: 1, 2: 2}); 1.38 +test({length: {valueOf: function () { return 3; }}, 0: 0, 1: 1, 2: 2});