Debian Codegraph @

js/src/tests/test262/language/statements/for-of/dstr/array-elem-iter-rtrn-close-null.js

raw
1 // This file was procedurally generated from the following sources:
2 // - src/dstr-assignment/array-elem-iter-rtrn-close-null.case
3 // - src/dstr-assignment/default/for-of.template
4 /*---
5 description: IteratorClose throws a TypeError when `return` returns a non-Object value (For..of statement)
6 esid: sec-for-in-and-for-of-statements-runtime-semantics-labelledevaluation
7 features: [Symbol.iterator, generators, destructuring-binding]
8 flags: [generated]
9 info: |
10 IterationStatement :
11 for ( LeftHandSideExpression of AssignmentExpression ) Statement
12
13 1. Let keyResult be the result of performing ? ForIn/OfHeadEvaluation(« »,
14 AssignmentExpression, iterate).
15 2. Return ? ForIn/OfBodyEvaluation(LeftHandSideExpression, Statement,
16 keyResult, assignment, labelSet).
17
18 13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation
19
20 [...]
21 4. If destructuring is true and if lhsKind is assignment, then
22 a. Assert: lhs is a LeftHandSideExpression.
23 b. Let assignmentPattern be the parse of the source text corresponding to
24 lhs using AssignmentPattern as the goal symbol.
25 [...]
26
27 ArrayAssignmentPattern : [ AssignmentElementList ]
28
29 [...]
30 5. If iteratorRecord.[[done]] is false, return IteratorClose(iterator,
31 result).
32 6. Return result.
33
34 7.4.6 IteratorClose( iterator, completion )
35
36 [...]
37 6. Let innerResult be Call(return, iterator, « »).
38 7. If completion.[[type]] is throw, return Completion(completion).
39 8. If innerResult.[[type]] is throw, return Completion(innerResult).
40 9. If Type(innerResult.[[value]]) is not Object, throw a TypeError
41 exception.
42
43 ---*/
44 var nextCount = 0;
45 var returnCount = 0;
46 var unreachable = 0;
47 var iterator = {
48 next: function() {
49 nextCount += 1;
50 return {done: false, value: undefined};
51 },
52 return: function() {
53 returnCount += 1;
54 return null;
55 }
56 };
57 var iterable = {};
58 iterable[Symbol.iterator] = function() {
59 return iterator;
60 };
61 function* g() {
62
63 var counter = 0;
64
65 for ([ {} = yield ] of [iterable]) {
66 unreachable += 1;
67 counter += 1;
68 }
69
70 assert.sameValue(counter, 1);
71
72 }
73
74 var iter = g();
75 iter.next();
76
77 assert.sameValue(nextCount, 1);
78 assert.sameValue(returnCount, 0);
79 assert.throws(TypeError, function() {
80 iter.return();
81 });
82 assert.sameValue(nextCount, 1);
83 assert.sameValue(returnCount, 1);
84 assert.sameValue(unreachable, 0, 'Unreachable statement was not executed');
85
86 reportCompare(0, 0);