lua-users home
lua-l archive

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


On Wed, Apr 28, 2021 at 01:03:32PM +0100, Marcus Mason wrote:
> I don't care about security I just need a way to hash arbitrary lua
> objects, lua already internally does this and I wonder if it's possible to
> expose that as a C library function. The other option would be to write a
> lua function which provides a hash code with the same semantics as the
> internal hash codes.

I think the problem you may have is that objects may be hashed by
identity rather than content, so two tables with the same values will
hash differently:

> a = { 1, 2, 3 }
> b = { 1, 2, 3 }
> t = { [a] = true }
> =t[a]
true
> =t[b]
nil

If you're happy with that, then tostring() will provide you with the
identity of tables and userdata (although be careful of metatables
overriding that).  I don't think there's a way of obtaining the hash (or
address) of a string from plain Lua, but the remaining data types can
all be hashed using algorithms written in Lua.

B.