[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: luasocket port to Lua 5.0 alpha
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 16 Aug 2002 11:29:05 -0300
> Is there a Lua 5 construction that allows userdata to be "typed" more
> simply tha n with metatables?
Lua itself uses two solutions: the first one is to consider the
metatable itself as the "type". You store it somewhere (upvalue of your
C functions, or in the registry), and then you compare the metatable of
your userdata against it. E.g.:
lua_pushliteral(L, "MyTypeName");
lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.MyTypeName */
if (lua_getmetatable(L, udata)) { /* udata has a metatable? */
if (lua_rawequal(L, -1, -2)) { /* and is the correct one? */
...
The other is to store the userdata as the index in a table of yours,
associated with any identification (If you have a table only for that
type, the presence of the userdata as an index is enough to ensure its
"autenticity"). It is important that such table has weak keys, otherwise
your userdata will never be collected.
> If not, then is the most appropriate solution to put a numeri c
> identifier in each object's metatable?
This is not very safe, as the Lua code can access the object metatable
and change that identifier. (Lua code cannot change the metatable of a
userdata, but by default it can access it, and change its contents.)
-- Roberto