From 0613eb2163415591b2cdb45ab2fa0844e9ff7ca0 Mon Sep 17 00:00:00 2001 From: ugetab Date: Sat, 1 May 2010 16:42:06 +0000 Subject: [PATCH] Addressed bug 2995032. LUA now throws a maximum of 8 errors when stopping a script. --- src/lua-engine.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index b9a4673d..dcd13c00 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -111,6 +111,8 @@ extern void WinLuaOnStop(int hDlgAsInt); static lua_State *L; +static int luaexiterrorcount = 8; + // Are we running any code right now? static char *luaScriptName = NULL; @@ -4591,6 +4593,9 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg) { //stop any lua we might already have had running FCEU_LuaStop(); + //Reinit the error count + luaexiterrorcount = 8; + if (!L) { L = lua_open(); @@ -4734,8 +4739,18 @@ void FCEU_LuaStop() { //already killed if (!L) return; - //execute the user's shutdown callbacks - CallExitFunction(); + // Since the script is exiting, we want to prevent an infinite loop. + // CallExitFunction() > HandleCallbackError() > FCEU_LuaStop() > CallExitFunction() ... + if (luaexiterrorcount > 0) { + luaexiterrorcount = luaexiterrorcount - 1; + //execute the user's shutdown callbacks + CallExitFunction(); + } + + luaexiterrorcount = luaexiterrorcount + 1; + + //already killed (after multiple errors) + if (!L) return; /*info.*/numMemHooks = 0; for(int i = 0; i < LUAMEMHOOK_COUNT; i++)