OLD | NEW |
(Empty) | |
| 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 --max-opt-count=100 --noalways-opt |
| 29 |
| 30 // We specify max-opt-count because we opt/deopt the same function many |
| 31 // times. |
| 32 |
| 33 // It's nice to run this in other browsers too. |
| 34 var standalone = false; |
| 35 if (standalone) { |
| 36 assertTrue = function(val) { |
| 37 if (val != true) { |
| 38 print("FAILURE"); |
| 39 } |
| 40 } |
| 41 |
| 42 assertFalse = function(val) { |
| 43 if (val != false) { |
| 44 print("FAILURE"); |
| 45 } |
| 46 } |
| 47 |
| 48 assertEquals = function(expected, val) { |
| 49 if (expected !== val) { |
| 50 print("FAILURE"); |
| 51 } |
| 52 } |
| 53 |
| 54 empty_func = function(name) { } |
| 55 assertUnoptimized = empty_func; |
| 56 assertOptimized = empty_func; |
| 57 |
| 58 optimize = empty_func; |
| 59 clearFunctionTypeFeedback = empty_func; |
| 60 deoptimizeFunction = empty_func; |
| 61 } else { |
| 62 optimize = function(name) { |
| 63 %OptimizeFunctionOnNextCall(name); |
| 64 } |
| 65 clearFunctionTypeFeedback = function(name) { |
| 66 %ClearFunctionTypeFeedback(name); |
| 67 } |
| 68 deoptimizeFunction = function(name) { |
| 69 %DeoptimizeFunction(name); |
| 70 } |
| 71 } |
| 72 |
| 73 function base_setter_test(create_func, index, store_value) { |
| 74 var calls = 0; |
| 75 |
| 76 // Testcase: setter in prototype chain |
| 77 foo = function(a) { a[index] = store_value; } |
| 78 var a = create_func(); |
| 79 var ap = []; |
| 80 ap.__defineSetter__(index, function() { calls++; }); |
| 81 |
| 82 foo(a); |
| 83 foo(a); |
| 84 foo(a); |
| 85 delete a[index]; |
| 86 |
| 87 assertEquals(0, calls); |
| 88 a.__proto__ = ap; |
| 89 foo(a); |
| 90 assertEquals(1, calls); |
| 91 optimize(foo); |
| 92 foo(a); |
| 93 assertEquals(2, calls); |
| 94 assertOptimized(foo); |
| 95 |
| 96 // Testcase: setter "deep" in prototype chain. |
| 97 clearFunctionTypeFeedback(foo); |
| 98 deoptimizeFunction(foo); |
| 99 clearFunctionTypeFeedback(foo); |
| 100 calls = 0; |
| 101 |
| 102 a = create_func(); |
| 103 var ap2 = []; |
| 104 a.__proto__ = ap2; |
| 105 foo(a); |
| 106 foo(a); |
| 107 foo(a); |
| 108 delete a[index]; |
| 109 |
| 110 assertEquals(0, calls); |
| 111 |
| 112 ap2.__proto__ = ap; // "sneak" in a callback. |
| 113 // The sneak case should be caught by unoptimized code too. |
| 114 assertUnoptimized(foo); |
| 115 foo(a); |
| 116 foo(a); |
| 117 foo(a); |
| 118 assertEquals(3, calls); |
| 119 |
| 120 // Testcase: setter added after optimization (feedback is monomorphic) |
| 121 clearFunctionTypeFeedback(foo); |
| 122 deoptimizeFunction(foo); |
| 123 clearFunctionTypeFeedback(foo); |
| 124 calls = 0; |
| 125 |
| 126 a = create_func(); |
| 127 ap2 = []; |
| 128 a.__proto__ = ap2; |
| 129 foo(a); |
| 130 foo(a); |
| 131 foo(a); |
| 132 optimize(foo); |
| 133 foo(a); |
| 134 assertOptimized(foo); |
| 135 delete a[index]; |
| 136 ap2.__proto__ = ap; |
| 137 foo(a); |
| 138 assertUnoptimized(foo); // map shape change should deopt foo. |
| 139 assertEquals(1, calls); |
| 140 |
| 141 // Testcase: adding additional setters to a prototype chain that already has |
| 142 // one shouldn't deopt anything. (ie, we aren't changing the map shape). |
| 143 clearFunctionTypeFeedback(foo); |
| 144 calls = 0; |
| 145 |
| 146 a = create_func(); |
| 147 a.__proto__ = ap2; |
| 148 bar = function(a) { a[index+1] = store_value; } |
| 149 bar(a); |
| 150 bar(a); |
| 151 bar(a); // store should be generic |
| 152 optimize(bar); |
| 153 bar(a); |
| 154 assertOptimized(bar); |
| 155 assertEquals(0, calls); |
| 156 delete a[index+1]; |
| 157 ap2.__defineSetter__(index+1, function() { calls++; }); |
| 158 bar(a); |
| 159 assertOptimized(bar); |
| 160 assertEquals(1, calls); |
| 161 } |
| 162 |
| 163 // Verify that map transitions don't confuse us. |
| 164 create_func_smi = function() { return [,,,,,,5]; } |
| 165 create_func_double = function() { return [0,,3.2,,,,5.5]; } |
| 166 create_func_fast = function() { return [,,,,,,true]; } |
| 167 create_func_dictionary = function() { var a = []; a.length = 100000; return a; } |
| 168 |
| 169 var cf = [create_func_smi, |
| 170 create_func_double, |
| 171 create_func_fast, |
| 172 create_func_dictionary]; |
| 173 |
| 174 var values = [3, 3.5, true]; |
| 175 |
| 176 for(var c = 0; c < 3; c++) { |
| 177 for(var s = 0; s < 3; s++) { |
| 178 base_setter_test(cf[c], 0, values[s]); |
| 179 base_setter_test(cf[c], 1, values[s]); |
| 180 } |
| 181 } |
OLD | NEW |