OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 248 |
249 // Now check the case with array of holes and some elements on prototype. | 249 // Now check the case with array of holes and some elements on prototype. |
250 (function() { | 250 (function() { |
251 var len = 9; | 251 var len = 9; |
252 | 252 |
253 var at3 = "@3"; | 253 var at3 = "@3"; |
254 var at7 = "@7"; | 254 var at7 = "@7"; |
255 | 255 |
256 for (var i = 0; i < 7; i++) { | 256 for (var i = 0; i < 7; i++) { |
257 var array = new Array(len); | 257 var array = new Array(len); |
| 258 var array_proto = []; |
| 259 array_proto[3] = at3; |
| 260 array_proto[7] = at7; |
| 261 array.__proto__ = array_proto; |
| 262 |
| 263 var spliced = array.splice(2, 2, 'one', undefined, 'two'); |
| 264 |
| 265 // Second hole (at index 3) of array turns into |
| 266 // value of Array.prototype[3] while copying. |
| 267 assertEquals([, at3], spliced); |
| 268 assertEquals([, , 'one', undefined, 'two', , , at7, at7, ,], array); |
| 269 |
| 270 // ... but array[3] and array[7] is actually a hole: |
| 271 assertTrue(delete array_proto[3]); |
| 272 assertEquals(undefined, array[3]); |
| 273 assertTrue(delete array_proto[7]); |
| 274 assertEquals(undefined, array[7]); |
| 275 |
| 276 // and now check hasOwnProperty |
| 277 assertFalse(array.hasOwnProperty(0), "array.hasOwnProperty(0)"); |
| 278 assertFalse(array.hasOwnProperty(1), "array.hasOwnProperty(1)"); |
| 279 assertTrue(array.hasOwnProperty(2)); |
| 280 assertTrue(array.hasOwnProperty(3)); |
| 281 assertTrue(array.hasOwnProperty(4)); |
| 282 assertFalse(array.hasOwnProperty(5), "array.hasOwnProperty(5)"); |
| 283 assertFalse(array.hasOwnProperty(6), "array.hasOwnProperty(6)"); |
| 284 assertFalse(array.hasOwnProperty(7), "array.hasOwnProperty(7)"); |
| 285 assertTrue(array.hasOwnProperty(8)); |
| 286 assertFalse(array.hasOwnProperty(9), "array.hasOwnProperty(9)"); |
| 287 |
| 288 // and now check couple of indices above length. |
| 289 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); |
| 290 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); |
| 291 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); |
| 292 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); |
| 293 assertFalse(array.hasOwnProperty(2 << 32 - 1), |
| 294 "array.hasOwnProperty(2 << 31 - 1)"); |
| 295 } |
| 296 })(); |
| 297 |
| 298 |
| 299 // Now check the case with array of holes and some elements on prototype. |
| 300 (function() { |
| 301 var len = 9; |
| 302 |
| 303 var at3 = "@3"; |
| 304 var at7 = "@7"; |
| 305 |
| 306 for (var i = 0; i < 7; i++) { |
| 307 var array = new Array(len); |
258 Array.prototype[3] = at3; | 308 Array.prototype[3] = at3; |
259 Array.prototype[7] = at7; | 309 Array.prototype[7] = at7; |
260 | 310 |
261 var spliced = array.splice(2, 2, 'one', undefined, 'two'); | 311 var spliced = array.splice(2, 2, 'one', undefined, 'two'); |
262 | 312 |
263 // Second hole (at index 3) of array turns into | 313 // Second hole (at index 3) of array turns into |
264 // value of Array.prototype[3] while copying. | 314 // value of Array.prototype[3] while copying. |
265 assertEquals([, at3], spliced); | 315 assertEquals([, at3], spliced); |
266 assertEquals([, , 'one', undefined, 'two', , , at7, at7, ,], array); | 316 assertEquals([, , 'one', undefined, 'two', , , at7, at7, ,], array); |
267 | 317 |
268 // ... but array[7] is actually a hole: | 318 // ... but array[3] and array[7] is actually a hole: |
| 319 assertTrue(delete Array.prototype[3]); |
| 320 assertEquals(undefined, array[3]); |
269 assertTrue(delete Array.prototype[7]); | 321 assertTrue(delete Array.prototype[7]); |
270 assertEquals(undefined, array[7]); | 322 assertEquals(undefined, array[7]); |
271 | 323 |
272 // and now check hasOwnProperty | 324 // and now check hasOwnProperty |
273 assertFalse(array.hasOwnProperty(0), "array.hasOwnProperty(0)"); | 325 assertFalse(array.hasOwnProperty(0), "array.hasOwnProperty(0)"); |
274 assertFalse(array.hasOwnProperty(1), "array.hasOwnProperty(1)"); | 326 assertFalse(array.hasOwnProperty(1), "array.hasOwnProperty(1)"); |
275 assertTrue(array.hasOwnProperty(2)); | 327 assertTrue(array.hasOwnProperty(2)); |
276 assertTrue(array.hasOwnProperty(3)); | 328 assertTrue(array.hasOwnProperty(3)); |
277 assertTrue(array.hasOwnProperty(4)); | 329 assertTrue(array.hasOwnProperty(4)); |
278 assertFalse(array.hasOwnProperty(5), "array.hasOwnProperty(5)"); | 330 assertFalse(array.hasOwnProperty(5), "array.hasOwnProperty(5)"); |
279 assertFalse(array.hasOwnProperty(6), "array.hasOwnProperty(6)"); | 331 assertFalse(array.hasOwnProperty(6), "array.hasOwnProperty(6)"); |
280 assertFalse(array.hasOwnProperty(7), "array.hasOwnProperty(7)"); | 332 assertFalse(array.hasOwnProperty(7), "array.hasOwnProperty(7)"); |
281 assertTrue(array.hasOwnProperty(8)); | 333 assertTrue(array.hasOwnProperty(8)); |
282 assertFalse(array.hasOwnProperty(9), "array.hasOwnProperty(9)"); | 334 assertFalse(array.hasOwnProperty(9), "array.hasOwnProperty(9)"); |
283 | 335 |
284 // and now check couple of indices above length. | 336 // and now check couple of indices above length. |
285 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); | 337 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); |
286 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); | 338 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); |
287 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); | 339 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); |
288 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); | 340 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); |
289 assertFalse(array.hasOwnProperty(2 << 32 - 1), "array.hasOwnProperty(2 << 31
- 1)"); | 341 assertFalse(array.hasOwnProperty(2 << 32 - 1), |
| 342 "array.hasOwnProperty(2 << 31 - 1)"); |
290 } | 343 } |
291 })(); | 344 })(); |
292 | 345 |
293 | 346 |
294 // Check the behaviour when approaching maximal values for length. | 347 // Check the behaviour when approaching maximal values for length. |
295 (function() { | 348 (function() { |
296 for (var i = 0; i < 7; i++) { | 349 for (var i = 0; i < 7; i++) { |
297 try { | 350 try { |
298 new Array((1 << 32) - 3).splice(-1, 0, 1, 2, 3, 4, 5); | 351 new Array((1 << 32) - 3).splice(-1, 0, 1, 2, 3, 4, 5); |
299 throw 'Should have thrown RangeError'; | 352 throw 'Should have thrown RangeError'; |
(...skipping 11 matching lines...) Expand all Loading... |
311 | 364 |
312 (function() { | 365 (function() { |
313 for (var i = 0; i < 7; i++) { | 366 for (var i = 0; i < 7; i++) { |
314 var a = [7, 8, 9]; | 367 var a = [7, 8, 9]; |
315 a.splice(0, 0, 1, 2, 3, 4, 5, 6); | 368 a.splice(0, 0, 1, 2, 3, 4, 5, 6); |
316 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a); | 369 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a); |
317 assertFalse(a.hasOwnProperty(10), "a.hasOwnProperty(10)"); | 370 assertFalse(a.hasOwnProperty(10), "a.hasOwnProperty(10)"); |
318 assertEquals(undefined, a[10]); | 371 assertEquals(undefined, a[10]); |
319 } | 372 } |
320 })(); | 373 })(); |
OLD | NEW |