Speculatively change iteration protocall to use the same next function
[WebKit-https.git] / LayoutTests / js / sequence-iterator-protocol-2.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <meta charset="utf-8">
5         <script src="../resources/js-test-pre.js"></script>
6     </head>
7     <body>
8         <pre id='console'></pre>
9         <canvas></canvas>
10
11         <script>
12             "use strict";
13
14             const log = console.log.bind(console);
15             const canvas = document.querySelector("canvas");
16             const ctx = canvas.getContext("2d");
17
18             let callCount = 0;
19             function makeValue(x) {
20                 return {
21                     valueOf() { ++callCount; return x; }
22                 };
23             }
24
25             let array = [makeValue(1), makeValue(2), makeValue(3)];
26             ctx.setLineDash(array);
27             let a = ctx.getLineDash();
28             log("Using an array but with objects with valueOf()");
29             log(a);
30
31             ctx.setLineDash([1,2,3]);
32             let b = ctx.getLineDash();
33             log(b);
34
35             if (a.toString() !== b.toString())
36                 throw new Error("Bad result. They should be equal.");
37
38             log(`callCount: ${callCount}`);
39             if (callCount !== 3)
40                 throw new Error("Bad result. callCount should be 3.");
41
42
43             let iter = [][Symbol.iterator]();
44             let iterProto = Object.getPrototypeOf(iter);
45             let oldNext = iterProto.next;
46
47             callCount = 0;
48             function hackedNext() {
49                 ++callCount;
50
51                 let val = oldNext.call(this);
52                 if ("value" in val) {
53                     val.value++;
54                 }
55                 return val;
56             }
57             const obj = {
58                 valueOf: function() {
59                     iterProto.next = hackedNext;
60                     return 2;
61                 }
62             };
63             array = [1, obj, 3];
64             ctx.setLineDash(array);
65             b = ctx.getLineDash();
66             log(`${b}`);
67
68             iterProto.next = oldNext;
69             ctx.setLineDash([1,2,3]);
70             a = ctx.getLineDash();
71             log(a);
72             if (a.toString() !== b.toString())
73                 throw new Error("Bad result. They should be equal.");
74
75             log(`callCount: ${callCount}`);
76             if (callCount !== 0)
77                 throw new Error("Bad result. callCount should be 0.");
78
79         </script>
80
81         <script src="../resources/js-test-post.js"></script>
82
83     </body>
84 </html>