lua-users home
lua-l archive

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


On Tue, Feb 13, 2007 at 03:48:16PM -0800, Wesley Smith wrote:
> I understand this is usally the case.  But I want per-instance custom
> behavior whereas the metatable is for class behavior.  From the wiki
> article:
> 
> "A userdata has both a metatable and an environment table. It seems
> logical that the metatable contains information which is general to
> the datatype of the userdata, while the environment table contains
> information which is specific to the instance of the userdata."

>From memory, isn't this Rici's suggestion as to how to use the two?

> What I don't undertand right now is how a userdata and its environment
> table interact.

A userdata doesn't interact with its environment table at all, to my
knowledge. Maybe I'm missing your question?

For functions, the environment is used for non-local variable lookups.

For user-data, the environment isn't used for anything (by lua) - but it
is available to your C code. You can set the table, get the table, look
things up in it from your C code.

Its a convenient place to store things for you, because otherwise if you
wanted to store lua-world information associated with whatever C-world
struct you are using in you user-data, you would have to store a
reference in the C-struct, then translate the ref to/from the table, and
manage garbage collection, etc.

Sam