summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/Array/from_proxy.js
blob: c3c82d0b7e4835b33268ab2fe47327a1f07cfb3a (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
51
52
53
54
55
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/ */

// Two tests involving Array.from and a Proxy.
var log = [];
function LoggingProxy(target) {
    var h = {
        defineProperty: function (t, id) {
            log.push("define", id);
            return true;
        },
        has: function (t, id) {
            log.push("has", id);
            return id in t;
        },
        get: function (t, id) {
            log.push("get", id);
            return t[id];
        },
        set: function (t, id, v) {
            log.push("set", id);
            t[id] = v;
            return true;
        }
    };
    return new Proxy(target || [], h);
}

// When the new object created by Array.from is a Proxy,
// Array.from calls handler.defineProperty to create new elements
// but handler.set to set the length.
LoggingProxy.from = Array.from;
LoggingProxy.from([3, 4, 5]);
assertDeepEq(log, ["define", "0", "define", "1", "define", "2", "set", "length"]);

// When the argument passed to Array.from is a Proxy, Array.from
// calls handler.get on it.
log = [];
assertDeepEq(Array.from(new LoggingProxy([3, 4, 5])), [3, 4, 5]);
assertDeepEq(log, ["get", Symbol.iterator,
                   "get", "length", "get", "0",
                   "get", "length", "get", "1",
                   "get", "length", "get", "2",
                   "get", "length"]);

// Array-like iteration only gets the length once.
log = [];
var arr = [5, 6, 7];
arr[Symbol.iterator] = undefined;
assertDeepEq(Array.from(new LoggingProxy(arr)), [5, 6, 7]);
assertDeepEq(log, ["get", Symbol.iterator,
                   "get", "length", "get", "0", "get", "1", "get", "2"]);

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