I'm learning how to bind C++ objects to Lua with type checking from the book Programming Gems 6 (Chapter 4.2). For the type checking the userdata/string pairs are being stored in an environment table, with the code given how to do that:
void Binder::pushusertype(void* udata, const char* tname) {
lua_pushlightuserdata(L, udata); // push address
lua_pushvalue(L, -1); // duplicate address
lua_pushstring(L, tname); // push type name
lua_rawset(L, LUA_ENVIRONMENTINDEX); // envtable[address] = tname
}
Where the Binder class has a Lua State as an attribute, named "L"
As you can see the address is pushed twice. As this small piece of code is only given as an example it doesn't seem like a duplicate address pushed onto the stack would serve any purpose outside of this function, which leads me to believe there's a specific reason for it. So my question is, why would one do this?