Error handling in Lua using longjmp

I am embedding the Lua interpreter in my current project (written in C ) and I am looking for an example of how to handle errors. This is what I still have ...

if(0 != setjmp(jmpbuffer)) /* Where does this buffer come from ? */
{
   printf("Aargh an error!\n");
   return;
}
lua_getfield(L, LUA_GLOBALSINDEX, "myfunction");
lua_call(L, 0, 0);
printf("Lua code ran OK.\n");

The manual simply says that errors are generated using the longjmp function , but longjmp needs a buffer. Should I provide this, or is Lua allocating a buffer? The manual is a bit vague.

+3
source share
2 answers

After some research and some RTFS, I solved this problem. I completely barked an absolutely wrong tree.

, Lua API , longjmp , longjmp API.

Lua, lua_pcall(). :

lua_getfield(L, LUA_GLOBALSINDEX, "myfunction");

if(0 != lua_pcall(L, 0, 0, 0))
   printf("Lua error: %s\n", lua_tostring(L, -1));
else
   printf("Lua code ran OK.\n");
+8

struct lua_longjmp, errorJmp struct lua_State. Lua lstate.h. Doxygen .

( Lua) LUAI_TRY .

, .

-1

Source: https://habr.com/ru/post/1732940/


All Articles