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// Flags: --nostress-opt 30 31// This code exists to eliminate the learning influence of AllocationSites 32// on the following tests. 33var __sequence = 0; 34function make_array_string(length) { 35 this.__sequence = this.__sequence + 1; 36 return "/* " + this.__sequence + " */ new Array(" + length + ");"; 37} 38function make_array(length) { 39 return eval(make_array_string(length)); 40} 41 42function test(test_double, test_object, set, length) { 43 // We apply the same operations to two identical arrays. The first array 44 // triggers an IC miss, upon which the conversion stub is generated, but the 45 // actual conversion is done in runtime. The second array, arriving at 46 // the previously patched IC, is then converted using the conversion stub. 47 var array_1 = make_array(length); 48 var array_2 = make_array(length); 49 50 // false, true, nice setter function, 20 51 assertTrue(%HasFastSmiElements(array_1)); 52 assertTrue(%HasFastSmiElements(array_2)); 53 for (var i = 0; i < length; i++) { 54 if (i == length - 5 && test_double) { 55 // Trigger conversion to fast double elements at length-5. 56 set(array_1, i, 0.5); 57 set(array_2, i, 0.5); 58 assertTrue(%HasFastDoubleElements(array_1)); 59 assertTrue(%HasFastDoubleElements(array_2)); 60 } else if (i == length - 3 && test_object) { 61 // Trigger conversion to fast object elements at length-3. 62 set(array_1, i, 'object'); 63 set(array_2, i, 'object'); 64 assertTrue(%HasFastObjectElements(array_1)); 65 assertTrue(%HasFastObjectElements(array_2)); 66 } else if (i != length - 7) { 67 // Set the element to an integer but leave a hole at length-7. 68 set(array_1, i, 2*i+1); 69 set(array_2, i, 2*i+1); 70 } 71 } 72 73 for (var i = 0; i < length; i++) { 74 if (i == length - 5 && test_double) { 75 assertEquals(0.5, array_1[i]); 76 assertEquals(0.5, array_2[i]); 77 } else if (i == length - 3 && test_object) { 78 assertEquals('object', array_1[i]); 79 assertEquals('object', array_2[i]); 80 } else if (i != length - 7) { 81 assertEquals(2*i+1, array_1[i]); 82 assertEquals(2*i+1, array_2[i]); 83 } else { 84 assertEquals(undefined, array_1[i]); 85 assertEquals(undefined, array_2[i]); 86 } 87 } 88 89 assertEquals(length, array_1.length); 90 assertEquals(length, array_2.length); 91} 92 93function run_test(test_double, test_object, set, length) { 94 test(test_double, test_object, set, length); 95 %ClearFunctionTypeFeedback(test); 96} 97 98run_test(false, false, function(a,i,v){ a[i] = v; }, 20); 99run_test(true, false, function(a,i,v){ a[i] = v; }, 20); 100run_test(false, true, function(a,i,v){ a[i] = v; }, 20); 101run_test(true, true, function(a,i,v){ a[i] = v; }, 20); 102 103run_test(false, false, function(a,i,v){ a[i] = v; }, 10000); 104run_test(true, false, function(a,i,v){ a[i] = v; }, 10000); 105run_test(false, true, function(a,i,v){ a[i] = v; }, 10000); 106run_test(true, true, function(a,i,v){ a[i] = v; }, 10000); 107 108// Check COW arrays 109function get_cow() { return [1, 2, 3]; } 110 111function transition(x) { x[0] = 1.5; } 112 113var ignore = get_cow(); 114transition(ignore); // Handled by runtime. 115var a = get_cow(); 116var b = get_cow(); 117transition(a); // Handled by IC. 118assertEquals(1.5, a[0]); 119assertEquals(1, b[0]); 120