lua-users home
lua-l archive

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


2015-08-21 0:05 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
> Maybe I am missing something, but if we want to put some extra bits in
> 'TValue' to good use and solve the OP problem, maybe a better idea would
> be to introduce metatables for light userdata.
>
> There would be a limit on the number of different metatables that light
> userdata can have, but it would be a reasonable high limit [2^25] for
> typical uses of metatables. If we adopt this limit for all metatables in
> the system, we could gain some space in tables and full userdata, too.
> (We could even gain individual metatables for numbers :-)

Something like this?

1. An array _METATABLES in the registry.
2. The 25 unused bits in the type tag are used to index into it.
0 means no metatable.
3. Any value can have a metatable.

This sounds great. Much better than exposing those bits directly
to the Lua user!

It's simpler, more orthogonal and more powerful than the current
implementation, except for the upper limit of 2^25-1 metatables.
Anybody with a current application that exceeds this?

There might be a baselib function `name` to go with it, that returns the
__name field of the metatable, its number in _METATABLES if there
is no name, nil if there is no metatable. luaL_newmetatable already
creates that field, user-created metatables could use it as appropriate.

A function could have __name, which when present would be used
in tracebacks.

A floating-point number could have __name, e.g.

gamma = setmetatable(0.57721566490153286,
   {__name="Euler's constant"})

The possibilities are legion!