]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | function shouldThrow(func, message) { |
2 | var error = null; | |
3 | try { | |
4 | func(); | |
5 | } catch (e) { | |
6 | error = e; | |
7 | } | |
8 | if (!error) | |
9 | throw new Error("not thrown."); | |
10 | if (String(error) !== message) | |
11 | throw new Error("bad error: " + String(error)); | |
12 | } | |
13 | ||
14 | var array = new Array(10); | |
15 | ||
16 | for (var i = 0; i < 10; ++i) { | |
17 | (function (index) { | |
18 | var seenOnce = false; | |
19 | var storage = null; | |
20 | Object.defineProperty(Array.prototype, index, { | |
21 | get() { | |
22 | throw new Error('get is called.' + index); | |
23 | return storage; | |
24 | }, | |
25 | set(value) { | |
26 | if (seenOnce) | |
27 | throw new Error('set is called.' + index); | |
28 | seenOnce = true; | |
29 | storage = value; | |
30 | return storage; | |
31 | } | |
32 | }); | |
33 | }(i)); | |
34 | } | |
35 | ||
36 | // No error, but all seenOnce becomes true. | |
37 | array.fill(42); | |
38 | ||
39 | // Ensures that all setter is called once. | |
40 | for (var i = 0; i < 10; ++i) { | |
41 | shouldThrow(function () { | |
42 | array[i] = i; | |
43 | }, "Error: set is called." + i); | |
44 | } |