[英]Does ES6 Tail Call Optimization Cover Generators?
Does ES6's support for tail call optimization cover tail calls in generators? ES6对尾调用优化的支持是否涵盖了生成器中的尾调用?
Suppose I have this generator for integers >= 0: 假设我有一个整数> = 0的生成器:
var nums = function* (n) {
n = n || 0;
yield n;
yield* nums(n + 1);
};
Currently, in Chrome and Firefox, it adds a stack level with each recursive call and eventually runs into a "maximum call stack size exceeded" error. 目前,在Chrome和Firefox中,它会在每次递归调用时添加堆栈级别,并最终遇到“超出最大调用堆栈大小”错误。 Will this still occur once ES6 is fully implemented?
一旦ES6完全实现,这仍然会发生吗?
(I know I can write the above generator iteratively and not run into the error. I'm just curious about whether TCO will take care of recursively defined generators.) (我知道我可以迭代地编写上面的生成器而不会遇到错误。我只是想知道TCO是否会处理递归定义的生成器。)
When a function call is made, according to the section Function call evaluation , 当进行函数调用时,根据函数调用评估部分,
- Let tailCall be IsInTailPosition( thisCall )
让tailCall成为IsInTailPosition( thisCall )
- Return ?
回来? EvaluateCall( func, ref, arguments, tailCall )
EvaluateCall( func,ref,arguments,tailCall )
The call will be evaluated based on the IsInTailPosition
's result. 将根据
IsInTailPosition
的结果评估调用。 And if if we check IsInTailPosition
, 如果我们检查
IsInTailPosition
,
- If body is the FunctionBody of a GeneratorBody , return false .
如果body是GeneratorBody的FunctionBody ,则返回false 。
So, if the function body is a generator, then Tail Call Optimization will not be done. 因此,如果函数体是生成器,则不会执行尾调用优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.