lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This may sound weird, but can the Lua VM be run in "slices", doing part
of its current job, returning control to its caller and resuming the
suspended job in a subsequent call?  How much would it take to achieve
such behaviour and which part of the code would I have to touch?

What I have in mind and why I ask so funny a question is this:  The
environment is an event driven program logic.  The main application
enters its main event handling loop.  Should no (user or OS driven)
events be available, I'd like to do "a little more work" in the Lua VM.
While I'd like the VM to return control in a timely manner to handle new
events.

Waiting for the current script or chunk of code to end may take too
long.  Since the Lua code is provided by users I don't even have control
about it nor can I guarantee it to end at all.  And parallel execution
(i.e. running the VM in a thread or in background) is not available in
this environment.

I'm afraid that the debug hooks won't help here either.  Getting called
after N lines of code or VM instructions is not the same as getting
control back from the lua_pcall() issued.  Unless I'm missing something
and this IS the way to go:  start main(), handle the first round of
events until none arrive any longer, start the Lua job while a debug
hook is registered, "consume" (i.e. handle) all events in the debug hook
when it's called, return to Lua, have it call the debug hook again a
little later, handle all events, continue in Lua, etc etc, until the Lua
job is done, returns to its caller (the application's main() routine),
where we can handle all the remaining events and maybe start another
complete cycle.  Looks like I have to think a little more about this ...

Then there still is the question how to abort such a Lua job from within
the debug hook.  Raising an exception may get caught from within the Lua
chunk (pcall()) and might not lead to the Lua VM terminating its job.
How can one "intervene" from inside a debug hook in such a way that the
Lua VM terminates its current task?

Suggestions?  Comments?  Alternatives?  Who's telling me that I'm sick
and should go a different path? :)


virtually yours
Gerhard Sittig
-- 
     If you don't understand or are scared by any of the above
             ask your parents or an adult to help you.