lua-users home
lua-l archive

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

2017-03-21 15:28 GMT+08:00 Sean Conner <>:
> It was thus said that the Great Xavier Wang once stated:
>> 2017-03-21 2:26 GMT+08:00 Tim Hill <>:
>> >
>> > On Mar 19, 2017, at 8:55 PM, 云风 Cloud Wu <> wrote:
>> > Tim Hill <>于2017年3月20日周一 上午11:12写道:
>> >>
>> >> 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?).
>> >>
>> > lightuserdata can be a sub struct of an complex C/C++ object .
>> >
>> > For example, in 3d engine, a node of a 3d object as a lightuserdata would be
>> > better than full userdata. If you query a node of a 3d object from lua , you
>> > can create a full userdata proxy for it, but you should also cache it in
>> > uservalue because it should be the same value in next query ( __eq is not
>> > enough if you want to use it as a table key).
>> >
>> > Using lightuserdata (with metatable) for a 3d node would simplify the
>> > bindings of 3d engine and reduce the overhead.
>> >
>> >
>> > But a light userdata with a metatable basically *IS* a full userdata. The
>> > only difference is that the full userdata is allocated by Lua, and undergoes
>> > garbage collection .. which you ALSO want for the __gc. As you noted, the
>> > technique here is to allocate a full userdata that just holds a C pointer
>> > (aka a light userdata).
>> >
>> > Your request won’t get you a lower overhead .. it will just get you
>> > something else that is identical to full userdata but with a different name.
>> IMO there are two different things in Lua: values and objects. values
>> are the thing that in local value or table slots, and objects are the
>> subject of garbage collect.
>   Lua has the following types:
>         nil
>         boolean
>         number
>         string                  gc but value semantics
>         table                   gc per-instance metatable
>         function                gc
>         thread                  gc
>         userdata                gc per-instance metatable
>         lightuserdata*
> (* In C, you can distinguish between a full userdata and a lightuserdata,
> but in Lua, type() does not make that distinction and simply returns
> 'userdata')

the "value" I mean is the slot that has C type TValue, and the
"object" I mean is the slot of GCObject.

string, clearly, is the "immutable" sematics, you just can't change
it. What I mean "control" the gc, means it support __gc, i.e. you will
give a callback when Lua decide to delete it.

Let clearly in theoretical. Lua is a language that have "forever
lifetime" sematics. All value are live forever. garbage collect is
just a engineering optimization for that, if a value you really don't
need, Lua know she could collect it safely. In this way, the real
sematics thing is "strong/weak ref", Lua leave you a way to tell her
whether a value you really want, or doesn't matter. It real changes
Lua's behavior, but in another way.

In metatable, __gc and __mode are only two thing to specific this. it
just a subset of Lua's collect system. beside table and full userdata,
all other value are silently collected, without let you know about it.

But all other things in metatable are real hooks to Lua when "it
doesn't know how to do", e.g. call a number, or concat two table.
metatable makes some of value has the different behavior than others.
it's a "behavior trait".

the uservalue propersal, add the ability to "identity each other" with
Lua values. it's a value tag to specific "where this value from", it
could come with value assignment, to track the value's transform in
Lua context. It's just another complete different function vs.
metatable. it's more like something like "metadata".

>   I can't tell if you have a version of Lua that does what you want, or are
> asking for Lua to support what you want ..
>   -spc

I of course know that Lua is "open source and close development", I
used years of Lua and really LOVE this language. I just express my own
idea about Lua, whether Roberto decide what to do, I still love Lua,
All thing I want is to make her more perfect. So if I'm wrong, I'll
never mind someone tell me that. But I don't need someone (beside
Roberto) to teach me "Lua is not my own pet", I already know that.

Xavier Wang.