diff --git a/include/extern.h b/include/extern.h index a6302da2c..9fe5f369d 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1646,6 +1646,7 @@ extern int l_obj_register(lua_State *); #if !defined(CROSSCOMPILE) || defined(CROSSCOMPILE_TARGET) extern lua_State * nhl_init(void); +extern void nhl_done(lua_State *); extern boolean nhl_loadlua(lua_State *, const char *); extern boolean load_lua(const char *); extern void nhl_error(lua_State *, const char *) NORETURN; diff --git a/include/flag.h b/include/flag.h index 1e45e948a..98c4bc0d1 100644 --- a/include/flag.h +++ b/include/flag.h @@ -174,6 +174,7 @@ struct instance_flags { * a structure of their own elsewhere some day. */ boolean debug_fuzzer; /* fuzz testing */ + boolean in_lua; /* executing a lua script */ boolean defer_plname; /* X11 hack: askname() might not set g.plname */ boolean herecmd_menu; /* use menu when mouseclick on yourself */ boolean invis_goldsym; /* gold symbol is ' '? */ diff --git a/src/dungeon.c b/src/dungeon.c index f70dcf6bf..f19534484 100644 --- a/src/dungeon.c +++ b/src/dungeon.c @@ -1175,7 +1175,7 @@ init_dungeons(void) so that it's hidden from '#wizwhere' feedback. */ } - lua_close(L); + nhl_done(L); for (i = 0; i < pd.n_brs; i++) { free((genericptr_t) pd.tmpbranch[i].name); diff --git a/src/mklev.c b/src/mklev.c index a3d53cdff..5d88fc0df 100644 --- a/src/mklev.c +++ b/src/mklev.c @@ -259,7 +259,7 @@ free_luathemes(boolean keependgame) /* False: exiting, True: discarding main dun if (keependgame && i == astral_level.dnum) continue; if (g.luathemes[i]) { - lua_close((lua_State *) g.luathemes[i]); + nhl_done((lua_State *) g.luathemes[i]); g.luathemes[i] = (lua_State *) 0; } } @@ -277,7 +277,7 @@ makerooms(void) if ((themes = nhl_init()) != 0) { if (!nhl_loadlua(themes, fname)) { /* loading lua failed, don't use themed rooms */ - lua_close(themes); + nhl_done(themes); themes = (lua_State *) 0; } else { /* success; save state for this dungeon branch */ diff --git a/src/nhlua.c b/src/nhlua.c index 1e25fc7f4..e92aed64b 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -1110,6 +1110,7 @@ nhl_init(void) { lua_State *L = luaL_newstate(); + iflags.in_lua = TRUE; luaL_openlibs(L); nhl_set_package_path(L, "./?.lua"); @@ -1130,13 +1131,20 @@ nhl_init(void) l_obj_register(L); if (!nhl_loadlua(L, "nhlib.lua")) { - lua_close(L); + nhl_done(L); return (lua_State *) 0; } return L; } +void +nhl_done(lua_State *L) +{ + lua_close(L); + iflags.in_lua = FALSE; +} + boolean load_lua(const char *name) { @@ -1154,7 +1162,7 @@ load_lua(const char *name) } give_up: - lua_close(L); + nhl_done(L); return ret; } diff --git a/src/questpgr.c b/src/questpgr.c index 895b42c5c..d04f7768a 100644 --- a/src/questpgr.c +++ b/src/questpgr.c @@ -542,7 +542,7 @@ com_pager_core(const char *section, const char *msgid, boolean showerror) free((genericptr_t) synopsis); if (fallback_msgid) free((genericptr_t) fallback_msgid); - lua_close(L); + nhl_done(L); return res; }