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/client/callbackhandeler.cpp | 27 +++++++++++++++++---------- src/client/callbackhandeler.hpp | 3 +++ src/shared/lua_api/common.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/client/callbackhandeler.cpp b/src/client/callbackhandeler.cpp index 469084d..e24763d 100644 --- a/src/client/callbackhandeler.cpp +++ b/src/client/callbackhandeler.cpp @@ -8,6 +8,7 @@ extern "C" { } #include #include +#include #include "callbackhandeler.hpp" @@ -36,16 +37,20 @@ GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d){ } void callMouse(lua_State* L, const char* funcname, double x, double y, double event){ - lua_getglobal(L,"GAME"); - lua_getfield(L,-1,funcname); + lua_getglobal(L,"GAME");//GAME + pusherrorfunc(L);//{GAME},errfun + lua_getfield(L,-2,funcname);//{GAME},errfunc,funcname? if(!lua_isnil(L,-1)){ - lua_pushnumber(L,x); - lua_pushnumber(L,y); - lua_pushnumber(L,event); - lua_call(L,3,0); - lua_pop(L,1); - }else{ + lua_pushnumber(L,x);//{GAME},errfunc,func,x + lua_pushnumber(L,y);//{GAME},errfunc,func,x,y + lua_pushnumber(L,event);//{GAME},errfunc,func,x,y,event + int err = lua_pcall(L,3,0,-4);//{GAME},errfunc + if(err){ + printf("Failed to call GAME.%s\n",funcname); + } lua_pop(L,2); + }else{ + lua_pop(L,3); } } @@ -177,12 +182,14 @@ bool GlobalEventReceiver::OnEvent(const SEvent& e){ lua_getglobal(L,"GAME");//{} lua_getfield(L,-1,"onKeyDown");//{},()|nil if(!lua_isnil(L,-1)){ + pusherrorfunc(L);//GAME,GAME.onKeyDown(),errfunc + lua_pushvalue(L,-2);//GAME,GAME.onKeyDown(),errfunc,onKeyDown() lua_pushnumber(L,se.Key); lua_pushboolean(L,se.PressedDown); lua_pushboolean(L,se.Control); lua_pushboolean(L,se.Shift); - lua_call(L,4,0); - lua_pop(L,1); + lua_pcall(L,4,0,-5);//GAME,GAME.onKeyDown() + lua_pop(L,2); }else{ lua_pop(L,2); } diff --git a/src/client/callbackhandeler.hpp b/src/client/callbackhandeler.hpp index 3d4894f..0728b29 100644 --- a/src/client/callbackhandeler.hpp +++ b/src/client/callbackhandeler.hpp @@ -20,4 +20,7 @@ public: GlobalEventReceiver(IrrlichtDevice* d); bool OnEvent(const irr::SEvent& e); }; + +int errfunc(lua_State *L); +void pusherrorfunc(lua_State *L); //GlobalEventReceiver::GlobalEventReceiver(IrrlichtDevice* d); 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