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