yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 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 --smi-only-arrays --expose-gc |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 29 | // Flags: --track-allocation-sites --noalways-opt |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 30 | |
| 31 | // TODO(mvstanton): remove --nooptimize-constructed-arrays and enable |
| 32 | // the constructed array code below when the feature is turned on |
| 33 | // by default. |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 34 | |
| 35 | // Test element kind of objects. |
| 36 | // Since --smi-only-arrays affects builtins, its default setting at compile |
| 37 | // time sticks if built with snapshot. If --smi-only-arrays is deactivated |
| 38 | // by default, only a no-snapshot build actually has smi-only arrays enabled |
| 39 | // in this test case. Depending on whether smi-only arrays are actually |
| 40 | // enabled, this test takes the appropriate code path to check smi-only arrays. |
| 41 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 42 | // support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8)); |
| 43 | support_smi_only_arrays = true; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 44 | optimize_constructed_arrays = false; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 45 | |
| 46 | if (support_smi_only_arrays) { |
| 47 | print("Tests include smi-only arrays."); |
| 48 | } else { |
| 49 | print("Tests do NOT include smi-only arrays."); |
| 50 | } |
| 51 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 52 | if (optimize_constructed_arrays) { |
| 53 | print("Tests include constructed array optimizations."); |
| 54 | } else { |
| 55 | print("Tests do NOT include constructed array optimizations."); |
| 56 | } |
| 57 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 58 | var elements_kind = { |
| 59 | fast_smi_only : 'fast smi only elements', |
| 60 | fast : 'fast elements', |
| 61 | fast_double : 'fast double elements', |
| 62 | dictionary : 'dictionary elements', |
| 63 | external_byte : 'external byte elements', |
| 64 | external_unsigned_byte : 'external unsigned byte elements', |
| 65 | external_short : 'external short elements', |
| 66 | external_unsigned_short : 'external unsigned short elements', |
| 67 | external_int : 'external int elements', |
| 68 | external_unsigned_int : 'external unsigned int elements', |
| 69 | external_float : 'external float elements', |
| 70 | external_double : 'external double elements', |
| 71 | external_pixel : 'external pixel elements' |
| 72 | } |
| 73 | |
| 74 | function getKind(obj) { |
| 75 | if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only; |
| 76 | if (%HasFastObjectElements(obj)) return elements_kind.fast; |
| 77 | if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; |
| 78 | if (%HasDictionaryElements(obj)) return elements_kind.dictionary; |
| 79 | } |
| 80 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 81 | function isHoley(obj) { |
| 82 | if (%HasFastHoleyElements(obj)) return true; |
| 83 | return false; |
| 84 | } |
| 85 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 86 | function assertKind(expected, obj, name_opt) { |
| 87 | if (!support_smi_only_arrays && |
| 88 | expected == elements_kind.fast_smi_only) { |
| 89 | expected = elements_kind.fast; |
| 90 | } |
| 91 | assertEquals(expected, getKind(obj), name_opt); |
| 92 | } |
| 93 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 94 | function assertHoley(obj, name_opt) { |
| 95 | assertEquals(true, isHoley(obj), name_opt); |
| 96 | } |
| 97 | |
| 98 | function assertNotHoley(obj, name_opt) { |
| 99 | assertEquals(false, isHoley(obj), name_opt); |
| 100 | } |
| 101 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 102 | if (support_smi_only_arrays) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 103 | |
| 104 | obj = []; |
| 105 | assertNotHoley(obj); |
| 106 | assertKind(elements_kind.fast_smi_only, obj); |
| 107 | |
| 108 | obj = [1, 2, 3]; |
| 109 | assertNotHoley(obj); |
| 110 | assertKind(elements_kind.fast_smi_only, obj); |
| 111 | |
| 112 | obj = new Array(); |
| 113 | assertNotHoley(obj); |
| 114 | assertKind(elements_kind.fast_smi_only, obj); |
| 115 | |
| 116 | obj = new Array(0); |
| 117 | assertNotHoley(obj); |
| 118 | assertKind(elements_kind.fast_smi_only, obj); |
| 119 | |
| 120 | obj = new Array(2); |
| 121 | assertHoley(obj); |
| 122 | assertKind(elements_kind.fast_smi_only, obj); |
| 123 | |
| 124 | obj = new Array(1,2,3); |
| 125 | assertNotHoley(obj); |
| 126 | assertKind(elements_kind.fast_smi_only, obj); |
| 127 | |
| 128 | obj = new Array(1, "hi", 2, undefined); |
| 129 | assertNotHoley(obj); |
| 130 | assertKind(elements_kind.fast, obj); |
| 131 | |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 132 | function fastliteralcase(literal, value) { |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 133 | literal[0] = value; |
| 134 | return literal; |
| 135 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 136 | |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 137 | function get_standard_literal() { |
| 138 | var literal = [1, 2, 3]; |
| 139 | return literal; |
| 140 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 141 | |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 142 | // Case: [1,2,3] as allocation site |
| 143 | obj = fastliteralcase(get_standard_literal(), 1); |
| 144 | assertKind(elements_kind.fast_smi_only, obj); |
| 145 | obj = fastliteralcase(get_standard_literal(), 1.5); |
| 146 | assertKind(elements_kind.fast_double, obj); |
| 147 | obj = fastliteralcase(get_standard_literal(), 2); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 148 | // TODO(hpayer): bring the following assert back as soon as allocation |
| 149 | // sites work again for fast literals |
| 150 | //assertKind(elements_kind.fast_double, obj); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 151 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 152 | // The test below is in a loop because arrays that live |
| 153 | // at global scope without the chance of being recreated |
| 154 | // don't have allocation site information attached. |
| 155 | for (i = 0; i < 2; i++) { |
| 156 | obj = fastliteralcase([5, 3, 2], 1.5); |
| 157 | assertKind(elements_kind.fast_double, obj); |
| 158 | obj = fastliteralcase([3, 6, 2], 1.5); |
| 159 | assertKind(elements_kind.fast_double, obj); |
| 160 | obj = fastliteralcase([2, 6, 3], 2); |
| 161 | assertKind(elements_kind.fast_smi_only, obj); |
| 162 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 163 | |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 164 | // Verify that we will not pretransition the double->fast path. |
| 165 | obj = fastliteralcase(get_standard_literal(), "elliot"); |
| 166 | assertKind(elements_kind.fast, obj); |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 167 | // This fails until we turn off optimistic transitions to the |
| 168 | // most general elements kind seen on keyed stores. It's a goal |
| 169 | // to turn it off, but for now we need it. |
| 170 | // obj = fastliteralcase(3); |
| 171 | // assertKind(elements_kind.fast_double, obj); |
| 172 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 173 | // Make sure this works in crankshafted code too. |
| 174 | %OptimizeFunctionOnNextCall(get_standard_literal); |
| 175 | get_standard_literal(); |
| 176 | obj = get_standard_literal(); |
| 177 | assertKind(elements_kind.fast_double, obj); |
| 178 | |
yangguo@chromium.org | 28381b4 | 2013-01-21 14:39:38 +0000 | [diff] [blame] | 179 | function fastliteralcase_smifast(value) { |
| 180 | var literal = [1, 2, 3, 4]; |
| 181 | literal[0] = value; |
| 182 | return literal; |
| 183 | } |
| 184 | |
| 185 | obj = fastliteralcase_smifast(1); |
| 186 | assertKind(elements_kind.fast_smi_only, obj); |
| 187 | obj = fastliteralcase_smifast("carter"); |
| 188 | assertKind(elements_kind.fast, obj); |
| 189 | obj = fastliteralcase_smifast(2); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 190 | // TODO(hpayer): bring the following assert back as soon as allocation |
| 191 | // sites work again for fast literals |
| 192 | //assertKind(elements_kind.fast, obj); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 193 | |
| 194 | if (optimize_constructed_arrays) { |
| 195 | function newarraycase_smidouble(value) { |
| 196 | var a = new Array(); |
| 197 | a[0] = value; |
| 198 | return a; |
| 199 | } |
| 200 | |
| 201 | // Case: new Array() as allocation site, smi->double |
| 202 | obj = newarraycase_smidouble(1); |
| 203 | assertKind(elements_kind.fast_smi_only, obj); |
| 204 | obj = newarraycase_smidouble(1.5); |
| 205 | assertKind(elements_kind.fast_double, obj); |
| 206 | obj = newarraycase_smidouble(2); |
| 207 | assertKind(elements_kind.fast_double, obj); |
| 208 | |
| 209 | function newarraycase_smiobj(value) { |
| 210 | var a = new Array(); |
| 211 | a[0] = value; |
| 212 | return a; |
| 213 | } |
| 214 | |
| 215 | // Case: new Array() as allocation site, smi->fast |
| 216 | obj = newarraycase_smiobj(1); |
| 217 | assertKind(elements_kind.fast_smi_only, obj); |
| 218 | obj = newarraycase_smiobj("gloria"); |
| 219 | assertKind(elements_kind.fast, obj); |
| 220 | obj = newarraycase_smiobj(2); |
| 221 | assertKind(elements_kind.fast, obj); |
| 222 | |
| 223 | function newarraycase_length_smidouble(value) { |
| 224 | var a = new Array(3); |
| 225 | a[0] = value; |
| 226 | return a; |
| 227 | } |
| 228 | |
| 229 | // Case: new Array(length) as allocation site |
| 230 | obj = newarraycase_length_smidouble(1); |
| 231 | assertKind(elements_kind.fast_smi_only, obj); |
| 232 | obj = newarraycase_length_smidouble(1.5); |
| 233 | assertKind(elements_kind.fast_double, obj); |
| 234 | obj = newarraycase_length_smidouble(2); |
| 235 | assertKind(elements_kind.fast_double, obj); |
| 236 | |
| 237 | // Try to continue the transition to fast object, but |
| 238 | // we will not pretransition from double->fast, because |
| 239 | // it may hurt performance ("poisoning"). |
| 240 | obj = newarraycase_length_smidouble("coates"); |
| 241 | assertKind(elements_kind.fast, obj); |
| 242 | obj = newarraycase_length_smidouble(2.5); |
| 243 | // However, because of optimistic transitions, we will |
| 244 | // transition to the most general kind of elements kind found, |
| 245 | // therefore I can't count on this assert yet. |
| 246 | // assertKind(elements_kind.fast_double, obj); |
| 247 | |
| 248 | function newarraycase_length_smiobj(value) { |
| 249 | var a = new Array(3); |
| 250 | a[0] = value; |
| 251 | return a; |
| 252 | } |
| 253 | |
| 254 | // Case: new Array(<length>) as allocation site, smi->fast |
| 255 | obj = newarraycase_length_smiobj(1); |
| 256 | assertKind(elements_kind.fast_smi_only, obj); |
| 257 | obj = newarraycase_length_smiobj("gloria"); |
| 258 | assertKind(elements_kind.fast, obj); |
| 259 | obj = newarraycase_length_smiobj(2); |
| 260 | assertKind(elements_kind.fast, obj); |
| 261 | |
| 262 | function newarraycase_list_smidouble(value) { |
| 263 | var a = new Array(1, 2, 3); |
| 264 | a[0] = value; |
| 265 | return a; |
| 266 | } |
| 267 | |
| 268 | obj = newarraycase_list_smidouble(1); |
| 269 | assertKind(elements_kind.fast_smi_only, obj); |
| 270 | obj = newarraycase_list_smidouble(1.5); |
| 271 | assertKind(elements_kind.fast_double, obj); |
| 272 | obj = newarraycase_list_smidouble(2); |
| 273 | assertKind(elements_kind.fast_double, obj); |
| 274 | |
| 275 | function newarraycase_list_smiobj(value) { |
| 276 | var a = new Array(4, 5, 6); |
| 277 | a[0] = value; |
| 278 | return a; |
| 279 | } |
| 280 | |
| 281 | obj = newarraycase_list_smiobj(1); |
| 282 | assertKind(elements_kind.fast_smi_only, obj); |
| 283 | obj = newarraycase_list_smiobj("coates"); |
| 284 | assertKind(elements_kind.fast, obj); |
| 285 | obj = newarraycase_list_smiobj(2); |
| 286 | assertKind(elements_kind.fast, obj); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 287 | |
| 288 | // Verify that cross context calls work |
| 289 | var realmA = Realm.current(); |
| 290 | var realmB = Realm.create(); |
| 291 | assertEquals(0, realmA); |
| 292 | assertEquals(1, realmB); |
| 293 | |
| 294 | function instanceof_check(type) { |
| 295 | assertTrue(new type() instanceof type); |
| 296 | assertTrue(new type(5) instanceof type); |
| 297 | assertTrue(new type(1,2,3) instanceof type); |
| 298 | } |
| 299 | |
| 300 | var realmBArray = Realm.eval(realmB, "Array"); |
| 301 | instanceof_check(Array); |
| 302 | instanceof_check(realmBArray); |
| 303 | %OptimizeFunctionOnNextCall(instanceof_check); |
| 304 | instanceof_check(Array); |
| 305 | instanceof_check(realmBArray); |
| 306 | assertTrue(2 != %GetOptimizationStatus(instanceof_check)); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 307 | } |
mvstanton@chromium.org | 6bec009 | 2013-01-23 13:46:53 +0000 | [diff] [blame] | 308 | } |