diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 6ca7a4bb..ff783cc7 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -357,6 +357,9 @@ static void FCEU_LuaOnStop() #ifdef __WIN_DRIVER__ TaseditorDisableManualFunctionIfNeeded(); #endif + + if (info_onstop) + info_onstop(info_uid); } @@ -649,13 +652,6 @@ static int emu_loadrom(lua_State *L) extern void LoadRecentRom(int slot); LoadRecentRom(0); } - if ( GameInfo ) - { - //printf("Currently Loaded ROM: '%s'\n", GameInfo->filename ); - lua_pushstring(L, GameInfo->filename); - return 1; - } - return 0; #elif defined(__QT_DRIVER__) const char *nameo2 = luaL_checkstring(L,1); std::string nameo; @@ -671,15 +667,17 @@ static int emu_loadrom(lua_State *L) // //printf("Failed to Load ROM: '%s'\n", nameo ); // reloadLastGame(); //} +#endif + +#if defined(__WIN_DRIVER__) || defined(__QT_DRIVER__) if ( GameInfo ) { //printf("Currently Loaded ROM: '%s'\n", GameInfo->filename ); lua_pushstring(L, GameInfo->filename); return 1; - } else { - return 0; } #endif + return 0; } @@ -2063,7 +2061,7 @@ static int memory_setregister(lua_State *L) } // Forces a stack trace and returns the string -static const char *CallLuaTraceback(lua_State *L) { +static const char *CallLuaTraceback(lua_State *L, int msgDepth = -1) { lua_getfield(L, LUA_GLOBALSINDEX, "debug"); if (!lua_istable(L, -1)) { lua_pop(L, 1); @@ -2076,20 +2074,23 @@ static const char *CallLuaTraceback(lua_State *L) { return ""; } - lua_pushvalue(L, 1); + if (msgDepth < 0) + msgDepth -= 2; // We pushed 2 onto the stack + + lua_pushvalue(L, msgDepth); lua_call(L, 1, 1); return lua_tostring(L, -1); } -void HandleCallbackError(lua_State* L) +void HandleCallbackError(lua_State* L, int msgDepth = -1) { //if(L->errfunc || L->errorJmp) // luaL_error(L, "%s", lua_tostring(L,-1)); //else { - const char *trace = CallLuaTraceback(L); + const char *trace = CallLuaTraceback(L, msgDepth); lua_pushnil(L); lua_setfield(L, LUA_REGISTRYINDEX, guiCallbackTable); @@ -2278,7 +2279,10 @@ static void CallRegisteredLuaMemHook_LuaMatch(unsigned int address, int size, un lua_pop(L,1); } } - lua_settop(L, 0); + + // L may have been closed by HandleCallbackError + if (L) + lua_settop(L, 0); } } // ++iter; @@ -6624,9 +6628,6 @@ void FCEU_LuaStop() { CoInitialize(0); #endif - if (info_onstop) - info_onstop(info_uid); - //lua_gc(L,LUA_GCCOLLECT,0);