Hello!
I am attempting to use FFI to leverage the ngx_hash_key function in
src/core/ngx_hash.c; however, because it returns a uintptr_t, I'm having
trouble getting it into other portions of my Lua code. For example, a
compiled-in implementation of the Jenkins one-at-a-time hash that
returns a unint32_t works just fine with the following code:
ffi.cdef[[
uint32_t JHASH(char *key, size_t len);
]]
local function jhash(str)
local buf = ffi.cast("char *", str)
local c = ffi.C.JHASH(buf, string.len(str))
ngx.say(c)
end
Where 'JHASH' is defined to:
uint32_t jenkins_one_at_a_time_hash(char *key, size_t len)
{
unsigned char *p = key;
uint32_t h = 0;
int i;
for ( i = 0; i < len; i++ )
h ^= ( h << 5 ) + ( h >> 2 ) + p[i];
return h;
}
This code works no problem. However, when trying to use ngx_hash_key as
follows:
ffi.cdef[[
typedef uintptr_t ngx_uint_t;
ngx_uint_t ngx_hash_key(char *key, size_t len);
]]
local function hash(str)
local buf = ffi.cast("char *", str)
local c = ffi.C.ngx_hash_key(buf, string.len(str))
ngx.say(c)
end
I see the following error:
bad argument #1 to 'say' (string, number, boolean, nil, ngx.null, or
array table expected, but got cdata).
Based on http://luajit.org/ext_ffi_semantics.html, I think this is
because ngx_hash_key is getting converted to '64 bit int cdata', not a
number. Any input on how to get this value to a type that Lua can
properly handle? Thanks!