OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --allow-natives-syntax --harmony-tailcalls |
| 6 "use strict"; |
| 7 |
| 8 Error.prepareStackTrace = (e,s) => s; |
| 9 |
| 10 function CheckStackTrace(expected) { |
| 11 var stack = (new Error()).stack; |
| 12 assertEquals("CheckStackTrace", stack[0].getFunctionName()); |
| 13 for (var i = 0; i < expected.length; i++) { |
| 14 assertEquals(expected[i].name, stack[i + 1].getFunctionName()); |
| 15 } |
| 16 } |
| 17 |
| 18 |
| 19 // Tail call when caller does not have an arguments adaptor frame. |
| 20 (function test() { |
| 21 // Caller and callee have same number of arguments. |
| 22 function f1(a) { |
| 23 CheckStackTrace([f1, test]); |
| 24 return 10 + a; |
| 25 } |
| 26 function g1(a) { return f1(2); } |
| 27 assertEquals(12, g1(1)); |
| 28 |
| 29 // Caller has more arguments than callee. |
| 30 function f2(a) { |
| 31 CheckStackTrace([f2, test]); |
| 32 return 10 + a; |
| 33 } |
| 34 function g2(a, b, c) { return f2(2); } |
| 35 assertEquals(12, g2(1, 2, 3)); |
| 36 |
| 37 // Caller has less arguments than callee. |
| 38 function f3(a, b, c) { |
| 39 CheckStackTrace([f3, test]); |
| 40 return 10 + a + b + c; |
| 41 } |
| 42 function g3(a) { return f3(2, 3, 4); } |
| 43 assertEquals(19, g3(1)); |
| 44 |
| 45 // Callee has arguments adaptor frame. |
| 46 function f4(a, b, c) { |
| 47 CheckStackTrace([f4, test]); |
| 48 return 10 + a; |
| 49 } |
| 50 function g4(a) { return f4(2); } |
| 51 assertEquals(12, g4(1)); |
| 52 })(); |
| 53 |
| 54 |
| 55 // Tail call when caller has an arguments adaptor frame. |
| 56 (function test() { |
| 57 // Caller and callee have same number of arguments. |
| 58 function f1(a) { |
| 59 CheckStackTrace([f1, test]); |
| 60 return 10 + a; |
| 61 } |
| 62 function g1(a) { return f1(2); } |
| 63 assertEquals(12, g1()); |
| 64 |
| 65 // Caller has more arguments than callee. |
| 66 function f2(a) { |
| 67 CheckStackTrace([f2, test]); |
| 68 return 10 + a; |
| 69 } |
| 70 function g2(a, b, c) { return f2(2); } |
| 71 assertEquals(12, g2()); |
| 72 |
| 73 // Caller has less arguments than callee. |
| 74 function f3(a, b, c) { |
| 75 CheckStackTrace([f3, test]); |
| 76 return 10 + a + b + c; |
| 77 } |
| 78 function g3(a) { return f3(2, 3, 4); } |
| 79 assertEquals(19, g3()); |
| 80 |
| 81 // Callee has arguments adaptor frame. |
| 82 function f4(a, b, c) { |
| 83 CheckStackTrace([f4, test]); |
| 84 return 10 + a; |
| 85 } |
| 86 function g4(a) { return f4(2); } |
| 87 assertEquals(12, g4()); |
| 88 })(); |
| 89 |
| 90 |
| 91 // Tail call bound function when caller does not have an arguments |
| 92 // adaptor frame. |
| 93 (function test() { |
| 94 // Caller and callee have same number of arguments. |
| 95 function f1(a) { |
| 96 assertEquals(153, this.a); |
| 97 CheckStackTrace([f1, test]); |
| 98 return 10 + a; |
| 99 } |
| 100 var b1 = f1.bind({a: 153}); |
| 101 function g1(a) { return b1(2); } |
| 102 assertEquals(12, g1(1)); |
| 103 |
| 104 // Caller has more arguments than callee. |
| 105 function f2(a) { |
| 106 assertEquals(153, this.a); |
| 107 CheckStackTrace([f2, test]); |
| 108 return 10 + a; |
| 109 } |
| 110 var b2 = f2.bind({a: 153}); |
| 111 function g2(a, b, c) { return b2(2); } |
| 112 assertEquals(12, g2(1, 2, 3)); |
| 113 |
| 114 // Caller has less arguments than callee. |
| 115 function f3(a, b, c) { |
| 116 assertEquals(153, this.a); |
| 117 CheckStackTrace([f3, test]); |
| 118 return 10 + a + b + c; |
| 119 } |
| 120 var b3 = f3.bind({a: 153}); |
| 121 function g3(a) { return b3(2, 3, 4); } |
| 122 assertEquals(19, g3(1)); |
| 123 |
| 124 // Callee has arguments adaptor frame. |
| 125 function f4(a, b, c) { |
| 126 assertEquals(153, this.a); |
| 127 CheckStackTrace([f4, test]); |
| 128 return 10 + a; |
| 129 } |
| 130 var b4 = f4.bind({a: 153}); |
| 131 function g4(a) { return b4(2); } |
| 132 assertEquals(12, g4(1)); |
| 133 })(); |
| 134 |
| 135 |
| 136 // Tail call bound function when caller has an arguments adaptor frame. |
| 137 (function test() { |
| 138 // Caller and callee have same number of arguments. |
| 139 function f1(a) { |
| 140 assertEquals(153, this.a); |
| 141 CheckStackTrace([f1, test]); |
| 142 return 10 + a; |
| 143 } |
| 144 var b1 = f1.bind({a: 153}); |
| 145 function g1(a) { return b1(2); } |
| 146 assertEquals(12, g1()); |
| 147 |
| 148 // Caller has more arguments than callee. |
| 149 function f2(a) { |
| 150 assertEquals(153, this.a); |
| 151 CheckStackTrace([f2, test]); |
| 152 return 10 + a; |
| 153 } |
| 154 var b2 = f2.bind({a: 153}); |
| 155 function g2(a, b, c) { return b2(2); } |
| 156 assertEquals(12, g2()); |
| 157 |
| 158 // Caller has less arguments than callee. |
| 159 function f3(a, b, c) { |
| 160 assertEquals(153, this.a); |
| 161 CheckStackTrace([f3, test]); |
| 162 return 10 + a + b + c; |
| 163 } |
| 164 var b3 = f3.bind({a: 153}); |
| 165 function g3(a) { return b3(2, 3, 4); } |
| 166 assertEquals(19, g3()); |
| 167 |
| 168 // Callee has arguments adaptor frame. |
| 169 function f4(a, b, c) { |
| 170 assertEquals(153, this.a); |
| 171 CheckStackTrace([f4, test]); |
| 172 return 10 + a; |
| 173 } |
| 174 var b4 = f4.bind({a: 153}); |
| 175 function g4(a) { return b4(2); } |
| 176 assertEquals(12, g4()); |
| 177 })(); |
OLD | NEW |