[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: coroutines
- From: Jamie Webb <j@...>
- Date: Fri, 7 May 2004 03:47:28 +0100
On Friday 07 May 2004 03:07, Eric Jacobs wrote:
> On Fri, 7 May 2004 01:17:04 +0100
>
> Jamie Webb <j@jmawebb.cjb.net> wrote:
> > If I really felt the need to implement logic as a single function rather
> > than a state machine, I might start thinking about preprocessing the Lua
> > code, e.g. to turn this:
> >
> > function my_game()
> > local x = do_stuff()
> > %YIELDPOINT%
> > return do_more_stuff(x)
> > end
> >
> > Into this:
> >
> > function my_game()
> > local x = do_stuff()
> > return my_yield(42, x)
> > end
> >
> > continuations[42] = function(x)
> > return do_more_stuff(x)
> > end
>
> This is exactly what coroutines do in Lua 5!
Not quite. The point of the code above is that my_yield can be a Lua function
which is able to serialise the current state (provided any userdata can be
serialised). It can then be recovered multiple times, and in different
instances of the VM.
-- Jamie Webb