-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua-komihash.c
62 lines (55 loc) · 1.33 KB
/
lua-komihash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <lua.h>
#include <lauxlib.h>
#include "komihash/komihash.h"
static int
lhash64(lua_State *L) {
size_t len;
const char *str = luaL_checklstring(L, 1, &len);
uint64_t ret = komihash(str, len, luaL_optinteger(L, 2, 0));
lua_pushlstring(L, (const char *)&ret, sizeof(uint64_t));
return 1;
}
struct rand {
uint64_t seed1;
uint64_t seed2;
};
#define MT_NAME ("_komihash_metatable")
static int
lrand(lua_State *L) {
struct rand *r = luaL_checkudata(L, 1, MT_NAME);
uint64_t ret = komirand(&r->seed1, &r->seed2);
lua_pushlstring(L, (const char *)&ret, sizeof(uint64_t));
return 1;
}
static int
lmetatable(lua_State *L) {
if (luaL_newmetatable(L, MT_NAME)) {
luaL_Reg l[] = {
{"rand", lrand},
{ NULL, NULL }
};
luaL_newlib(L, l);
lua_setfield(L, -2, "__index");
}
return 1;
}
static int
lnew(lua_State *L) {
struct rand *r = lua_newuserdatauv(L, sizeof(struct rand), 0);
r->seed1 = luaL_checkinteger(L, 1);
r->seed2 = luaL_checkinteger(L, 2);
lmetatable(L);
lua_setmetatable(L, -2);
return 1;
}
LUAMOD_API int
luaopen_lkomihash(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "hash64", lhash64 },
{ "new", lnew },
{ NULL, NULL },
};
luaL_newlib(L, l);
return 1;
}