Immediate closing of iterators

Chris Hansen renselp at gmail.com
Wed Dec 20 15:49:55 PST 2006


> function gen() {
>    yield 1; yield 2
> }
> for (let i in gen())
>    print(i)
>
> The generator function gen, when invoked to the right of 'in' in the
> loop head, constructs a generator-iterator that is referenced only by
> the implementation (a stack slot in typical implementations).  Its
> reference can't be discovered by any meta-object protocol.  It should
> die when the loop completes, abruptly or normally.

That's true, the question is: how do you make sure that it's _only_ on
the stack and not stored in some instance variable or closure if you
don't run a full GC?

function gen1() {
  yield 1; yield 2;
}

function gen2() {
  globalVar = gen1();
  return globalVar;
}

for (let i in gen2()) {
  print(i);
}

In this case the generator shouldn't be closed.  If gen2 had been defined as

function gen2() {
  return gen1();
}

I would expect that it should.


-- Chris



More information about the Es4-discuss mailing list