1 // Copyright 2011 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 function init_array(a) { 30 for (var i = 0; i < 10; ++i ){ 31 a[i] = i; 32 } 33 } 34 35 function init_sparse_array(a) { 36 for (var i = 0; i < 10; ++i ){ 37 a[i] = i; 38 } 39 a[200000] = 256; 40 return %NormalizeElements(a); 41 } 42 43 function testPolymorphicLoads() { 44 function make_polymorphic_load_function() { 45 function load(a, i) { 46 return a[i]; 47 } 48 49 var object_array = new Object; 50 var sparse_object_array = new Object; 51 var js_array = new Array(10); 52 var sparse_js_array = %NormalizeElements([]); 53 54 init_array(object_array); 55 init_array(js_array); 56 init_sparse_array(sparse_object_array); 57 init_sparse_array(sparse_js_array); 58 59 assertEquals(1, load(object_array, 1)); 60 assertEquals(1, load(js_array, 1)); 61 assertEquals(1, load(sparse_object_array, 1)); 62 assertEquals(1, load(sparse_js_array, 1)); 63 64 return load; 65 } 66 67 var object_array = new Object; 68 var sparse_object_array = new Object; 69 var js_array = new Array(10); 70 var sparse_js_array = %NormalizeElements([]); 71 72 init_array(object_array); 73 init_array(js_array); 74 init_sparse_array(sparse_object_array); 75 init_sparse_array(sparse_js_array); 76 77 load = make_polymorphic_load_function(); 78 assertEquals(undefined, load(js_array, new Object())); 79 load = make_polymorphic_load_function(); 80 assertEquals(undefined, load(object_array, new Object())); 81 load = make_polymorphic_load_function(); 82 assertEquals(undefined, load(sparse_js_array, new Object())); 83 load = make_polymorphic_load_function(); 84 assertEquals(undefined, load(sparse_object_array, new Object())); 85 86 // Try with crankshaft. 87 load = make_polymorphic_load_function(); 88 %OptimizeFunctionOnNextCall(load); 89 assertEquals(1, load(object_array, 1)); 90 assertEquals(1, load(js_array, 1)); 91 assertEquals(1, load(sparse_object_array, 1)); 92 assertEquals(1, load(sparse_js_array, 1)); 93 94 load = make_polymorphic_load_function(); 95 %OptimizeFunctionOnNextCall(load); 96 assertEquals(undefined, load(js_array, new Object())); 97 load = make_polymorphic_load_function(); 98 %OptimizeFunctionOnNextCall(load); 99 assertEquals(undefined, load(object_array, new Object())); 100 load = make_polymorphic_load_function(); 101 %OptimizeFunctionOnNextCall(load); 102 assertEquals(undefined, load(sparse_js_array, new Object())); 103 load = make_polymorphic_load_function(); 104 %OptimizeFunctionOnNextCall(load); 105 assertEquals(undefined, load(sparse_object_array, new Object())); 106 } 107 108 function testPolymorphicStores() { 109 function make_polymorphic_store_function() { 110 function store(a, i, val) { 111 a[i] = val; 112 } 113 114 var object_array = new Object; 115 var sparse_object_array = new Object; 116 var js_array = new Array(10); 117 var sparse_js_array = []; 118 sparse_js_array.length = 200001; 119 120 init_array(object_array); 121 init_array(js_array); 122 init_sparse_array(sparse_object_array); 123 init_sparse_array(sparse_js_array); 124 125 store(object_array, 1, 256); 126 store(js_array, 1, 256); 127 store(sparse_object_array, 1, 256); 128 store(sparse_js_array, 1, 256); 129 130 return store; 131 } 132 133 var object_array = new Object; 134 var sparse_object_array = new Object; 135 var js_array = new Array(10); 136 var sparse_js_array = %NormalizeElements([]); 137 sparse_js_array.length = 200001; 138 assertTrue(%HasDictionaryElements(sparse_js_array)); 139 140 init_array(object_array); 141 init_array(js_array); 142 init_sparse_array(sparse_object_array); 143 init_sparse_array(sparse_js_array); 144 145 store = make_polymorphic_store_function(); 146 store(object_array, 2, 257); 147 store = make_polymorphic_store_function(); 148 store(js_array, 2, 257); 149 store = make_polymorphic_store_function(); 150 store(sparse_object_array, 2, 257); 151 store = make_polymorphic_store_function(); 152 store(sparse_js_array, 2, 257); 153 154 assertEquals(257, object_array[2]); 155 assertEquals(257, js_array[2]); 156 assertEquals(257, sparse_js_array[2]); 157 assertEquals(257, sparse_object_array[2]); 158 159 // Now try Crankshaft optimized polymorphic stores 160 store = make_polymorphic_store_function(); 161 %OptimizeFunctionOnNextCall(store); 162 store(object_array, 3, 258); 163 store = make_polymorphic_store_function(); 164 %OptimizeFunctionOnNextCall(store); 165 store(js_array, 3, 258); 166 store = make_polymorphic_store_function(); 167 %OptimizeFunctionOnNextCall(store); 168 store(sparse_object_array, 3, 258); 169 store = make_polymorphic_store_function(); 170 %OptimizeFunctionOnNextCall(store); 171 store(sparse_js_array, 3, 258); 172 173 assertEquals(258, object_array[3]); 174 assertEquals(258, js_array[3]); 175 assertEquals(258, sparse_js_array[3]); 176 assertEquals(258, sparse_object_array[3]); 177 } 178 179 testPolymorphicLoads(); 180 testPolymorphicStores(); 181