lua-users home
lua-l archive

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

I'm trying to replace an in-house scripting language in our embedded system
with Lua 5.1.1. The platform is an 8260 PPC running Linux.  We need the
ability to execute scripts even when a prior script is running.  After a lot
of web browsing, it seemed like the proper way to implement this in Lua was
with coroutines.  Force the active coroutine to yield, start the new one,
and when the new one completes, resume the old one.

To start a command:

lua_State* LCurrent = lua_newthread(L);
int error = luaL_loadbuffer(LCurrent, commandString, strlen(commandString), "line");
int nThreadResult = lua_resume(LCurrent,0);

When a new command arrives I set a hook with a line count for the active

lua_sethook(LCurrent, l_yield, LUA_MASKCOUNT, 1);

l_yield() then forces the coroutine to yield.  This all seems to work fine.
lua_resume() returns 0 when the coroutine completes normally and LUA_YIELD
when it is forced to yield.

The problem I have is with error handling.  I catch syntax errors just fine
with the return value from luaL_loadbuffer().  Its execution errors that I
have trouble with. Based upon PIL, I would expect errors to cause
lua_resume() to return an error code in the same fashion as lua_pcall().
What I'm seeing instead is that I get a panic ("unprotected error in call to
Lua API") which halts my application.

Any suggestions on how to catch execution errors in this situation?  I
originally tried surrounding the command string with a pcall() to catch the
error, but this was obviously wrong because then I couldn't yield the
coroutine properly.