Debian Codegraph @

js/src/jit-test/tests/basic/non-extensible-elements9.js

raw
1 function testNonExtensibleStoreFallibleT() {
2 // Create an array with initialized-length = capacity = 2.
3 var x = [8, 0];
4
5 // Make it non-extensible.
6 Object.preventExtensions(x);
7
8 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
9 x.length = 1;
10
11 // There's enough capacity in the elements storage to save the new element,
12 // but we still need to reject the store since the object is non-extensible.
13 x[1] = 4;
14
15 assertEq(x.length, 1);
16 assertEq(x[0], 8);
17 }
18
19 for (var i = 0; i < 15; ++i)
20 testNonExtensibleStoreFallibleT();
21
22 // Repeat testNonExtensibleStoreFallibleT for the MIRType::Value specialization.
23 function testNonExtensibleStoreFallibleV(i) {
24 // Create an array with initialized-length = capacity = 2.
25 var x = [8, ""];
26
27 // Make it non-extensible.
28 Object.preventExtensions(x);
29
30 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
31 x.length = 1;
32
33 // There's enough capacity in the elements storage to save the new element,
34 // but we still need to reject the store since the object is non-extensible.
35 x[1] = [true, 1][i & 1];
36
37 assertEq(x.length, 1);
38 assertEq(x[0], 8);
39 }
40
41 for (var i = 0; i < 15; ++i)
42 testNonExtensibleStoreFallibleV(i);
43
44 function testForInIterationNonExtensibleStoreFallibleT() {
45 // Create an array with initialized-length = capacity = 2.
46 var x = [8, 0];
47
48 // Make it non-extensible.
49 Object.preventExtensions(x);
50
51 // Modifying an array's length takes a different path during for-in
52 // iteration of the array.
53 for (var k in x) {
54 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
55 x.length = 1;
56 }
57
58 // There's enough capacity in the elements storage to save the new element,
59 // but we still need to reject the store since the object is non-extensible.
60 x[1] = 4;
61
62 assertEq(x.length, 1);
63 assertEq(x[0], 8);
64 }
65
66 for (var i = 0; i < 15; ++i)
67 testForInIterationNonExtensibleStoreFallibleT();
68
69 // Repeat testForInIterationNonExtensibleStoreFallibleT for the MIRType::Value specialization.
70 function testForInIterationNonExtensibleStoreFallibleV(i) {
71 // Create an array with initialized-length = capacity = 2.
72 var x = [8, ""];
73
74 // Make it non-extensible.
75 Object.preventExtensions(x);
76
77 // Modifying an array's length takes a different path during for-in
78 // iteration of the array.
79 for (var k in x) {
80 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
81 x.length = 1;
82 break;
83 }
84
85 // There's enough capacity in the elements storage to save the new element,
86 // but we still need to reject the store since the object is non-extensible.
87 x[1] = [true, 1][i & 1];
88
89 assertEq(x.length, 1);
90 assertEq(x[0], 8);
91 }
92
93 for (var i = 0; i < 15; ++i)
94 testForInIterationNonExtensibleStoreFallibleV(i);
95
96 function testNonExtensibleArrayPop() {
97 // Create an array with initialized-length = capacity = 2.
98 var x = [8, 0];
99
100 // Make it non-extensible.
101 Object.preventExtensions(x);
102
103 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
104 x.pop();
105
106 // There's enough capacity in the elements storage to save the new element,
107 // but we still need to reject the store since the object is non-extensible.
108 x[1] = 4;
109
110 assertEq(x.length, 1);
111 assertEq(x[0], 8);
112 }
113
114 for (var i = 0; i < 15; ++i)
115 testNonExtensibleArrayPop();
116
117 function testNonExtensibleArrayPopNonWritable() {
118 // Create an array with initialized-length = capacity = 2.
119 var x = [8, 0];
120
121 // Make it non-extensible.
122 Object.preventExtensions(x);
123
124 // And make the "length" property non-writable.
125 Object.defineProperty(x, "length", {writable: false});
126
127 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
128 // This doesn't update |x.length|, because the "length" property is non-writable.
129 try {
130 x.pop();
131 } catch {}
132
133 // There's enough capacity in the elements storage to save the new element,
134 // but we still need to reject the store since the object is non-extensible.
135 for (var i = 0; i < 100; ++i)
136 x[1] = 4;
137
138 assertEq(x.length, 2);
139 assertEq(x[0], 8);
140 assertEq(x[1], undefined);
141 assertEq(1 in x, false);
142 }
143
144 for (var i = 0; i < 15; ++i)
145 testNonExtensibleArrayPopNonWritable();
146
147 function testNonExtensibleArrayShift() {
148 // Create an array with initialized-length = capacity = 2.
149 var x = [8, 0];
150
151 // Make it non-extensible.
152 Object.preventExtensions(x);
153
154 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
155 x.shift();
156
157 // There's enough capacity in the elements storage to save the new element,
158 // but we still need to reject the store since the object is non-extensible.
159 x[1] = 4;
160
161 assertEq(x.length, 1);
162 assertEq(x[0], 0);
163 }
164
165 for (var i = 0; i < 15; ++i)
166 testNonExtensibleArrayShift();
167
168 function testNonExtensibleArraySplice() {
169 // Create an array with initialized-length = capacity = 2.
170 var x = [8, 0];
171
172 // Make it non-extensible.
173 Object.preventExtensions(x);
174
175 // Now reduce initialized-length by one, so that initialized-length < capacity is true.
176 x.splice(1, 1);
177
178 // There's enough capacity in the elements storage to save the new element,
179 // but we still need to reject the store since the object is non-extensible.
180 x[1] = 4;
181
182 assertEq(x.length, 1);
183 assertEq(x[0], 8);
184 }
185
186 for (var i = 0; i < 15; ++i)
187 testNonExtensibleArraySplice();