lua-users home
lua-l archive

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


2014-04-22 8:44 GMT+02:00 steve donovan <steve.j.donovan@gmail.com>:
> On Tue, Apr 22, 2014 at 8:22 AM, Coroutines <coroutines@gmail.com> wrote:
>> I still don't know what's going on this thread, but for the sake of
>> variety -- light, shallow, and full userdata :p
>
> Man, it took me a while in the beginning to understand the difference
> between light and full, but it's easy enough: light is just a pointer,
> and all light userdata share the same metatable (like strings, etc);
> full metadata can have individual metatables.  Now imagine writing the
> manual to explain the meaning of 'shallow' ;)

I'm not too sure whether this might be the same as Tom's solution, but
one can use a trick similar to the one PiL uses for tracking all table
accesses.

I.e. that {~~one ring to rule them all~>one metatable for all light userdata~~}
could have metamethods that look up the first argument in a table of functions
and call that function. One would then have say

    subtype_mt = {__add == ... -- just like the argument to setmetatable}
    shallow.setmetatable(some_light_userdata,subtype_mt)

The function shallow.setmetatable would do something like

   actual_meta[some_light_userdata] = subtype_mt

and the __add function of the light userdata metatable would do
something like

   local meta = actual_meta[arg1]
   if not meta then error(...) end
   local add = meta.__add
   if not add then error(...) end
   return add(ar1,arg2)

If memory is more important than CPU time, this is the answer.
If CPU time is more important than memory, use full userdata.
If memory and CPU time are both more important than one's
dedication to the cause of "pure" Lua, use Peng Zhicheng's patch.