Debian Codegraph @

js/src/tests/non262/Array/iterator_edge_cases.js

raw
1 // SKIP test262 export
2 // Pending review.
3
4 // Test that we can't confuse %ArrayIteratorPrototype% for an
5 // ArrayIterator object.
6 function TestArrayIteratorPrototypeConfusion() {
7 var iter = [][Symbol.iterator]();
8 assertThrowsInstanceOfWithMessage(
9 () => iter.next.call(Object.getPrototypeOf(iter)),
10 TypeError,
11 "next method called on incompatible Array Iterator");
12 }
13 TestArrayIteratorPrototypeConfusion();
14
15 // Tests that we can use %ArrayIteratorPrototype%.next on a
16 // cross-compartment iterator.
17 function TestArrayIteratorWrappers() {
18 var iter = [][Symbol.iterator]();
19 assertDeepEq(iter.next.call(newGlobal().eval('[5][Symbol.iterator]()')),
20 { value: 5, done: false })
21 }
22 TestArrayIteratorWrappers();
23
24 // Tests that calling |next| on an array iterator after iteration has finished
25 // doesn't get the array's |length| property.
26 function TestIteratorNextGetLength() {
27 var lengthCalledTimes = 0;
28 var array = {
29 __proto__: Array.prototype,
30 get length() {
31 lengthCalledTimes += 1;
32 return {
33 valueOf() {
34 return 0;
35 }
36 };
37 }
38 };
39 var it = array[Symbol.iterator]();
40 it.next();
41 it.next();
42 assertEq(1, lengthCalledTimes);
43 }
44 TestIteratorNextGetLength();
45
46
47 if (typeof reportCompare === "function")
48 reportCompare(true, true);