event_callback: Fix stack slot leak
[luaevent.git] / src / luaevent.c
blobc9b54305664a58440f84dc3a7934ef02767063d4
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
4 #include "luaevent.h"
5 #include "event_callback.h"
6 #include "event_buffer.h"
7 #include "buffer_event.h"
9 #include <lua.h>
10 #include <lauxlib.h>
11 #include <assert.h>
13 #define EVENT_BASE_MT "EVENT_BASE_MT"
15 #ifdef _WIN32
16 #include <winsock2.h>
17 #endif
19 le_base* event_base_get(lua_State* L, int idx) {
20 return (le_base*)luaL_checkudata(L, idx, EVENT_BASE_MT);
23 int luaevent_newbase(lua_State* L) {
24 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
25 base->loop_L = NULL; /* No running loop */
26 base->base = event_init();
27 luaL_getmetatable(L, EVENT_BASE_MT);
28 lua_setmetatable(L, -2);
29 return 1;
32 static int luaevent_base_gc(lua_State* L) {
33 le_base *base = event_base_get(L, 1);
34 if(base->base) {
35 event_base_free(base->base);
36 base->base = NULL;
38 return 0;
41 int getSocketFd(lua_State* L, int idx) {
42 int fd;
43 luaL_checktype(L, idx, LUA_TUSERDATA);
44 lua_getfield(L, idx, "getfd");
45 if(lua_isnil(L, -1))
46 return luaL_error(L, "Socket type missing 'getfd' method");
47 lua_pushvalue(L, idx);
48 lua_call(L, 1, 1);
49 fd = lua_tointeger(L, -1);
50 lua_pop(L, 1);
51 return fd;
54 void load_timeval(double time, struct timeval *tv) {
55 tv->tv_sec = (int)time;
56 tv->tv_usec = (int)(time * 1000000) % 1000000;
59 /* sock, event, callback, timeout */
60 static int luaevent_addevent(lua_State* L) {
61 int fd, event;
62 le_callback* arg = event_callback_push(L, 1, 4);
63 struct timeval *tv = &arg->timeout;
64 if(lua_isnil(L, 2) && lua_isnumber(L, 5)) {
65 fd = -1; /* Per event_timer_set.... */
66 } else {
67 fd = getSocketFd(L, 2);
69 event = luaL_checkinteger(L, 3);
70 if(lua_isnumber(L, 5)) {
71 double time = lua_tonumber(L, 5);
72 load_timeval(time, tv);
73 } else {
74 tv = NULL;
77 /* Setup event... */
78 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
79 event_base_set(arg->base->base, &arg->ev);
80 event_add(&arg->ev, tv);
81 return 1;
84 static int luaevent_loop(lua_State* L) {
85 le_base *base = event_base_get(L, 1);
86 base->loop_L = L;
87 int ret = event_base_loop(base->base, 0);
88 lua_pushinteger(L, ret);
89 return 1;
92 static luaL_Reg base_funcs[] = {
93 { "addevent", luaevent_addevent },
94 { "loop", luaevent_loop },
95 { NULL, NULL }
98 static luaL_Reg funcs[] = {
99 { "new", luaevent_newbase },
100 { NULL, NULL }
103 typedef struct {
104 const char* name;
105 int value;
106 } namedInteger;
108 static namedInteger consts[] = {
109 {"LEAVE", -1},
110 {"EV_READ", EV_READ},
111 {"EV_WRITE", EV_WRITE},
112 {"EV_TIMEOUT", EV_TIMEOUT},
113 {"EV_SIGNAL", EV_SIGNAL},
114 {"EV_PERSIST", EV_PERSIST},
115 /* bufferevent */
116 {"EVBUFFER_READ", EVBUFFER_READ},
117 {"EVBUFFER_WRITE", EVBUFFER_WRITE},
118 {"EVBUFFER_EOF", EVBUFFER_EOF},
119 {"EVBUFFER_ERROR", EVBUFFER_ERROR},
120 {"EVBUFFER_TIMEOUT", EVBUFFER_TIMEOUT},
121 {NULL, 0}
124 void setNamedIntegers(lua_State* L, namedInteger* p) {
125 while(p->name) {
126 lua_pushinteger(L, p->value);
127 lua_setfield(L, -2, p->name);
128 p++;
132 /* Verified ok */
133 int luaopen_luaevent_core(lua_State* L) {
134 #ifdef _WIN32
135 WORD wVersionRequested = MAKEWORD(2, 2);
136 WSADATA wsaData;
137 WSAStartup(wVersionRequested, &wsaData);
138 #endif
139 event_init( );
140 /* Register external items */
141 event_callback_register(L);
142 event_buffer_register(L);
143 buffer_event_register(L);
144 lua_settop(L, 0);
145 /* Setup metatable */
146 luaL_newmetatable(L, EVENT_BASE_MT);
147 lua_newtable(L);
148 luaL_register(L, NULL, base_funcs);
149 lua_setfield(L, -2, "__index");
150 lua_pushcfunction(L, luaevent_base_gc);
151 lua_setfield(L, -2, "__gc");
152 lua_pop(L, 1);
154 luaL_register(L, "luaevent.core", funcs);
155 setNamedIntegers(L, consts);
156 return 1;