13ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Copyright 2012 the V8 project authors. All rights reserved. 2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without 3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are 4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met: 5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions of source code must retain the above copyright 7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// notice, this list of conditions and the following disclaimer. 8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions in binary form must reproduce the above 9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// copyright notice, this list of conditions and the following 10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// disclaimer in the documentation and/or other materials provided 11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// with the distribution. 12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Neither the name of Google Inc. nor the names of its 13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contributors may be used to endorse or promote products derived 14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// from this software without specific prior written permission. 15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 28257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch// Flags: --allow-natives-syntax --expose-gc 296ded16be15dd865a9b21ea304d5273c8be299c87Steve Block 308b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch// This is a regression test for overlapping key and value registers. 318b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochfunction f(a) { 328b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch a[0] = 0; 338b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch a[1] = 0; 346ded16be15dd865a9b21ea304d5273c8be299c87Steve Block} 35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 368b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochvar a = new Int32Array(2); 378b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochfor (var i = 0; i < 5; i++) { 388b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch f(a); 3925f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen} 408b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch%OptimizeFunctionOnNextCall(f); 418b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochf(a); 4225f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen 438b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochassertEquals(0, a[0]); 448b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochassertEquals(0, a[1]); 4525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen 463ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// No-parameter constructor should fail right now. 473ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction abfunc1() { 483ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch return new ArrayBuffer(); 493ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 503ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertThrows(abfunc1); 513ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 523ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Test derivation from an ArrayBuffer 533ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar ab = new ArrayBuffer(12); 543ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar derived_uint8 = new Uint8Array(ab); 553ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertEquals(12, derived_uint8.length); 563ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar derived_uint32 = new Uint32Array(ab); 573ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertEquals(3, derived_uint32.length); 583ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar derived_uint32_2 = new Uint32Array(ab,4); 593ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertEquals(2, derived_uint32_2.length); 603ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar derived_uint32_3 = new Uint32Array(ab,4,1); 613ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertEquals(1, derived_uint32_3.length); 623ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 633ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// If a given byteOffset and length references an area beyond the end of the 643ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// ArrayBuffer an exception is raised. 653ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction abfunc3() { 663ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch new Uint32Array(ab,4,3); 673ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 683ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertThrows(abfunc3); 693ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction abfunc4() { 703ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch new Uint32Array(ab,16); 713ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 723ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertThrows(abfunc4); 733ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 743ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// The given byteOffset must be a multiple of the element size of the specific 753ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// type, otherwise an exception is raised. 763ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction abfunc5() { 773ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch new Uint32Array(ab,5); 783ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 793ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertThrows(abfunc5); 803ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 813ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// If length is not explicitly specified, the length of the ArrayBuffer minus 823ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// the byteOffset must be a multiple of the element size of the specific type, 833ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// or an exception is raised. 843ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar ab2 = new ArrayBuffer(13); 853ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction abfunc6() { 863ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch new Uint32Array(ab2,4); 873ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 883ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertThrows(abfunc6); 893ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 908b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch// Test the correct behavior of the |BYTES_PER_ELEMENT| property (which is 918b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch// "constant", but not read-only). 928b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdocha = new Int32Array(2); 938b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochassertEquals(4, a.BYTES_PER_ELEMENT); 948b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdocha.BYTES_PER_ELEMENT = 42; 958b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochassertEquals(42, a.BYTES_PER_ELEMENT); 968b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdocha = new Uint8Array(2); 978b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochassertEquals(1, a.BYTES_PER_ELEMENT); 988b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdocha = new Int16Array(2); 998b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochassertEquals(2, a.BYTES_PER_ELEMENT); 100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 101257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch// Test Float64Arrays. 102257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction get(a, index) { 103257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return a[index]; 104257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 105257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction set(a, index, value) { 106257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch a[index] = value; 107257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 108257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction temp() { 109257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochvar array = new Float64Array(2); 110257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfor (var i = 0; i < 5; i++) { 111257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch set(array, 0, 2.5); 112257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(2.5, array[0]); 113257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 114257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch%OptimizeFunctionOnNextCall(set); 115257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochset(array, 0, 2.5); 116257744e915dfc84d6d07a6b2accf8402d9ffc708Ben MurdochassertEquals(2.5, array[0]); 117257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochset(array, 1, 3.5); 118257744e915dfc84d6d07a6b2accf8402d9ffc708Ben MurdochassertEquals(3.5, array[1]); 119257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfor (var i = 0; i < 5; i++) { 120257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(2.5, get(array, 0)); 121257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(3.5, array[1]); 122257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 123257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch%OptimizeFunctionOnNextCall(get); 124257744e915dfc84d6d07a6b2accf8402d9ffc708Ben MurdochassertEquals(2.5, get(array, 0)); 125257744e915dfc84d6d07a6b2accf8402d9ffc708Ben MurdochassertEquals(3.5, get(array, 1)); 126257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 127257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 128589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch// Test non-number parameters. 129589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdochvar array_with_length_from_non_number = new Int32Array("2"); 130589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben MurdochassertEquals(2, array_with_length_from_non_number.length); 131589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdocharray_with_length_from_non_number = new Int32Array(undefined); 132589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben MurdochassertEquals(0, array_with_length_from_non_number.length); 133589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdochvar foo = { valueOf: function() { return 3; } }; 134589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdocharray_with_length_from_non_number = new Int32Array(foo); 135589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben MurdochassertEquals(3, array_with_length_from_non_number.length); 136589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdochfoo = { toString: function() { return "4"; } }; 137589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdocharray_with_length_from_non_number = new Int32Array(foo); 138589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben MurdochassertEquals(4, array_with_length_from_non_number.length); 139589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch 140589d6979ff2ef66fca2d8fa51404c369ca5e9250Ben Murdoch 141257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch// Test loads and stores. 142257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochtypes = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, 143257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch Uint32Array, PixelArray, Float32Array, Float64Array]; 144257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 145257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochtest_result_nan = [NaN, 0, 0, 0, 0, 0, 0, 0, NaN, NaN]; 146257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochtest_result_low_int = [-1, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1, -1]; 1473fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochtest_result_low_double = [-1.25, -1, 255, -1, 65535, -1, 0xFFFFFFFF, 0, -1.25, -1.25]; 148257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochtest_result_middle = [253.75, -3, 253, 253, 253, 253, 253, 254, 253.75, 253.75]; 149257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochtest_result_high_int = [256, 0, 0, 256, 256, 256, 256, 255, 256, 256]; 1503fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochtest_result_high_double = [256.25, 0, 0, 256, 256, 256, 256, 255, 256.25, 256.25]; 151257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 152257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochconst kElementCount = 40; 153257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 154257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_load(array, sum) { 155257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch for (var i = 0; i < kElementCount; i++) { 156257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[i]; 157257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 158257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return sum; 159257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 160257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 161257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_load_const_key(array, sum) { 162257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[0]; 163257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[1]; 164257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[2]; 165257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return sum; 166257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 167257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 168257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store(array, sum) { 169257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch for (var i = 0; i < kElementCount; i++) { 170257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[i] = i+1; 171257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 172257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return sum; 173257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 174257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 175257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store_const_key(array, sum) { 176257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[0] = 1; 177257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[1] = 2; 178257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum += array[2] = 3; 179257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return sum; 180257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 181257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 1823fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochfunction zero() { 1833fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch return 0.0; 1843fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch} 185257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 1863fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochfunction test_store_middle_tagged(array, sum) { 187257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array[0] = 253.75; 188257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return array[0]; 189257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 190257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 1913fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochfunction test_store_high_tagged(array, sum) { 1923fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch array[0] = 256.25; 1933fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch return array[0]; 1943fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch} 1953fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch 1963fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochfunction test_store_middle_double(array, sum) { 1973fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch array[0] = 253.75 + zero(); // + forces double type feedback 1983fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch return array[0]; 1993fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch} 200257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 201257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store_high_double(array, sum) { 2023fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch array[0] = 256.25 + zero(); // + forces double type feedback 203257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return array[0]; 204257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 205257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 206257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store_high_double(array, sum) { 207257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array[0] = 256.25; 208257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return array[0]; 209257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 210257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 211257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store_low_int(array, sum) { 212257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array[0] = -1; 213257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return array[0]; 214257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 215257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 2163fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochfunction test_store_low_tagged(array, sum) { 2173fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch array[0] = -1.25; 2183fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch return array[0]; 2193fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch} 2203fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch 2213fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdochfunction test_store_low_double(array, sum) { 2223fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch array[0] = -1.25 + zero(); // + forces double type feedback 2233fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch return array[0]; 2243fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch} 2253fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch 226257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store_high_int(array, sum) { 227257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array[0] = 256; 228257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return array[0]; 229257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 230257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 231257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction test_store_nan(array, sum) { 232257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array[0] = NaN; 233257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return array[0]; 234257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 235257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 236257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochconst kRuns = 10; 237257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 238257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction run_test(test_func, array, expected_result) { 239257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch for (var i = 0; i < 5; i++) test_func(array, 0); 240257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch %OptimizeFunctionOnNextCall(test_func); 241257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch var sum = 0; 242257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch for (var i = 0; i < kRuns; i++) { 243257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch sum = test_func(array, sum); 244257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 245257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(expected_result, sum); 246257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch %DeoptimizeFunction(test_func); 247257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch gc(); // Makes V8 forget about type information for test_func. 248257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 249257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 25069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdochfunction run_bounds_test(test_func, array, expected_result) { 25169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch assertEquals(undefined, a[kElementCount]); 25269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch a[kElementCount] = 456; 25369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch assertEquals(undefined, a[kElementCount]); 25469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch assertEquals(undefined, a[kElementCount+1]); 25569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch a[kElementCount+1] = 456; 25669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch assertEquals(undefined, a[kElementCount+1]); 25769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch} 25869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch 259257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfor (var t = 0; t < types.length; t++) { 260257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch var type = types[t]; 261257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch var a = new type(kElementCount); 26269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch 263257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch for (var i = 0; i < kElementCount; i++) { 264257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch a[i] = i; 265257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 266257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 267257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch // Run test functions defined above. 268257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_load, a, 780 * kRuns); 269257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_load_const_key, a, 3 * kRuns); 270257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_store, a, 820 * kRuns); 271257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_store_const_key, a, 6 * kRuns); 272257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_store_low_int, a, test_result_low_int[t]); 2733fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch run_test(test_store_low_double, a, test_result_low_double[t]); 2743fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch run_test(test_store_low_tagged, a, test_result_low_double[t]); 275257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_store_high_int, a, test_result_high_int[t]); 276257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_store_nan, a, test_result_nan[t]); 277257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch run_test(test_store_middle_double, a, test_result_middle[t]); 2783fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch run_test(test_store_middle_tagged, a, test_result_middle[t]); 2793fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch run_test(test_store_high_double, a, test_result_high_double[t]); 2803fb3ca8c7ca439d408449a395897395c0faae8d1Ben Murdoch run_test(test_store_high_tagged, a, test_result_high_double[t]); 281257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 282257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch // Test the correct behavior of the |length| property (which is read-only). 283257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch if (t != 0) { 284257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(kElementCount, a.length); 285257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch a.length = 2; 286257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(kElementCount, a.length); 287257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertTrue(delete a.length); 288257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch a.length = 2; 289257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch assertEquals(2, a.length); 29069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch 29169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch // Make sure bounds checks are handled correctly for external arrays. 29269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch run_bounds_test(a); 29369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch run_bounds_test(a); 29469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch run_bounds_test(a); 29569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch %OptimizeFunctionOnNextCall(run_bounds_test); 29669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch run_bounds_test(a); 29769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch %DeoptimizeFunction(run_bounds_test); 29869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch gc(); // Makes V8 forget about type information for test_func. 29969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch 300257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 301257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 302257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch function array_load_set_smi_check(a) { 303257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return a[0] = a[0] = 1; 304257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 305257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 306257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array_load_set_smi_check(a); 307257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array_load_set_smi_check(0); 308257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 309257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch function array_load_set_smi_check2(a) { 310257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch return a[0] = a[0] = 1; 311257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch } 312257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 313257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array_load_set_smi_check2(a); 314257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch %OptimizeFunctionOnNextCall(array_load_set_smi_check2); 315257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array_load_set_smi_check2(a); 316257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch array_load_set_smi_check2(0); 317257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch %DeoptimizeFunction(array_load_set_smi_check2); 318257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch gc(); // Makes V8 forget about type information for array_load_set_smi_check. 319257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch} 3203ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 3213ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Check handling of undefined in 32- and 64-bit external float arrays. 3223ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 3233ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction store_float32_undefined(ext_array) { 3243ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch ext_array[0] = undefined; 3253ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 3263ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 3273ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar float32_array = new Float32Array(1); 3283ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Make sure runtime does it right 3293ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochstore_float32_undefined(float32_array); 3303ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertTrue(isNaN(float32_array[0])); 3313ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Make sure the ICs do it right 3323ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochstore_float32_undefined(float32_array); 3333ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertTrue(isNaN(float32_array[0])); 3343ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Make sure that Cranskshft does it right. 3353ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch%OptimizeFunctionOnNextCall(store_float32_undefined); 3363ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochstore_float32_undefined(float32_array); 3373ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertTrue(isNaN(float32_array[0])); 3383ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 3393ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochfunction store_float64_undefined(ext_array) { 3403ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch ext_array[0] = undefined; 3413ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch} 3423ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch 3433ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvar float64_array = new Float64Array(1); 3443ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Make sure runtime does it right 3453ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochstore_float64_undefined(float64_array); 3463ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertTrue(isNaN(float64_array[0])); 3473ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Make sure the ICs do it right 3483ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochstore_float64_undefined(float64_array); 3493ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertTrue(isNaN(float64_array[0])); 3503ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Make sure that Cranskshft does it right. 3513ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch%OptimizeFunctionOnNextCall(store_float64_undefined); 3523ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochstore_float64_undefined(float64_array); 3533ef787dbeca8a5fb1086949cda830dccee07bfbdBen MurdochassertTrue(isNaN(float64_array[0])); 354