From 32a629f2a3d78d892f8b36982240dd294adf846b Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:54:01 +0100 Subject: [PATCH] Lua: make require return true for modules with no return this fixes the require cache not working --- src/luasandbox/require.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/luasandbox/require.h b/src/luasandbox/require.h index c17a4d6b..d7ddc1f8 100644 --- a/src/luasandbox/require.h +++ b/src/luasandbox/require.h @@ -61,6 +61,11 @@ static inline int luasandbox_require(lua_State *L) if (luaL_loadbufferx(L, pscript.c_str(), pscript.length(), filename.c_str(), "t") == LUA_OK) { lua_pushstring(L, name); if (lua_pcall(L, 1, 1, 0) == LUA_OK) { + if (lua_isnil(L, -1)) { + // if module returned nil, we return (and cache) true instead + lua_pop(L, 1); + lua_pushboolean(L, 1); + } lua_pushvalue(L, -1); // duplicate result lua_setfield(L, -3, name); lua_remove(L, -2); // remove _LOADED from stack