yield equivalent in C/JavaScript?

Robin Becker robin at jessikat.fsnet.co.uk
Sat Jun 29 18:49:18 EDT 2002


In article <7xy9cxhlvx.fsf at ruckus.brouhaha.com>, Paul Rubin <phr-
n2002b at NOSPAMnightsong.com> writes
>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.
the generators here are things  associated with compression or
decompression methods. The complexity is such that a severe
reformulation will be tough. I understand the concept, but I think it's
easier to just try and consume the whole input at one go and pass it on.
-- 
Robin Becker



More information about the Python-list mailing list