1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Check that slicing array of holes keeps it as array of holes
30 var array = new Array(10);
31 for (var i = 0; i < 7; i++) {
32 var sliced = array.slice();
33 assertEquals(array.length, sliced.length);
34 assertFalse(0 in sliced);
39 // Check various variants of empty array's slicing.
41 for (var i = 0; i < 7; i++) {
42 assertEquals([], [].slice(0, 0));
43 assertEquals([], [].slice(1, 0));
44 assertEquals([], [].slice(0, 1));
45 assertEquals([], [].slice(-1, 0));
50 // Check various forms of arguments omission.
52 var array = new Array(7);
54 for (var i = 0; i < 7; i++) {
55 assertEquals(array, array.slice());
56 assertEquals(array, array.slice(0));
57 assertEquals(array, array.slice(undefined));
58 assertEquals(array, array.slice("foobar"));
59 assertEquals(array, array.slice(undefined, undefined));
64 // Check variants of negatives and positive indices.
66 var array = new Array(7);
68 for (var i = 0; i < 7; i++) {
69 assertEquals(7, array.slice(-100).length);
70 assertEquals(3, array.slice(-3).length);
71 assertEquals(3, array.slice(4).length);
72 assertEquals(1, array.slice(6).length);
73 assertEquals(0, array.slice(7).length);
74 assertEquals(0, array.slice(8).length);
75 assertEquals(0, array.slice(100).length);
77 assertEquals(0, array.slice(0, -100).length);
78 assertEquals(4, array.slice(0, -3).length);
79 assertEquals(4, array.slice(0, 4).length);
80 assertEquals(6, array.slice(0, 6).length);
81 assertEquals(7, array.slice(0, 7).length);
82 assertEquals(7, array.slice(0, 8).length);
83 assertEquals(7, array.slice(0, 100).length);
87 obj = { toString: function() { throw 'Exception'; } };
89 // More than 2 arguments:
90 assertEquals(7, array.slice(0, 7, obj, null, undefined).length);
93 assertEquals(1, array.slice({valueOf: function() { return 1; }},
94 {toString: function() { return 2; }}).length);
96 // Throwing an exception in conversion:
98 assertEquals(7, array.slice(0, obj).length);
99 throw 'Should have thrown';
101 assertEquals('Exception', e);
107 // Nasty: modify the array in ToInteger.
111 bad_guy = { valueOf: function() { array.push(array.length); return -1; } };
113 for (var i = 0; i < 13; i++) {
114 var sliced = array.slice(bad_guy);
116 assertEquals(expected, array);
117 // According to the spec (15.4.4.10), length is calculated before
118 // performing ToInteger on arguments.
120 assertEquals([], sliced); // Length was 0, nothing to get.
122 // Actually out of array [0..i] we get [i - 1] as length is i.
123 assertEquals([i - 1], sliced);
129 // Now check the case with array of holes and some elements on prototype.
130 // Note: that is important that this test runs before the next one
131 // as the next one tampers Array.prototype.
134 var array = new Array(len);
139 for (var i = 0; i < 7; i++) {
140 var array_proto = [];
141 array_proto[3] = at3;
142 array_proto[7] = at7;
143 array.__proto__ = array_proto;
145 assertEquals(len, array.length);
146 for (var i = 0; i < array.length; i++) {
147 assertEquals(array[i], array_proto[i]);
150 var sliced = array.slice();
152 assertEquals(len, sliced.length);
154 assertTrue(delete array_proto[3]);
155 assertTrue(delete array_proto[7]);
157 // Note that slice copies values from prototype into the array.
158 assertEquals(array[3], undefined);
159 assertFalse(array.hasOwnProperty(3));
160 assertEquals(sliced[3], at3);
161 assertTrue(sliced.hasOwnProperty(3));
163 assertEquals(array[7], undefined);
164 assertFalse(array.hasOwnProperty(7));
165 assertEquals(sliced[7], at7);
166 assertTrue(sliced.hasOwnProperty(7));
168 // ... but keeps the rest as holes:
169 array_proto[5] = "@5";
170 assertEquals(array[5], array_proto[5]);
171 assertFalse(array.hasOwnProperty(5));
176 // Now check the case with array of holes and some elements on prototype.
179 var array = new Array(len);
184 for (var i = 0; i < 7; i++) {
185 Array.prototype[3] = at3;
186 Array.prototype[7] = at7;
188 assertEquals(len, array.length);
189 for (var i = 0; i < array.length; i++) {
190 assertEquals(array[i], Array.prototype[i]);
193 var sliced = array.slice();
195 assertEquals(len, sliced.length);
197 assertTrue(delete Array.prototype[3]);
198 assertTrue(delete Array.prototype[7]);
200 // Note that slice copies values from prototype into the array.
201 assertEquals(array[3], undefined);
202 assertFalse(array.hasOwnProperty(3));
203 assertEquals(sliced[3], at3);
204 assertTrue(sliced.hasOwnProperty(3));
206 assertEquals(array[7], undefined);
207 assertFalse(array.hasOwnProperty(7));
208 assertEquals(sliced[7], at7);
209 assertTrue(sliced.hasOwnProperty(7));
211 // ... but keeps the rest as holes:
212 Array.prototype[5] = "@5";
213 assertEquals(array[5], Array.prototype[5]);
214 assertFalse(array.hasOwnProperty(5));
215 assertEquals(sliced[5], Array.prototype[5]);
216 assertFalse(sliced.hasOwnProperty(5));
218 assertTrue(delete Array.prototype[5]);
222 // Check slicing on arguments object.
224 function func(expected, a0, a1, a2) {
225 assertEquals(expected, Array.prototype.slice.call(arguments, 1));
230 func(['a', 1], 'a', 1);
231 func(['a', 1, undefined], 'a', 1, undefined);
232 func(['a', 1, undefined, void(0)], 'a', 1, undefined, void(0));
235 // Check slicing on arguments object when missing arguments get assigined.
237 function func(x, y) {
238 assertEquals(1, arguments.length);
239 assertEquals(undefined, y);
241 assertEquals(1, arguments.length); // arguments length is the same.
242 assertEquals([x], Array.prototype.slice.call(arguments, 0));
248 // Check slicing on arguments object when length property has been set.
250 function func(x, y) {
251 assertEquals(1, arguments.length);
252 arguments.length = 7;
253 assertEquals([x,,,,,,,], Array.prototype.slice.call(arguments, 0));
259 // Check slicing on arguments object when length property has been set to
260 // some strange value.
262 function func(x, y) {
263 assertEquals(1, arguments.length);
264 arguments.length = 'foobar';
265 assertEquals([], Array.prototype.slice.call(arguments, 0));
271 // Check slicing on arguments object when extra argument has been added
272 // via indexed assignment.
274 function func(x, y) {
275 assertEquals(1, arguments.length);
277 assertEquals([x], Array.prototype.slice.call(arguments, 0));
283 // Check slicing on arguments object when argument has been deleted by index.
285 function func(x, y, z) {
286 assertEquals(3, arguments.length);
288 assertEquals([x,,z], Array.prototype.slice.call(arguments, 0));
294 // Check slicing of holey objects with elements in the prototype
298 arguments.__proto__[1] = 5;
299 var result = Array.prototype.slice.call(arguments);
300 delete arguments.__proto__[1];
301 assertEquals([1,5,3], result);