diff options
Diffstat (limited to 'src/l_timer.c')
-rw-r--r-- | src/l_timer.c | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/src/l_timer.c b/src/l_timer.c index 129fd13..f510e22 100644 --- a/src/l_timer.c +++ b/src/l_timer.c @@ -35,19 +35,35 @@ #include <sys/time.h> #include <string.h> -#include <stdio.h> #include <errno.h> #include "l_timer.h" -#define LUA_TIMER_UDATA_NAME "struct timeval" +typedef struct timeval timer_t; +#define LUA_TIMER_UDATA_NAME "timer_t" + +static int l_timer_add(lua_State *L); +static int l_timer_sub(lua_State *L); + +static timer_t* newtimerudata(lua_State *L) +{ + timer_t* tv = (timer_t*)lua_newuserdata(L, sizeof(timer_t)); + if(luaL_newmetatable(L, LUA_TIMER_UDATA_NAME)) { + lua_pushliteral(L, "__add"); + lua_pushcfunction(L, l_timer_add); + lua_settable(L, -3); + lua_pushliteral(L, "__sub"); + lua_pushcfunction(L, l_timer_sub); + lua_settable(L, -3); + } + lua_setmetatable(L, -2); + return tv; +} static int l_timer_new(lua_State *L) { int ms = luaL_optint(L, 1, 0); - struct timeval* tv = (struct timeval*)lua_newuserdata(L, sizeof(struct timeval)); - luaL_newmetatable(L, LUA_TIMER_UDATA_NAME); - lua_setmetatable(L, -2); + timer_t* tv = newtimerudata(L); tv->tv_sec = ms/1000; tv->tv_usec = (ms%1000)*1000; return 1; @@ -55,44 +71,38 @@ static int l_timer_new(lua_State *L) static int l_timer_now(lua_State *L) { - struct timeval* now = (struct timeval*)lua_newuserdata(L, sizeof(struct timeval)); + timer_t* now = newtimerudata(L); int ret = gettimeofday(now, NULL); if(ret) { lua_pushnil(L); lua_pushstring(L, strerror(errno)); // FIXXXXXME: strerror is not threadsafe!!! return 2; } - luaL_newmetatable(L, LUA_TIMER_UDATA_NAME); - lua_setmetatable(L, -2); return 1; } static int l_timer_add(lua_State *L) { - struct timeval* a = (struct timeval*)luaL_checkudata(L, 1, LUA_TIMER_UDATA_NAME); - struct timeval* b = (struct timeval*)luaL_checkudata(L, 2, LUA_TIMER_UDATA_NAME); - struct timeval* res = (struct timeval*)lua_newuserdata(L, sizeof(struct timeval)); - luaL_newmetatable(L, LUA_TIMER_UDATA_NAME); - lua_setmetatable(L, -2); + timer_t* a = (timer_t*)luaL_checkudata(L, 1, LUA_TIMER_UDATA_NAME); + timer_t* b = (timer_t*)luaL_checkudata(L, 2, LUA_TIMER_UDATA_NAME); + timer_t* res = newtimerudata(L); timeradd(a, b, res); return 1; } static int l_timer_sub(lua_State *L) { - struct timeval* a = (struct timeval*)luaL_checkudata(L, 1, LUA_TIMER_UDATA_NAME); - struct timeval* b = (struct timeval*)luaL_checkudata(L, 2, LUA_TIMER_UDATA_NAME); - struct timeval* res = (struct timeval*)lua_newuserdata(L, sizeof(struct timeval)); - luaL_newmetatable(L, LUA_TIMER_UDATA_NAME); - lua_setmetatable(L, -2); + timer_t* a = (timer_t*)luaL_checkudata(L, 1, LUA_TIMER_UDATA_NAME); + timer_t* b = (timer_t*)luaL_checkudata(L, 2, LUA_TIMER_UDATA_NAME); + timer_t* res = newtimerudata(L); timersub(a, b, res); return 1; } static int l_timer_cmp(lua_State *L) { - struct timeval* a = (struct timeval*)luaL_checkudata(L, 1, LUA_TIMER_UDATA_NAME); - struct timeval* b = (struct timeval*)luaL_checkudata(L, 2, LUA_TIMER_UDATA_NAME); + timer_t* a = (timer_t*)luaL_checkudata(L, 1, LUA_TIMER_UDATA_NAME); + timer_t* b = (timer_t*)luaL_checkudata(L, 2, LUA_TIMER_UDATA_NAME); if(timercmp(a, b, <)) lua_pushinteger(L, -1); else if(timercmp(a, b, >)) |