Immediate closing of iterators

Brendan Eich brendan at
Wed Dec 20 11:46:39 PST 2006

On Dec 20, 2006, at 3:02 AM, Chris Hansen wrote:

>>> So, could the spec require Javascript to always close the iterator
>>> when leaving a loop?
>> So long as it's a non-escaping generator-iterator created by the
>> loop, then the spec could mandate that.  It requires some extra work
>> by non-reference-counting implementations.  They need to keep track
>> of such generator-iterators across nested loops in each live function
>> or script activation, and close each generator-iterator as control
>> exits its loop.
> Unless the definition of "created by the loop" is very strict won't
> this effectively mandate a full GC whenever you leave a for-in loop?
> Even reference counting implementations would have to detect
> unreachable cycles in the object graph.

function gen() {
   yield 1; yield 2
for (let i in gen())

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.


More information about the Es4-discuss mailing list