* Multiple Lua support fixes:

- Move call to info_onstop from FCEU_LuaStop to FCEU_LuaOnStop so it will always get called
- Do NOT assume the error message from lua_pcall will always be in stack slot 1, as sometimes it won't be
- Return nil from rom_gethash rather than crashing if no ROM is loaded
This commit is contained in:
TheRealQuantam 2024-01-28 19:44:06 -08:00
commit fdc1aec7c9
1 changed files with 25 additions and 21 deletions

View File

@ -371,6 +371,9 @@ static void FCEU_LuaOnStop()
#ifdef __WIN_DRIVER__
TaseditorDisableManualFunctionIfNeeded();
#endif
if (info_onstop)
info_onstop(info_uid);
}
@ -663,13 +666,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;
@ -685,15 +681,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;
}
@ -1507,12 +1505,18 @@ static int rom_getfilename(lua_State *L) {
static int rom_gethash(lua_State *L) {
const char *type = luaL_checkstring(L, 1);
MD5DATA md5hash = GameInfo->MD5;
if (GameInfo != nullptr)
{
MD5DATA md5hash = GameInfo->MD5;
if (!type) lua_pushstring(L, "");
else if (!stricmp(type, "md5")) lua_pushstring(L, md5_asciistr(md5hash));
else if (!stricmp(type, "base64")) lua_pushstring(L, BytesToString(md5hash.data, MD5DATA::size).c_str());
else lua_pushstring(L, "");
}
else
lua_pushnil(L);
if (!type) lua_pushstring(L, "");
else if (!stricmp(type, "md5")) lua_pushstring(L, md5_asciistr(md5hash));
else if (!stricmp(type, "base64")) lua_pushstring(L, BytesToString(md5hash.data, MD5DATA::size).c_str());
else lua_pushstring(L, "");
return 1;
}
@ -2077,7 +2081,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);
@ -2090,20 +2094,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, bool stop)
void HandleCallbackError(lua_State* L, bool stop, 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);
@ -6666,9 +6673,6 @@ void FCEU_LuaStop() {
CoInitialize(0);
#endif
if (info_onstop)
info_onstop(info_uid);
//lua_gc(L,LUA_GCCOLLECT,0);