lua-users home
lua-l archive

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


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 :-)

> On Feb 26, 2007, at 1:56 PM, Sam Roberts wrote:
> 
> >
> >>REGISTRY[weakvaluedtable][table] = ptr
> >
> >^-- probably unnecessary, see below
> >
> >>// from C++ ptr to table:
> >>
> >>getfenv(REGISTRY[weakvaluedtable][ptr])

^--- This lookup you pseudocode here...

> >>// from table to C++ ptr:
> >>
> >>REGISTRY[weakvaluedtable][table]
> >
> >Why would you would you want to map the table to the ptr?
> >
> >lua code will have only a udata, and will pass a udata to C code,  
> >your C
> >code will call lua_touserdata() to get the ptr. No table required.
> 
> Because I want to call back into the lua State from C++ at a later  
> point in time (after the main Lua chunk has run).  That's why I need to
> 	A) prevent the table being collected until I'm ready
> 	B) get to the userdata/table from a raw pointer

--- gets you the udata/table from a raw ptr...

> >>
> >
> >Optional: if you need to go from udata -> table, maybe to store some
> >number of lua values with your udata, thats what lua_getfenv() is for.
> 
> 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.


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

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.

So, it has no point (that I can see):

A) It doesn't extend the lifetime of the table past the lifetime of the
udata, the table will be available for GC as soon as the udata.__gc
metamethod returns.

B) It doesn't help you get from ptr to udata, or from
udata to table.


Btw, have you read

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

?

Its brief, but the PIL describes this, in specific relation to
callbacks. Its more detailed in the 2nd edition, which also describes
lua_get/set fenv for udata.

Cheers,
Sam