2016-01-26 11:07:15 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Flags: --allow-natives-syntax --harmony-tailcalls --stack-size=100
|
2016-03-07 14:33:54 +00:00
|
|
|
// TODO(v8:4698), TODO(ishell): support these cases.
|
2016-03-08 00:11:57 +00:00
|
|
|
// Flags: --nostress-opt
|
2016-01-26 11:07:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Tail calls work only in strict mode.
|
|
|
|
//
|
|
|
|
(function() {
|
|
|
|
function f(n) {
|
|
|
|
if (n <= 0) {
|
2016-03-07 14:33:54 +00:00
|
|
|
return "foo";
|
2016-01-26 11:07:15 +00:00
|
|
|
}
|
|
|
|
return f(n - 1);
|
|
|
|
}
|
2016-02-17 15:19:02 +00:00
|
|
|
assertThrows(()=>{ f(1e5) });
|
2016-02-11 10:26:39 +00:00
|
|
|
%OptimizeFunctionOnNextCall(f);
|
2016-02-17 15:19:02 +00:00
|
|
|
assertThrows(()=>{ f(1e5) });
|
2016-01-26 11:07:15 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Tail call normal functions.
|
|
|
|
//
|
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
function f(n) {
|
|
|
|
if (n <= 0) {
|
2016-03-07 14:33:54 +00:00
|
|
|
return "foo";
|
2016-01-26 11:07:15 +00:00
|
|
|
}
|
|
|
|
return f(n - 1);
|
|
|
|
}
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
2016-02-11 10:26:39 +00:00
|
|
|
%OptimizeFunctionOnNextCall(f);
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
2016-01-26 11:07:15 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-03-07 14:33:54 +00:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
function f(n) {
|
|
|
|
if (n <= 0) {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
return f(n - 1, 42); // Call with arguments adaptor.
|
|
|
|
}
|
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
%OptimizeFunctionOnNextCall(f);
|
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-01-26 11:07:15 +00:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
function f(n){
|
|
|
|
if (n <= 0) {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
return g(n - 1);
|
|
|
|
}
|
|
|
|
function g(n){
|
|
|
|
if (n <= 0) {
|
|
|
|
return "bar";
|
|
|
|
}
|
|
|
|
return f(n - 1);
|
|
|
|
}
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
assertEquals("bar", f(1e5 + 1));
|
2016-02-11 10:26:39 +00:00
|
|
|
%OptimizeFunctionOnNextCall(f);
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
assertEquals("bar", f(1e5 + 1));
|
2016-01-26 11:07:15 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-03-07 14:33:54 +00:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
function f(n){
|
|
|
|
if (n <= 0) {
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
return g(n - 1, 42); // Call with arguments adaptor.
|
|
|
|
}
|
|
|
|
function g(n){
|
|
|
|
if (n <= 0) {
|
|
|
|
return "bar";
|
|
|
|
}
|
|
|
|
return f(n - 1, 42); // Call with arguments adaptor.
|
|
|
|
}
|
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
assertEquals("bar", f(1e5 + 1));
|
|
|
|
%OptimizeFunctionOnNextCall(f);
|
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
assertEquals("bar", f(1e5 + 1));
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-01-26 11:07:15 +00:00
|
|
|
//
|
|
|
|
// Tail call bound functions.
|
|
|
|
//
|
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
function f0(n) {
|
|
|
|
if (n <= 0) {
|
|
|
|
return "foo";
|
|
|
|
}
|
2016-02-11 10:26:39 +00:00
|
|
|
return f_bound(n - 1);
|
|
|
|
}
|
|
|
|
var f_bound = f0.bind({});
|
|
|
|
function f(n) {
|
|
|
|
return f_bound(n);
|
2016-01-26 11:07:15 +00:00
|
|
|
}
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
2016-02-11 10:26:39 +00:00
|
|
|
%OptimizeFunctionOnNextCall(f);
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
2016-01-26 11:07:15 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
"use strict";
|
|
|
|
function f0(n){
|
|
|
|
if (n <= 0) {
|
|
|
|
return "foo";
|
|
|
|
}
|
2016-02-11 10:26:39 +00:00
|
|
|
return g_bound(n - 1);
|
2016-01-26 11:07:15 +00:00
|
|
|
}
|
|
|
|
function g0(n){
|
|
|
|
if (n <= 0) {
|
|
|
|
return "bar";
|
|
|
|
}
|
2016-02-11 10:26:39 +00:00
|
|
|
return f_bound(n - 1);
|
2016-01-26 11:07:15 +00:00
|
|
|
}
|
2016-02-11 10:26:39 +00:00
|
|
|
var f_bound = f0.bind({});
|
|
|
|
var g_bound = g0.bind({});
|
|
|
|
function f(n) {
|
|
|
|
return f_bound(n);
|
|
|
|
}
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
assertEquals("bar", f(1e5 + 1));
|
2016-02-11 10:26:39 +00:00
|
|
|
%OptimizeFunctionOnNextCall(f);
|
2016-02-17 15:19:02 +00:00
|
|
|
assertEquals("foo", f(1e5));
|
|
|
|
assertEquals("bar", f(1e5 + 1));
|
2016-01-26 11:07:15 +00:00
|
|
|
})();
|