lua-users home
lua-l archive

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


On Fri, Mar 24, 2017 at 1:16 PM, Tim Hill <drtimhill@gmail.com> wrote:
>
>> On Mar 24, 2017, at 10:26 AM, Coda Highland <chighland@gmail.com> wrote:
>>
>>
>> Well yes, of course, you make a weak-valued table with lightuserdata
>> keys and full userdata values and marshal your pointers through that,
>> but it's not immediately obvious and there are a number of possible
>> pitfalls in the implementation (for example, forgetting that
>> weak-valued tables are a thing makes this solution either ridiculously
>> leaky or annoyingly difficult to maintain).
>>
>> This has come up a number of times before, so while I don't think that
>> "per-value metatables" is the right solution to the task, it does at
>> least indicate that the issue is fairly common.
>>
>
>
> I don’t think is a particularly complex design pattern, but I do agree it might be one that is worth documenting somewhere (in the wiki perhaps?). In any case the OP was suggesting a core design change in Lua as a solution when I think this pattern does all he needs.
>
> —Tim

And as it happens, I agree with you. I'm just explaining it instead of
dismissing it.

> Not if you have __eq in the metatable. And in any case this only happens if your C code actually creates more than one full userdata for a given C pointer, something that is in the control of the coder (it’s easy to use a weak table in the registry to lookup a full userdata by the encapsulated void* light userdata).

That works for normal equality comparisons but that doesn't help for
cases where raw equality is the only thing taken into consideration,
e.g. table indexing.

/s/ Adam