lua-users home
lua-l archive

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


On 11/04/2011 16:32, Roberto Ierusalimschy whispered from the shadows...:
>                                                     You do not
> have transparent access to them, unless you add a metamethod to
> your global table that redirects accesses to this thread-specific
> table.

I'm still struggling with making this work. I create the master state
and immediately create a metatable to define a custom __index function
written in C that will transparently provide thread context data to
functions loaded into the master state.

Now from my understanding, the functions loaded into the master state
use the master state's global environment by default and so would
"inherit" the custom __index (ignoring issue of __index implementation
just now).

However when I call a Lua defined hook function (defined in the master
state) from within a Lua thread, eg. hook.connect() and then attempt to
reference tables and other data defined in the thread's global space by
C, the lua_thread_index() defined as the __index for the master state is
never called by hooks to find unknown values.

Below is how I setup the master state.

static void
lua_master_set_mt(lua_State *L)
{
	/* Create master state's metatable. */
	(void) lua_pushthread(L);			/* L */
	lua_newtable(L);				/* L mt */
	lua_pushcfunction(L, lua_thread_index);		/* L mt __index */
	lua_setfield(L, -2, "__index");			/* L mt */
	(void) lua_setmetatable(L, -2);			/* L */
	lua_pop(L, 1);					/* -- */

	/* Weak __T table keyed by thread, value is context table. */
	lua_newtable(L);				/* __T */
	lua_pushvalue(L, -1);				/* __T __T */
	lua_pushliteral(L, "k");			/* __T __T mode */
	lua_setfield(L, -2, "__mode");			/* __T __T */
	lua_setmetatable(L, -2);			/* __T */

	lua_setglobal(L, "__T");			/* -- */
}


-- 
Anthony C Howe            Skype: SirWumpus                  SnertSoft
                        Twitter: SirWumpus      BarricadeMX & Milters
http://snert.com/      http://nanozen.info/     http://snertsoft.com/