From 0d7fc06eb9e87d8e2e916c59e8996cd27af664eb Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Sat, 3 Feb 2024 12:55:07 +0200 Subject: [PATCH] Do garbage collection on luacore Another lua state that is not freed until end of game, so clean up the stack and do GC regularly. --- src/allmain.c | 1 + src/cmd.c | 5 ++++- src/do.c | 1 + src/mklev.c | 1 + src/nhlua.c | 11 +++++++++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/allmain.c b/src/allmain.c index 8a9f8319d..6c86710e2 100644 --- a/src/allmain.c +++ b/src/allmain.c @@ -512,6 +512,7 @@ moveloop_core(void) lua_getglobal(gl.luacore, "nh_callback_run"); lua_pushstring(gl.luacore, nhcb_name[NHCB_END_TURN]); nhl_pcall_handle(gl.luacore, 1, 0, "moveloop_core", NHLpa_panic); + lua_settop(gl.luacore, 0); } } diff --git a/src/cmd.c b/src/cmd.c index 282439fd4..a337922e3 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -487,8 +487,11 @@ can_do_extcmd(const struct ext_func_tab *extcmd) lua_pushstring(gl.luacore, nhcb_name[NHCB_CMD_BEFORE]); lua_pushstring(gl.luacore, extcmd->ef_txt); nhl_pcall_handle(gl.luacore, 2, 1, "can_do_extcmd", NHLpa_panic); - if (!lua_toboolean(gl.luacore, -1)) + if (!lua_toboolean(gl.luacore, -1)) { + lua_settop(gl.luacore, 0); return FALSE; + } + lua_settop(gl.luacore, 0); } if (!wizard && (ecflags & WIZMODECMD)) { diff --git a/src/do.c b/src/do.c index 3f0c068a4..0bbf96d3a 100644 --- a/src/do.c +++ b/src/do.c @@ -1558,6 +1558,7 @@ goto_level( lua_getglobal(gl.luacore, "nh_callback_run"); lua_pushstring(gl.luacore, nhcb_name[NHCB_LVL_LEAVE]); nhl_pcall_handle(gl.luacore, 1, 0, "goto_level", NHLpa_panic); + lua_settop(gl.luacore, 0); } /* tethered movement makes level change while trapped feasible */ diff --git a/src/mklev.c b/src/mklev.c index 9cf7b40c5..d35d9d97a 100644 --- a/src/mklev.c +++ b/src/mklev.c @@ -1243,6 +1243,7 @@ makelevel(void) lua_getglobal(gl.luacore, "nh_callback_run"); lua_pushstring(gl.luacore, nhcb_name[NHCB_LVL_ENTER]); nhl_pcall_handle(gl.luacore, 1, 0, "makelevel", NHLpa_panic); + lua_settop(gl.luacore, 0); } } diff --git a/src/nhlua.c b/src/nhlua.c index 95db470d8..012e1e8d1 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -174,6 +174,7 @@ l_nhcore_call(int callidx) nhcore_call_names[callidx]);*/ nhcore_call_available[callidx] = FALSE; } + lua_settop(gl.luacore, 0); } DISABLE_WARNING_UNREACHABLE_CODE @@ -1115,6 +1116,7 @@ nhl_variable(lua_State *L) lua_getglobal(gl.luacore, "nh_lua_variables"); if (!lua_istable(gl.luacore, -1)) { impossible("nh_lua_variables is not a lua table"); + lua_settop(gl.luacore, 0); return 0; } @@ -1139,6 +1141,7 @@ nhl_variable(lua_State *L) nhl_pcall_handle(L, 0, 1, "nhl_variable-1", NHLpa_panic); } else nhl_error(L, "Cannot get variable of that type"); + lua_settop(gl.luacore, 0); return 1; } else if (argc == 2) { /* set nh_lua_variables[key] = value; @@ -1168,6 +1171,7 @@ nhl_variable(lua_State *L) nhl_pcall_handle(gl.luacore, 0, 0, "nhl_variable-3", NHLpa_panic); } else nhl_error(L, "Cannot set variable of that type"); + lua_settop(gl.luacore, 0); return 0; } else nhl_error(L, "Wrong number of arguments"); @@ -1189,6 +1193,7 @@ get_nh_lua_variables(void) lua_getglobal(gl.luacore, "nh_lua_variables"); if (!lua_istable(gl.luacore, -1)) { impossible("nh_lua_variables is not a lua table"); + lua_settop(gl.luacore, 0); return key; } @@ -1196,10 +1201,12 @@ get_nh_lua_variables(void) if (lua_type(gl.luacore, -1) == LUA_TFUNCTION) { if (nhl_pcall_handle(gl.luacore, 0, 1, "get_nh_lua_variables", NHLpa_impossible)) { + lua_settop(gl.luacore, 0); return key; } key = dupstr(lua_tostring(gl.luacore, -1)); } + lua_settop(gl.luacore, 0); return key; } @@ -1237,6 +1244,7 @@ restore_luadata(NHFILE *nhfp) luaL_loadstring(gl.luacore, lua_data); free(lua_data); nhl_pcall_handle(gl.luacore, 0, 0, "restore_luadata", NHLpa_panic); + lua_settop(gl.luacore, 0); } /* local stairs = stairways(); */ @@ -1552,6 +1560,7 @@ nhl_callback(lua_State *L) lua_pushstring(gl.luacore, cb); lua_pushstring(gl.luacore, fn); nhl_pcall_handle(gl.luacore, 2, 0, "nhl_callback", NHLpa_panic); + lua_settop(gl.luacore, 0); } return 0; } @@ -1951,6 +1960,8 @@ nhl_pcall_handle(lua_State *L, int nargs, int nresults, const char *name, lua_tostring(L, -1)); } } + if (L == gl.luacore) + lua_gc(L, LUA_GCCOLLECT); return rv; }