c++lualua-userdata

What happens if I push userdata twice using the same key?


I would like to know what happens if I push the lightuserdata into the registry twice using the same key.

My Code:

MyData *x, *y; //let's say these are valid pointers

lua_pushstring(L, "my_data");
lua_pushlightuserdata(L, static_cast<void *>(x));
lua_settable(L, LUA_REGISTRYINDEX);

lua_pushstring(L, "my_data");
lua_pushlightuserdata(L, static_cast<void *>(y));
lua_settable(L, LUA_REGISTRYINDEX);

lua_pushstring(L, "my_data");
lua_gettable(L, LUA_REGISTRYINDEX);
MyData *data = static_cast<MyData *>(lua_touserdata(L, -1));

//would the data be x or y?

Would the previously pushed pointer (x) be replaced with the new one (y)?

ADDED: And is there a way to check the list of keys that are currently registered?


Solution

  • The Lua registry is an ordinary Lua table. Your code is equivalent to

    registry.my_data = x
    registry.my_data = y
    

    So, yes, after these two lines the value of registry.my_data is the value of y.