lua-users home
lua-l archive

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


Ah, now I read Rici's note.

So in pseudocode again:

// creation (synchronized) of table, userdata & c++ instance:

ptr = [C++ instance]
udata = userdata[ptr]	// boxed pointer
table = lua_newtable
setfenv(udata, table)
REGISTRY[weakvaluedtable][ptr] = udata
REGISTRY[weakvaluedtable][table] = ptr

// from C++ ptr to table:

getfenv(REGISTRY[weakvaluedtable][ptr])

// from table to C++ ptr:

REGISTRY[weakvaluedtable][table]

// garbage collection:

udata.__gc { // remove ptr & table entries from REGISTRY [weakvaluedtable]) }




Surely there is a more efficient way to do this!?


On Feb 26, 2007, at 12:44 PM, Graham Wakefield wrote:

This works for getting from a userdata (on the stack) to a table.

This doesn't help in getting from a C++ pointer to the table, since I can't get a userdata on the stack from a C++ pointer; or can I?

On Feb 26, 2007, at 6:29 AM, David Burgess wrote:

Answering one of your questions:

Use LUA_ENVIRONINDEX rather than the LUA_REGISTRYINDEX,
see liolib.c for an example of its usage.

And I think this recent quote from Rici Lake applies:

The simplest way is to keep a weak-valued table whose keys are
lightuserdata (the pointer to the c structure) and whose values are the
equivalent userdata object (containing the boxed pointer).

db