diff options
| author | Jacob1 <jfu614@gmail.com> | 2012-05-24 00:08:53 (GMT) |
|---|---|---|
| committer | Jacob1 <jfu614@gmail.com> | 2012-05-24 00:08:53 (GMT) |
| commit | be2fe943c049d479bec9c476e196037678e05b32 (patch) | |
| tree | 0344a7fa848353d560c45a85a0949a34a12aba50 /src | |
| parent | b9439454360fa5f1b340459aca44597e24f21489 (diff) | |
| download | powder-be2fe943c049d479bec9c476e196037678e05b32.zip powder-be2fe943c049d479bec9c476e196037678e05b32.tar.gz | |
Prevent accidental infinite loops in lua
Also fix glitch when step_functions[0] is unregistered but others aren't
Diffstat (limited to 'src')
| -rw-r--r-- | src/interface.c | 1 | ||||
| -rw-r--r-- | src/luaconsole.c | 35 |
2 files changed, 26 insertions, 10 deletions
diff --git a/src/interface.c b/src/interface.c index 9e8c6db..f2f4b18 100644 --- a/src/interface.c +++ b/src/interface.c @@ -2683,6 +2683,7 @@ int sdl_poll(void) { SDL_Event event; sdl_key=sdl_rkey=sdl_wheel=sdl_ascii=0; + loop_time = SDL_GetTicks(); while (SDL_PollEvent(&event)) { switch (event.type) diff --git a/src/luaconsole.c b/src/luaconsole.c index 759dd75..92b58bb 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -32,6 +32,7 @@ int tptProperties; //Table for some TPT properties int tptPropertiesVersion; int tptElements; //Table for TPT element names int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPartMeta, tptPart, cIndex; +int loop_time = 0; void luacon_open(){ int i = 0, j; char tmpname[12]; @@ -219,6 +220,7 @@ tpt.partsdata = nil"); { lua_el_mode[i] = 0; } + lua_sethook(l, &lua_hook, LUA_MASKCOUNT, 200); } #ifndef FFI int luacon_partread(lua_State* l){ @@ -733,26 +735,39 @@ int luacon_step(int mx, int my, int selectl, int selectr){ lua_setfield(l, tptProperties, "mousey"); lua_setfield(l, tptProperties, "selectedl"); lua_setfield(l, tptProperties, "selectedr"); - if(step_functions[0]){ - //Set mouse globals - for(i = 0; i<6; i++){ - if(step_functions[i]){ - lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); - callret = lua_pcall(l, 0, 0, 0); - if (callret) + for(i = 0; i<6; i++){ + if(step_functions[i]){ + loop_time = SDL_GetTicks(); + lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); + callret = lua_pcall(l, 0, 0, 0); + if (callret) + { + // failed, TODO: better error reporting + printf("%s\n",luacon_geterror()); + if (!strcmp(luacon_geterror(),"Error: Infinite loop")) { - // failed, TODO: better error reporting - printf("%s\n",luacon_geterror()); + lua_pushcfunction(l,&luatpt_unregister_step); + lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); + lua_pcall(l, 1, 0, 0); } } } - return tempret; } return 0; } int luacon_eval(char *command){ + loop_time = SDL_GetTicks(); return luaL_dostring (l, command); } +void lua_hook(lua_State *L, lua_Debug *ar) +{ + if(ar->event == LUA_HOOKCOUNT && SDL_GetTicks()-loop_time > 3000) + { + if (confirm_ui(vid_buf,"Infinite Loop","The Lua code might have an infinite loop. Press OK to stop it","OK")) + luaL_error(l,"Error: Infinite loop"); + loop_time = SDL_GetTicks(); + } +} int luacon_part_update(int t, int i, int x, int y, int surround_space, int nt) { int retval = 0; |
