yield equivalent in C/JavaScript?

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Sat Jun 29 16:26:26 EDT 2002

There's no native way to do it in C; in some enviroments you might be
able to simulate it with longjmp madness or threads.

In Javascript the only approach I can think of is make a "virtual
machine" that executes javascript statements using JS's eval function,
and implement something like threads or coroutines in the virtual
machine.  I did something like that once--it wasn't as bad as it
might sound, but it may not be worth it for your application.
You can just organize your generator functions like a state machine
instead, saving their locals and yielding location in some structure
returned to the caller.  I haven't examined any Python generator
bytecode but I suspect that's how it works.  By being limited to
"simple" generators (can't yield except to one's direct caller)
it's enough to just save the local variables and "return" address
in the state object, rather than a full continuation including
the call stack.

More information about the Python-list mailing list