lua-users home
lua-l archive

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


Hello everybody,

I would like to embed Lua in my C program, and I would like to know if I can do what I want with it.

I want to execute a Lua function (i'll call it first()), which eventually call some C function.
In response to some signals generated by custom hardware I want to stop the exection of the function first().
I've read I can only do it safely by calling lua_sethook(). In particular I am using LUA_MASKLINE as mask for the hook.

1st Question) If first() is in the middle of a C call i can't stop it, right?

Moreover, depending on the signal from the hardware I want to start the execution of another function, say second().
So, in the hook function a generate a new lua thread with lua_newthread(orig_thread), where orig_thread is the lua_State* of first(), and then start this new thread with resume.
I am using thread because
This second() function may change some global Lua variables, and then return to the execution of first(). In case I need it, I can also stop the execution of first() by yielding it, finishing with the execution of second(), and then exit my program.
It actually works, but I believe that using the debug function lua_sethook is not the correct approach.

2nd Question) Is there another way to implement this?

Furthermore, in case I get a particular signal while second() is running, I would also like to stop it, execute a third() function, then go back to second() then go back to first() (or stop, for example, the execution of second(), and go back to first()).
I've read I can't do it, in Lua manual (www.lua.org/manual/5.1/manual.html#lua_Hook):
"While Lua is running a hook, it disables other calls to hooks. Therefore, if a hook calls back Lua to execute a function or a chunk, this execution occurs without any calls to hooks."

3rd Question) Does anybody know what could be the best Lua approach to implement this thing?

All my regards,

Arithmeth