[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: fun with coroutines
- From: Benoit Germain <bgermain@...>
- Date: Wed, 30 Oct 2002 18:15:55 +0100
I have made some modifications to lua-5.0-alpha that enable a script to call
a coroutine with different arguments at each frame. My use is the following:
the coroutine body takes as arguments a system date and a time delta since
the previous date it was called. Obviously these values change at each call,
and I did not want the overhead of putting them in a table being the real
(and constant) argument of the coroutine.
The changes are relatively small. Is there a possibility to have them added
to the 5.0-beta release if I provide a suitable patch file ?
> -----Original Message-----
> From: Roberto Ierusalimschy [mailto:roberto@inf.puc-rio.br]
> Sent: mercredi 30 octobre 2002 17:39
> To: Multiple recipients of list
> Subject: Re: fun with coroutines
>
>
> > -What does the magic value which coroutine.create() returns
> mean? It seems
> > to be a pointer to a c function, which is always the same.
> How does lua know
> > how to use this to restart a running coroutine? (Is this related to
> > "upvalues" somehow?
>
> You may think about the current implementation as
>
> function coroutine.create (...)
> local co = reallycreate(...)
> return function () coroutine.resume(co) end
> end
>
> That is, coroutine.create creates the coroutine and returns a function
> that resumes the coroutine when called. (Yes, it uses upvalues so that
> the inner function, written in C, can access the "co" value.)
>
> We will change that for Lua 5.0 beta, and we hope things will become
> clearer. coroutine.create will do the job of "reallycreate", and will
> return the coroutine itself. A new function, coroutine.wrap, will do
> the job of the current coroutine.create (wrapping the coroutine inside
> a function).
>
>
> > -Is there a way to get the "magic value" for the currently running
> > coroutine?
>
> No.
>
>
> > -How do I kill a couroutine which is *not* the currently
> running one?
>
> You do not need to. If you never resume it again, it will never run
> again. (Its resources are eventually garbage collected.)
>
>
> > -When I call a coroutine, can I find out if it has
> "return"ed, or yield()ed?
>
> No directly. But you can use some protocol: e.g. the
> coroutine returns nil,
> but never yields nil. Maybe in the future we will add a
> "coroutine.status"
> function that returns the status of a coroutine: initialized, resumed,
> terminated.
>
> -- Roberto
>