summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Array/from_iterable.js
blob: 568181961271ac28c6d169f1070ed96003bb22c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/ */

// Array.from works on arguments objects.
(function () {
    assertDeepEq(Array.from(arguments), ["arg0", "arg1", undefined]);
})("arg0", "arg1", undefined);

// If an object has both .length and [@@iterator] properties, [@@iterator] is used.
var a = ['a', 'e', 'i', 'o', 'u'];
a[Symbol.iterator] = function* () {
    for (var i = 5; i--; )
        yield this[i];
};

var log = '';
function f(x) {
    log += x;
    return x + x;
}

var b = Array.from(a, f);
assertDeepEq(b, ['uu', 'oo', 'ii', 'ee', 'aa']);
assertEq(log, 'uoiea');

// In fact, if [@@iterator] is present, .length isn't queried at all.
var pa = new Proxy(a, {
    has: function (target, id) {
        if (id === "length")
            throw new Error(".length should not be queried (has)");
        return id in target;
    },
    get: function (target, id) {
        if (id === "length")
            throw new Error(".length should not be queried (get)");
        return target[id];
    },
    getOwnPropertyDescriptor: function (target, id) {
        if (id === "length")
            throw new Error(".length should not be queried (getOwnPropertyDescriptor)");
        return Object.getOwnPropertyDescriptor(target, id)
    }
});
log = "";
b = Array.from(pa, f);
assertDeepEq(b, ['uu', 'oo', 'ii', 'ee', 'aa']);
assertEq(log, 'uoiea');

if (typeof reportCompare === 'function')
    reportCompare(0, 0);