lua-users home
lua-l archive

Re: Type of userdata

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012年1月13日 Dirk Laurie <dirk.laurie@gmail.com>:
> 2012年1月12日 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
>>> Is there any way to find out from Lua what the registry type of a
>>> userdata is, short
>>> of giving it a `type` method?
>>
>> If you used luaL_newmetatable to assign a metatable to the udata,
>> then you can search the registry for an entry whose value is that
>> metatable. The key is then the "type" you seek.
>>
>> This example print "FILE*":
>>
>> t=getmetatable(io.stdin)
>> for k,v in pairs(debug.getregistry()) do
>>        if v==t then print(k) end
>> end
>>
>>
>
> Aha!  Thanks!
>
Or if the library is yours' yourself, just make a reverse match table
when you using luaL_newmetatable (untested):
luaL_newmetatable(L, "foo");
lua_getfield(L, LUA_REGISTRYINDEX, "typeinfo");
lua_pushvalue(L, -2);
lua_pushliteral(L, "foo");
lua_rawset(L, -3);
lua_pop(L, 1);

AltStyle によって変換されたページ (->オリジナル) /