erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 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 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax --expose-gc |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 29 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 30 | // Helper |
| 31 | function assertInstance(o, f) { |
| 32 | assertSame(o.constructor, f); |
| 33 | assertInstanceof(o, f); |
| 34 | } |
| 35 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 36 | // This is a regression test for overlapping key and value registers. |
| 37 | function f(a) { |
| 38 | a[0] = 0; |
| 39 | a[1] = 0; |
| 40 | } |
| 41 | |
| 42 | var a = new Int32Array(2); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 43 | for (var i = 0; i < 5; i++) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 44 | f(a); |
| 45 | } |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 46 | %OptimizeFunctionOnNextCall(f); |
| 47 | f(a); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 48 | |
| 49 | assertEquals(0, a[0]); |
| 50 | assertEquals(0, a[1]); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 51 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 52 | // Test derivation from an ArrayBuffer |
| 53 | var ab = new ArrayBuffer(12); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 54 | assertInstance(ab, ArrayBuffer); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 55 | var derived_uint8 = new Uint8Array(ab); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 56 | assertInstance(derived_uint8, Uint8Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 57 | assertSame(ab, derived_uint8.buffer); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 58 | assertEquals(12, derived_uint8.length); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 59 | assertEquals(12, derived_uint8.byteLength); |
| 60 | assertEquals(0, derived_uint8.byteOffset); |
| 61 | assertEquals(1, derived_uint8.BYTES_PER_ELEMENT); |
| 62 | var derived_uint8_2 = new Uint8Array(ab,7); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 63 | assertInstance(derived_uint8_2, Uint8Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 64 | assertSame(ab, derived_uint8_2.buffer); |
| 65 | assertEquals(5, derived_uint8_2.length); |
| 66 | assertEquals(5, derived_uint8_2.byteLength); |
| 67 | assertEquals(7, derived_uint8_2.byteOffset); |
| 68 | assertEquals(1, derived_uint8_2.BYTES_PER_ELEMENT); |
| 69 | var derived_int16 = new Int16Array(ab); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 70 | assertInstance(derived_int16, Int16Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 71 | assertSame(ab, derived_int16.buffer); |
| 72 | assertEquals(6, derived_int16.length); |
| 73 | assertEquals(12, derived_int16.byteLength); |
| 74 | assertEquals(0, derived_int16.byteOffset); |
| 75 | assertEquals(2, derived_int16.BYTES_PER_ELEMENT); |
| 76 | var derived_int16_2 = new Int16Array(ab,6); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 77 | assertInstance(derived_int16_2, Int16Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 78 | assertSame(ab, derived_int16_2.buffer); |
| 79 | assertEquals(3, derived_int16_2.length); |
| 80 | assertEquals(6, derived_int16_2.byteLength); |
| 81 | assertEquals(6, derived_int16_2.byteOffset); |
| 82 | assertEquals(2, derived_int16_2.BYTES_PER_ELEMENT); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 83 | var derived_uint32 = new Uint32Array(ab); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 84 | assertInstance(derived_uint32, Uint32Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 85 | assertSame(ab, derived_uint32.buffer); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 86 | assertEquals(3, derived_uint32.length); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 87 | assertEquals(12, derived_uint32.byteLength); |
| 88 | assertEquals(0, derived_uint32.byteOffset); |
| 89 | assertEquals(4, derived_uint32.BYTES_PER_ELEMENT); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 90 | var derived_uint32_2 = new Uint32Array(ab,4); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 91 | assertInstance(derived_uint32_2, Uint32Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 92 | assertSame(ab, derived_uint32_2.buffer); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 93 | assertEquals(2, derived_uint32_2.length); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 94 | assertEquals(8, derived_uint32_2.byteLength); |
| 95 | assertEquals(4, derived_uint32_2.byteOffset); |
| 96 | assertEquals(4, derived_uint32_2.BYTES_PER_ELEMENT); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 97 | var derived_uint32_3 = new Uint32Array(ab,4,1); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 98 | assertInstance(derived_uint32_3, Uint32Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 99 | assertSame(ab, derived_uint32_3.buffer); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 100 | assertEquals(1, derived_uint32_3.length); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 101 | assertEquals(4, derived_uint32_3.byteLength); |
| 102 | assertEquals(4, derived_uint32_3.byteOffset); |
| 103 | assertEquals(4, derived_uint32_3.BYTES_PER_ELEMENT); |
| 104 | var derived_float64 = new Float64Array(ab,0,1); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 105 | assertInstance(derived_float64, Float64Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 106 | assertSame(ab, derived_float64.buffer); |
| 107 | assertEquals(1, derived_float64.length); |
| 108 | assertEquals(8, derived_float64.byteLength); |
| 109 | assertEquals(0, derived_float64.byteOffset); |
| 110 | assertEquals(8, derived_float64.BYTES_PER_ELEMENT); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 111 | |
| 112 | // If a given byteOffset and length references an area beyond the end of the |
| 113 | // ArrayBuffer an exception is raised. |
| 114 | function abfunc3() { |
| 115 | new Uint32Array(ab,4,3); |
| 116 | } |
| 117 | assertThrows(abfunc3); |
| 118 | function abfunc4() { |
| 119 | new Uint32Array(ab,16); |
| 120 | } |
| 121 | assertThrows(abfunc4); |
| 122 | |
| 123 | // The given byteOffset must be a multiple of the element size of the specific |
| 124 | // type, otherwise an exception is raised. |
| 125 | function abfunc5() { |
| 126 | new Uint32Array(ab,5); |
| 127 | } |
| 128 | assertThrows(abfunc5); |
| 129 | |
| 130 | // If length is not explicitly specified, the length of the ArrayBuffer minus |
| 131 | // the byteOffset must be a multiple of the element size of the specific type, |
| 132 | // or an exception is raised. |
| 133 | var ab2 = new ArrayBuffer(13); |
| 134 | function abfunc6() { |
| 135 | new Uint32Array(ab2,4); |
| 136 | } |
| 137 | assertThrows(abfunc6); |
| 138 | |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 139 | // Test that an array constructed without an array buffer creates one properly. |
| 140 | a = new Uint8Array(31); |
| 141 | assertEquals(a.byteLength, a.buffer.byteLength); |
| 142 | assertEquals(a.length, a.buffer.byteLength); |
| 143 | assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength); |
| 144 | a = new Int16Array(5); |
| 145 | assertEquals(a.byteLength, a.buffer.byteLength); |
| 146 | assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength); |
| 147 | a = new Float64Array(7); |
| 148 | assertEquals(a.byteLength, a.buffer.byteLength); |
| 149 | assertEquals(a.length * a.BYTES_PER_ELEMENT, a.buffer.byteLength); |
| 150 | |
| 151 | // Test that an implicitly created buffer is a valid buffer. |
| 152 | a = new Float64Array(7); |
| 153 | assertSame(a.buffer, (new Uint16Array(a.buffer)).buffer); |
| 154 | assertSame(a.buffer, (new Float32Array(a.buffer,4)).buffer); |
| 155 | assertSame(a.buffer, (new Int8Array(a.buffer,3,51)).buffer); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 156 | assertInstance(a.buffer, ArrayBuffer); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 157 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 158 | // Test the correct behavior of the |BYTES_PER_ELEMENT| property |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 159 | a = new Int32Array(2); |
| 160 | assertEquals(4, a.BYTES_PER_ELEMENT); |
| 161 | a.BYTES_PER_ELEMENT = 42; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 162 | a = new Uint8Array(2); |
| 163 | assertEquals(1, a.BYTES_PER_ELEMENT); |
| 164 | a = new Int16Array(2); |
| 165 | assertEquals(2, a.BYTES_PER_ELEMENT); |
| 166 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 167 | // Test Float64Arrays. |
| 168 | function get(a, index) { |
| 169 | return a[index]; |
| 170 | } |
| 171 | function set(a, index, value) { |
| 172 | a[index] = value; |
| 173 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 174 | function temp() { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 175 | var array = new Float64Array(2); |
| 176 | for (var i = 0; i < 5; i++) { |
| 177 | set(array, 0, 2.5); |
| 178 | assertEquals(2.5, array[0]); |
| 179 | } |
| 180 | %OptimizeFunctionOnNextCall(set); |
| 181 | set(array, 0, 2.5); |
| 182 | assertEquals(2.5, array[0]); |
| 183 | set(array, 1, 3.5); |
| 184 | assertEquals(3.5, array[1]); |
| 185 | for (var i = 0; i < 5; i++) { |
| 186 | assertEquals(2.5, get(array, 0)); |
| 187 | assertEquals(3.5, array[1]); |
| 188 | } |
| 189 | %OptimizeFunctionOnNextCall(get); |
| 190 | assertEquals(2.5, get(array, 0)); |
| 191 | assertEquals(3.5, get(array, 1)); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 192 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 193 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 194 | // Test non-number parameters. |
| 195 | var array_with_length_from_non_number = new Int32Array("2"); |
| 196 | assertEquals(2, array_with_length_from_non_number.length); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 197 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 198 | // Test loads and stores. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 199 | types = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 200 | Uint32Array, Uint8ClampedArray, Float32Array, Float64Array]; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 201 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 202 | test_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN]; |
| 203 | test_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1]; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 204 | test_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -1.25]; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 205 | test_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75]; |
| 206 | test_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256]; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 207 | test_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25]; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 208 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 209 | const kElementCount = 40; |
| 210 | |
| 211 | function test_load(array, sum) { |
| 212 | for (var i = 0; i < kElementCount; i++) { |
| 213 | sum += array[i]; |
| 214 | } |
| 215 | return sum; |
| 216 | } |
| 217 | |
| 218 | function test_load_const_key(array, sum) { |
| 219 | sum += array[0]; |
| 220 | sum += array[1]; |
| 221 | sum += array[2]; |
| 222 | return sum; |
| 223 | } |
| 224 | |
| 225 | function test_store(array, sum) { |
| 226 | for (var i = 0; i < kElementCount; i++) { |
| 227 | sum += array[i] = i+1; |
| 228 | } |
| 229 | return sum; |
| 230 | } |
| 231 | |
| 232 | function test_store_const_key(array, sum) { |
| 233 | sum += array[0] = 1; |
| 234 | sum += array[1] = 2; |
| 235 | sum += array[2] = 3; |
| 236 | return sum; |
| 237 | } |
| 238 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 239 | function zero() { |
| 240 | return 0.0; |
| 241 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 242 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 243 | function test_store_middle_tagged(array, sum) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 244 | array[0] = 253.75; |
| 245 | return array[0]; |
| 246 | } |
| 247 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 248 | function test_store_high_tagged(array, sum) { |
| 249 | array[0] = 256.25; |
| 250 | return array[0]; |
| 251 | } |
| 252 | |
| 253 | function test_store_middle_double(array, sum) { |
| 254 | array[0] = 253.75 + zero(); // + forces double type feedback |
| 255 | return array[0]; |
| 256 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 257 | |
| 258 | function test_store_high_double(array, sum) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 259 | array[0] = 256.25 + zero(); // + forces double type feedback |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 260 | return array[0]; |
| 261 | } |
| 262 | |
| 263 | function test_store_high_double(array, sum) { |
| 264 | array[0] = 256.25; |
| 265 | return array[0]; |
| 266 | } |
| 267 | |
| 268 | function test_store_low_int(array, sum) { |
| 269 | array[0] = -1; |
| 270 | return array[0]; |
| 271 | } |
| 272 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 273 | function test_store_low_tagged(array, sum) { |
| 274 | array[0] = -1.25; |
| 275 | return array[0]; |
| 276 | } |
| 277 | |
| 278 | function test_store_low_double(array, sum) { |
| 279 | array[0] = -1.25 + zero(); // + forces double type feedback |
| 280 | return array[0]; |
| 281 | } |
| 282 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 283 | function test_store_high_int(array, sum) { |
| 284 | array[0] = 256; |
| 285 | return array[0]; |
| 286 | } |
| 287 | |
| 288 | function test_store_nan(array, sum) { |
| 289 | array[0] = NaN; |
| 290 | return array[0]; |
| 291 | } |
| 292 | |
| 293 | const kRuns = 10; |
| 294 | |
| 295 | function run_test(test_func, array, expected_result) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 296 | for (var i = 0; i < 5; i++) test_func(array, 0); |
| 297 | %OptimizeFunctionOnNextCall(test_func); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 298 | var sum = 0; |
| 299 | for (var i = 0; i < kRuns; i++) { |
| 300 | sum = test_func(array, sum); |
| 301 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 302 | assertEquals(expected_result, sum); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 303 | %DeoptimizeFunction(test_func); |
| 304 | gc(); // Makes V8 forget about type information for test_func. |
| 305 | } |
| 306 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 307 | function run_bounds_test(test_func, array, expected_result) { |
| 308 | assertEquals(undefined, a[kElementCount]); |
| 309 | a[kElementCount] = 456; |
| 310 | assertEquals(undefined, a[kElementCount]); |
| 311 | assertEquals(undefined, a[kElementCount+1]); |
| 312 | a[kElementCount+1] = 456; |
| 313 | assertEquals(undefined, a[kElementCount+1]); |
| 314 | } |
| 315 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 316 | for (var t = 0; t < types.length; t++) { |
| 317 | var type = types[t]; |
| 318 | var a = new type(kElementCount); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 319 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 320 | for (var i = 0; i < kElementCount; i++) { |
| 321 | a[i] = i; |
| 322 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 323 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 324 | // Run test functions defined above. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 325 | run_test(test_load, a, 780 * kRuns); |
| 326 | run_test(test_load_const_key, a, 3 * kRuns); |
| 327 | run_test(test_store, a, 820 * kRuns); |
| 328 | run_test(test_store_const_key, a, 6 * kRuns); |
| 329 | run_test(test_store_low_int, a, test_result_low_int[t]); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 330 | run_test(test_store_low_double, a, test_result_low_double[t]); |
| 331 | run_test(test_store_low_tagged, a, test_result_low_double[t]); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 332 | run_test(test_store_high_int, a, test_result_high_int[t]); |
| 333 | run_test(test_store_nan, a, test_result_nan[t]); |
| 334 | run_test(test_store_middle_double, a, test_result_middle[t]); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 335 | run_test(test_store_middle_tagged, a, test_result_middle[t]); |
| 336 | run_test(test_store_high_double, a, test_result_high_double[t]); |
| 337 | run_test(test_store_high_tagged, a, test_result_high_double[t]); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 338 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 339 | // Test the correct behavior of the |length| property (which is read-only). |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 340 | if (t != 0) { |
| 341 | assertEquals(kElementCount, a.length); |
| 342 | a.length = 2; |
| 343 | assertEquals(kElementCount, a.length); |
| 344 | assertTrue(delete a.length); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 345 | |
| 346 | // Make sure bounds checks are handled correctly for external arrays. |
| 347 | run_bounds_test(a); |
| 348 | run_bounds_test(a); |
| 349 | run_bounds_test(a); |
| 350 | %OptimizeFunctionOnNextCall(run_bounds_test); |
| 351 | run_bounds_test(a); |
| 352 | %DeoptimizeFunction(run_bounds_test); |
| 353 | gc(); // Makes V8 forget about type information for test_func. |
| 354 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 355 | } |
ager@chromium.org | 560b07b | 2011-05-23 16:33:44 +0000 | [diff] [blame] | 356 | |
| 357 | function array_load_set_smi_check(a) { |
| 358 | return a[0] = a[0] = 1; |
| 359 | } |
| 360 | |
| 361 | array_load_set_smi_check(a); |
| 362 | array_load_set_smi_check(0); |
| 363 | |
| 364 | function array_load_set_smi_check2(a) { |
| 365 | return a[0] = a[0] = 1; |
| 366 | } |
| 367 | |
| 368 | array_load_set_smi_check2(a); |
| 369 | %OptimizeFunctionOnNextCall(array_load_set_smi_check2); |
| 370 | array_load_set_smi_check2(a); |
| 371 | array_load_set_smi_check2(0); |
| 372 | %DeoptimizeFunction(array_load_set_smi_check2); |
| 373 | gc(); // Makes V8 forget about type information for array_load_set_smi_check. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 374 | } |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 375 | |
| 376 | // Check handling of undefined in 32- and 64-bit external float arrays. |
| 377 | |
| 378 | function store_float32_undefined(ext_array) { |
| 379 | ext_array[0] = undefined; |
| 380 | } |
| 381 | |
| 382 | var float32_array = new Float32Array(1); |
| 383 | // Make sure runtime does it right |
| 384 | store_float32_undefined(float32_array); |
| 385 | assertTrue(isNaN(float32_array[0])); |
| 386 | // Make sure the ICs do it right |
| 387 | store_float32_undefined(float32_array); |
| 388 | assertTrue(isNaN(float32_array[0])); |
| 389 | // Make sure that Cranskshft does it right. |
| 390 | %OptimizeFunctionOnNextCall(store_float32_undefined); |
| 391 | store_float32_undefined(float32_array); |
| 392 | assertTrue(isNaN(float32_array[0])); |
| 393 | |
| 394 | function store_float64_undefined(ext_array) { |
| 395 | ext_array[0] = undefined; |
| 396 | } |
| 397 | |
| 398 | var float64_array = new Float64Array(1); |
| 399 | // Make sure runtime does it right |
| 400 | store_float64_undefined(float64_array); |
| 401 | assertTrue(isNaN(float64_array[0])); |
| 402 | // Make sure the ICs do it right |
| 403 | store_float64_undefined(float64_array); |
| 404 | assertTrue(isNaN(float64_array[0])); |
| 405 | // Make sure that Cranskshft does it right. |
| 406 | %OptimizeFunctionOnNextCall(store_float64_undefined); |
| 407 | store_float64_undefined(float64_array); |
| 408 | assertTrue(isNaN(float64_array[0])); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 409 | |
| 410 | |
| 411 | // Check handling of 0-sized buffers and arrays. |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 412 | ab = new ArrayBuffer(0); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 413 | assertInstance(ab, ArrayBuffer); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 414 | assertEquals(0, ab.byteLength); |
| 415 | a = new Int8Array(ab); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 416 | assertInstance(a, Int8Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 417 | assertEquals(0, a.byteLength); |
| 418 | assertEquals(0, a.length); |
| 419 | a[0] = 1; |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 420 | assertEquals(undefined, a[0]); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 421 | ab = new ArrayBuffer(16); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 422 | assertInstance(ab, ArrayBuffer); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 423 | a = new Float32Array(ab,4,0); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 424 | assertInstance(a, Float32Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 425 | assertEquals(0, a.byteLength); |
| 426 | assertEquals(0, a.length); |
| 427 | a[0] = 1; |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 428 | assertEquals(undefined, a[0]); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 429 | a = new Uint16Array(0); |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 430 | assertInstance(a, Uint16Array); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 431 | assertEquals(0, a.byteLength); |
| 432 | assertEquals(0, a.length); |
| 433 | a[0] = 1; |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 434 | assertEquals(undefined, a[0]); |
| 435 | |
yangguo@chromium.org | c74d674 | 2012-06-29 15:15:45 +0000 | [diff] [blame] | 436 | // Check construction from arrays. |
| 437 | a = new Uint32Array([]); |
| 438 | assertInstance(a, Uint32Array); |
| 439 | assertEquals(0, a.length); |
| 440 | assertEquals(0, a.byteLength); |
| 441 | assertEquals(0, a.buffer.byteLength); |
| 442 | assertEquals(4, a.BYTES_PER_ELEMENT); |
| 443 | assertInstance(a.buffer, ArrayBuffer); |
| 444 | a = new Uint16Array([1,2,3]); |
| 445 | assertInstance(a, Uint16Array); |
| 446 | assertEquals(3, a.length); |
| 447 | assertEquals(6, a.byteLength); |
| 448 | assertEquals(6, a.buffer.byteLength); |
| 449 | assertEquals(2, a.BYTES_PER_ELEMENT); |
| 450 | assertEquals(1, a[0]); |
| 451 | assertEquals(3, a[2]); |
| 452 | assertInstance(a.buffer, ArrayBuffer); |
| 453 | a = new Uint32Array(a); |
| 454 | assertInstance(a, Uint32Array); |
| 455 | assertEquals(3, a.length); |
| 456 | assertEquals(12, a.byteLength); |
| 457 | assertEquals(12, a.buffer.byteLength); |
| 458 | assertEquals(4, a.BYTES_PER_ELEMENT); |
| 459 | assertEquals(1, a[0]); |
| 460 | assertEquals(3, a[2]); |
| 461 | assertInstance(a.buffer, ArrayBuffer); |
| 462 | |
| 463 | // Check subarrays. |
| 464 | a = new Uint16Array([1,2,3,4,5,6]); |
| 465 | aa = a.subarray(3); |
| 466 | assertInstance(aa, Uint16Array); |
| 467 | assertEquals(3, aa.length); |
| 468 | assertEquals(6, aa.byteLength); |
| 469 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 470 | assertSame(a.buffer, aa.buffer); |
| 471 | aa = a.subarray(3,5); |
| 472 | assertInstance(aa, Uint16Array); |
| 473 | assertEquals(2, aa.length); |
| 474 | assertEquals(4, aa.byteLength); |
| 475 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 476 | assertSame(a.buffer, aa.buffer); |
| 477 | aa = a.subarray(4,8); |
| 478 | assertInstance(aa, Uint16Array); |
| 479 | assertEquals(2, aa.length); |
| 480 | assertEquals(4, aa.byteLength); |
| 481 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 482 | assertSame(a.buffer, aa.buffer); |
| 483 | aa = a.subarray(9); |
| 484 | assertInstance(aa, Uint16Array); |
| 485 | assertEquals(0, aa.length); |
| 486 | assertEquals(0, aa.byteLength); |
| 487 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 488 | assertSame(a.buffer, aa.buffer); |
| 489 | aa = a.subarray(-4); |
| 490 | assertInstance(aa, Uint16Array); |
| 491 | assertEquals(4, aa.length); |
| 492 | assertEquals(8, aa.byteLength); |
| 493 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 494 | assertSame(a.buffer, aa.buffer); |
| 495 | aa = a.subarray(-3,-1); |
| 496 | assertInstance(aa, Uint16Array); |
| 497 | assertEquals(2, aa.length); |
| 498 | assertEquals(4, aa.byteLength); |
| 499 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 500 | assertSame(a.buffer, aa.buffer); |
| 501 | aa = a.subarray(3,2); |
| 502 | assertInstance(aa, Uint16Array); |
| 503 | assertEquals(0, aa.length); |
| 504 | assertEquals(0, aa.byteLength); |
| 505 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 506 | assertSame(a.buffer, aa.buffer); |
| 507 | aa = a.subarray(-3,-4); |
| 508 | assertInstance(aa, Uint16Array); |
| 509 | assertEquals(0, aa.length); |
| 510 | assertEquals(0, aa.byteLength); |
| 511 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 512 | assertSame(a.buffer, aa.buffer); |
| 513 | aa = a.subarray(0,-8); |
| 514 | assertInstance(aa, Uint16Array); |
| 515 | assertEquals(0, aa.length); |
| 516 | assertEquals(0, aa.byteLength); |
| 517 | assertEquals(2, aa.BYTES_PER_ELEMENT); |
| 518 | assertSame(a.buffer, aa.buffer); |
| 519 | |
| 520 | assertThrows(function(){ a.subarray.call({}, 0) }); |
| 521 | assertThrows(function(){ a.subarray.call([], 0) }); |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 522 | |
| 523 | // Call constructors directly as functions, and through .call and .apply |
| 524 | |
| 525 | b = ArrayBuffer(100) |
| 526 | a = Int8Array(b, 5, 77) |
| 527 | assertInstance(b, ArrayBuffer) |
| 528 | assertInstance(a, Int8Array) |
| 529 | assertSame(b, a.buffer) |
| 530 | assertEquals(5, a.byteOffset) |
| 531 | assertEquals(77, a.byteLength) |
| 532 | b = ArrayBuffer.call(null, 10) |
| 533 | a = Uint16Array.call(null, b, 2, 4) |
| 534 | assertInstance(b, ArrayBuffer) |
| 535 | assertInstance(a, Uint16Array) |
| 536 | assertSame(b, a.buffer) |
| 537 | assertEquals(2, a.byteOffset) |
| 538 | assertEquals(8, a.byteLength) |
| 539 | b = ArrayBuffer.apply(null, [1000]) |
| 540 | a = Float32Array.apply(null, [b, 128, 1]) |
| 541 | assertInstance(b, ArrayBuffer) |
| 542 | assertInstance(a, Float32Array) |
| 543 | assertSame(b, a.buffer) |
| 544 | assertEquals(128, a.byteOffset) |
| 545 | assertEquals(4, a.byteLength) |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 546 | |
| 547 | |
| 548 | // Test array.set in different combinations. |
| 549 | |
| 550 | function assertArrayPrefix(expected, array) { |
| 551 | for (var i = 0; i < expected.length; ++i) { |
| 552 | assertEquals(expected[i], array[i]); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | var a11 = new Int16Array([1, 2, 3, 4, 0, -1]) |
| 557 | var a12 = new Uint16Array(15) |
| 558 | a12.set(a11, 3) |
| 559 | assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12) |
| 560 | assertThrows(function(){ a11.set(a12) }) |
| 561 | |
| 562 | var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}] |
| 563 | var a22 = new Int32Array(12) |
| 564 | a22.set(a21, 2) |
| 565 | assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22) |
| 566 | |
| 567 | var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3]) |
| 568 | var a32 = a31.subarray(2, 6) |
| 569 | a31.set(a32, 4) |
| 570 | assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31) |
| 571 | assertArrayPrefix([6, 8, 6, 8], a32) |
| 572 | |
| 573 | var a4 = new Uint8ClampedArray([3,2,5,6]) |
| 574 | a4.set(a4) |
| 575 | assertArrayPrefix([3, 2, 5, 6], a4) |
| 576 | |
| 577 | // Cases with overlapping backing store but different element sizes. |
| 578 | var b = new ArrayBuffer(4) |
| 579 | var a5 = new Int16Array(b) |
| 580 | var a50 = new Int8Array(b) |
| 581 | var a51 = new Int8Array(b, 0, 2) |
| 582 | var a52 = new Int8Array(b, 1, 2) |
| 583 | var a53 = new Int8Array(b, 2, 2) |
| 584 | |
| 585 | a5.set([0x5050, 0x0a0a]) |
| 586 | assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) |
| 587 | assertArrayPrefix([0x50, 0x50], a51) |
| 588 | assertArrayPrefix([0x50, 0x0a], a52) |
| 589 | assertArrayPrefix([0x0a, 0x0a], a53) |
| 590 | |
| 591 | a50.set([0x50, 0x50, 0x0a, 0x0a]) |
| 592 | a51.set(a5) |
| 593 | assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50) |
| 594 | |
| 595 | a50.set([0x50, 0x50, 0x0a, 0x0a]) |
| 596 | a52.set(a5) |
| 597 | assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) |
| 598 | |
| 599 | a50.set([0x50, 0x50, 0x0a, 0x0a]) |
| 600 | a53.set(a5) |
| 601 | assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50) |
| 602 | |
| 603 | a50.set([0x50, 0x51, 0x0a, 0x0b]) |
| 604 | a5.set(a51) |
| 605 | assertArrayPrefix([0x0050, 0x0051], a5) |
| 606 | |
| 607 | a50.set([0x50, 0x51, 0x0a, 0x0b]) |
| 608 | a5.set(a52) |
| 609 | assertArrayPrefix([0x0051, 0x000a], a5) |
| 610 | |
| 611 | a50.set([0x50, 0x51, 0x0a, 0x0b]) |
| 612 | a5.set(a53) |
| 613 | assertArrayPrefix([0x000a, 0x000b], a5) |
| 614 | |
| 615 | // Mixed types of same size. |
| 616 | var a61 = new Float32Array([1.2, 12.3]) |
| 617 | var a62 = new Int32Array(2) |
| 618 | a62.set(a61) |
| 619 | assertArrayPrefix([1, 12], a62) |
| 620 | a61.set(a62) |
| 621 | assertArrayPrefix([1, 12], a61) |
| 622 | |
| 623 | // Invalid source |
| 624 | assertThrows(function() { a.set(0) }) |
| 625 | assertThrows(function() { a.set({}) }) |
| 626 | |
| 627 | |
| 628 | // Test arraybuffer.slice |
| 629 | |
| 630 | var a0 = new Int8Array([1, 2, 3, 4, 5, 6]) |
| 631 | var b0 = a0.buffer |
| 632 | |
| 633 | var b1 = b0.slice(0) |
| 634 | assertEquals(b0.byteLength, b1.byteLength) |
| 635 | assertArrayPrefix([1, 2, 3, 4, 5, 6], Int8Array(b1)) |
| 636 | |
| 637 | var b2 = b0.slice(3) |
| 638 | assertEquals(b0.byteLength - 3, b2.byteLength) |
| 639 | assertArrayPrefix([4, 5, 6], Int8Array(b2)) |
| 640 | |
| 641 | var b3 = b0.slice(2, 4) |
| 642 | assertEquals(2, b3.byteLength) |
| 643 | assertArrayPrefix([3, 4], Int8Array(b3)) |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 644 | |
| 645 | function goo(a, i) { |
| 646 | return a[i]; |
| 647 | } |
| 648 | |
| 649 | function boo(a, i, v) { |
| 650 | return a[i] = v; |
| 651 | } |
| 652 | |
| 653 | function do_tagged_index_external_array_test(constructor) { |
| 654 | var t_array = new constructor([1, 2, 3, 4, 5, 6]); |
| 655 | assertEquals(1, goo(t_array, 0)); |
| 656 | assertEquals(1, goo(t_array, 0)); |
| 657 | boo(t_array, 0, 13); |
| 658 | assertEquals(13, goo(t_array, 0)); |
| 659 | %OptimizeFunctionOnNextCall(goo); |
| 660 | %OptimizeFunctionOnNextCall(boo); |
| 661 | boo(t_array, 0, 15); |
| 662 | assertEquals(15, goo(t_array, 0)); |
| 663 | %ClearFunctionTypeFeedback(goo); |
| 664 | %ClearFunctionTypeFeedback(boo); |
| 665 | } |
| 666 | |
| 667 | do_tagged_index_external_array_test(Int8Array); |
| 668 | do_tagged_index_external_array_test(Uint8Array); |
| 669 | do_tagged_index_external_array_test(Int16Array); |
| 670 | do_tagged_index_external_array_test(Uint16Array); |
| 671 | do_tagged_index_external_array_test(Int32Array); |
| 672 | do_tagged_index_external_array_test(Uint32Array); |
| 673 | do_tagged_index_external_array_test(Float32Array); |
| 674 | do_tagged_index_external_array_test(Float64Array); |
| 675 | |
| 676 | var built_in_array = new Array(1, 2, 3, 4, 5, 6); |
| 677 | assertEquals(1, goo(built_in_array, 0)); |
| 678 | assertEquals(1, goo(built_in_array, 0)); |
| 679 | %OptimizeFunctionOnNextCall(goo); |
| 680 | %OptimizeFunctionOnNextCall(boo); |
| 681 | boo(built_in_array, 0, 11); |
| 682 | assertEquals(11, goo(built_in_array, 0)); |
| 683 | %ClearFunctionTypeFeedback(goo); |
| 684 | %ClearFunctionTypeFeedback(boo); |
| 685 | |
| 686 | built_in_array = new Array(1.5, 2, 3, 4, 5, 6); |
| 687 | assertEquals(1.5, goo(built_in_array, 0)); |
| 688 | assertEquals(1.5, goo(built_in_array, 0)); |
| 689 | %OptimizeFunctionOnNextCall(goo); |
| 690 | %OptimizeFunctionOnNextCall(boo); |
| 691 | boo(built_in_array, 0, 2.5); |
| 692 | assertEquals(2.5, goo(built_in_array, 0)); |
| 693 | %ClearFunctionTypeFeedback(goo); |
| 694 | %ClearFunctionTypeFeedback(boo); |