]> git.saurik.com Git - apple/javascriptcore.git/blame - tests/stress/array-find-does-not-lookup-twice.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / array-find-does-not-lookup-twice.js
CommitLineData
ed1e77d3
A
1function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + actual);
4}
5
6function shouldThrow(func, message) {
7 var error = null;
8 try {
9 func();
10 } catch (e) {
11 error = e;
12 }
13 if (!error)
14 throw new Error("not thrown.");
15 if (String(error) !== message)
16 throw new Error("bad error: " + String(error));
17}
18
19var array = new Array(10);
20
21for (var i = 0; i < 10; ++i) {
22 (function (index) {
23 var seenOnce = false;
24 Object.defineProperty(array, index, {
25 get() {
26 if (seenOnce)
27 throw new Error('get is called.' + index);
28 seenOnce = true;
29 return index;
30 }
31 });
32 }(i));
33}
34
35shouldBe(array.length, 10);
36
37// Doesn't throw any errors.
38var findValue = array.find(function (value) {
39 return value === 9;
40});
41shouldBe(findValue, 9);
42
43for (var i = 0; i < 10; ++i) {
44 shouldThrow(function () {
45 var value = array[i];
46 }, "Error: get is called." + i);
47}