lua-users home
lua-l archive

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


Hi, Sorry for not being clearer.  Hope this helps:

On Feb 26, 2007, at 4:38 PM, Sam Roberts wrote:

On Mon, Feb 26, 2007 at 02:59:39PM -0800, Graham Wakefield wrote:
Thanks, but I think perhaps I wasn't clear enough about my needs; see
below:

I'm certainly not understanding :-)

OK. I run a chunk of lua code in a lua_State. Some calls in this lua code call lua_CFunctions that will create my table/c++ instance pairs.

THEN, after the lua chunk has run (possibly several seconds or longer later, depending on C++ events), I want to call back to functions in the user tables created in the lua_State. This is not the result of a lua_CFunction call, it is from a C API call (e.g. lua_pcall()).

Somehow I need to get the userdata back onto the lua stack, based upon the identity of the C++ instance pointer. getfenv() will not help, since it requires that the call was made as the result of a lua_CFunction call from Lua, not a lua_pcall from C.


// from C++ ptr to table:

getfenv(REGISTRY[weakvaluedtable][ptr])

^--- This lookup you pseudocode here...
--- gets you the udata/table from a raw ptr...

Yes, but how do I get to the udata from a raw C++ pointer, after the
main Lua chunk has run (i.e. in response to a C++ event)?

--- as you need.

This will not work because it is only valid in the context of a lua_CFunction call, and my call comes from a later C++ triggered lua_pcall().


As for (A) above, "until you are ready", when will you "be ready"?

When I receive an event from a C++ object external to Lua. This could be milliseconds later, or hours later.


The implementation you sketched out removed the table->ptr mapping in
the udata's __gc metamethod, so the table has no life beyond the udata's
life.

Yes. The userdata has a metamethod kill() to remove it, which should also remove the table. I don't want to do this immediately that the Lua chunk is completed, but as the result of a callback back into Lua.

Btw, have you read

  http://www.lua.org/pil/28.5.html

?

Yes, all of PIL2 at length, many times.