summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Array/from-iterator-close.js
blob: 553c44e467840418a49c9240e821d1e1b1b5f660 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var BUGNUMBER = 1180306;
var summary = 'Array.from should close iterator on error';

print(BUGNUMBER + ": " + summary);

function test(ctor, { mapVal=undefined,
                      nextVal=undefined,
                      nextThrowVal=undefined,
                      modifier=undefined,
                      exceptionVal=undefined,
                      exceptionType=undefined,
                      closed=true }) {
    let iterable = {
        closed: false,
        [Symbol.iterator]() {
            let iterator = {
                first: true,
                next() {
                    if (this.first) {
                        this.first = false;
                        if (nextThrowVal)
                            throw nextThrowVal;
                        return nextVal;
                    }
                    return { value: undefined, done: true };
                },
                return() {
                    iterable.closed = true;
                    return {};
                }
            };
            if (modifier)
                modifier(iterator, iterable);

            return iterator;
        }
    };
    if (exceptionVal) {
        let caught = false;
        try {
            ctor.from(iterable, mapVal);
        } catch (e) {
            assertEq(e, exceptionVal);
            caught = true;
        }
        assertEq(caught, true);
    } else if (exceptionType) {
        assertThrowsInstanceOf(() => ctor.from(iterable, mapVal), exceptionType);
    } else {
        ctor.from(iterable, mapVal);
    }
    assertEq(iterable.closed, closed);
}

// == Error cases with close ==

// ES 2017 draft 22.1.2.1 step 5.e.i.1.
// Cannot test.

// ES 2017 draft 22.1.2.1 step 5.e.vi.2.
test(Array, {
    mapVal: () => { throw "map throws"; },
    nextVal: { value: 1, done: false },
    exceptionVal: "map throws",
    closed: true,
});

// ES 2017 draft 22.1.2.1 step 5.e.ix.
class MyArray extends Array {
    constructor() {
        return new Proxy({}, {
            defineProperty() {
                throw "defineProperty throws";
            }
        });
    }
}
test(MyArray, {
    nextVal: { value: 1, done: false },
    exceptionVal: "defineProperty throws",
    closed: true,
});

// ES 2021 draft 7.4.6 step 5.
// if GetMethod fails, the thrown value should be ignored.
test(MyArray, {
    nextVal: { value: 1, done: false },
    modifier: (iterator, iterable) => {
        Object.defineProperty(iterator, "return", {
            get: function() {
                iterable.closed = true;
                throw "return getter throws";
            }
        });
    },
    exceptionVal: "defineProperty throws",
    closed: true,
});
test(MyArray, {
    nextVal: { value: 1, done: false },
    modifier: (iterator, iterable) => {
        Object.defineProperty(iterator, "return", {
            get: function() {
                iterable.closed = true;
                return "non object";
            }
        });
    },
    exceptionVal: "defineProperty throws",
    closed: true,
});
test(MyArray, {
    nextVal: { value: 1, done: false },
    modifier: (iterator, iterable) => {
        Object.defineProperty(iterator, "return", {
            get: function() {
                iterable.closed = true;
                // Non callable.
                return {};
            }
        });
    },
    exceptionVal: "defineProperty throws",
    closed: true,
});

// ES 2017 draft 7.4.6 steps 6.
// if return method throws, the thrown value should be ignored.
test(MyArray, {
    nextVal: { value: 1, done: false },
    modifier: (iterator, iterable) => {
        iterator.return = function() {
            iterable.closed = true;
            throw "return throws";
        };
    },
    exceptionVal: "defineProperty throws",
    closed: true,
});

test(MyArray, {
    nextVal: { value: 1, done: false },
    modifier: (iterator, iterable) => {
        iterator.return = function() {
            iterable.closed = true;
            return "non object";
        };
    },
    exceptionVal: "defineProperty throws",
    closed: true,
});

// == Error cases without close ==

// ES 2017 draft 22.1.2.1 step 5.e.iii.
test(Array, {
    nextThrowVal: "next throws",
    exceptionVal: "next throws",
    closed: false,
});

test(Array, {
    nextVal: { value: {}, get done() { throw "done getter throws"; } },
    exceptionVal: "done getter throws",
    closed: false,
});

// ES 2017 draft 22.1.2.1 step 5.e.v.
test(Array, {
    nextVal: { get value() { throw "value getter throws"; }, done: false },
    exceptionVal: "value getter throws",
    closed: false,
});

// == Successful cases ==

test(Array, {
    nextVal: { value: 1, done: false },
    closed: false,
});

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