[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: PiL2, ch 28.3: Adding Lua methods to an array?
- From: "nvleer"<nvleer@...>
- Date: Fri, 9 Jun 2006 17:54:48 -0400 (EDT)
One approach I use is set an environment on the userdata when it is created. I use this table to store any properties or methods associated with the object. In the __index metamethod, I also allow rollover into the klass' metatable:
int indexMetaMethod( lua_State *L )
{
lua_getfenv( L, 1 );
lua_pushvalue( L, 2 );
lua_rawget( L, -2 );
if (!lua_isnil( L, -1 ))
return 1;
/* get the field from the object's klass table */
lua_pop( L, 2 );
lua_getmetatable( L, 1 );
lua_pushvalue( L, 2 );
lua_rawget( L, -2 );
if (!lua_isnil( L, -1 ))
return 1;
return 0;
}
_______________________________________________
No banners. No pop-ups. No kidding.
Make My Way your home on the Web - http://www.myway.com