star-throw-is-null.js (1682B)
1 // Copyright (C) 2020 Alexey Shvayka. 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 description: > 6 If iterator's "throw" method is `null`, 7 IteratorClose is called before rising TypeError. 8 info: | 9 YieldExpression : yield * AssignmentExpression 10 11 [...] 12 7. Repeat, 13 [...] 14 b. Else if received.[[Type]] is throw, then 15 i. Let throw be ? GetMethod(iterator, "throw"). 16 ii. If throw is not undefined, then 17 [...] 18 iii. Else, 19 [...] 20 4. Else, perform ? IteratorClose(iteratorRecord, closeCompletion). 21 [...] 22 6. Throw a TypeError exception. 23 24 GetMethod ( V, P ) 25 26 [...] 27 2. Let func be ? GetV(V, P). 28 3. If func is either undefined or null, return undefined. 29 30 IteratorClose ( iteratorRecord, completion ) 31 32 [...] 33 4. Let innerResult be GetMethod(iterator, "return"). 34 5. If innerResult.[[Type]] is normal, then 35 a. Let return be innerResult.[[Value]]. 36 b. If return is undefined, return Completion(completion). 37 features: [generators, Symbol.iterator] 38 ---*/ 39 40 var throwGets = 0; 41 var returnGets = 0; 42 var iterable = { 43 next: function() { 44 return {value: 1, done: false}; 45 }, 46 get throw() { 47 throwGets += 1; 48 return null; 49 }, 50 get return() { 51 returnGets += 1; 52 }, 53 }; 54 55 iterable[Symbol.iterator] = function() { 56 return iterable; 57 }; 58 59 function* generator() { 60 yield* iterable; 61 } 62 63 var iterator = generator(); 64 iterator.next(); 65 66 assert.throws(TypeError, function() { 67 iterator.throw(); 68 }); 69 70 assert.sameValue(throwGets, 1); 71 assert.sameValue(returnGets, 1); 72 73 reportCompare(0, 0);