Ada*_*rce 3 c error-handling lua
我在我当前的项目中嵌入了一个Lua解释器(用C语言编写),我正在寻找一个如何处理错误的例子.这就是我到目前为止......
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");
Run Code Online (Sandbox Code Playgroud)
手册只是说使用longjmp函数抛出错误但longjmp需要缓冲区.我必须提供它还是Lua分配缓冲区?手册对此有点模糊.
经过一些研究和一些RTFS,我已经解决了这个问题.我一直在咆哮完全错误的树.
即使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");
Run Code Online (Sandbox Code Playgroud)