Impossible to exit infinite while loop #197

This commit is contained in:
BADIM-PC\Vadim 2017-12-12 12:50:39 +03:00
parent 67bfbf4729
commit 18aca45673
3 changed files with 35 additions and 1 deletions

View File

@ -29,6 +29,8 @@
#include "machine.h" #include "machine.h"
#include "ext/moonscript.h" #include "ext/moonscript.h"
#define LUA_LOC_STACK 1E8 // 100.000.000
static const char TicMachine[] = "_TIC80"; static const char TicMachine[] = "_TIC80";
s32 luaopen_lpeg(lua_State *L); s32 luaopen_lpeg(lua_State *L);
@ -1061,6 +1063,21 @@ static const lua_CFunction ApiFunc[] =
STATIC_ASSERT(api_func, COUNT_OF(ApiKeywords) == COUNT_OF(ApiFunc)); STATIC_ASSERT(api_func, COUNT_OF(ApiKeywords) == COUNT_OF(ApiFunc));
static void checkForceExit(lua_State *lua, lua_Debug *luadebug)
{
tic_machine* machine = getLuaMachine(lua);
tic_tick_data* tick = machine->data;
if(tick->hook)
{
tick->hook(tick->data);
if(tick->forceExit)
luaL_error(lua, "script execution was interrupted");
}
}
static void initAPI(tic_machine* machine) static void initAPI(tic_machine* machine)
{ {
lua_pushlightuserdata(machine->lua, machine); lua_pushlightuserdata(machine->lua, machine);
@ -1072,6 +1089,8 @@ static void initAPI(tic_machine* machine)
registerLuaFunction(machine, lua_dofile, "dofile"); registerLuaFunction(machine, lua_dofile, "dofile");
registerLuaFunction(machine, lua_loadfile, "loadfile"); registerLuaFunction(machine, lua_loadfile, "loadfile");
lua_sethook(machine->lua, &checkForceExit, LUA_MASKCOUNT, LUA_LOC_STACK);
} }
void closeLua(tic_machine* machine) void closeLua(tic_machine* machine)
@ -1216,7 +1235,7 @@ void callLuaTick(tic_machine* machine)
lua_getglobal(lua, TicFunc); lua_getglobal(lua, TicFunc);
if(lua_isfunction(lua, -1)) if(lua_isfunction(lua, -1))
{ {
if(lua_pcall(lua, 0, 0, 0) != LUA_OK) if(lua_pcall(lua, 0, 0, 0) != LUA_OK)
machine->data->error(machine->data->data, lua_tostring(lua, -1)); machine->data->error(machine->data->data, lua_tostring(lua, -1));
} }
else else

View File

@ -178,6 +178,16 @@ static void preseed()
#endif #endif
} }
static void poll(void* data)
{
Run* run = (Run*)data;
while(pollEvent());
if (getStudioMode() != TIC_RUN_MODE)
run->tickData.forceExit = true;
}
void initRun(Run* run, Console* console, tic_mem* tic) void initRun(Run* run, Console* console, tic_mem* tic)
{ {
*run = (Run) *run = (Run)
@ -197,6 +207,8 @@ void initRun(Run* run, Console* console, tic_mem* tic)
.data = run, .data = run,
.exit = onExit, .exit = onExit,
.preprocessor = processDoFile, .preprocessor = processDoFile,
.hook = poll,
.forceExit = false,
}, },
}; };

View File

@ -45,16 +45,19 @@ typedef struct
typedef void(*TraceOutput)(void*, const char*, u8 color); typedef void(*TraceOutput)(void*, const char*, u8 color);
typedef void(*ErrorOutput)(void*, const char*); typedef void(*ErrorOutput)(void*, const char*);
typedef void(*ExitCallback)(void*); typedef void(*ExitCallback)(void*);
typedef void(*HookCallback)(void*);
typedef struct typedef struct
{ {
TraceOutput trace; TraceOutput trace;
ErrorOutput error; ErrorOutput error;
ExitCallback exit; ExitCallback exit;
HookCallback hook;
u64 (*counter)(); u64 (*counter)();
u64 (*freq)(); u64 (*freq)();
u64 start; u64 start;
bool forceExit;
void (*preprocessor)(void* data, char* dst); void (*preprocessor)(void* data, char* dst);