1// Copyright 2013 the V8 project authors. All rights reserved. 2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions 6// are met: 7// 1. Redistributions of source code must retain the above copyright 8// notice, this list of conditions and the following disclaimer. 9// 2. Redistributions in binary form must reproduce the above copyright 10// notice, this list of conditions and the following disclaimer in the 11// documentation and/or other materials provided with the distribution. 12// 13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24description("This tests that arrays have holes that you can see the prototype through, not just missing values."); 25 26function isHole(array, index) 27{ 28 if (index >= array.length) 29 return "bad index: past length"; 30 // Check if we can see through the hole into another room. 31 Array.prototype[index] = "room"; 32 var isHole = array[index] == "room"; 33 delete Array.prototype[index]; 34 return isHole; 35} 36 37function showHoles(array) 38{ 39 var string = "["; 40 for (i = 0; i < array.length; ++i) { 41 if (i) 42 string += ", "; 43 if (isHole(array, i)) 44 string += "hole"; 45 else 46 string += array[i]; 47 } 48 string += "]"; 49 return string; 50} 51 52function returnTrue() 53{ 54 return true; 55} 56 57var a; 58 59function addToArray(arg) 60{ 61 a.push(arg); 62} 63 64function addToArrayReturnFalse(arg) 65{ 66 a.push(arg); 67 return false; 68} 69 70function addToArrayReturnTrue(arg) 71{ 72 a.push(arg); 73 return true; 74} 75 76shouldBe("var a = []; a.length = 1; showHoles(a)", "'[hole]'"); 77shouldBe("var a = []; a[0] = undefined; showHoles(a)", "'[undefined]'"); 78shouldBe("var a = []; a[0] = undefined; delete a[0]; showHoles(a)", "'[hole]'"); 79 80shouldBe("showHoles([0, , 2])", "'[0, hole, 2]'"); 81shouldBe("showHoles([0, 1, ,])", "'[0, 1, hole]'"); 82shouldBe("showHoles([0, , 2].concat([3, , 5]))", "'[0, hole, 2, 3, hole, 5]'"); 83shouldBe("showHoles([0, , 2, 3].reverse())", "'[3, 2, hole, 0]'"); 84shouldBe("a = [0, , 2, 3]; a.shift(); showHoles(a)", "'[hole, 2, 3]'"); 85shouldBe("showHoles([0, , 2, 3].slice(0, 3))", "'[0, hole, 2]'"); 86shouldBe("showHoles([0, , 2, 3].sort())", "'[0, 2, 3, hole]'"); 87shouldBe("showHoles([0, undefined, 2, 3].sort())", "'[0, 2, 3, undefined]'"); 88shouldBe("a = [0, , 2, 3]; a.splice(2, 3, 5, 6); showHoles(a)", "'[0, hole, 5, 6]'"); 89shouldBe("a = [0, , 2, 3]; a.unshift(4); showHoles(a)", "'[4, 0, hole, 2, 3]'"); 90shouldBe("showHoles([0, , 2, 3].filter(returnTrue))", "'[0, 2, 3]'"); 91shouldBe("showHoles([0, undefined, 2, 3].filter(returnTrue))", "'[0, undefined, 2, 3]'"); 92shouldBe("showHoles([0, , 2, 3].map(returnTrue))", "'[true, hole, true, true]'"); 93shouldBe("showHoles([0, undefined, 2, 3].map(returnTrue))", "'[true, true, true, true]'"); 94shouldBe("a = []; [0, , 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, 2, 3]'"); 95shouldBe("a = []; [0, undefined, 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, undefined, 2, 3]'"); 96shouldBe("a = []; [0, , 2, 3].forEach(addToArray); showHoles(a)", "'[0, 2, 3]'"); 97shouldBe("a = []; [0, undefined, 2, 3].forEach(addToArray); showHoles(a)", "'[0, undefined, 2, 3]'"); 98shouldBe("a = []; [0, , 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, 2, 3]'"); 99shouldBe("a = []; [0, undefined, 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, undefined, 2, 3]'"); 100shouldBe("[0, , 2, 3].indexOf()", "-1"); 101shouldBe("[0, undefined, 2, 3].indexOf()", "1"); 102shouldBe("[0, , 2, 3].lastIndexOf()", "-1"); 103shouldBe("[0, undefined, 2, 3].lastIndexOf()", "1"); 104 105Object.prototype[1] = "peekaboo"; 106 107shouldBe("showHoles([0, , 2])", "'[0, hole, 2]'"); 108shouldBe("showHoles([0, 1, ,])", "'[0, 1, hole]'"); 109shouldBe("showHoles([0, , 2].concat([3, , 5]))", "'[0, peekaboo, 2, 3, peekaboo, 5]'"); 110shouldBe("showHoles([0, , 2, 3].reverse())", "'[3, 2, peekaboo, 0]'"); 111shouldBe("a = [0, , 2, 3]; a.shift(); showHoles(a)", "'[peekaboo, 2, 3]'"); 112shouldBe("showHoles([0, , 2, 3].slice(0, 3))", "'[0, peekaboo, 2]'"); 113shouldBe("showHoles([0, , 2, 3].sort())", "'[0, 2, 3, hole]'"); 114shouldBe("showHoles([0, undefined, 2, 3].sort())", "'[0, 2, 3, undefined]'"); 115shouldBe("a = [0, , 2, 3]; a.splice(2, 3, 5, 6); showHoles(a)", "'[0, hole, 5, 6]'"); 116shouldBe("a = [0, , 2, 3]; a.unshift(4); showHoles(a)", "'[4, 0, peekaboo, 2, 3]'"); 117shouldBe("showHoles([0, , 2, 3].filter(returnTrue))", "'[0, peekaboo, 2, 3]'"); 118shouldBe("showHoles([0, undefined, 2, 3].filter(returnTrue))", "'[0, undefined, 2, 3]'"); 119shouldBe("showHoles([0, , 2, 3].map(returnTrue))", "'[true, true, true, true]'"); 120shouldBe("showHoles([0, undefined, 2, 3].map(returnTrue))", "'[true, true, true, true]'"); 121shouldBe("a = []; [0, , 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, peekaboo, 2, 3]'"); 122shouldBe("a = []; [0, undefined, 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, undefined, 2, 3]'"); 123shouldBe("a = []; [0, , 2, 3].forEach(addToArray); showHoles(a)", "'[0, peekaboo, 2, 3]'"); 124shouldBe("a = []; [0, undefined, 2, 3].forEach(addToArray); showHoles(a)", "'[0, undefined, 2, 3]'"); 125shouldBe("a = []; [0, , 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, peekaboo, 2, 3]'"); 126shouldBe("a = []; [0, undefined, 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, undefined, 2, 3]'"); 127shouldBe("[0, , 2, 3].indexOf()", "-1"); 128shouldBe("[0, , 2, 3].indexOf('peekaboo')", "1"); 129shouldBe("[0, undefined, 2, 3].indexOf()", "1"); 130shouldBe("[0, , 2, 3].lastIndexOf()", "-1"); 131shouldBe("[0, , 2, 3].lastIndexOf('peekaboo')", "1"); 132shouldBe("[0, undefined, 2, 3].lastIndexOf()", "1"); 133 134delete Object.prototype[1]; 135