| 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 |
Abrupt completion returned after protocol violation (and a `return` method |
| 8 |
is defined) |
| 9 |
info: | |
| 10 |
YieldExpression : yield * AssignmentExpression |
| 11 |
|
| 12 |
1. Let exprRef be the result of evaluating AssignmentExpression. |
| 13 |
2. Let value be ? GetValue(exprRef). |
| 14 |
3. Let iterator be ? GetIterator(value). |
| 15 |
4. Let received be NormalCompletion(undefined). |
| 16 |
5. Repeat |
| 17 |
a. If received.[[Type]] is normal, then |
| 18 |
[...] |
| 19 |
b. Else if received.[[Type]] is throw, then |
| 20 |
i. Let throw be ? GetMethod(iterator, "throw"). |
| 21 |
ii. If throw is not undefined, then |
| 22 |
[...] |
| 23 |
iii. Else, |
| 24 |
1. NOTE: If iterator does not have a throw method, this throw is |
| 25 |
going to terminate the yield* loop. But first we need to give |
| 26 |
iterator a chance to clean up. |
| 27 |
2. Perform ? IteratorClose(iterator, Completion{[[Type]]: normal, |
| 28 |
[[Value]]: empty, [[Target]]: empty}). |
| 29 |
3. NOTE: The next step throws a TypeError to indicate that there |
| 30 |
was a yield* protocol violation: iterator does not have a throw |
| 31 |
method. |
| 32 |
4. Throw a TypeError exception. |
| 33 |
|
| 34 |
7.4.6 IteratorClose |
| 35 |
|
| 36 |
1. Assert: Type(iterator) is Object. |
| 37 |
2. Assert: completion is a Completion Record. |
| 38 |
3. Let return be ? GetMethod(iterator, "return"). |
| 39 |
4. If return is undefined, return Completion(completion). |
| 40 |
5. Let innerResult be Call(return, iterator, « »). |
| 41 |
6. If completion.[[Type]] is throw, return Completion(completion). |
| 42 |
7. If innerResult.[[Type]] is throw, return Completion(innerResult). |
| 43 |
8. If Type(innerResult.[[Value]]) is not Object, throw a TypeError exception. |
| 44 |
9. Return Completion(completion). |
| 45 |
features: [generators, Symbol.iterator] |
| 46 |
---*/ |
| 47 |
|
| 48 |
var badIter = {}; |
| 49 |
var throwCount = 0; |
| 50 |
var thisValue, args; |
| 51 |
var poisonedReturn = { |
| 52 |
next: function() { |
| 53 |
return { done: false }; |
| 54 |
}, |
| 55 |
return: function() { |
| 56 |
thisValue = this; |
| 57 |
args = arguments; |
| 58 |
return 'this value has no effect on the protocol'; |
| 59 |
} |
| 60 |
}; |
| 61 |
Object.defineProperty(poisonedReturn, 'throw', { |
| 62 |
get: function() { |
| 63 |
throwCount += 1; |
| 64 |
} |
| 65 |
}); |
| 66 |
badIter[Symbol.iterator] = function() { |
| 67 |
return poisonedReturn; |
| 68 |
}; |
| 69 |
function* g() { |
| 70 |
try { |
| 71 |
yield * badIter; |
| 72 |
} catch (err) { |
| 73 |
caught = err; |
| 74 |
} |
| 75 |
} |
| 76 |
var iter = g(); |
| 77 |
var caught; |
| 78 |
|
| 79 |
iter.next(); |
| 80 |
iter.throw(); |
| 81 |
|
| 82 |
assert.sameValue(throwCount, 1); |
| 83 |
assert.sameValue(thisValue, poisonedReturn, '"this" value'); |
| 84 |
assert.sameValue( |
| 85 |
typeof args, 'object', 'method invoked, arguments object provided' |
| 86 |
); |
| 87 |
assert.sameValue(args.length, 0); |
| 88 |
assert.sameValue(typeof caught, 'object', 'object value thrown'); |
| 89 |
assert.sameValue(caught.constructor, TypeError); |