summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/for-of/next-shenanigans.js
blob: 6918763faf8d0cb3ff67228d8db7e142617d1f58 (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
// Test for-of with iter.next and monkeypatching.

function* g(n) { for (var i=0; i<n; i++) yield i; }
var GeneratorObjectPrototype = Object.getPrototypeOf(g).prototype;
var GeneratorObjectPrototype_next = GeneratorObjectPrototype.next;

// Monkeypatch next on an iterator.
var inner = g(20);
var n = 0;
for (let x of inner) {
    if (n == 0) {
        assertEq(x, n++);
    } else if (n == 1) {
        assertEq(x, n++);
        inner.next = function() { return { value: 42, done: false }; };
    } else if (n == 2) {
        assertEq(x, 42);
        inner.next = function() { return { value: 100, done: true }; };
    } else
        throw 'not reached';
}

// Monkeypatch next on the prototype.
var inner = g(20);
var n = 0;
for (let x of inner) {
    if (n == 0) {
        assertEq(x, n++);
    } else if (n == 1) {
        assertEq(x, n++);
        GeneratorObjectPrototype.next =
            function() { return { value: 42, done: false }; };
    } else if (n == 2) {
        n++;
        assertEq(x, 42);
        GeneratorObjectPrototype.next = GeneratorObjectPrototype_next;
    } else if (n <= 20) {
        assertEq(x, n++ - 1);
    } else
        throw 'not reached';
}