1aee75551e
There was a bug in for-of loops without newly declared variables: If, in performing the assignment, an exception were thrown, then IteratorClose would not be called. The problem was that the assignment is done as part of assign_each, which happens before the loop is put back in the state which is recognized to be breaking/throwing/returning early. This patch modifies the for-of desugaring by setting the loop state before, rather than after, evaluating the assign_each portion, which is responsible for evaluating the assignment in for-of loops which do not have a declaration. This patch, together with https://codereview.chromium.org/1728973002 , allow all test262 iterator return-related tests to pass. R=rossberg BUG=v8:4776 LOG=Y Review URL: https://codereview.chromium.org/1731773003 Cr-Commit-Position: refs/heads/master@{#34262}
374 lines
6.9 KiB
JavaScript
374 lines
6.9 KiB
JavaScript
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// Flags: --harmony-iterator-close
|
|
|
|
function* g() { yield 42; return 88 };
|
|
|
|
|
|
// Return method is "undefined".
|
|
{
|
|
g.prototype.return = null;
|
|
|
|
assertEquals(undefined, (() => {
|
|
for (let x of g()) { break; }
|
|
})());
|
|
|
|
assertEquals(undefined, (() => {
|
|
for (x of g()) { break; }
|
|
})());
|
|
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) { throw 42; }
|
|
}, 42);
|
|
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) { throw 42; }
|
|
}, 42);
|
|
|
|
assertEquals(42, (() => {
|
|
for (let x of g()) { return 42; }
|
|
})());
|
|
|
|
assertEquals(42, (() => {
|
|
for (x of g()) { return 42; }
|
|
})());
|
|
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
}
|
|
|
|
|
|
// Return method is not callable.
|
|
{
|
|
g.prototype.return = 666;
|
|
|
|
assertThrows(() => {
|
|
for (let x of g()) { break; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (x of g()) { break; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (let x of g()) { throw 666; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (x of g()) { throw 666; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (let x of g()) { return 666; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (x of g()) { return 666; }
|
|
}, TypeError);
|
|
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
}
|
|
|
|
|
|
// Return method does not return an object.
|
|
{
|
|
g.prototype.return = () => 666;
|
|
|
|
assertThrows(() => {
|
|
for (let x of g()) { break; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (x of g()) { break; }
|
|
}, TypeError);
|
|
|
|
// Throw from the body of a for loop 'wins' vs throw
|
|
// originating from a bad 'return' value.
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) { throw 666; }
|
|
}, 666);
|
|
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) { throw 666; }
|
|
}, 666);
|
|
|
|
assertThrows(() => {
|
|
for (let x of g()) { return 666; }
|
|
}, TypeError);
|
|
|
|
assertThrows(() => {
|
|
for (x of g()) { return 666; }
|
|
}, TypeError);
|
|
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
|
|
assertEquals(42, eval('for (x of g()) { x; }'));
|
|
}
|
|
|
|
|
|
// Return method returns an object.
|
|
{
|
|
let log = [];
|
|
g.prototype.return = (...args) => { log.push(args); return {} };
|
|
|
|
log = [];
|
|
for (let x of g()) { break; }
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
for (x of g()) { break; }
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) { throw 42; }
|
|
}, 42);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) { throw 42; }
|
|
}, 42);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertEquals(42, (() => {
|
|
for (let x of g()) { return 42; }
|
|
})());
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertEquals(42, (() => {
|
|
for (x of g()) { return 42; }
|
|
})());
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
assertEquals([], log);
|
|
|
|
log = [];
|
|
assertEquals(42, eval('for (x of g()) { x; }'));
|
|
assertEquals([], log);
|
|
|
|
// Even if doing the assignment throws, still call return
|
|
x = { set attr(_) { throw 1234; } };
|
|
assertThrowsEquals(() => {
|
|
for (x.attr of g()) { throw 456; }
|
|
}, 1234);
|
|
assertEquals([[]], log);
|
|
}
|
|
|
|
|
|
// Return method throws.
|
|
{
|
|
let log = [];
|
|
g.prototype.return = (...args) => { log.push(args); throw 23 };
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) { break; }
|
|
}, 23);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) { break; }
|
|
}, 23);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) { throw 42; }
|
|
}, 42);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) { throw 42; }
|
|
}, 42);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) { return 42; }
|
|
}, 23);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) { return 42; }
|
|
}, 23);
|
|
assertEquals([[]], log);
|
|
|
|
log = [];
|
|
assertEquals(42, eval('for (let x of g()) { x; }'));
|
|
assertEquals([], log);
|
|
|
|
log = [];
|
|
assertEquals(42, eval('for (x of g()) { x; }'));
|
|
assertEquals([], log);
|
|
}
|
|
|
|
|
|
// Next method throws.
|
|
{
|
|
g.prototype.next = () => { throw 666; };
|
|
g.prototype.return = () => { assertUnreachable() };
|
|
|
|
assertThrowsEquals(() => {
|
|
for (let x of g()) {}
|
|
}, 666);
|
|
|
|
assertThrowsEquals(() => {
|
|
for (x of g()) {}
|
|
}, 666);
|
|
}
|
|
|
|
|
|
// Nested loops.
|
|
{
|
|
function* g1() { yield 1; yield 2; throw 3; }
|
|
function* g2() { yield -1; yield -2; throw -3; }
|
|
|
|
assertDoesNotThrow(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) break;
|
|
}
|
|
if (x == 2) break;
|
|
}
|
|
}, -3);
|
|
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
}
|
|
}
|
|
}, -3);
|
|
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) break;
|
|
}
|
|
}
|
|
}, 3);
|
|
|
|
assertDoesNotThrow(() => {
|
|
l: for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) break l;
|
|
}
|
|
}
|
|
});
|
|
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
throw 4;
|
|
}
|
|
}
|
|
}, 4);
|
|
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) throw 4;
|
|
}
|
|
}
|
|
}, 4);
|
|
|
|
let log = [];
|
|
g1.prototype.return = () => { log.push(1); throw 5 };
|
|
g2.prototype.return = () => { log.push(2); throw -5 };
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) break;
|
|
}
|
|
if (x == 2) break;
|
|
}
|
|
}, -5);
|
|
assertEquals([2, 1], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
}
|
|
}
|
|
}, -3);
|
|
assertEquals([1], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) break;
|
|
}
|
|
}
|
|
}, -5);
|
|
assertEquals([2, 1], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
l: for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) break l;
|
|
}
|
|
}
|
|
}, -5);
|
|
assertEquals([2, 1], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
throw 4;
|
|
}
|
|
}
|
|
}, 4);
|
|
assertEquals([2, 1], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
for (let y of g2()) {
|
|
if (y == -2) throw 4;
|
|
}
|
|
}
|
|
}, 4);
|
|
assertEquals([2, 1], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
try {
|
|
for (let y of g2()) {
|
|
}
|
|
} catch (_) {}
|
|
}
|
|
}, 3);
|
|
assertEquals([], log);
|
|
|
|
log = [];
|
|
assertThrowsEquals(() => {
|
|
for (let x of g1()) {
|
|
try {
|
|
for (let y of g2()) {
|
|
}
|
|
} catch (_) {}
|
|
if (x == 2) break;
|
|
}
|
|
}, 5);
|
|
assertEquals([1], log);
|
|
}
|