Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 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 | |
| 43 | var at3 = '@3' |
| 44 | var at7 = '@7' |
| 45 | |
| 46 | Array.prototype.__proto__ = {3: at3}; |
| 47 | Array.prototype.__proto__.__proto__ = {7: at7}; |
| 48 | |
| 49 | var a = new Array(13) |
| 50 | |
| 51 | assertEquals(at3, a[3]) |
| 52 | assertFalse(a.hasOwnProperty(3)) |
| 53 | |
| 54 | assertEquals(at7, a[7]) |
| 55 | assertFalse(a.hasOwnProperty(7)) |
| 56 | |
| 57 | assertEquals(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 | |
| 61 | assertEquals(at3, a[2]) |
| 62 | assertTrue(a.hasOwnProperty(2)) |
| 63 | assertEquals(at3, a[3]) |
| 64 | assertFalse(a.hasOwnProperty(3)) |
| 65 | |
| 66 | assertEquals(at7, a[6]) |
| 67 | assertTrue(a.hasOwnProperty(6)) |
| 68 | assertEquals(at7, a[7]) |
| 69 | assertFalse(a.hasOwnProperty(7)) |
| 70 | |
| 71 | a.unshift('foo', 'bar') |
| 72 | // Side-effects: Array.prototype[3] now percolates into a[5] and Array.prototype[7] |
| 73 | // into a[9]. |
| 74 | |
| 75 | assertEquals(at3, a[3]) |
| 76 | assertFalse(a.hasOwnProperty(3)) |
| 77 | assertEquals(at3, a[4]) |
| 78 | assertTrue(a.hasOwnProperty(4)) |
| 79 | assertEquals(at3, a[5]) |
| 80 | assertTrue(a.hasOwnProperty(5)) |
| 81 | |
| 82 | assertEquals(undefined, a[6]) |
| 83 | assertFalse(a.hasOwnProperty(6)) |
| 84 | |
| 85 | assertEquals(at7, a[7]) |
| 86 | assertFalse(a.hasOwnProperty(7)) |
| 87 | assertEquals(at7, a[8]) |
| 88 | assertTrue(a.hasOwnProperty(8)) |
| 89 | assertEquals(at7, a[9]) |
| 90 | assertTrue(a.hasOwnProperty(9)) |
| 91 | |
| 92 | var sliced = a.slice(3, 10) |
| 93 | // Slice must keep intact a and reify holes at indices 0--2 and 4--6. |
| 94 | |
| 95 | assertEquals(at3, a[3]) |
| 96 | assertFalse(a.hasOwnProperty(3)) |
| 97 | assertEquals(at3, a[4]) |
| 98 | assertTrue(a.hasOwnProperty(4)) |
| 99 | assertEquals(at3, a[5]) |
| 100 | assertTrue(a.hasOwnProperty(5)) |
| 101 | |
| 102 | assertEquals(undefined, a[6]) |
| 103 | assertFalse(a.hasOwnProperty(6)) |
| 104 | |
| 105 | assertEquals(at7, a[7]) |
| 106 | assertFalse(a.hasOwnProperty(7)) |
| 107 | assertEquals(at7, a[8]) |
| 108 | assertTrue(a.hasOwnProperty(8)) |
| 109 | assertEquals(at7, a[9]) |
| 110 | assertTrue(a.hasOwnProperty(9)) |
| 111 | |
| 112 | assertEquals(at3, sliced[0]) |
| 113 | assertTrue(sliced.hasOwnProperty(0)) |
| 114 | assertEquals(at3, sliced[1]) |
| 115 | assertTrue(sliced.hasOwnProperty(1)) |
| 116 | assertEquals(at3, sliced[2]) |
| 117 | assertTrue(sliced.hasOwnProperty(2)) |
| 118 | |
| 119 | // Note: sliced[3] comes directly from Array.prototype[3] |
| 120 | assertEquals(at3, sliced[3]); |
| 121 | assertFalse(sliced.hasOwnProperty(3)) |
| 122 | |
| 123 | assertEquals(at7, sliced[4]) |
| 124 | assertTrue(sliced.hasOwnProperty(4)) |
| 125 | assertEquals(at7, sliced[5]) |
| 126 | assertTrue(sliced.hasOwnProperty(5)) |
| 127 | assertEquals(at7, sliced[6]) |
| 128 | assertTrue(sliced.hasOwnProperty(6)) |
| 129 | |
| 130 | |
| 131 | // Splice is too complicated the operation, start afresh. |
| 132 | |
| 133 | // Shrking array. |
| 134 | var a0 = [0, 1, , , 4, 5, , , , 9] |
| 135 | var result = a0.splice(4, 1) |
| 136 | // Side-effects: everything before 4 is kept intact: |
| 137 | |
| 138 | assertEquals(0, a0[0]) |
| 139 | assertTrue(a0.hasOwnProperty(0)) |
| 140 | assertEquals(1, a0[1]) |
| 141 | assertTrue(a0.hasOwnProperty(1)) |
| 142 | assertEquals(undefined, a0[2]) |
| 143 | assertFalse(a0.hasOwnProperty(2)) |
| 144 | assertEquals(at3, a0[3]) |
| 145 | assertFalse(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 | |
| 150 | assertEquals(5, a0[4]) |
| 151 | assertTrue(a0.hasOwnProperty(4)) |
| 152 | assertEquals(undefined, a0[5]) |
| 153 | assertFalse(a0.hasOwnProperty(5)) |
| 154 | assertEquals(at7, a0[6]) |
| 155 | assertTrue(a0.hasOwnProperty(6)) |
| 156 | assertEquals(at7, a0[7]) |
| 157 | assertFalse(a0.hasOwnProperty(7)) |
| 158 | assertEquals(9, a0[8]) |
| 159 | assertTrue(a0.hasOwnProperty(8)) |
| 160 | |
| 161 | // Growing array. |
| 162 | var a1 = [0, 1, , , 4, 5, , , , 9] |
| 163 | var result = a1.splice(4, 0, undefined) |
| 164 | // Side-effects: everything before 4 is kept intact: |
| 165 | |
| 166 | assertEquals(0, a1[0]) |
| 167 | assertTrue(a1.hasOwnProperty(0)) |
| 168 | assertEquals(1, a1[1]) |
| 169 | assertTrue(a1.hasOwnProperty(1)) |
| 170 | assertEquals(undefined, a1[2]) |
| 171 | assertFalse(a1.hasOwnProperty(2)) |
| 172 | assertEquals(at3, a1[3]) |
| 173 | assertFalse(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 | |
| 178 | assertEquals(undefined, a1[4]) |
| 179 | assertTrue(a1.hasOwnProperty(4)) |
| 180 | assertEquals(4, a1[5]) |
| 181 | assertTrue(a1.hasOwnProperty(5)) |
| 182 | assertEquals(5, a1[6]) |
| 183 | assertTrue(a1.hasOwnProperty(6)) |
| 184 | assertEquals(at7, a1[7]) |
| 185 | assertFalse(a1.hasOwnProperty(7)) |
| 186 | assertEquals(at7, a1[8]) |
| 187 | assertTrue(a1.hasOwnProperty(8)) |
| 188 | assertEquals(undefined, a1[9]) |
| 189 | assertFalse(a1.hasOwnProperty(9)) |
| 190 | assertEquals(9, a1[10]) |
| 191 | assertTrue(a1.hasOwnProperty(10)) |