> On Mar 19, 2017, at 12:03 PM, Xavier Wang <weasley.wx@gmail.com> wrote:
>
> Some a long time ago Roberto make a joke about per-value metatable
> here[1], in a feature request raised by cloud wu[2].
>
> I found that's very exciting[3], but I missing the message that
> Roberto says that is just a joke[4]. So sad, because this won't in Lua
> because it can not support __gc meta-method.
>
> __gc method may called when last value in Lua disappeared, if a
> metatable attach to value, it's no way to know how to free the gc
> objects. So metatable can not attach with value together.
>
> but, for now only full user data has the user value, setting/getting
> it using Lua API: lua_getuservalue()/lua_setuservalue(). So can we use
> the extra bits in value and make every value has uservalue? a integer
> is enough because we could put real value into a ref table. or make a
> internal array for the uservalue? When a value disappear we could just
> free the slot that this value use? or any ways to put a pointer in
> this field?
>
Other than tables or full userdata (which have per-value metatables), what do you need? I’m unclear on what use a metatable on a particular string, or a light userdata would have (why would it not be a full userdata in that case?).
It opens safe global metatable for numbers and light userdata, etc. metatable could check uservalue to detect whether you could do specific things on specific values. Though I really want is per-value metatables...
It seems only full userdata and string has __gc metamethod, another solution is make per value metatable, and move __gc to gc object, use collectgarbage() or lua_gc() to specific __gc.
—Tim