| 1 |
// Copyright (C) 2017 André Bargull. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset |
| 6 |
description: > |
| 7 |
Iterators should be closed via their `return` method when iteration is |
| 8 |
interrupted via a `continue` statement. |
| 9 |
info: | |
| 10 |
13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet ) |
| 11 |
... |
| 12 |
5. Repeat, |
| 13 |
... |
| 14 |
i. Let result be the result of evaluating stmt. |
| 15 |
... |
| 16 |
k. If LoopContinues(result, labelSet) is false, then |
| 17 |
i. If iterationKind is enumerate, then |
| 18 |
... |
| 19 |
ii. Else, |
| 20 |
1. Assert: iterationKind is iterate. |
| 21 |
2. Return ? IteratorClose(iteratorRecord, UpdateEmpty(result, V)). |
| 22 |
... |
| 23 |
|
| 24 |
features: [Symbol.iterator] |
| 25 |
---*/ |
| 26 |
|
| 27 |
var startedCount = 0; |
| 28 |
var returnCount = 0; |
| 29 |
var iterationCount = 0; |
| 30 |
var iterable = {}; |
| 31 |
|
| 32 |
iterable[Symbol.iterator] = function() { |
| 33 |
return { |
| 34 |
next: function() { |
| 35 |
startedCount += 1; |
| 36 |
return { done: false, value: null }; |
| 37 |
}, |
| 38 |
return: function() { |
| 39 |
returnCount += 1; |
| 40 |
return {}; |
| 41 |
} |
| 42 |
}; |
| 43 |
}; |
| 44 |
|
| 45 |
L: do { |
| 46 |
for (var x of iterable) { |
| 47 |
assert.sameValue( |
| 48 |
startedCount, 1, 'Value is retrieved' |
| 49 |
); |
| 50 |
assert.sameValue( |
| 51 |
returnCount, 0, 'Iterator is not closed' |
| 52 |
); |
| 53 |
iterationCount += 1; |
| 54 |
continue L; |
| 55 |
} |
| 56 |
} while (false); |
| 57 |
|
| 58 |
assert.sameValue( |
| 59 |
startedCount, 1, 'Iterator does not restart following interruption' |
| 60 |
); |
| 61 |
assert.sameValue(iterationCount, 1, 'A single iteration occurs'); |
| 62 |
assert.sameValue( |
| 63 |
returnCount, 1, 'Iterator is closed after `continue` statement' |
| 64 |
); |
| 65 |
|
| 66 |
reportCompare(0, 0); |