[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Type-testing with protected metatables
- From: Mark Hamburg <mhamburg@...>
- Date: Wed, 29 Dec 2004 00:32:57 -0800
If I have Lua code implementing a type via a table and I choose to protect
the metatable by defining the __metatable entry, is there a good way to have
the code that defines the type do tests on objects to see if they match the
type?
My solution was to define a C routine: checkmetatable( obj, meta ) that
returns true if and only if the metatable for obj is raw-equal to meta. This
bypasses the protection applied to metatables with respect to Lua.
If this is the way to do it, then I would suggest that this or something
like it be added to the standard library. It doesn't seem like it would
violate security to ask whether an object has a metatable that we already
know about via other means.
Mark
/* Not compiled since I stripped out some stuff special to my environment...
*/
int checkmetatable( lua_State* L ) {
lua_settop( L, 2 ); /* Error checking for sufficient parameters? */
lua_pushboolean( L,
lua_getmetatable( L, 1 ) && lua_rawequal( L, -1, 2 ) );
return 1;
}