From 1d255d1d5f55584626ae247610f379ccdb8f2ec5 Mon Sep 17 00:00:00 2001 From: valid-ptr Date: Mon, 15 Feb 2021 16:24:54 +0300 Subject: Bug fix for Lua 5.1/LuaJIT: lua_getiuservalue must check if lua_getfenv returns global environment --- src/compat.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/compat.c b/src/compat.c index 9f36090..bccd05f 100644 --- a/src/compat.c +++ b/src/compat.c @@ -59,6 +59,20 @@ int lua_getiuservalue( lua_State* L, int idx, int n) return LUA_TNONE; } lua_getuservalue( L, idx); + +#if LUA_VERSION_NUM == 501 + /* default environment is not a nil (see lua_getfenv) */ + lua_getglobal(L, "package"); + if (lua_rawequal(L, -2, -1) || lua_rawequal(L, -2, LUA_GLOBALSINDEX)) + { + lua_pop(L, 2); + lua_pushnil( L); + + return LUA_TNONE; + } + lua_pop(L, 1); /* remove package */ +#endif + return lua_type( L, -1); } -- cgit v1.2.3-55-g6feb From a9daf44c335351366b96f2971b2f8acc227430b1 Mon Sep 17 00:00:00 2001 From: valid-ptr Date: Wed, 24 Feb 2021 19:04:23 +0300 Subject: Compat lua_setiuservalue fixed for Lua 5.1 and LuaJIT --- src/compat.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/compat.c b/src/compat.c index bccd05f..d9bc3dd 100644 --- a/src/compat.c +++ b/src/compat.c @@ -78,11 +78,16 @@ int lua_getiuservalue( lua_State* L, int idx, int n) int lua_setiuservalue( lua_State* L, int idx, int n) { - if( n > 1) + if( n > 1 +#if LUA_VERSION_NUM == 501 + || lua_type( L, -1) != LUA_TTABLE +#endif + ) { lua_pop( L, 1); return 0; } + (void) lua_setuservalue( L, idx); return 1; // I guess anything non-0 is ok } -- cgit v1.2.3-55-g6feb