开发者

Lua C API: Delete metatable created with luaL_newmetatable?

开发者 https://www.devze.com 2023-02-11 23:36 出处:网络
How can I d开发者_开发技巧elete a metatable foo created with luaL_newmetatable( L, \"foo\" );, so that luaL_getmetatable( L, \"foo\" ); will push a NIL value again?Whatever the reason you may have to

How can I d开发者_开发技巧elete a metatable foo created with luaL_newmetatable( L, "foo" );, so that luaL_getmetatable( L, "foo" ); will push a NIL value again?


Whatever the reason you may have to delete the metatable, it is possible. luaL_newmetatable(L, "foo") creates a table, which is stored in the Lua registry with the key "foo".

To delete the table, just set the field "foo" in the registry to nil. The code in C:

lua_pushnil(L);
lua_setfield(L, LUA_REGISTRYINDEX, "foo");

Equivalent code in Lua:

debug.getregistry()["foo"] = nil
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号