LUA_PushLightUserdata takes the fun out of lib_cvFindVar

This commit is contained in:
James R 2020-02-02 15:19:19 -08:00
parent b77d597965
commit 0f3a44887c
3 changed files with 25 additions and 16 deletions

View file

@ -431,23 +431,9 @@ static int lib_cvRegisterVar(lua_State *L)
static int lib_cvFindVar(lua_State *L) static int lib_cvFindVar(lua_State *L)
{ {
consvar_t *cv; LUA_PushLightUserdata(L, CV_FindVar(luaL_checkstring(L,1)), META_CVAR);
if (( cv = CV_FindVar(luaL_checkstring(L,1)) ))
{
lua_settop(L,1);/* We only want one argument in the stack. */
lua_pushlightuserdata(L, cv);/* Now the second value on stack. */
luaL_getmetatable(L, META_CVAR);
/*
The metatable is the last value on the stack, so this
applies it to the second value, which is the cvar.
*/
lua_setmetatable(L,2);
lua_pushvalue(L,2);
return 1; return 1;
} }
else
return 0;
}
// CONS_Printf for a single player // CONS_Printf for a single player
// Use 'print' in baselib for a global message. // Use 'print' in baselib for a global message.

View file

@ -568,6 +568,28 @@ fixed_t LUA_EvalMath(const char *word)
return res; return res;
} }
/*
LUA_PushUserdata but no userdata is created.
You can't invalidate it therefore.
*/
void LUA_PushLightUserdata (lua_State *L, void *data, const char *meta)
{
if (data)
{
lua_pushlightuserdata(L, data);
luaL_getmetatable(L, meta);
/*
The metatable is the last value on the stack, so this
applies it to the second value, which is the userdata.
*/
lua_setmetatable(L, -2);
lua_pushvalue(L, -1);
}
else
lua_pushnil(L);
}
// Takes a pointer, any pointer, and a metatable name // Takes a pointer, any pointer, and a metatable name
// Creates a userdata for that pointer with the given metatable // Creates a userdata for that pointer with the given metatable
// Pushes it to the stack and stores it in the registry. // Pushes it to the stack and stores it in the registry.

View file

@ -46,6 +46,7 @@ void LUA_LoadLump(UINT16 wad, UINT16 lump);
void LUA_DumpFile(const char *filename); void LUA_DumpFile(const char *filename);
#endif #endif
fixed_t LUA_EvalMath(const char *word); fixed_t LUA_EvalMath(const char *word);
void LUA_PushLightUserdata(lua_State *L, void *data, const char *meta);
void LUA_PushUserdata(lua_State *L, void *data, const char *meta); void LUA_PushUserdata(lua_State *L, void *data, const char *meta);
void LUA_InvalidateUserdata(void *data); void LUA_InvalidateUserdata(void *data);
void LUA_InvalidateLevel(void); void LUA_InvalidateLevel(void);