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