star-rhs-iter-thrw-violation-rtrn-get-err.js (2045B)
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 when accessing iterator `return` property after 8 protocol violation 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 30 7.4.6 IteratorClose 31 32 1. Assert: Type(iterator) is Object. 33 2. Assert: completion is a Completion Record. 34 3. Let return be ? GetMethod(iterator, "return"). 35 features: [generators, Symbol.iterator] 36 ---*/ 37 38 var thrown = new Test262Error(); 39 var badIter = {}; 40 var callCount = 0; 41 var poisonedReturn = { 42 next: function() { 43 return { done: false }; 44 } 45 }; 46 Object.defineProperty(poisonedReturn, 'throw', { 47 get: function() { 48 callCount += 1; 49 } 50 }); 51 Object.defineProperty(poisonedReturn, 'return', { 52 get: function() { 53 throw thrown; 54 } 55 }); 56 badIter[Symbol.iterator] = function() { 57 return poisonedReturn; 58 }; 59 function* g() { 60 try { 61 yield * badIter; 62 } catch (err) { 63 caught = err; 64 } 65 } 66 var iter = g(); 67 var caught; 68 69 iter.next(); 70 iter.throw(); 71 72 assert.sameValue(callCount, 1); 73 assert.sameValue(caught, thrown); 74 75 reportCompare(0, 0);