Hash in native extension

I would like to get a hash in a native extension function. I am passing the hash as an argument to the C function. But it seems that I always get 0. If I print the hex value of the hash in lua just before calling the c function I get:

fd99e8b1e3ff9e17

that looks as a valid value to me.

But when I try to get the value in C with the lines

uint64_t h = lua_tointeger(L, HASH_INDEX);
printf(“hash = %llu\n”, h);

I always get 0.

Maybe lua_tointeger is not the right way to get a hash from the stack? It seems to me that hash are 64 bit unsigned int and that also lua_tointeger returns a 64 bit unsigned int.

Any help is highly apreciated!

Thank you!

It’s not an integer, it’s a Lua userdata object.
You get it using the dmScript::CheckHash()

2 Likes

Some important detail: the function lua_tointeger returns a lua_Integer. It is defined like this:

typedef ptrdiff_t lua_Integer;

This means that on 32-bit targets, such as HTML5, lua_Integer will be 32-bit. So avoid using lua_pushinteger and lua_tointeger when working with hash values, to not inadvertently truncate the values.

Defold’s hash type is always 64-bit, and defined like this:

typedef uint64_t dmhash_t;
1 Like

@Mathias_Westerdahl @GooseSwanson Thank you for your help!!! Now it is clear how to get properly use a hash in native code.