star-rhs-iter-thrw-violation-no-rtrn.js (2451B)
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 not 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 features: [generators, Symbol.iterator] 41 ---*/ 42 43 var badIter = {}; 44 var throwCount = 0; 45 var returnCount = 0; 46 var spyResult = { 47 next: function() { 48 return { done: false }; 49 } 50 }; 51 Object.defineProperty(spyResult, 'throw', { 52 get: function() { 53 throwCount += 1; 54 } 55 }); 56 Object.defineProperty(spyResult, 'return', { 57 get: function() { 58 returnCount += 1; 59 } 60 }); 61 badIter[Symbol.iterator] = function() { 62 return spyResult; 63 }; 64 function* g() { 65 try { 66 yield * badIter; 67 } catch (err) { 68 caught = err; 69 } 70 } 71 var iter = g(); 72 var caught; 73 74 iter.next(); 75 iter.throw(); 76 77 assert.sameValue(throwCount, 1, '`throw` property access'); 78 assert.sameValue(returnCount, 1, '`return` property access'); 79 assert.sameValue(typeof caught, 'object'); 80 assert.sameValue(caught.constructor, TypeError); 81 82 reportCompare(0, 0);