js/src/jit-test/tests/for-of/next-shenanigans.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Test for-of with iter.next and monkeypatching.
michael@0 2
michael@0 3 function* g(n) { for (var i=0; i<n; i++) yield i; }
michael@0 4 var GeneratorObjectPrototype = Object.getPrototypeOf(g).prototype;
michael@0 5 var GeneratorObjectPrototype_next = GeneratorObjectPrototype.next;
michael@0 6
michael@0 7 // Monkeypatch next on an iterator.
michael@0 8 var inner = g(20);
michael@0 9 var n = 0;
michael@0 10 for (let x of inner) {
michael@0 11 if (n == 0) {
michael@0 12 assertEq(x, n++);
michael@0 13 } else if (n == 1) {
michael@0 14 assertEq(x, n++);
michael@0 15 inner.next = function() { return { value: 42, done: false }; };
michael@0 16 } else if (n == 2) {
michael@0 17 assertEq(x, 42);
michael@0 18 inner.next = function() { return { value: 100, done: true }; };
michael@0 19 } else
michael@0 20 throw 'not reached';
michael@0 21 }
michael@0 22
michael@0 23 // Monkeypatch next on the prototype.
michael@0 24 var inner = g(20);
michael@0 25 var n = 0;
michael@0 26 for (let x of inner) {
michael@0 27 if (n == 0) {
michael@0 28 assertEq(x, n++);
michael@0 29 } else if (n == 1) {
michael@0 30 assertEq(x, n++);
michael@0 31 GeneratorObjectPrototype.next =
michael@0 32 function() { return { value: 42, done: false }; };
michael@0 33 } else if (n == 2) {
michael@0 34 n++;
michael@0 35 assertEq(x, 42);
michael@0 36 GeneratorObjectPrototype.next = GeneratorObjectPrototype_next;
michael@0 37 } else if (n <= 20) {
michael@0 38 assertEq(x, n++ - 1);
michael@0 39 } else
michael@0 40 throw 'not reached';
michael@0 41 }

mercurial