lua-users home
lua-l archive

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

Hello everybody,

I'd like to use Lua inside  my C program, and I'd like to know what I can do with Lua and what is the better approach to my problem. I hope someone on the list might help.

I want to open a file, and execute a function from there. The function does some lua stuff and eventually calls some C function which interact with custom hardware. Easy, till now.
Problems arise when I want to stop the execution of the function in response to some signal from the hardware.
I've read I can safely stop Lua code execution only using lua_sethook.
But, what I also want to do is to execute another lua function, and then go back to the previous one. It actually works.
Here is what I do:
In the hook function, I generate a new thread with lua_newthread and resume it. I can modify globals variables in the lua scripts, call C function, and also stop the previuosly running function, if needed. Otherwise, I can go back to continue with execution of the previous function.
An example lua script could be:
function first ()

    while (1) do a=a+1 end

function second ()
    while (1) do end


What I can't do is nesting calls. While the interrupting function is running, I can't stop it to call another function, then go back to the previous call, then back to the first function. From lua manual (
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."

I believe using a debug function (lua_sethook) is not the correct approach to the problem. And I only solved part of the problem (I can make just one nesting). But I'd like to know what could the correct approach to the problem.

Any ideas?

All my regards,