|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 * Contributor: |
|
5 * Jeff Walden <jwalden+code@mit.edu> |
|
6 */ |
|
7 |
|
8 //----------------------------------------------------------------------------- |
|
9 var BUGNUMBER = 858381; |
|
10 var summary = |
|
11 "Array length redefinition behavior with non-configurable elements"; |
|
12 |
|
13 print(BUGNUMBER + ": " + summary); |
|
14 |
|
15 /************** |
|
16 * BEGIN TEST * |
|
17 **************/ |
|
18 |
|
19 function addDataProperty(obj, prop, value, enumerable, configurable, writable) |
|
20 { |
|
21 var desc = |
|
22 { enumerable: enumerable, |
|
23 configurable: configurable, |
|
24 writable: writable, |
|
25 value: value }; |
|
26 Object.defineProperty(obj, prop, desc); |
|
27 } |
|
28 |
|
29 function nonstrict() |
|
30 { |
|
31 var arr = [0, , 2, , , 5]; |
|
32 |
|
33 addDataProperty(arr, 31415926, "foo", true, true, true); |
|
34 addDataProperty(arr, 123456789, "bar", true, true, false); |
|
35 addDataProperty(arr, 8675309, "qux", false, true, true); |
|
36 addDataProperty(arr, 1735039, "eit", false, true, false); |
|
37 addDataProperty(arr, 987654321, "fun", false, true, false); |
|
38 |
|
39 // non-array indexes to spice things up |
|
40 addDataProperty(arr, "foopy", "sdfsd", false, false, false); |
|
41 addDataProperty(arr, 4294967296, "psych", true, false, false); |
|
42 addDataProperty(arr, 4294967295, "psych", true, false, false); |
|
43 |
|
44 addDataProperty(arr, 27182818, "eep", false, false, false); |
|
45 |
|
46 // Truncate...but only as far as possible. |
|
47 arr.length = 1; |
|
48 |
|
49 assertEq(arr.length, 27182819); |
|
50 |
|
51 var props = Object.getOwnPropertyNames(arr).sort(); |
|
52 var expected = |
|
53 ["0", "2", "5", "1735039", "8675309", "27182818", |
|
54 "foopy", "4294967296", "4294967295", "length"].sort(); |
|
55 |
|
56 assertEq(props.length, expected.length); |
|
57 for (var i = 0; i < props.length; i++) |
|
58 assertEq(props[i], expected[i], "unexpected property: " + props[i]); |
|
59 } |
|
60 nonstrict(); |
|
61 |
|
62 function strict() |
|
63 { |
|
64 "use strict"; |
|
65 |
|
66 var arr = [0, , 2, , , 5]; |
|
67 |
|
68 addDataProperty(arr, 31415926, "foo", true, true, true); |
|
69 addDataProperty(arr, 123456789, "bar", true, true, false); |
|
70 addDataProperty(arr, 8675309, "qux", false, true, true); |
|
71 addDataProperty(arr, 1735039, "eit", false, true, false); |
|
72 addDataProperty(arr, 987654321, "fun", false, true, false); |
|
73 |
|
74 // non-array indexes to spice things up |
|
75 addDataProperty(arr, "foopy", "sdfsd", false, false, false); |
|
76 addDataProperty(arr, 4294967296, "psych", true, false, false); |
|
77 addDataProperty(arr, 4294967295, "psych", true, false, false); |
|
78 |
|
79 addDataProperty(arr, 27182818, "eep", false, false, false); |
|
80 |
|
81 try |
|
82 { |
|
83 arr.length = 1; |
|
84 throw new Error("didn't throw?!"); |
|
85 } |
|
86 catch (e) |
|
87 { |
|
88 assertEq(e instanceof TypeError, true, |
|
89 "non-configurable property should trigger TypeError, got " + e); |
|
90 } |
|
91 |
|
92 assertEq(arr.length, 27182819); |
|
93 |
|
94 var props = Object.getOwnPropertyNames(arr).sort(); |
|
95 var expected = |
|
96 ["0", "2", "5", "1735039", "8675309", "27182818", |
|
97 "foopy", "4294967296", "4294967295", "length"].sort(); |
|
98 |
|
99 assertEq(props.length, expected.length); |
|
100 for (var i = 0; i < props.length; i++) |
|
101 assertEq(props[i], expected[i], "unexpected property: " + props[i]); |
|
102 } |
|
103 strict(); |
|
104 |
|
105 /******************************************************************************/ |
|
106 |
|
107 if (typeof reportCompare === "function") |
|
108 reportCompare(true, true); |
|
109 |
|
110 print("Tests complete"); |