[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to approach this?
- From: Andrew Starks <andrew.starks@...>
- Date: Wed, 17 Sep 2014 06:57:08 -0500
On Wednesday, September 17, 2014, Jerome Vuarand <jerome.vuarand@gmail.com> wrote:
2014-09-17 10:10 GMT+01:00 Dirk Schippers <lua@frixx-it.com>:
> The one point that was very important for me: lightuserdata can't have
> metatables.
In Lua all values can have metatables. But a crucial distinction is
that some types have a per-value metatable, while other types have
only one per-type (as in Lua's built-in types, not your own types).
Full userdata belong to the first case. Each full userdata can have
its own metatable, which they can share or not. Light userdata belong
to the second case. There is only one metatable for all light
userdata. Here is a quote from the manual:
Tables and full userdata have individual metatables (although
multiple tables and userdata can share their metatables). Values of
all other types share one single metatable per type; that is, there is
one single metatable for all numbers, one for all strings, etc. By
default, a value has no metatable, but the string library sets a
metatable for the string type (see §6.4).
Remember that light userdata belong to the second case, like numbers or strings.
> That's strange because I created light userdata, I set a metatable to it and
> my stack-debug showed that the light userdata had a metatable.
> So although I had read that in the manual too, I thought the comment was
> about something else.
If you set the metatable of one light userdata, you're replacing the
metatable of all other existing light userdata in the interpreter. If
you check it just after setting it, it will be the value you expect.
But as soon as you create another light userdata, and assign it a
different metatable, the metatable for the first light userdata will
be wrong.
> The concept of the user values is new to me: I have searched the lua
> programming guide and haven't found it, I only saw two functions in the
> reference guide (lua_setuservalue and lua_getuservalue). Is there any more
> info on this?
The manual is indeed a bit lacking on that topic. Here is what the 5.1
manual used to say (updated for 5.2):
[User value] associated with userdata have no meaning for Lua. It
is only a convenience feature for programmers to associate a table to
a userdata.
As it says, it's just a convenience feature, which can be seen as
redundant (you can do the same association with a weak table in the
registry). It's typically used to store per-object information when
the metatable stores per-class information. But you can do the
opposite, or use them in any other way you see fit.
The most well-travelled path being:
Use a single metatable for all of the full userdata values in your *class*.
Use a new uservalue (or upvalue) for each object in your class. Store per-object Lua data in here and configure your metamethods so that your user is writing to this table.
Use light userdata to create a link between C values and Lua values, which can persist between calls to the Lua API, provided that your using the registry.
-Andrew
- References:
- How to approach this?, Dirk Schippers
- Re: How to approach this?, Udo Schroeter
- Re: How to approach this?, Dirk Schippers
- Re: How to approach this?, Udo Schroeter
- Re: How to approach this?, Dirk Schippers
- Re: How to approach this?, Udo Schroeter
- Re: How to approach this?, Dirk Schippers
- Re: How to approach this?, Andrew Starks
- Re: How to approach this?, Philipp Janda
- Re: How to approach this?, Dirk Schippers
- Re: How to approach this?, Jerome Vuarand