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 4// met: 5// 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. 15// 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. 27 28//////////////////////////////////////////////////////////////////////// 29// Tests below verify that elements set on Array.prototype's proto propagate 30// for various Array.prototype functions (like unshift, shift, etc.) 31// If add any new tests here, consider adding them to all other files: 32// array-elements-from-array-prototype.js 33// array-elements-from-array-prototype-chain.js 34// array-elements-from-object-prototype.js 35// those ideally should be identical modulo host of elements and 36// the way elements introduced. 37// 38// Note: they are put into a separate file as we need maximally clean 39// VM setup---some optimizations might be already turned off in 40// 'dirty' VM. 41//////////////////////////////////////////////////////////////////////// 42 43var at3 = '@3' 44var at7 = '@7' 45 46Array.prototype.__proto__ = {3: at3}; 47Array.prototype.__proto__.__proto__ = {7: at7}; 48 49var a = new Array(13) 50 51assertEquals(at3, a[3]) 52assertFalse(a.hasOwnProperty(3)) 53 54assertEquals(at7, a[7]) 55assertFalse(a.hasOwnProperty(7)) 56 57assertEquals(undefined, a.shift(), 'hole should be returned as undefined') 58// Side-effects: Array.prototype[3] percolates into a[2] and Array.prototype[7[ 59// into a[6], still visible at the corresponding indices. 60 61assertEquals(at3, a[2]) 62assertTrue(a.hasOwnProperty(2)) 63assertEquals(at3, a[3]) 64assertFalse(a.hasOwnProperty(3)) 65 66assertEquals(at7, a[6]) 67assertTrue(a.hasOwnProperty(6)) 68assertEquals(at7, a[7]) 69assertFalse(a.hasOwnProperty(7)) 70 71a.unshift('foo', 'bar') 72// Side-effects: Array.prototype[3] now percolates into a[5] and Array.prototype[7] 73// into a[9]. 74 75assertEquals(at3, a[3]) 76assertFalse(a.hasOwnProperty(3)) 77assertEquals(at3, a[4]) 78assertTrue(a.hasOwnProperty(4)) 79assertEquals(at3, a[5]) 80assertTrue(a.hasOwnProperty(5)) 81 82assertEquals(undefined, a[6]) 83assertFalse(a.hasOwnProperty(6)) 84 85assertEquals(at7, a[7]) 86assertFalse(a.hasOwnProperty(7)) 87assertEquals(at7, a[8]) 88assertTrue(a.hasOwnProperty(8)) 89assertEquals(at7, a[9]) 90assertTrue(a.hasOwnProperty(9)) 91 92var sliced = a.slice(3, 10) 93// Slice must keep intact a and reify holes at indices 0--2 and 4--6. 94 95assertEquals(at3, a[3]) 96assertFalse(a.hasOwnProperty(3)) 97assertEquals(at3, a[4]) 98assertTrue(a.hasOwnProperty(4)) 99assertEquals(at3, a[5]) 100assertTrue(a.hasOwnProperty(5)) 101 102assertEquals(undefined, a[6]) 103assertFalse(a.hasOwnProperty(6)) 104 105assertEquals(at7, a[7]) 106assertFalse(a.hasOwnProperty(7)) 107assertEquals(at7, a[8]) 108assertTrue(a.hasOwnProperty(8)) 109assertEquals(at7, a[9]) 110assertTrue(a.hasOwnProperty(9)) 111 112assertEquals(at3, sliced[0]) 113assertTrue(sliced.hasOwnProperty(0)) 114assertEquals(at3, sliced[1]) 115assertTrue(sliced.hasOwnProperty(1)) 116assertEquals(at3, sliced[2]) 117assertTrue(sliced.hasOwnProperty(2)) 118 119// Note: sliced[3] comes directly from Array.prototype[3] 120assertEquals(at3, sliced[3]); 121assertFalse(sliced.hasOwnProperty(3)) 122 123assertEquals(at7, sliced[4]) 124assertTrue(sliced.hasOwnProperty(4)) 125assertEquals(at7, sliced[5]) 126assertTrue(sliced.hasOwnProperty(5)) 127assertEquals(at7, sliced[6]) 128assertTrue(sliced.hasOwnProperty(6)) 129 130 131// Splice is too complicated the operation, start afresh. 132 133// Shrking array. 134var a0 = [0, 1, , , 4, 5, , , , 9] 135var result = a0.splice(4, 1) 136// Side-effects: everything before 4 is kept intact: 137 138assertEquals(0, a0[0]) 139assertTrue(a0.hasOwnProperty(0)) 140assertEquals(1, a0[1]) 141assertTrue(a0.hasOwnProperty(1)) 142assertEquals(undefined, a0[2]) 143assertFalse(a0.hasOwnProperty(2)) 144assertEquals(at3, a0[3]) 145assertFalse(a0.hasOwnProperty(3)) 146 147// 4 and above shifted left by one reifying at7 into a0[6] and keeping 148// a hole at a0[7] 149 150assertEquals(5, a0[4]) 151assertTrue(a0.hasOwnProperty(4)) 152assertEquals(undefined, a0[5]) 153assertFalse(a0.hasOwnProperty(5)) 154assertEquals(at7, a0[6]) 155assertTrue(a0.hasOwnProperty(6)) 156assertEquals(at7, a0[7]) 157assertFalse(a0.hasOwnProperty(7)) 158assertEquals(9, a0[8]) 159assertTrue(a0.hasOwnProperty(8)) 160 161// Growing array. 162var a1 = [0, 1, , , 4, 5, , , , 9] 163var result = a1.splice(4, 0, undefined) 164// Side-effects: everything before 4 is kept intact: 165 166assertEquals(0, a1[0]) 167assertTrue(a1.hasOwnProperty(0)) 168assertEquals(1, a1[1]) 169assertTrue(a1.hasOwnProperty(1)) 170assertEquals(undefined, a1[2]) 171assertFalse(a1.hasOwnProperty(2)) 172assertEquals(at3, a1[3]) 173assertFalse(a1.hasOwnProperty(3)) 174 175// Now owned undefined resides at 4 and rest is shifted right by one 176// reifying at7 into a0[8] and keeping a hole at a0[7]. 177 178assertEquals(undefined, a1[4]) 179assertTrue(a1.hasOwnProperty(4)) 180assertEquals(4, a1[5]) 181assertTrue(a1.hasOwnProperty(5)) 182assertEquals(5, a1[6]) 183assertTrue(a1.hasOwnProperty(6)) 184assertEquals(at7, a1[7]) 185assertFalse(a1.hasOwnProperty(7)) 186assertEquals(at7, a1[8]) 187assertTrue(a1.hasOwnProperty(8)) 188assertEquals(undefined, a1[9]) 189assertFalse(a1.hasOwnProperty(9)) 190assertEquals(9, a1[10]) 191assertTrue(a1.hasOwnProperty(10)) 192