| 1 |
// Copyright (C) 2016 the V8 project authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
/*--- |
| 4 |
esid: sec-generator-function-definitions-runtime-semantics-evaluation |
| 5 |
es6id: 14.4.14 |
| 6 |
description: Abrupt completion returned when accessing iterator `return` method |
| 7 |
info: | |
| 8 |
YieldExpression : yield * AssignmentExpression |
| 9 |
|
| 10 |
1. Let exprRef be the result of evaluating AssignmentExpression. |
| 11 |
2. Let value be ? GetValue(exprRef). |
| 12 |
3. Let iterator be ? GetIterator(value). |
| 13 |
4. Let received be NormalCompletion(undefined). |
| 14 |
5. Repeat |
| 15 |
a. If received.[[Type]] is normal, then |
| 16 |
[...] |
| 17 |
b. Else if received.[[Type]] is throw, then |
| 18 |
[...] |
| 19 |
c. Else, |
| 20 |
i. Assert: received.[[Type]] is return. |
| 21 |
ii. Let return be ? GetMethod(iterator, "return"). |
| 22 |
features: [generators, Symbol.iterator] |
| 23 |
---*/ |
| 24 |
|
| 25 |
var thrown = new Test262Error(); |
| 26 |
var badIter = {}; |
| 27 |
var callCount = 0; |
| 28 |
var poisonedReturn = { |
| 29 |
next: function() { |
| 30 |
return { done: false }; |
| 31 |
} |
| 32 |
}; |
| 33 |
Object.defineProperty(poisonedReturn, 'throw', { |
| 34 |
get: function() { |
| 35 |
callCount += 1; |
| 36 |
} |
| 37 |
}); |
| 38 |
Object.defineProperty(poisonedReturn, 'return', { |
| 39 |
get: function() { |
| 40 |
throw thrown; |
| 41 |
} |
| 42 |
}); |
| 43 |
badIter[Symbol.iterator] = function() { |
| 44 |
return poisonedReturn; |
| 45 |
}; |
| 46 |
function* g() { |
| 47 |
try { |
| 48 |
yield * badIter; |
| 49 |
} catch (err) { |
| 50 |
caught = err; |
| 51 |
} |
| 52 |
} |
| 53 |
var iter = g(); |
| 54 |
var result, caught; |
| 55 |
|
| 56 |
iter.next(); |
| 57 |
|
| 58 |
assert.sameValue(caught, undefined, '`return` property not accessed eagerly'); |
| 59 |
|
| 60 |
result = iter.return(); |
| 61 |
|
| 62 |
assert.sameValue(result.value, undefined); |
| 63 |
assert.sameValue(result.done, true); |
| 64 |
assert.sameValue(caught, thrown); |
| 65 |
assert.sameValue(callCount, 0, 'iterator `throw` property is not accessed'); |