| 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: > |
| 7 |
"Return" completion returned when `return` method is not defined |
| 8 |
info: | |
| 9 |
YieldExpression : yield * AssignmentExpression |
| 10 |
|
| 11 |
1. Let exprRef be the result of evaluating AssignmentExpression. |
| 12 |
2. Let value be ? GetValue(exprRef). |
| 13 |
3. Let iterator be ? GetIterator(value). |
| 14 |
4. Let received be NormalCompletion(undefined). |
| 15 |
5. Repeat |
| 16 |
a. If received.[[Type]] is normal, then |
| 17 |
[...] |
| 18 |
b. Else if received.[[Type]] is throw, then |
| 19 |
[...] |
| 20 |
c. Else, |
| 21 |
i. Assert: received.[[Type]] is return. |
| 22 |
ii. Let return be ? GetMethod(iterator, "return"). |
| 23 |
iii. If return is undefined, return Completion(received). |
| 24 |
features: [generators, Symbol.iterator] |
| 25 |
---*/ |
| 26 |
|
| 27 |
var badIter = {}; |
| 28 |
var throwCount = 0; |
| 29 |
var returnCount = 0; |
| 30 |
var hitNextStatement = false; |
| 31 |
var hitCatch = false; |
| 32 |
var hitFinally = false; |
| 33 |
var spyResult = { |
| 34 |
next: function() { |
| 35 |
return { done: false }; |
| 36 |
} |
| 37 |
}; |
| 38 |
Object.defineProperty(spyResult, 'throw', { |
| 39 |
get: function() { |
| 40 |
throwCount += 1; |
| 41 |
} |
| 42 |
}); |
| 43 |
Object.defineProperty(spyResult, 'return', { |
| 44 |
get: function() { |
| 45 |
returnCount += 1; |
| 46 |
} |
| 47 |
}); |
| 48 |
badIter[Symbol.iterator] = function() { |
| 49 |
return spyResult; |
| 50 |
}; |
| 51 |
function* g() { |
| 52 |
try { |
| 53 |
yield * badIter; |
| 54 |
hitNextStatement = true; |
| 55 |
} catch (_) { |
| 56 |
hitCatch = true; |
| 57 |
} finally { |
| 58 |
hitFinally = true; |
| 59 |
} |
| 60 |
} |
| 61 |
var iter = g(); |
| 62 |
|
| 63 |
iter.next(); |
| 64 |
iter.return(); |
| 65 |
|
| 66 |
assert.sameValue(throwCount, 0, '`throw` property access'); |
| 67 |
assert.sameValue(returnCount, 1, '`return` property access'); |
| 68 |
assert.sameValue( |
| 69 |
hitFinally, true, 'Generator execution was resumed' |
| 70 |
); |
| 71 |
assert.sameValue( |
| 72 |
hitNextStatement, false, 'Abrupt completion interrupted control flow' |
| 73 |
); |
| 74 |
assert.sameValue( |
| 75 |
hitCatch, false, 'Abrupt completion not interpreted as "throw"' |
| 76 |
); |