| OLD | NEW |
| (Empty) | |
| 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 |
| 29 // Flags: --track-allocation-sites --noalways-opt |
| 30 |
| 31 // Test element kind of objects. |
| 32 // Since --smi-only-arrays affects builtins, its default setting at compile |
| 33 // time sticks if built with snapshot. If --smi-only-arrays is deactivated |
| 34 // by default, only a no-snapshot build actually has smi-only arrays enabled |
| 35 // in this test case. Depending on whether smi-only arrays are actually |
| 36 // enabled, this test takes the appropriate code path to check smi-only arrays. |
| 37 |
| 38 // support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8)); |
| 39 support_smi_only_arrays = true; |
| 40 optimize_constructed_arrays = true; |
| 41 |
| 42 if (support_smi_only_arrays) { |
| 43 print("Tests include smi-only arrays."); |
| 44 } else { |
| 45 print("Tests do NOT include smi-only arrays."); |
| 46 } |
| 47 |
| 48 if (optimize_constructed_arrays) { |
| 49 print("Tests include constructed array optimizations."); |
| 50 } else { |
| 51 print("Tests do NOT include constructed array optimizations."); |
| 52 } |
| 53 |
| 54 var elements_kind = { |
| 55 fast_smi_only : 'fast smi only elements', |
| 56 fast : 'fast elements', |
| 57 fast_double : 'fast double elements', |
| 58 dictionary : 'dictionary elements', |
| 59 external_byte : 'external byte elements', |
| 60 external_unsigned_byte : 'external unsigned byte elements', |
| 61 external_short : 'external short elements', |
| 62 external_unsigned_short : 'external unsigned short elements', |
| 63 external_int : 'external int elements', |
| 64 external_unsigned_int : 'external unsigned int elements', |
| 65 external_float : 'external float elements', |
| 66 external_double : 'external double elements', |
| 67 external_pixel : 'external pixel elements' |
| 68 } |
| 69 |
| 70 function getKind(obj) { |
| 71 if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only; |
| 72 if (%HasFastObjectElements(obj)) return elements_kind.fast; |
| 73 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double; |
| 74 if (%HasDictionaryElements(obj)) return elements_kind.dictionary; |
| 75 } |
| 76 |
| 77 function isHoley(obj) { |
| 78 if (%HasFastHoleyElements(obj)) return true; |
| 79 return false; |
| 80 } |
| 81 |
| 82 function assertKind(expected, obj, name_opt) { |
| 83 if (!support_smi_only_arrays && |
| 84 expected == elements_kind.fast_smi_only) { |
| 85 expected = elements_kind.fast; |
| 86 } |
| 87 assertEquals(expected, getKind(obj), name_opt); |
| 88 } |
| 89 |
| 90 if (support_smi_only_arrays && optimize_constructed_arrays) { |
| 91 |
| 92 // Verify that basic elements kind feedback works for non-constructor |
| 93 // array calls (as long as the call is made through an IC, and not |
| 94 // a CallStub). |
| 95 (function (){ |
| 96 function create0() { |
| 97 return Array(); |
| 98 } |
| 99 |
| 100 // Calls through ICs need warm up through uninitialized, then |
| 101 // premonomorphic first. |
| 102 create0(); |
| 103 create0(); |
| 104 a = create0(); |
| 105 assertKind(elements_kind.fast_smi_only, a); |
| 106 a[0] = 3.5; |
| 107 b = create0(); |
| 108 assertKind(elements_kind.fast_double, b); |
| 109 |
| 110 function create1(arg) { |
| 111 return Array(arg); |
| 112 } |
| 113 |
| 114 create1(0); |
| 115 create1(0); |
| 116 a = create1(0); |
| 117 assertFalse(isHoley(a)); |
| 118 assertKind(elements_kind.fast_smi_only, a); |
| 119 a[0] = "hello"; |
| 120 b = create1(10); |
| 121 assertTrue(isHoley(b)); |
| 122 assertKind(elements_kind.fast, b); |
| 123 |
| 124 a = create1(100000); |
| 125 assertKind(elements_kind.dictionary, a); |
| 126 |
| 127 function create3(arg1, arg2, arg3) { |
| 128 return Array(arg1, arg2, arg3); |
| 129 } |
| 130 |
| 131 create3(); |
| 132 create3(); |
| 133 a = create3(1,2,3); |
| 134 a[0] = 3.5; |
| 135 b = create3(1,2,3); |
| 136 assertKind(elements_kind.fast_double, b); |
| 137 assertFalse(isHoley(b)); |
| 138 })(); |
| 139 |
| 140 |
| 141 // Verify that keyed calls work |
| 142 (function (){ |
| 143 function create0(name) { |
| 144 return this[name](); |
| 145 } |
| 146 |
| 147 name = "Array"; |
| 148 create0(name); |
| 149 create0(name); |
| 150 a = create0(name); |
| 151 a[0] = 3.5; |
| 152 b = create0(name); |
| 153 assertKind(elements_kind.fast_double, b); |
| 154 })(); |
| 155 |
| 156 |
| 157 // Verify that the IC can't be spoofed by patching |
| 158 (function (){ |
| 159 function create0() { |
| 160 return Array(); |
| 161 } |
| 162 |
| 163 create0(); |
| 164 create0(); |
| 165 a = create0(); |
| 166 assertKind(elements_kind.fast_smi_only, a); |
| 167 var oldArray = this.Array; |
| 168 this.Array = function() { return ["hi"]; }; |
| 169 b = create0(); |
| 170 assertEquals(["hi"], b); |
| 171 this.Array = oldArray; |
| 172 })(); |
| 173 |
| 174 // Verify that calls are still made through an IC after crankshaft, |
| 175 // though the type information is reset. |
| 176 // TODO(mvstanton): instead, consume the type feedback gathered up |
| 177 // until crankshaft time. |
| 178 (function (){ |
| 179 function create0() { |
| 180 return Array(); |
| 181 } |
| 182 |
| 183 create0(); |
| 184 create0(); |
| 185 a = create0(); |
| 186 a[0] = 3.5; |
| 187 %OptimizeFunctionOnNextCall(create0); |
| 188 create0(); |
| 189 create0(); |
| 190 b = create0(); |
| 191 assertKind(elements_kind.fast_smi_only, b); |
| 192 b[0] = 3.5; |
| 193 c = create0(); |
| 194 assertKind(elements_kind.fast_double, c); |
| 195 assertTrue(2 != %GetOptimizationStatus(create0)); |
| 196 })(); |
| 197 |
| 198 |
| 199 // Verify that cross context calls work |
| 200 (function (){ |
| 201 var realmA = Realm.current(); |
| 202 var realmB = Realm.create(); |
| 203 assertEquals(0, realmA); |
| 204 assertEquals(1, realmB); |
| 205 |
| 206 function instanceof_check(type) { |
| 207 assertTrue(type() instanceof type); |
| 208 assertTrue(type(5) instanceof type); |
| 209 assertTrue(type(1,2,3) instanceof type); |
| 210 } |
| 211 |
| 212 var realmBArray = Realm.eval(realmB, "Array"); |
| 213 instanceof_check(Array); |
| 214 instanceof_check(Array); |
| 215 instanceof_check(Array); |
| 216 instanceof_check(realmBArray); |
| 217 instanceof_check(realmBArray); |
| 218 instanceof_check(realmBArray); |
| 219 })(); |
| 220 } |
| OLD | NEW |