From c90b3ba171e8c989354c007df58dec88667d0bcb Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Tue, 12 Dec 2017 13:46:05 +0300 Subject: [PATCH] Impossible to exit infinite while loop #197 review changes for https://github.com/nesbox/TIC-80/commit/18aca45673f2311215b24db42f65fa23cc2dcbc4#r26209938 --- src/luaapi.c | 9 ++------- src/run.c | 10 +++------- src/ticapi.h | 5 ++--- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/luaapi.c b/src/luaapi.c index 7f01694..13453e5 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -1069,13 +1069,8 @@ static void checkForceExit(lua_State *lua, lua_Debug *luadebug) tic_tick_data* tick = machine->data; - if(tick->hook) - { - tick->hook(tick->data); - - if(tick->forceExit) - luaL_error(lua, "script execution was interrupted"); - } + if(tick->forceExit && tick->forceExit(tick->data)) + luaL_error(lua, "script execution was interrupted"); } static void initAPI(tic_machine* machine) diff --git a/src/run.c b/src/run.c index ca847a4..1c05f72 100644 --- a/src/run.c +++ b/src/run.c @@ -178,14 +178,11 @@ static void preseed() #endif } -static void poll(void* data) +static bool forceExit(void* data) { - Run* run = (Run*)data; - while(pollEvent()); - if (getStudioMode() != TIC_RUN_MODE) - run->tickData.forceExit = true; + return getStudioMode() != TIC_RUN_MODE; } void initRun(Run* run, Console* console, tic_mem* tic) @@ -207,8 +204,7 @@ void initRun(Run* run, Console* console, tic_mem* tic) .data = run, .exit = onExit, .preprocessor = processDoFile, - .hook = poll, - .forceExit = false, + .forceExit = forceExit, }, }; diff --git a/src/ticapi.h b/src/ticapi.h index bb35833..ea61302 100644 --- a/src/ticapi.h +++ b/src/ticapi.h @@ -45,19 +45,18 @@ typedef struct typedef void(*TraceOutput)(void*, const char*, u8 color); typedef void(*ErrorOutput)(void*, const char*); typedef void(*ExitCallback)(void*); -typedef void(*HookCallback)(void*); +typedef bool(*CheckForceExit)(void*); typedef struct { TraceOutput trace; ErrorOutput error; ExitCallback exit; - HookCallback hook; + CheckForceExit forceExit; u64 (*counter)(); u64 (*freq)(); u64 start; - bool forceExit; void (*preprocessor)(void* data, char* dst);