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.
This commit is contained in:
Pasi Kallinen
2024-02-03 12:55:10 +02:00
parent e7a7526ea9
commit 0d7fc06eb9
5 changed files with 18 additions and 1 deletions
+1
View File
@@ -512,6 +512,7 @@ moveloop_core(void)
lua_getglobal(gl.luacore, "nh_callback_run"); lua_getglobal(gl.luacore, "nh_callback_run");
lua_pushstring(gl.luacore, nhcb_name[NHCB_END_TURN]); lua_pushstring(gl.luacore, nhcb_name[NHCB_END_TURN]);
nhl_pcall_handle(gl.luacore, 1, 0, "moveloop_core", NHLpa_panic); nhl_pcall_handle(gl.luacore, 1, 0, "moveloop_core", NHLpa_panic);
lua_settop(gl.luacore, 0);
} }
} }
+4 -1
View File
@@ -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, nhcb_name[NHCB_CMD_BEFORE]);
lua_pushstring(gl.luacore, extcmd->ef_txt); lua_pushstring(gl.luacore, extcmd->ef_txt);
nhl_pcall_handle(gl.luacore, 2, 1, "can_do_extcmd", NHLpa_panic); 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; return FALSE;
}
lua_settop(gl.luacore, 0);
} }
if (!wizard && (ecflags & WIZMODECMD)) { if (!wizard && (ecflags & WIZMODECMD)) {
+1
View File
@@ -1558,6 +1558,7 @@ goto_level(
lua_getglobal(gl.luacore, "nh_callback_run"); lua_getglobal(gl.luacore, "nh_callback_run");
lua_pushstring(gl.luacore, nhcb_name[NHCB_LVL_LEAVE]); lua_pushstring(gl.luacore, nhcb_name[NHCB_LVL_LEAVE]);
nhl_pcall_handle(gl.luacore, 1, 0, "goto_level", NHLpa_panic); nhl_pcall_handle(gl.luacore, 1, 0, "goto_level", NHLpa_panic);
lua_settop(gl.luacore, 0);
} }
/* tethered movement makes level change while trapped feasible */ /* tethered movement makes level change while trapped feasible */
+1
View File
@@ -1243,6 +1243,7 @@ makelevel(void)
lua_getglobal(gl.luacore, "nh_callback_run"); lua_getglobal(gl.luacore, "nh_callback_run");
lua_pushstring(gl.luacore, nhcb_name[NHCB_LVL_ENTER]); lua_pushstring(gl.luacore, nhcb_name[NHCB_LVL_ENTER]);
nhl_pcall_handle(gl.luacore, 1, 0, "makelevel", NHLpa_panic); nhl_pcall_handle(gl.luacore, 1, 0, "makelevel", NHLpa_panic);
lua_settop(gl.luacore, 0);
} }
} }
+11
View File
@@ -174,6 +174,7 @@ l_nhcore_call(int callidx)
nhcore_call_names[callidx]);*/ nhcore_call_names[callidx]);*/
nhcore_call_available[callidx] = FALSE; nhcore_call_available[callidx] = FALSE;
} }
lua_settop(gl.luacore, 0);
} }
DISABLE_WARNING_UNREACHABLE_CODE DISABLE_WARNING_UNREACHABLE_CODE
@@ -1115,6 +1116,7 @@ nhl_variable(lua_State *L)
lua_getglobal(gl.luacore, "nh_lua_variables"); lua_getglobal(gl.luacore, "nh_lua_variables");
if (!lua_istable(gl.luacore, -1)) { if (!lua_istable(gl.luacore, -1)) {
impossible("nh_lua_variables is not a lua table"); impossible("nh_lua_variables is not a lua table");
lua_settop(gl.luacore, 0);
return 0; return 0;
} }
@@ -1139,6 +1141,7 @@ nhl_variable(lua_State *L)
nhl_pcall_handle(L, 0, 1, "nhl_variable-1", NHLpa_panic); nhl_pcall_handle(L, 0, 1, "nhl_variable-1", NHLpa_panic);
} else } else
nhl_error(L, "Cannot get variable of that type"); nhl_error(L, "Cannot get variable of that type");
lua_settop(gl.luacore, 0);
return 1; return 1;
} else if (argc == 2) { } else if (argc == 2) {
/* set nh_lua_variables[key] = value; /* 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); nhl_pcall_handle(gl.luacore, 0, 0, "nhl_variable-3", NHLpa_panic);
} else } else
nhl_error(L, "Cannot set variable of that type"); nhl_error(L, "Cannot set variable of that type");
lua_settop(gl.luacore, 0);
return 0; return 0;
} else } else
nhl_error(L, "Wrong number of arguments"); nhl_error(L, "Wrong number of arguments");
@@ -1189,6 +1193,7 @@ get_nh_lua_variables(void)
lua_getglobal(gl.luacore, "nh_lua_variables"); lua_getglobal(gl.luacore, "nh_lua_variables");
if (!lua_istable(gl.luacore, -1)) { if (!lua_istable(gl.luacore, -1)) {
impossible("nh_lua_variables is not a lua table"); impossible("nh_lua_variables is not a lua table");
lua_settop(gl.luacore, 0);
return key; return key;
} }
@@ -1196,10 +1201,12 @@ get_nh_lua_variables(void)
if (lua_type(gl.luacore, -1) == LUA_TFUNCTION) { if (lua_type(gl.luacore, -1) == LUA_TFUNCTION) {
if (nhl_pcall_handle(gl.luacore, 0, 1, "get_nh_lua_variables", if (nhl_pcall_handle(gl.luacore, 0, 1, "get_nh_lua_variables",
NHLpa_impossible)) { NHLpa_impossible)) {
lua_settop(gl.luacore, 0);
return key; return key;
} }
key = dupstr(lua_tostring(gl.luacore, -1)); key = dupstr(lua_tostring(gl.luacore, -1));
} }
lua_settop(gl.luacore, 0);
return key; return key;
} }
@@ -1237,6 +1244,7 @@ restore_luadata(NHFILE *nhfp)
luaL_loadstring(gl.luacore, lua_data); luaL_loadstring(gl.luacore, lua_data);
free(lua_data); free(lua_data);
nhl_pcall_handle(gl.luacore, 0, 0, "restore_luadata", NHLpa_panic); nhl_pcall_handle(gl.luacore, 0, 0, "restore_luadata", NHLpa_panic);
lua_settop(gl.luacore, 0);
} }
/* local stairs = stairways(); */ /* local stairs = stairways(); */
@@ -1552,6 +1560,7 @@ nhl_callback(lua_State *L)
lua_pushstring(gl.luacore, cb); lua_pushstring(gl.luacore, cb);
lua_pushstring(gl.luacore, fn); lua_pushstring(gl.luacore, fn);
nhl_pcall_handle(gl.luacore, 2, 0, "nhl_callback", NHLpa_panic); nhl_pcall_handle(gl.luacore, 2, 0, "nhl_callback", NHLpa_panic);
lua_settop(gl.luacore, 0);
} }
return 0; return 0;
} }
@@ -1951,6 +1960,8 @@ nhl_pcall_handle(lua_State *L, int nargs, int nresults, const char *name,
lua_tostring(L, -1)); lua_tostring(L, -1));
} }
} }
if (L == gl.luacore)
lua_gc(L, LUA_GCCOLLECT);
return rv; return rv;
} }