lua-users home
lua-l archive

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

On Oct 12, 2009, at 9:01 AM, Mark Hamburg wrote:

Though note that depending on how you do lifetime management, you may actually want to use a weak table mapping light userdata pointers to full userdata proxies.

You could even use a metatable on this mapping table to do the proxy construction thereby allowing one to write:

	pushMappingTable( L );	/* e.g., fetch it from the registry */
	lua_pushlightuserdata( L, p );
	lua_gettable( L, -2 );	/* Fetch or construct the proxy. */
	lua_remove( L, -2 );	/* Remove the proxy table. */

If you can store the proxy table as the environment to your C code or as an upvalue, then it becomes even simpler. For example:

	lua_pushlightuserdata( L, p );
	lua_gettable( L, LUA_ENVIRONINDEX );

Which incidentally makes me long a bit for a few more special indices as much for code convenience as performance. For example, when building full userdata objects that use their environment tables to link to other Lua objects, a LUA_SELF_ENVIRONINDEX would save one the trouble of fetching the environment onto the stack and tracking its position. That's obviously not a huge burden, but it makes short method implementations a bit harder to follow. In the example here, a special index for a global weak table would help.

That said, I don't have a firm proposal on any of these and its a slippery slope. I'll just note that LUA_ENVIRONINDEX wasn't technically needed given lua_getfenv but it does make using the environment table a lot more natural.