proper error reporting for callbacks

This commit is contained in:
Tim Felgentreff 2020-12-04 10:04:54 +01:00
parent 385b2d6f68
commit d71498b5aa
4 changed files with 23 additions and 17 deletions

2
.vscode/launch.json vendored
View file

@ -10,7 +10,7 @@
},
"windows": {
"type": "cppvsdbg",
"program": "${workspaceFolder}/build/Debug/stratagus.exe",
"program": "${workspaceFolder}/build/Debug/stratagus-dbg.exe",
"args": ["-W", "-a", "-d", "${workspaceFolder}\\..\\data.${input:game}"],
"environment": [{"name": "PATH", "value": "${workspaceFolder}\\..\\dependencies\\bin;${env:PATH}"}],
"externalConsole": true,

View file

@ -70,6 +70,7 @@ extern lua_State *Lua;
extern int LuaLoadFile(const std::string &file, const std::string &strArg = "");
extern int LuaCall(int narg, int clear, bool exitOnError = true);
extern int LuaCall(lua_State *L, int narg, int nresults, int base, bool exitOnError = true);
#define LuaError(l, args) \
do { \

View file

@ -177,18 +177,7 @@ int LuaCallback::popInteger()
*/
void LuaCallback::run(int results)
{
//FIXME call error reporting function
int status = lua_pcall(luastate, arguments, results, base);
if (status) {
const char *msg = lua_tostring(luastate, -1);
if (msg == NULL) {
msg = "(error with no message)";
}
fprintf(stderr, "%s\n", msg);
lua_pop(luastate, 1);
}
LuaCall(luastate, arguments, results, base, false);
rescount = results;
}

View file

@ -159,12 +159,28 @@ static int luatraceback(lua_State *L)
int LuaCall(int narg, int clear, bool exitOnError)
{
const int base = lua_gettop(Lua) - narg; // function index
lua_pushcfunction(Lua, luatraceback); // push traceback function
lua_insert(Lua, base); // put it under chunk and args
return LuaCall(Lua, narg, clear ? 0 : LUA_MULTRET, base, exitOnError);
}
/**
** Call a lua function
**
** @param L Pointer to Lua state
** @param narg Number of arguments
** @param nresults Number of return values
** @param base Stack index of the function to call
** @param exitOnError Exit the program when an error occurs
**
** @return 0 in success, else exit.
*/
int LuaCall(lua_State *L, int narg, int nresults, int base, bool exitOnError)
{
lua_pushcfunction(L, luatraceback); // push traceback function
lua_insert(L, base); // put it under chunk and args
signal(SIGINT, laction);
const int status = lua_pcall(Lua, narg, (clear ? 0 : LUA_MULTRET), base);
const int status = lua_pcall(L, narg, nresults, base);
signal(SIGINT, SIG_DFL);
lua_remove(Lua, base); // remove traceback function
lua_remove(L, base); // remove traceback function
return report(status, exitOnError);
}