From 6e5697a125973bca83d368364e088777d23f824a Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Wed, 31 Oct 2018 12:40:59 -0400 Subject: Added error handeling function Added an error handeling function to avoid crashes on errors in callbacked handeled code (ex, error in button press) --- src/shared/lua_api/common.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/shared/lua_api') diff --git a/src/shared/lua_api/common.cpp b/src/shared/lua_api/common.cpp index 55f2b23..57d1db0 100644 --- a/src/shared/lua_api/common.cpp +++ b/src/shared/lua_api/common.cpp @@ -235,3 +235,41 @@ int popvector2i(lua_State* L, long* a, long* b){ return 0; } + +//errfunc("mssg",false) +int errfunc(lua_State *L){ + printf("Error function called\n"); + int nargs = lua_gettop(L); + for(int i = 1; i <= nargs; i++){ + printf("Arguemtn %d to errorfun was type %s\n",i,lua_typename(L,lua_type(L,i))); + } + if (!lua_isstring(L, 1)){ /* 'message' not a string? */ + printf("Error is not a string, it is a %s\n",lua_typename(L,lua_type(L,1))); + //return 1; [> keep it intact <] + } + printf("Error:%s\n",lua_tostring(L,1)); + lua_getglobal(L, "debug");//error,{debug} + lua_getglobal(L, "print");//error,{debug},print + if (!lua_istable(L, -2)) { + printf("debug is not a table, abort\n"); + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -2, "traceback");//error,{debug},print,debug.traceback + if (!lua_isfunction(L, -1)) { + printf("debug does not have a .traceback function, abort\n"); + lua_pop(L, 2); + return 1; + } + lua_pushvalue(L, -4); //error,{debug},print,debug.traceback,error + //lua_pushinteger(L, 3); //error,{debug},print,debug.traceback,error,2 + lua_call(L, 2, 1);//error,{debug},print,str_traceback + printf("Error:%s\n",lua_tostring(L,-1)); + lua_call(L,1,0);//error,{debug} + lua_pop(L,1); + printf("Returning"); + return 1; +} +void pusherrorfunc(lua_State *L){ + lua_pushcfunction(L,errfunc); +} -- cgit v1.2.3-70-g09d2