Prevent accidental infinite loops in lua

Also fix glitch when step_functions[0] is unregistered but others aren't
This commit is contained in:
Jacob1 2012-05-23 20:08:53 -04:00
parent b943945436
commit be2fe943c0
5 changed files with 29 additions and 11 deletions

2
.gitignore vendored
View File

@ -17,4 +17,4 @@ src/python/stdlib/*
*.bat *.bat
*.o *.o
*.me *.me
* *

View File

@ -189,6 +189,7 @@ extern int ngrav_enable;
int limitFPS; int limitFPS;
int water_equal_test; int water_equal_test;
extern int quickoptions_tooltip_fade; extern int quickoptions_tooltip_fade;
extern int loop_time;
extern int debug_flags; extern int debug_flags;
#define DEBUG_PERF_FRAMECOUNT 256 #define DEBUG_PERF_FRAMECOUNT 256

View File

@ -62,6 +62,7 @@ int luacon_particle_getproperty(char * key, int * format);
int luacon_transition_getproperty(char * key, int * format); int luacon_transition_getproperty(char * key, int * format);
int luacon_element_getproperty(char * key, int * format, unsigned int * modified_stuff); int luacon_element_getproperty(char * key, int * format, unsigned int * modified_stuff);
int process_command_lua(pixel *vid_buf, char *console, char *console_error); int process_command_lua(pixel *vid_buf, char *console, char *console_error);
void lua_hook(lua_State *L, lua_Debug *ar);
int getPartIndex_curIdx; int getPartIndex_curIdx;

View File

@ -2683,6 +2683,7 @@ int sdl_poll(void)
{ {
SDL_Event event; SDL_Event event;
sdl_key=sdl_rkey=sdl_wheel=sdl_ascii=0; sdl_key=sdl_rkey=sdl_wheel=sdl_ascii=0;
loop_time = SDL_GetTicks();
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
switch (event.type) switch (event.type)

View File

@ -32,6 +32,7 @@ int tptProperties; //Table for some TPT properties
int tptPropertiesVersion; int tptPropertiesVersion;
int tptElements; //Table for TPT element names int tptElements; //Table for TPT element names
int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPartMeta, tptPart, cIndex; int tptParts, tptPartsMeta, tptElementTransitions, tptPartsCData, tptPartMeta, tptPart, cIndex;
int loop_time = 0;
void luacon_open(){ void luacon_open(){
int i = 0, j; int i = 0, j;
char tmpname[12]; char tmpname[12];
@ -219,6 +220,7 @@ tpt.partsdata = nil");
{ {
lua_el_mode[i] = 0; lua_el_mode[i] = 0;
} }
lua_sethook(l, &lua_hook, LUA_MASKCOUNT, 200);
} }
#ifndef FFI #ifndef FFI
int luacon_partread(lua_State* l){ 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, "mousey");
lua_setfield(l, tptProperties, "selectedl"); lua_setfield(l, tptProperties, "selectedl");
lua_setfield(l, tptProperties, "selectedr"); lua_setfield(l, tptProperties, "selectedr");
if(step_functions[0]){ for(i = 0; i<6; i++){
//Set mouse globals if(step_functions[i]){
for(i = 0; i<6; i++){ loop_time = SDL_GetTicks();
if(step_functions[i]){ lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);
lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); callret = lua_pcall(l, 0, 0, 0);
callret = lua_pcall(l, 0, 0, 0); if (callret)
if (callret) {
// failed, TODO: better error reporting
printf("%s\n",luacon_geterror());
if (!strcmp(luacon_geterror(),"Error: Infinite loop"))
{ {
// failed, TODO: better error reporting lua_pushcfunction(l,&luatpt_unregister_step);
printf("%s\n",luacon_geterror()); lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);
lua_pcall(l, 1, 0, 0);
} }
} }
} }
return tempret;
} }
return 0; return 0;
} }
int luacon_eval(char *command){ int luacon_eval(char *command){
loop_time = SDL_GetTicks();
return luaL_dostring (l, command); 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 luacon_part_update(int t, int i, int x, int y, int surround_space, int nt)
{ {
int retval = 0; int retval = 0;