3

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?

4

1 回答 1

3

Lua 注册表是一个普通的 Lua 表。您的代码相当于

registry.my_data = x
registry.my_data = y

所以,是的,在这两行之后, 的值registry.my_data就是 的值y

于 2018-08-06T11:01:08.703 回答