return when desugaring to closures

Mark S. Miller erights at
Mon Aug 25 16:10:16 PDT 2008

On Fri, Aug 22, 2008 at 7:27 AM, Neil Mix <nmix at> wrote:
>>> Since generators are shallow, it seems clear that there
>>> should be some local cps-style transform of generators into the
>>> remainder of the language. However, it would be a relief to know what
>>> that cps-style transform actually looks like, and what edge cases it
>>> has. Anyone care to post a proposal?
>> Can't be done without defining the entire language in CPS. Yes, it's
>> shallow, but a captured activation can involve any arbitrary combination
>> of expressions other than function call.
> I've done meta-circular implementations of generators (or something like
> them) twice now.  It's amateur work ;) so put it in the FWIW category, but
> in case you find it useful "research":
> A) This does source transformation
> of code into suspendable/resumable "partial continuations" (see
> )

Neil, I think Narrative JS is really cool. Kudos.

> This project was more ambitious than implementing generators since it was
> capturing multiple stack frames.

Yes, this would correspond to deep generators or coroutines, which
have many of the virtues and many of the hazards of continuations. It
isn't appropriate for future standard EcmaScripts, but again, it is
really cool.

However, this reopens the question. If deep generators are
implementable by a global cps transform, are shallow generators
implementable by a local cps transform? If not, can anyone explain the
issues using a simple example? Thanks.

> B) A meta-circular
> implementation of a JS evaluator that supported generators (intended to be
> used with function.toString(), so the parser is overly forgiving, ignore the
> parser.)
> My strategy in this one was to parse into "bytecode" and run in a
> mini-interpreter for the bytecode.  This was way easier because you get to
> use goto in the interpreter and the current state of the interpreter can be
> captured in a closure, making it easily suspendable/resumable.  It was also
> much easier to discover and fix the bugs that I never even considered in
> narrativejs.

This approach would seem relevant to adding generators by adding new
semantic state. But would it help us understand how to add generators
by a local code transformation?


More information about the Es-discuss mailing list