How would shallow generators compose with lambda?

Brendan Eich brendan at
Fri May 15 14:05:19 PDT 2009

On May 15, 2009, at 1:47 PM, Waldemar Horwat wrote:

>> Did you actually test this? Firefox 2 and up work the same.
>> js> g = gen()
>> [object Generator]
>> js>
>> 1
>> js>
>> 2
>> js>
>> typein:6: ReferenceError: alert is not defined
>> js> alert=print
>> function print() {
>>    [native code]
>> }
>> js> g = gen(42)
>> [object Generator]
>> js>
>> 1
>> js>
>> 2
>> js>
>> Finally called up to three times?!
>> uncaught exception: [object StopIteration]
>> Exactly one finally.
> That behavior is wrong in the case that g gets forgotten after  
> calling next just once.

Progress! You're not alleging longjmps or multiple finallys ;-).

> You're silently exiting through a finally clause without calling it.

You must mean where I hit that ReferenceError due to alert not being  
defined, then reassigned to g without closing it or calling  
and hitting the StopIteration.

(I //totally// so meant to do that in order to provoke this  
discussion. :-P)

The alternative, which we prototyped and rejected, but which CPython  
implements, is to automate calling the generator's close method from  
the garbage collector (close "throws a return" at the generator,  
causing finallys to run). But this is a burden on either or both of  
content authors and implementators, since it makes finally execution  
unpredictably tardy, requires certain kinds of GC, exposes two-phase  
finalization, etc.

In browsers, automating close from the GC opens up trivial denial of  
service attacks that could prevent pages from being unloaded, which  
browsers can and must defeat in ways that again mean "finally does not  
always run."

The fact is, in browsers at least, finally does not always run --  
ignoring generators. There are good DOS-prevention reasons for  
skipping it.

If you want automated close calling in the absence of DOS suppression,  
use for-in. This was suggested by Chris Hansen here:

That whole "Immediate closing of iterators" thread is worth a read if  
you missed it.


More information about the es-discuss mailing list