summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/Map/constructor-iterator-close.js
blob: ba4bee4a74874ddffe808a15c7a80680eb3e964e (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var BUGNUMBER = 1180306;
var summary = 'Map/Set/WeakMap/WeakSet constructor should close iterator on error';

print(BUGNUMBER + ": " + summary);

function test(ctors, { nextVal=undefined,
                       nextThrowVal=undefined,
                       modifier=undefined,
                       exceptionVal=undefined,
                       exceptionType=undefined,
                       closed=true }) {
    function getIterable() {
        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;
            }
        };
        return iterable;
    }

    for (let ctor of ctors) {
        let iterable = getIterable();
        if (exceptionVal) {
            let caught = false;
            try {
                new ctor(iterable);
            } catch (e) {
                assertEq(e, exceptionVal);
                caught = true;
            }
            assertEq(caught, true);
        } else if (exceptionType) {
            assertThrowsInstanceOf(() => new ctor(iterable), exceptionType);
        } else {
            new ctor(iterable);
        }
        assertEq(iterable.closed, closed);
    }
}

// == Error cases with close ==

// ES 2017 draft 23.1.1.1 Map step 8.d.ii.
// ES 2017 draft 23.3.1.1 WeakMap step 8.d.ii.
test([Map, WeakMap], {
    nextVal: { value: "non object", done: false },
    exceptionType: TypeError,
    closed: true,
});

// ES 2017 draft 23.1.1.1 Map step 8.f.
// ES 2017 draft 23.3.1.1 WeakMap step 8.f.
test([Map, WeakMap], {
    nextVal: { value: { get 0() { throw "0 getter throws"; } }, done: false },
    exceptionVal: "0 getter throws",
    closed: true,
});

// ES 2017 draft 23.1.1.1 Map step 8.h.
// ES 2017 draft 23.3.1.1 WeakMap step 8.h.
test([Map, WeakMap], {
    nextVal: { value: { 0: {}, get 1() { throw "1 getter throws"; } }, done: false },
    exceptionVal: "1 getter throws",
    closed: true,
});

// ES 2017 draft 23.1.1.1 Map step 8.j.
// ES 2017 draft 23.3.1.1 WeakMap step 8.j.
class MyMap extends Map {
    set(k, v) {
        throw "setter throws";
    }
}
class MyWeakMap extends WeakMap {
    set(k, v) {
        throw "setter throws";
    }
}
test([MyMap, MyWeakMap], {
    nextVal: { value: [{}, {}], done: false },
    exceptionVal: "setter throws",
    closed: true,
});

// ES 2017 draft 23.2.1.1 Set step 8.e.
// ES 2017 draft 23.4.1.1 WeakSet step 8.e.
class MySet extends Set {
    add(v) {
        throw "adder throws";
    }
}
class MyWeakSet extends WeakSet {
    add(v) {
        throw "adder throws";
    }
}
test([MySet, MyWeakSet], {
    nextVal: { value: {}, done: false },
    exceptionVal: "adder throws",
    closed: true,
});

// ES 2017 draft 7.4.6 step 3.
// if GetMethod fails, the thrown value should be used.
test([MyMap, MySet, MyWeakMap, MyWeakSet], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        Object.defineProperty(iterator, "return", {
            get: function() {
                iterable.closed = true;
                throw "return getter throws";
            }
        });
    },
    exceptionVal: "return getter throws",
    closed: true,
});
test([MyMap, MySet, MyWeakMap, MyWeakSet], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        Object.defineProperty(iterator, "return", {
            get: function() {
                iterable.closed = true;
                return "non object";
            }
        });
    },
    exceptionType: TypeError,
    closed: true,
});
test([MyMap, MySet, MyWeakMap, MyWeakSet], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        Object.defineProperty(iterator, "return", {
            get: function() {
                iterable.closed = true;
                // Non callable.
                return {};
            }
        });
    },
    exceptionType: TypeError,
    closed: true,
});

// ES 2017 draft 7.4.6 steps 6.
// if return method throws, the thrown value should be ignored.
test([MyMap, MyWeakMap], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        iterator.return = function() {
            iterable.closed = true;
            throw "return throws";
        };
    },
    exceptionVal: "setter throws",
    closed: true,
});
test([MySet, MyWeakSet], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        iterator.return = function() {
            iterable.closed = true;
            throw "return throws";
        };
    },
    exceptionVal: "adder throws",
    closed: true,
});

test([MyMap, MyWeakMap], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        iterator.return = function() {
            iterable.closed = true;
            return "non object";
        };
    },
    exceptionVal: "setter throws",
    closed: true,
});
test([MySet, MyWeakSet], {
    nextVal: { value: [{}, {}], done: false },
    modifier: (iterator, iterable) => {
        iterator.return = function() {
            iterable.closed = true;
            return "non object";
        };
    },
    exceptionVal: "adder throws",
    closed: true,
});

// == Error cases without close ==

// ES 2017 draft 23.1.1.1 Map step 8.a.
// ES 2017 draft 23.3.1.1 WeakMap step 8.a.
// ES 2017 draft 23.2.1.1 Set step 8.a.
// ES 2017 draft 23.4.1.1 WeakSet step 8.a.
test([Map, WeakMap, Set, WeakSet], {
    nextThrowVal: "next throws",
    exceptionVal: "next throws",
    closed: false,
});
test([Map, WeakMap, Set, WeakSet], {
    nextVal: { value: {}, get done() { throw "done getter throws"; } },
    exceptionVal: "done getter throws",
    closed: false,
});

// ES 2017 draft 23.1.1.1 Map step 8.c.
// ES 2017 draft 23.3.1.1 WeakMap step 8.c.
// ES 2017 draft 23.2.1.1 Set step 8.c.
// ES 2017 draft 23.4.1.1 WeakSet step 8.c.
test([Map, WeakMap, Set, WeakSet], {
    nextVal: { get value() { throw "value getter throws"; }, done: false },
    exceptionVal: "value getter throws",
    closed: false,
});

// == Successful cases ==

test([Map, WeakMap], {
    nextVal: { value: [{}, {}], done: false },
    closed: false,
});
test([Set, WeakSet], {
    nextVal: { value: {}, done: false },
    closed: false,
});

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