lua-users home
lua-l archive

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


On Nov 7, 2010, at 10:37 AM, Drake Wilson wrote:

> Quoth Mark Hamburg <mark@grubmah.com>, on 2010-11-07 10:15:21 -0800:
>> Per the discussions about strings for looking up values, one of my standard Lua utility routines is roughly:
>> 
>> 	int pushTableForKey( lua_State* L, int sourceIndex, const void* udkey ) {
>> 		// Normalize sourceIndex to avoid problems with relative addresses after the push.
>> 		lua_pushlightuserdata( L, (void*) udkey );
>> 		lua_gettable( L, sourceIndex );
>> 		if( !lua_isnil( L, -1 ) ) return 0;
>> 		lua_pop( L, 1 );
>> 		lua_newtable( L );
>> 		return 1;
>> 	}
> 
> Just out of curiosity, do you ever store the new table back into
> <table at sourceIndex>[<lightuserdata udkey>]?  Was that part omitted
> for clarity?

Oops. I was writing that from memory since my personal e-mail machine is not the same as my main dev machine.

	int pushTableForKey( lua_State* L, int sourceIndex, const void* udkey ) {
		// Normalize sourceIndex to avoid problems with relative addresses after the push.
		lua_pushlightuserdata( L, (void*) udkey );
		lua_gettable( L, sourceIndex );
		if( !lua_isnil( L, -1 ) ) return 0;
		lua_pop( L, 1 );
		lua_newtable( L );
		lua_pushlightuserdata( L, (void*) udkey );
		lua_pushvalue( L, -2 );
		lua_settable( L, sourceIndex );
		return 1;
	}

Mark