Are E6 chip protection generators created?

Does ES6 support tail call optimization for tail calls in generators?

Suppose I have this generator for integers> = 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, ultimately, starts with the error "maximum call stack size." Will this continue as soon as ES6 is fully implemented?

(I know that I can write the generator above iteratively and not confuse the error. I'm just wondering if the TCO will take care of the recursively defined generators.)

+6
source share
1 answer

When a function call is executed, in accordance with the section Checking a function call ,

  1. Let tailCall - IsInTailPosition (thisCall)
  2. Return? EvaluateCall (func, ref, arguments, tailCall)

The call will be evaluated based on the result of IsInTailPosition . And if we check IsInTailPosition ,

  1. If the body is a FunctionBody for GeneratorBody, return false .

So, if the function body is a generator, Tail Call optimization will not be performed.

+4
source

Source: https://habr.com/ru/post/986855/


All Articles