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
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.
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.
28 // Flags: --allow-natives-syntax --max-opt-count=100 --noalways-opt
30 // We specify max-opt-count because we opt/deopt the same function many
33 // It's nice to run this in other browsers too.
34 var standalone = false;
36 assertTrue = function(val) {
42 assertFalse = function(val) {
48 assertEquals = function(expected, val) {
49 if (expected !== val) {
54 empty_func = function(name) { }
55 assertUnoptimized = empty_func;
56 assertOptimized = empty_func;
58 optimize = empty_func;
59 clearFunctionTypeFeedback = empty_func;
60 deoptimizeFunction = empty_func;
62 optimize = function(name) {
63 %OptimizeFunctionOnNextCall(name);
65 clearFunctionTypeFeedback = function(name) {
66 %ClearFunctionTypeFeedback(name);
68 deoptimizeFunction = function(name) {
69 %DeoptimizeFunction(name);
73 function base_setter_test(create_func, index, store_value) {
76 // Testcase: setter in prototype chain
77 foo = function(a) { a[index] = store_value; }
78 var a = create_func();
80 ap.__defineSetter__(index, function() { calls++; });
87 assertEquals(0, calls);
90 assertEquals(1, calls);
93 assertEquals(2, calls);
96 // Testcase: setter added on prototype chain object already in place.
97 clearFunctionTypeFeedback(foo);
98 deoptimizeFunction(foo);
99 clearFunctionTypeFeedback(foo);
108 apap.__defineSetter__(index, function() { calls++; });
112 assertEquals(3, calls);
114 // Testcase: setter "deep" in prototype chain.
115 clearFunctionTypeFeedback(foo);
116 deoptimizeFunction(foo);
117 clearFunctionTypeFeedback(foo);
128 assertEquals(0, calls);
130 ap2.__proto__ = ap; // "sneak" in a callback.
131 // The sneak case should be caught by unoptimized code too.
132 assertUnoptimized(foo);
136 assertEquals(3, calls);
138 // Testcase: setter added after optimization (feedback is monomorphic)
139 clearFunctionTypeFeedback(foo);
140 deoptimizeFunction(foo);
141 clearFunctionTypeFeedback(foo);
152 assertOptimized(foo);
156 assertUnoptimized(foo); // map shape change should deopt foo.
157 assertEquals(1, calls);
159 // Testcase: adding additional setters to a prototype chain that already has
160 // one shouldn't deopt anything. (ie, we aren't changing the map shape).
161 clearFunctionTypeFeedback(foo);
166 bar = function(a) { a[index+1] = store_value; }
169 bar(a); // store should be generic
172 assertOptimized(bar);
173 assertEquals(0, calls);
175 ap2.__defineSetter__(index+1, function() { calls++; });
177 assertOptimized(bar);
178 assertEquals(1, calls);
181 // Verify that map transitions don't confuse us.
182 create_func_smi = function() { return [,,,,,,5]; }
183 create_func_double = function() { return [0,,3.2,,,,5.5]; }
184 create_func_fast = function() { return [,,,,,,true]; }
185 create_func_dictionary = function() { var a = []; a.length = 100000; return a; }
187 var cf = [create_func_smi,
190 create_func_dictionary];
192 var values = [3, 3.5, true];
194 for(var c = 0; c < cf.length; c++) {
195 for(var s = 0; s < values.length; s++) {
196 base_setter_test(cf[c], 0, values[s]);
197 base_setter_test(cf[c], 1, values[s]);