OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --allow-natives-syntax --nostress-opt --turbo | 5 // Flags: --allow-natives-syntax --nostress-opt --turbo |
6 // Flags: --nonative-context-specialization | 6 // Flags: --nonative-context-specialization |
7 | 7 |
8 var p0 = new Object(); | 8 var p0 = new Object(); |
9 var p1 = new Object(); | 9 var p1 = new Object(); |
10 var p2 = new Object(); | 10 var p2 = new Object(); |
11 | 11 |
12 // Ensure 1 parameter passed straight-through is handled correctly | 12 // Ensure 1 parameter passed straight-through is handled correctly |
13 var count1 = 100000; | 13 var count1 = 100000; |
14 tailee1 = function() { | 14 tailee1 = function() { |
15 "use strict"; | 15 "use strict"; |
16 if (count1-- == 0) { | 16 if (count1-- == 0) { |
17 return this; | 17 return this; |
18 } | 18 } |
19 return %_Call(tailee1, this); | 19 return %_TailCall(tailee1, this); |
20 }; | 20 }; |
21 | 21 |
22 %OptimizeFunctionOnNextCall(tailee1); | 22 %OptimizeFunctionOnNextCall(tailee1); |
23 assertEquals(p0, tailee1.call(p0)); | 23 assertEquals(p0, tailee1.call(p0)); |
24 | 24 |
25 // Ensure 2 parameters passed straight-through trigger a tail call are handled | 25 // Ensure 2 parameters passed straight-through trigger a tail call are handled |
26 // correctly and don't cause a stack overflow. | 26 // correctly and don't cause a stack overflow. |
27 var count2 = 100000; | 27 var count2 = 100000; |
28 tailee2 = function(px) { | 28 tailee2 = function(px) { |
29 "use strict"; | 29 "use strict"; |
30 assertEquals(p2, px); | 30 assertEquals(p2, px); |
31 assertEquals(p1, this); | 31 assertEquals(p1, this); |
32 count2 = ((count2 | 0) - 1) | 0; | 32 count2 = ((count2 | 0) - 1) | 0; |
33 if ((count2 | 0) === 0) { | 33 if ((count2 | 0) === 0) { |
34 return this; | 34 return this; |
35 } | 35 } |
36 return %_Call(tailee2, this, px); | 36 return %_TailCall(tailee2, this, px); |
37 }; | 37 }; |
38 | 38 |
39 %OptimizeFunctionOnNextCall(tailee2); | 39 %OptimizeFunctionOnNextCall(tailee2); |
40 assertEquals(p1, tailee2.call(p1, p2)); | 40 assertEquals(p1, tailee2.call(p1, p2)); |
41 | 41 |
42 // Ensure swapped 2 parameters don't trigger a tail call (parameter swizzling | 42 // Ensure swapped 2 parameters trigger a tail call and do the appropriate |
43 // for the tail call isn't supported yet). | 43 // parameters swapping |
44 var count3 = 100000; | 44 var count3 = 999999; |
45 tailee3 = function(px) { | 45 tailee3 = function(px) { |
46 "use strict"; | 46 "use strict"; |
47 if (count3-- == 0) { | 47 if (count3-- == 0) { |
48 return this; | 48 return this; |
49 } | 49 } |
50 return %_Call(tailee3, px, this); | 50 return %_TailCall(tailee3, px, this); |
51 }; | 51 }; |
52 | 52 |
53 %OptimizeFunctionOnNextCall(tailee3); | 53 %OptimizeFunctionOnNextCall(tailee3); |
54 assertThrows(function() { tailee3.call(p1, p2); }); | 54 assertEquals(p2, tailee3.call(p1, p2)); |
55 | 55 |
56 // Ensure too many parameters defeats the tail call optimization (currently | 56 // Ensure too many parameters defeats the tail call optimization (currently |
57 // unsupported). | 57 // unsupported). |
58 var count4 = 1000000; | 58 var count4 = 1000000; |
59 tailee4 = function(px) { | 59 tailee4 = function(px) { |
60 "use strict"; | 60 "use strict"; |
61 if (count4-- == 0) { | 61 if (count4-- == 0) { |
62 return this; | 62 return this; |
63 } | 63 } |
64 return %_Call(tailee4, this, px, undefined); | 64 return %_TailCall(tailee4, this, px, undefined); |
65 }; | 65 }; |
66 | 66 |
67 %OptimizeFunctionOnNextCall(tailee4); | 67 %OptimizeFunctionOnNextCall(tailee4); |
68 assertThrows(function() { tailee4.call(p1, p2); }); | 68 assertThrows(function() { tailee4.call(p1, p2); }); |
69 | 69 |
70 // Ensure too few parameters defeats the tail call optimization (currently | 70 // Ensure that calling the arguments adapter defeats the tail call optimization. |
71 // unsupported). | |
72 var count5 = 1000000; | 71 var count5 = 1000000; |
73 tailee5 = function(px) { | 72 tailee5 = function(px) { |
74 "use strict"; | 73 "use strict"; |
75 if (count5-- == 0) { | 74 if (count5-- == 0) { |
76 return this; | 75 return this; |
77 } | 76 } |
78 return %_Call(tailee5, this); | 77 return %_TailCall(tailee5, this); |
79 }; | 78 }; |
80 | 79 |
81 %OptimizeFunctionOnNextCall(tailee5); | 80 %OptimizeFunctionOnNextCall(tailee5); |
82 assertThrows(function() { tailee5.call(p1, p2); }); | 81 assertThrows(function() { tailee5.call(p1, p2); }); |
| 82 |
| 83 // Ensure tail calls with fewer stack parameters properly re-arranges the stack. |
| 84 tailee6 = function(px) { |
| 85 return px; |
| 86 } |
| 87 |
| 88 tailee7 = function(px, py, pz, pa, pb, pc) { |
| 89 "use strict"; |
| 90 return %_TailCall(tailee6, this, pc); |
| 91 }; |
| 92 |
| 93 %OptimizeFunctionOnNextCall(tailee6); |
| 94 %OptimizeFunctionOnNextCall(tailee7); |
| 95 assertEquals(110, tailee7.call(null, 15, 16, 17, 18, 0, 110)); |
| 96 |
| 97 tailee8 = function(px, py, pz, pa, pb) { |
| 98 return pb + pz + px; |
| 99 } |
| 100 |
| 101 tailee9 = function(px, py, pz, pa, pb, pc) { |
| 102 "use strict"; |
| 103 return %_TailCall(tailee8, this, pb, py, px, pa, pz); |
| 104 }; |
| 105 |
| 106 %OptimizeFunctionOnNextCall(tailee8); |
| 107 %OptimizeFunctionOnNextCall(tailee9); |
| 108 assertEquals(32, tailee9.call(null, 15, 16, 17, 18, 0, 110)); |
OLD | NEW |