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 24 description("This tests that arrays have holes that you can see the prototype through, not just missing values."); 25 26 function 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 37 function 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 52 function returnTrue() 53 { 54 return true; 55 } 56 57 var a; 58 59 function addToArray(arg) 60 { 61 a.push(arg); 62 } 63 64 function addToArrayReturnFalse(arg) 65 { 66 a.push(arg); 67 return false; 68 } 69 70 function addToArrayReturnTrue(arg) 71 { 72 a.push(arg); 73 return true; 74 } 75 76 shouldBe("var a = []; a.length = 1; showHoles(a)", "'[hole]'"); 77 shouldBe("var a = []; a[0] = undefined; showHoles(a)", "'[undefined]'"); 78 shouldBe("var a = []; a[0] = undefined; delete a[0]; showHoles(a)", "'[hole]'"); 79 80 shouldBe("showHoles([0, , 2])", "'[0, hole, 2]'"); 81 shouldBe("showHoles([0, 1, ,])", "'[0, 1, hole]'"); 82 shouldBe("showHoles([0, , 2].concat([3, , 5]))", "'[0, hole, 2, 3, hole, 5]'"); 83 shouldBe("showHoles([0, , 2, 3].reverse())", "'[3, 2, hole, 0]'"); 84 shouldBe("a = [0, , 2, 3]; a.shift(); showHoles(a)", "'[hole, 2, 3]'"); 85 shouldBe("showHoles([0, , 2, 3].slice(0, 3))", "'[0, hole, 2]'"); 86 shouldBe("showHoles([0, , 2, 3].sort())", "'[0, 2, 3, hole]'"); 87 shouldBe("showHoles([0, undefined, 2, 3].sort())", "'[0, 2, 3, undefined]'"); 88 shouldBe("a = [0, , 2, 3]; a.splice(2, 3, 5, 6); showHoles(a)", "'[0, hole, 5, 6]'"); 89 shouldBe("a = [0, , 2, 3]; a.unshift(4); showHoles(a)", "'[4, 0, hole, 2, 3]'"); 90 shouldBe("showHoles([0, , 2, 3].filter(returnTrue))", "'[0, 2, 3]'"); 91 shouldBe("showHoles([0, undefined, 2, 3].filter(returnTrue))", "'[0, undefined, 2, 3]'"); 92 shouldBe("showHoles([0, , 2, 3].map(returnTrue))", "'[true, hole, true, true]'"); 93 shouldBe("showHoles([0, undefined, 2, 3].map(returnTrue))", "'[true, true, true, true]'"); 94 shouldBe("a = []; [0, , 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, 2, 3]'"); 95 shouldBe("a = []; [0, undefined, 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, undefined, 2, 3]'"); 96 shouldBe("a = []; [0, , 2, 3].forEach(addToArray); showHoles(a)", "'[0, 2, 3]'"); 97 shouldBe("a = []; [0, undefined, 2, 3].forEach(addToArray); showHoles(a)", "'[0, undefined, 2, 3]'"); 98 shouldBe("a = []; [0, , 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, 2, 3]'"); 99 shouldBe("a = []; [0, undefined, 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, undefined, 2, 3]'"); 100 shouldBe("[0, , 2, 3].indexOf()", "-1"); 101 shouldBe("[0, undefined, 2, 3].indexOf()", "1"); 102 shouldBe("[0, , 2, 3].lastIndexOf()", "-1"); 103 shouldBe("[0, undefined, 2, 3].lastIndexOf()", "1"); 104 105 Object.prototype[1] = "peekaboo"; 106 107 shouldBe("showHoles([0, , 2])", "'[0, hole, 2]'"); 108 shouldBe("showHoles([0, 1, ,])", "'[0, 1, hole]'"); 109 shouldBe("showHoles([0, , 2].concat([3, , 5]))", "'[0, peekaboo, 2, 3, peekaboo, 5]'"); 110 shouldBe("showHoles([0, , 2, 3].reverse())", "'[3, 2, peekaboo, 0]'"); 111 shouldBe("a = [0, , 2, 3]; a.shift(); showHoles(a)", "'[peekaboo, 2, 3]'"); 112 shouldBe("showHoles([0, , 2, 3].slice(0, 3))", "'[0, peekaboo, 2]'"); 113 shouldBe("showHoles([0, , 2, 3].sort())", "'[0, 2, 3, hole]'"); 114 shouldBe("showHoles([0, undefined, 2, 3].sort())", "'[0, 2, 3, undefined]'"); 115 shouldBe("a = [0, , 2, 3]; a.splice(2, 3, 5, 6); showHoles(a)", "'[0, hole, 5, 6]'"); 116 shouldBe("a = [0, , 2, 3]; a.unshift(4); showHoles(a)", "'[4, 0, peekaboo, 2, 3]'"); 117 shouldBe("showHoles([0, , 2, 3].filter(returnTrue))", "'[0, peekaboo, 2, 3]'"); 118 shouldBe("showHoles([0, undefined, 2, 3].filter(returnTrue))", "'[0, undefined, 2, 3]'"); 119 shouldBe("showHoles([0, , 2, 3].map(returnTrue))", "'[true, true, true, true]'"); 120 shouldBe("showHoles([0, undefined, 2, 3].map(returnTrue))", "'[true, true, true, true]'"); 121 shouldBe("a = []; [0, , 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, peekaboo, 2, 3]'"); 122 shouldBe("a = []; [0, undefined, 2, 3].every(addToArrayReturnTrue); showHoles(a)", "'[0, undefined, 2, 3]'"); 123 shouldBe("a = []; [0, , 2, 3].forEach(addToArray); showHoles(a)", "'[0, peekaboo, 2, 3]'"); 124 shouldBe("a = []; [0, undefined, 2, 3].forEach(addToArray); showHoles(a)", "'[0, undefined, 2, 3]'"); 125 shouldBe("a = []; [0, , 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, peekaboo, 2, 3]'"); 126 shouldBe("a = []; [0, undefined, 2, 3].some(addToArrayReturnFalse); showHoles(a)", "'[0, undefined, 2, 3]'"); 127 shouldBe("[0, , 2, 3].indexOf()", "-1"); 128 shouldBe("[0, , 2, 3].indexOf('peekaboo')", "1"); 129 shouldBe("[0, undefined, 2, 3].indexOf()", "1"); 130 shouldBe("[0, , 2, 3].lastIndexOf()", "-1"); 131 shouldBe("[0, , 2, 3].lastIndexOf('peekaboo')", "1"); 132 shouldBe("[0, undefined, 2, 3].lastIndexOf()", "1"); 133 134 delete Object.prototype[1]; 135