[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua callinfo
- From: Ben Sunshine-Hill <sneftel@...>
- Date: Sun, 20 Feb 2005 23:26:21 -0800
On Fri, 18 Feb 2005 12:32:22 +0100, Dino Patti <email@example.com> wrote:
> I'm trying to serialize(persist) and deserialize(unpersist) a lua state
> using the Pluto lib. It seems to work fine (got good help from the author).
> But now I face a difficult task. I have to start(resume) the same place
> where I left(yielded) the original script I serialized.
> The script contains many yields and resumes, and I want to start the script
> again as if it wasn't persisted/unpersisted but it just resumed.
The key here is that in order to save a thread including its execution
position, that thread _must_ be yielded. There's a lot of different
ways to accomplish this, based on how your control flows, but if
you're not really making use of coroutines otherwise, probably the
simplest way is to run the main script _as_ a coroutine with
lua_newthread. Then, in a loop, resume the thread, and when it returns
from the resume (by yielding from the script) check what value is
returned by the yield. For instance (this code is purely from memory):
SIGNAL_CONTINUE = 0,
SIGNAL_SAVEANDCONTINUE = 1,
SIGNAL_EXIT = 2
lua_State *L2 = lua_createthread(...);
int signal = lua_tonumber(L2, -1);
// do pluto persistence
// clean up and exit
In the script, you can call yield(0) to simply do nothing (it's
unclear why you'd want to do this), yield(1) to save the state and
continue execution, or yield(2) to exit.