Recently I have been exploring the luac 5.1 api and I was wondering if it is possible to loop through every global value in the 5.1 api, I know it is possible to do this in 5.2 ( referenced here ) as lua_pushglobaltable(lua_State*) exists. I know there is LUA_GLOBALSINDEX however I'm not sure how to use it for this purpose. Any help would be greatly appreciated!
Thanks :)
You can use the code in the answer you've mentioned. Just do this:
#define lua_pushglobaltable(L) lua_pushvalue(L,LUA_GLOBALSINDEX)
Here is a complete program that lists all global variables. If you remove the define, it works in Lua 5.2 and 5.3.
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#define lua_pushglobaltable(L) lua_pushvalue(L,LUA_GLOBALSINDEX)
int main(void)
{
lua_State *L=luaL_newstate();
luaL_openlibs(L);
lua_pushglobaltable(L);
lua_pushnil(L);
while (lua_next(L,-2) != 0) {
puts(lua_tostring(L,-2));
lua_pop(L,1);
}
lua_pop(L,1);
lua_close(L);
return 0;
}