diff --git a/src/deh_lua.c b/src/deh_lua.c index b8daa0430..9679780d7 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -600,14 +600,9 @@ static int ScanConstants(lua_State *L, boolean mathlib, const char *word) return 0; } -static inline int lib_getenum(lua_State *L) +static inline int getEnum(lua_State *L, boolean mathlib, const char *word) { - const char *word; fixed_t i; - boolean mathlib = lua_toboolean(L, lua_upvalueindex(1)); - if (lua_type(L,2) != LUA_TSTRING) - return 0; - word = lua_tostring(L,2); // check actions, super and globals first, as they don't have _G caching implemented // so they benefit from being checked first @@ -674,6 +669,46 @@ static inline int lib_getenum(lua_State *L) else if ((!mathlib && LUA_PushGlobals(L, word)) || ScanConstants(L, mathlib, word)) return 1; + return -1; +} + +static int constants_get(lua_State *L) +{ + const char *key; + int ret; + + if (!lua_isstring(L, 2)) + return 0; + + key = luaL_checkstring(L, 2); + + // In Lua, mathlib is never there + ret = getEnum(L, false, key); + + if (ret != -1) + // Don't allow A_* or super. + // All userdata is meant to be considered global variables, + // so no need to get more specific than "is it userdata?" + if (!lua_isuserdata(L, -1) && !lua_isfunction(L, -1)) + return ret; + + return 0; +} + +static inline int lib_getenum(lua_State *L) +{ + const char *word; + int ret; + boolean mathlib = lua_toboolean(L, lua_upvalueindex(1)); + if (lua_type(L,2) != LUA_TSTRING) + return 0; + word = lua_tostring(L,2); + + ret = getEnum(L, mathlib, word); + + if (ret != -1) + return ret; + if (mathlib) return luaL_error(L, "constant '%s' could not be parsed.\n", word); return 0; @@ -776,6 +811,15 @@ int LUA_SOCLib(lua_State *L) LUA_SetCFunctionField(L, "__call", action_call); lua_pop(L, 1); + // Allow access to constants without forcing the use of name comparison checks Lua-side + // This table will not access global variables, only constants + lua_newuserdata(L, 0); + lua_createtable(L, 0, 2); + lua_pushcfunction(L, constants_get); + lua_setfield(L, -2, "__index"); + lua_setmetatable(L, -2); + lua_setglobal(L, "constants"); + return 0; }