Debian Codegraph @

js/src/tests/test262/language/statements/for-of/iterator-next-reference.js

raw
1 // Copyright (C) 2013 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-getiterator
5 description: >
6 The iterator's `next` method should be accessed only once with each
7 iteration as per the `GetIterator` abstract operation (7.4.1).
8 features: [Symbol.iterator, for-of]
9 ---*/
10
11 var iterable = {};
12 var iterator = {};
13 var iterationCount = 0;
14 var loadNextCount = 0;
15
16 iterable[Symbol.iterator] = function() {
17 return iterator;
18 };
19
20 function next() {
21 if (iterationCount) return { done: true };
22 return { value: 45, done: false };
23 }
24 Object.defineProperty(iterator, 'next', {
25 get() { loadNextCount++; return next; },
26 configurable: true
27 });
28
29 for (var x of iterable) {
30 assert.sameValue(x, 45);
31
32 Object.defineProperty(iterator, 'next', {
33 get: function() {
34 throw new Test262Error(
35 'Should not access the `next` method after the iteration prologue.');
36 }
37 });
38 iterationCount++;
39 }
40 assert.sameValue(iterationCount, 1);
41 assert.sameValue(loadNextCount, 1);
42
43 reportCompare(0, 0);