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 |
| 29 |
| 30 // It's nice to run this in Firefox too. |
| 31 var standalone = false; |
| 32 if (standalone) { |
| 33 assertTrue = function(val) { |
| 34 if (val != true) { |
| 35 print("FAILURE"); |
| 36 } |
| 37 } |
| 38 |
| 39 assertFalse = function(val) { |
| 40 if (val != false) { |
| 41 print("FAILURE"); |
| 42 } |
| 43 } |
| 44 |
| 45 optimize = function(name) { } |
| 46 } else { |
| 47 optimize = function(name) { |
| 48 %OptimizeFunctionOnNextCall(name); |
| 49 } |
| 50 } |
| 51 |
| 52 var calls = 0; |
| 53 |
| 54 // Verify that we can't "sneak in" an element setter by modifying the prototype |
| 55 // chain. |
| 56 (function() { |
| 57 function run(array) { |
| 58 var runlength = 10; |
| 59 calls = 0; |
| 60 for (var i = 0; i < runlength; ++i) { |
| 61 array[0] = 3; |
| 62 } |
| 63 return calls == runlength; |
| 64 } |
| 65 |
| 66 var a_prototype = []; |
| 67 a_prototype.__defineSetter__(0, function(val) { |
| 68 calls++; |
| 69 }); |
| 70 |
| 71 // No callback installed. Drive the setter to monomorphic. |
| 72 var a = [, 3]; |
| 73 assertFalse(run(a)); |
| 74 run(a); |
| 75 run(a); |
| 76 delete a[0]; |
| 77 a.__proto__ = a_prototype; |
| 78 // Altering the prototype chain causes a miss and then we go generic. |
| 79 assertTrue(run(a)); |
| 80 assertTrue(run(a)); |
| 81 |
| 82 |
| 83 // Now throw away the IC information by defining a throw-away setter. And try |
| 84 // to crankshaft before we discover we shouldn't be monomorphic. |
| 85 a = []; |
| 86 a.__defineSetter__(0, function() { }); |
| 87 |
| 88 a = [,3]; |
| 89 run(a); |
| 90 run(a); |
| 91 run(a); // keyed store IC is monomorphic |
| 92 optimize(run); |
| 93 run(a); |
| 94 delete a[0]; |
| 95 a.__proto__ = a_prototype; |
| 96 assertTrue(run(a)); |
| 97 assertTrue(run(a)); |
| 98 })(); |
| 99 |
| 100 |
| 101 Array.prototype.__defineSetter__(0, function(val) { |
| 102 calls++; |
| 103 }); |
| 104 |
| 105 |
| 106 (function() { |
| 107 function run(array, index, value) { |
| 108 var runlength = 10; |
| 109 calls = 0; |
| 110 for (var i = 0; i < runlength; ++i) { |
| 111 array[index] = value; |
| 112 } |
| 113 return calls == runlength; |
| 114 } |
| 115 |
| 116 // Try "warming up" the ic into a monomorphic state to see if we can trick it |
| 117 // into ignoring the callback. The object map doesn't have the setter callback
. |
| 118 run({}, 0, true); |
| 119 run({}, 0, true); |
| 120 run({}, 0, true); |
| 121 |
| 122 // Make sure the setter is called if the element is not defined, |
| 123 // but is not called if the element is defined. |
| 124 var typeArray = [1,2.5,true]; |
| 125 for (var types = 0; types < 3; types++) { |
| 126 value = typeArray[types]; |
| 127 for (var i = 0; i < 2; i++) { |
| 128 assertTrue(run([], 0, value)); // undefined |
| 129 assertFalse(run([0], 0, value)); // defined. |
| 130 assertTrue(run([,3], 0, value)); // undefined |
| 131 assertTrue(run(new Array(10), 0, value)); // undefined |
| 132 // "sneak" an element in with shift. |
| 133 o = []; o[1] = 0; o.shift(1); |
| 134 assertFalse(run(o, 0, value)); |
| 135 optimize(run); |
| 136 run([], 0, value); |
| 137 } |
| 138 } |
| 139 |
| 140 // Make sure we can turn it off. |
| 141 delete Array.prototype[0]; |
| 142 assertFalse(run([], 0, true)); |
| 143 })(); |
| 144 |
| 145 Object.prototype.__defineSetter__(0, function(val) { |
| 146 calls++; |
| 147 }); |
| 148 |
| 149 (function() { |
| 150 function run(obj) { |
| 151 var runlength = 10; |
| 152 calls = 0; |
| 153 for (var i = 0; i < runlength; ++i) { |
| 154 obj[0] = true; |
| 155 } |
| 156 return calls == runlength; |
| 157 } |
| 158 |
| 159 for (var i = 0; i < 2; i++) { |
| 160 assertTrue(run({})); |
| 161 o = { foo: 3 }; |
| 162 assertTrue(run(o)); |
| 163 o = {}; o[1] = 3; |
| 164 assertTrue(run(o)); |
| 165 o = { 0: 0 }; |
| 166 assertFalse(run(o)); |
| 167 delete o[0]; |
| 168 assertTrue(run(o)); |
| 169 optimize(run); |
| 170 run([]); |
| 171 } |
| 172 |
| 173 delete Object.prototype[0]; |
| 174 assertFalse(run({})); |
| 175 })(); |
OLD | NEW |