Lua:查询用户数据对象的metatable的名称

问题描述:

我想查询某个对象的metatable的名称。Lua:查询用户数据对象的metatable的名称

想想看,我有些元表注册为以下几点:

Object obj; // some C object 

luaL_newmetatable(lua, "my_metatable"); // it's empty 

lua_pushlightuserdata(lua, &obj); 
luaL_setmetatable(lua, "my_metatable"); 
lua_setglobal(lua, "obj_"); 

文档here状态THA luaL_newmetatable确实双重关联,即它使用的名称作为关键表和表作为关键到名称。因此,这方面的知识,我想我可以实现我的目标,如下所示:

int getMTName(lua_State *L) 
{ 
    lua_getmetatable(L, 1); // get the metatable of the object 
    lua_rawget(L, LUA_REGISTRYINDEX); // since the metatable is a key 
             // to its name in registry, use 
             // it for querying the name 
    return 1; // the bottom of the stack is now the name of metatable 
} 

并将其注册,如:

lua_pushcfunction(lua, getMTName); 
lua_setglobal(lua, "getMTName"); 

但不幸的是,它没有工作,它返回nil。那么,我有什么不好?

在这里,一些完整的源代码(在C++):

extern "C" 
{ 
#include <lua.h> 
#include <lualib.h> 
#include <lauxlib.h> 
} 

#include <iostream> 

struct Object { 
    int x; 
}; 

int getMTName(lua_State *L) 
{ 
    lua_getmetatable(L, 1); 
    lua_rawget(L, LUA_REGISTRYINDEX); 
    return 1; 
} 

int main(int argc, char **argv) 
{ 

    lua_State *L =luaL_newstate(); 
    luaL_openlibs(L); 

    Object obj; 

    lua_pushcfunction(L, getMTName); 
    lua_setglobal(L, "getMTName"); 

    luaL_newmetatable(L, "my_metatable"); 

    lua_pushlightuserdata(L, &obj); 
    luaL_setmetatable(L, "my_metatable"); 
    lua_setglobal(L, "obj_"); 

    int e = luaL_dostring(L, "print(getMTName(obj_))"); 

    if (e) 
    { 
     std::cerr << "ERR: " << lua_tostring(L, -1) << std::endl; 
     lua_pop(L, 1); 
    } 

    return 0; 

} 

输出为nil。我的Lua版本是5.3。

好的,现在我明白了。查看https://www.lua.org/source/5.3/lauxlib.c.html#luaL_newmetatable的源代码,我注意到这种双重关联是在metatable中使用“__name”完成的,而不是使用表作为注册表中其名称的关键字。这个行为从Lua 5.3开始。

示例代码:

extern "C" 
{ 
#include <lua.h> 
#include <lualib.h> 
#include <lauxlib.h> 
} 

#include <iostream> 

struct Object { 
    int x; 
}; 

int getMTName(lua_State *L) 
{ 
    lua_getmetatable(L, 1); 
    lua_pushstring(L, "__name"); 
    lua_rawget(L, 2); 
    return 1; 
} 

int main(int argc, char **argv) 
{ 

    lua_State *L =luaL_newstate(); 
    luaL_openlibs(L); 

    Object obj; 

    lua_pushcfunction(L, getMTName); 
    lua_setglobal(L, "getMTName"); 

    luaL_newmetatable(L, "my_metatable"); 

    lua_pushlightuserdata(L, &obj); 
    luaL_setmetatable(L, "my_metatable"); 
    lua_setglobal(L, "obj_"); 

    int e = luaL_dostring(L, "print(getMTName(obj_))"); 

    if (e) 
    { 
     std::cerr << "ERR: " << lua_tostring(L, -1) << std::endl; 
     lua_pop(L, 1); 
    } 

    return 0; 

} 
+1

的Lua 5.0(这是Lua版本由PIL的免费在线版本覆盖)确实如您最初提到的双重关联。请参阅[这里](https://www.lua.org/source/5.0/lauxlib.c.html#luaL_newmetatable)。较新的Lua版本不再那样做了。 – siffiejoe

+0

@siffiejoe谢谢你的信息。 –