Debian Codegraph @

js/src/jit-test/tests/arguments/slice.js

raw
1 // Tests when |arguments| are used with Array.prototype.slice.
2
3 function testBasic() {
4 // Return the number of arguments.
5 function argslen() { return arguments.length; }
6
7 // Return the first argument.
8 function arg0() { return arguments[0]; }
9
10 // Return the argument at the request index.
11 function argIndex(i) { return arguments[i]; }
12
13 // Call the above functions when no formals are present.
14 function noFormalsLen() { return argslen(...Array.prototype.slice.call(arguments)); }
15 function noFormals0() { return arg0(...Array.prototype.slice.call(arguments)); }
16 function noFormalsIndex() { return argIndex(...Array.prototype.slice.call(arguments)); }
17
18 // Call the above functions when some formals are present.
19 function formalsLen(x, y, z) { return argslen(...Array.prototype.slice.call(arguments)); }
20 function formals0(x, y, z) { return arg0(...Array.prototype.slice.call(arguments)); }
21 function formalsIndex(x, y, z) { return argIndex(...Array.prototype.slice.call(arguments)); }
22
23 // Call the above functions when a rest argument is present.
24 function restLen(...rest) { return argslen(...Array.prototype.slice.call(arguments)); }
25 function rest0(...rest) { return arg0(...Array.prototype.slice.call(arguments)); }
26 function restIndex(...rest) { return argIndex(...Array.prototype.slice.call(arguments)); }
27
28 // Call the above functions when default parameters are present.
29 function defaultLen(x = 0) { return argslen(...Array.prototype.slice.call(arguments)); }
30 function default0(x = 0) { return arg0(...Array.prototype.slice.call(arguments)); }
31 function defaultIndex(x = 0) { return argIndex(...Array.prototype.slice.call(arguments)); }
32
33 for (let i = 0; i < 100; ++i) {
34 assertEq(noFormalsLen(), 0);
35 assertEq(noFormalsLen(1), 1);
36 assertEq(noFormalsLen(1, 2, 3), 3);
37
38 assertEq(formalsLen(), 0);
39 assertEq(formalsLen(1), 1);
40 assertEq(formalsLen(1, 2, 3), 3);
41
42 assertEq(restLen(), 0);
43 assertEq(restLen(1), 1);
44 assertEq(restLen(1, 2, 3), 3);
45
46 assertEq(defaultLen(), 0);
47 assertEq(defaultLen(1), 1);
48 assertEq(defaultLen(1, 2, 3), 3);
49
50 assertEq(noFormals0(), undefined);
51 assertEq(noFormals0(100), 100);
52 assertEq(noFormals0(100, 200, 300), 100);
53
54 assertEq(formals0(), undefined);
55 assertEq(formals0(100), 100);
56 assertEq(formals0(100, 200, 300), 100);
57
58 assertEq(rest0(), undefined);
59 assertEq(rest0(100), 100);
60 assertEq(rest0(100, 200, 300), 100);
61
62 assertEq(default0(), undefined);
63 assertEq(default0(100), 100);
64 assertEq(default0(100, 200, 300), 100);
65
66 assertEq(noFormalsIndex(), undefined);
67 assertEq(noFormalsIndex(0), 0);
68 assertEq(noFormalsIndex(0, 100), 0);
69 assertEq(noFormalsIndex(0, 100, 200, 300), 0);
70 assertEq(noFormalsIndex(1, 100), 100);
71 assertEq(noFormalsIndex(1, 100, 200, 300), 100);
72
73 assertEq(formalsIndex(), undefined);
74 assertEq(formalsIndex(0), 0);
75 assertEq(formalsIndex(0, 100), 0);
76 assertEq(formalsIndex(0, 100, 200, 300), 0);
77 assertEq(formalsIndex(1, 100), 100);
78 assertEq(formalsIndex(1, 100, 200, 300), 100);
79
80 assertEq(restIndex(), undefined);
81 assertEq(restIndex(0), 0);
82 assertEq(restIndex(0, 100), 0);
83 assertEq(restIndex(0, 100, 200, 300), 0);
84 assertEq(restIndex(1, 100), 100);
85 assertEq(restIndex(1, 100, 200, 300), 100);
86
87 assertEq(defaultIndex(), undefined);
88 assertEq(defaultIndex(0), 0);
89 assertEq(defaultIndex(0, 100), 0);
90 assertEq(defaultIndex(0, 100, 200, 300), 0);
91 assertEq(defaultIndex(1, 100), 100);
92 assertEq(defaultIndex(1, 100, 200, 300), 100);
93 }
94 }
95 testBasic();
96
97 // Same as testBasic, except that the first argument is removed.
98 function testRemoveFirst() {
99 // Return the number of arguments.
100 function argslen() { return arguments.length; }
101
102 // Return the first argument.
103 function arg0() { return arguments[0]; }
104
105 // Return the argument at the request index.
106 function argIndex(i) { return arguments[i]; }
107
108 // Call the above functions when no formals are present.
109 function noFormalsLen() { return argslen(...Array.prototype.slice.call(arguments, 1)); }
110 function noFormals0() { return arg0(...Array.prototype.slice.call(arguments, 1)); }
111 function noFormalsIndex() { return argIndex(...Array.prototype.slice.call(arguments, 1)); }
112
113 // Call the above functions when some formals are present.
114 function formalsLen(x, y, z) { return argslen(...Array.prototype.slice.call(arguments, 1)); }
115 function formals0(x, y, z) { return arg0(...Array.prototype.slice.call(arguments, 1)); }
116 function formalsIndex(x, y, z) { return argIndex(...Array.prototype.slice.call(arguments, 1)); }
117
118 // Call the above functions when a rest argument is present.
119 function restLen(...rest) { return argslen(...Array.prototype.slice.call(arguments, 1)); }
120 function rest0(...rest) { return arg0(...Array.prototype.slice.call(arguments, 1)); }
121 function restIndex(...rest) { return argIndex(...Array.prototype.slice.call(arguments, 1)); }
122
123 // Call the above functions when default parameters are present.
124 function defaultLen(x = 0) { return argslen(...Array.prototype.slice.call(arguments, 1)); }
125 function default0(x = 0) { return arg0(...Array.prototype.slice.call(arguments, 1)); }
126 function defaultIndex(x = 0) { return argIndex(...Array.prototype.slice.call(arguments, 1)); }
127
128 for (let i = 0; i < 100; ++i) {
129 assertEq(noFormalsLen(), 0);
130 assertEq(noFormalsLen(1), 0);
131 assertEq(noFormalsLen(1, 2, 3), 2);
132
133 assertEq(formalsLen(), 0);
134 assertEq(formalsLen(1), 0);
135 assertEq(formalsLen(1, 2, 3), 2);
136
137 assertEq(restLen(), 0);
138 assertEq(restLen(1), 0);
139 assertEq(restLen(1, 2, 3), 2);
140
141 assertEq(defaultLen(), 0);
142 assertEq(defaultLen(1), 0);
143 assertEq(defaultLen(1, 2, 3), 2);
144
145 assertEq(noFormals0(), undefined);
146 assertEq(noFormals0(100), undefined);
147 assertEq(noFormals0(100, 200, 300), 200);
148
149 assertEq(formals0(), undefined);
150 assertEq(formals0(100), undefined);
151 assertEq(formals0(100, 200, 300), 200);
152
153 assertEq(rest0(), undefined);
154 assertEq(rest0(100), undefined);
155 assertEq(rest0(100, 200, 300), 200);
156
157 assertEq(default0(), undefined);
158 assertEq(default0(100), undefined);
159 assertEq(default0(100, 200, 300), 200);
160
161 assertEq(noFormalsIndex(), undefined);
162 assertEq(noFormalsIndex(0), undefined);
163 assertEq(noFormalsIndex(0, 100), undefined);
164 assertEq(noFormalsIndex(0, 0, 100), 0);
165 assertEq(noFormalsIndex(0, 0, 100, 200, 300), 0);
166 assertEq(noFormalsIndex(0, 1, 100), 100);
167 assertEq(noFormalsIndex(0, 1, 100, 200, 300), 100);
168
169 assertEq(formalsIndex(), undefined);
170 assertEq(formalsIndex(0), undefined);
171 assertEq(formalsIndex(0, 100), undefined);
172 assertEq(formalsIndex(0, 0, 100), 0);
173 assertEq(formalsIndex(0, 0, 100, 200, 300), 0);
174 assertEq(formalsIndex(0, 1, 100), 100);
175 assertEq(formalsIndex(0, 1, 100, 200, 300), 100);
176
177 assertEq(restIndex(), undefined);
178 assertEq(restIndex(0), undefined);
179 assertEq(restIndex(0, 0), 0);
180 assertEq(restIndex(0, 0, 100), 0);
181 assertEq(restIndex(0, 0, 100, 200, 300), 0);
182 assertEq(restIndex(0, 1, 100), 100);
183 assertEq(restIndex(0, 1, 100, 200, 300), 100);
184
185 assertEq(defaultIndex(), undefined);
186 assertEq(defaultIndex(0), undefined);
187 assertEq(defaultIndex(0, 0), 0);
188 assertEq(defaultIndex(0, 0, 100), 0);
189 assertEq(defaultIndex(0, 0, 100, 200, 300), 0);
190 assertEq(defaultIndex(0, 1, 100), 100);
191 assertEq(defaultIndex(0, 1, 100, 200, 300), 100);
192 }
193 }
194 testRemoveFirst();
195
196 // Same as testBasic, except that the last argument is removed.
197 function testRemoveLast() {
198 // Return the number of arguments.
199 function argslen() { return arguments.length; }
200
201 // Return the first argument.
202 function arg0() { return arguments[0]; }
203
204 // Return the argument at the request index.
205 function argIndex(i) { return arguments[i]; }
206
207 // Call the above functions when no formals are present.
208 function noFormalsLen() { return argslen(...Array.prototype.slice.call(arguments, 0, -1)); }
209 function noFormals0() { return arg0(...Array.prototype.slice.call(arguments, 0, -1)); }
210 function noFormalsIndex() { return argIndex(...Array.prototype.slice.call(arguments, 0, -1)); }
211
212 // Call the above functions when some formals are present.
213 function formalsLen(x, y, z) { return argslen(...Array.prototype.slice.call(arguments, 0, -1)); }
214 function formals0(x, y, z) { return arg0(...Array.prototype.slice.call(arguments, 0, -1)); }
215 function formalsIndex(x, y, z) { return argIndex(...Array.prototype.slice.call(arguments, 0, -1)); }
216
217 // Call the above functions when a rest argument is present.
218 function restLen(...rest) { return argslen(...Array.prototype.slice.call(arguments, 0, -1)); }
219 function rest0(...rest) { return arg0(...Array.prototype.slice.call(arguments, 0, -1)); }
220 function restIndex(...rest) { return argIndex(...Array.prototype.slice.call(arguments, 0, -1)); }
221
222 // Call the above functions when default parameters are present.
223 function defaultLen(x = 0) { return argslen(...Array.prototype.slice.call(arguments, 0, -1)); }
224 function default0(x = 0) { return arg0(...Array.prototype.slice.call(arguments, 0, -1)); }
225 function defaultIndex(x = 0) { return argIndex(...Array.prototype.slice.call(arguments, 0, -1)); }
226
227 for (let i = 0; i < 100; ++i) {
228 assertEq(noFormalsLen(), 0);
229 assertEq(noFormalsLen(1), 0);
230 assertEq(noFormalsLen(1, 2, 3), 2);
231
232 assertEq(formalsLen(), 0);
233 assertEq(formalsLen(1), 0);
234 assertEq(formalsLen(1, 2, 3), 2);
235
236 assertEq(restLen(), 0);
237 assertEq(restLen(1), 0);
238 assertEq(restLen(1, 2, 3), 2);
239
240 assertEq(defaultLen(), 0);
241 assertEq(defaultLen(1), 0);
242 assertEq(defaultLen(1, 2, 3), 2);
243
244 assertEq(noFormals0(), undefined);
245 assertEq(noFormals0(100), undefined);
246 assertEq(noFormals0(100, 200, 300), 100);
247
248 assertEq(formals0(), undefined);
249 assertEq(formals0(100), undefined);
250 assertEq(formals0(100, 200, 300), 100);
251
252 assertEq(rest0(), undefined);
253 assertEq(rest0(100), undefined);
254 assertEq(rest0(100, 200, 300), 100);
255
256 assertEq(default0(), undefined);
257 assertEq(default0(100), undefined);
258 assertEq(default0(100, 200, 300), 100);
259
260 assertEq(noFormalsIndex(), undefined);
261 assertEq(noFormalsIndex(0), undefined);
262 assertEq(noFormalsIndex(0, 100), 0);
263 assertEq(noFormalsIndex(0, 100, 200, 300), 0);
264 assertEq(noFormalsIndex(1, 100), undefined);
265 assertEq(noFormalsIndex(1, 100, 200, 300), 100);
266
267 assertEq(formalsIndex(), undefined);
268 assertEq(formalsIndex(0), undefined);
269 assertEq(formalsIndex(0, 100), 0);
270 assertEq(formalsIndex(0, 100, 200, 300), 0);
271 assertEq(formalsIndex(1, 100), undefined);
272 assertEq(formalsIndex(1, 100, 200, 300), 100);
273
274 assertEq(restIndex(), undefined);
275 assertEq(restIndex(0), undefined);
276 assertEq(restIndex(0, 100), 0);
277 assertEq(restIndex(0, 100, 200, 300), 0);
278 assertEq(restIndex(1, 100), undefined);
279 assertEq(restIndex(1, 100, 200, 300), 100);
280
281 assertEq(defaultIndex(), undefined);
282 assertEq(defaultIndex(0), undefined);
283 assertEq(defaultIndex(0, 100), 0);
284 assertEq(defaultIndex(0, 100, 200, 300), 0);
285 assertEq(defaultIndex(1, 100), undefined);
286 assertEq(defaultIndex(1, 100, 200, 300), 100);
287 }
288 }
289 testRemoveLast();
290
291 function testOverriddenLength() {
292 function g(x, y = 0) {
293 return x + y;
294 }
295
296 function f(i) {
297 if (i === 100) {
298 arguments.length = 1;
299 }
300 var args = Array.prototype.slice.call(arguments);
301 return g.apply(null, args);
302 }
303
304 for (let i = 0; i <= 150; ++i) {
305 assertEq(f(i, i), i !== 100 ? i * 2 : i);
306 }
307 }
308 testOverriddenLength();
309
310 function testOverriddenElement() {
311 function g(x, y) {
312 return x + y;
313 }
314
315 function f(i) {
316 if (i === 100) {
317 arguments[1] = 0;
318 }
319 var args = Array.prototype.slice.call(arguments);
320 return g(args[0], args[1]);
321 }
322
323 for (let i = 0; i <= 150; ++i) {
324 assertEq(f(i, i), i !== 100 ? i * 2 : i);
325 }
326 }
327 testOverriddenElement();
328
329 function testDeletedElement() {
330 function g(x, y = 0) {
331 return x + y;
332 }
333
334 function f(i) {
335 if (i === 100) {
336 delete arguments[1];
337 }
338 var args = Array.prototype.slice.call(arguments);
339 return g.apply(null, args);
340 }
341
342 for (let i = 0; i <= 150; ++i) {
343 assertEq(f(i, i), i !== 100 ? i * 2 : i);
344 }
345 }
346 testDeletedElement();
347
348 function testForwardedArg() {
349 function g(x, y) {
350 return x + y;
351 }
352
353 function f(i) {
354 function closedOver() {
355 if (i === 100) i = 0;
356 }
357 closedOver();
358 var args = Array.prototype.slice.call(arguments);
359 return g(args[0], args[1]);
360 }
361
362 for (let i = 0; i <= 150; ++i) {
363 assertEq(f(i, i), i !== 100 ? i * 2 : i);
364 }
365 }
366 testForwardedArg();