* Adjusted licensing and added README.
[luaevent.git] / luaevent / src / luaevent.c
blob311ad0efe8eb592259120516315889ef2812555e
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
4 #include "luaevent.h"
6 #include <lua.h>
7 #include <lauxlib.h>
9 #define EVENT_BASE_MT "EVENT_BASE_MT"
10 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
11 #define EVENT_BASE_LOCATION 1
13 void setEventBase(lua_State* L, struct event_base* base) {
14 struct event_base** pbase = lua_newuserdata(L, sizeof(base));
15 *pbase = base;
16 luaL_getmetatable(L, EVENT_BASE_MT);
17 lua_setmetatable(L, -2);
18 lua_rawseti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
20 struct event_base* getEventBase(lua_State* L) {
21 struct event_base* base;
22 lua_rawgeti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
23 base = *(struct event_base**)lua_topointer(L, -1);
24 lua_pop(L, 1);
25 return base;
28 void freeCallbackArgs(le_callback* arg) {
29 if(arg->L) {
30 lua_State* L = arg->L;
31 arg->L = NULL;
32 event_del(&arg->ev);
33 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
36 /* le_callback is allocated at the beginning of the coroutine in which it
37 is used, no need to manually de-allocate */
39 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
40 static void luaevent_callback(int fd, short event, void* p) {
41 le_callback* arg = p;
42 lua_State* L = arg->L;
43 int ret;
44 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
45 lua_pushinteger(L, event);
46 lua_call(L, 1, 1);
47 ret = lua_tointeger(L, -1);
48 lua_pop(L, 1);
49 if(ret == -1) {
50 freeCallbackArgs(arg);
51 } else {
52 struct event *ev = &arg->ev;
53 int newEvent = ret;
54 if(newEvent != event) { // Need to hook up new event...
55 event_del(ev);
56 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
57 event_add(ev, NULL);
62 static int luaevent_base_gc(lua_State* L) {
63 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
64 if(*pbase) {
65 event_base_free(*pbase);
66 *pbase = NULL;
68 return 0;
71 static int luaevent_cb_gc(lua_State* L) {
72 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
73 freeCallbackArgs(arg);
74 return 0;
77 int getSocketFd(lua_State* L, int idx) {
78 int fd;
79 luaL_checktype(L, idx, LUA_TUSERDATA);
80 lua_getfield(L, idx, "getfd");
81 if(lua_isnil(L, -1))
82 return luaL_error(L, "Socket type missing 'getfd' method");
83 lua_pushvalue(L, idx);
84 lua_call(L, 1, 1);
85 fd = lua_tointeger(L, -1);
86 lua_pop(L, 1);
87 return fd;
90 /* Expected to be called at the beginning of the coro that uses it..
91 Value must be kept until coro is complete....
93 /* sock, event, callback */
94 static int luaevent_addevent(lua_State* L) {
95 int fd, event, callbackRef;
96 le_callback* arg;
97 fd = getSocketFd(L, 1);
98 event = luaL_checkinteger(L, 2);
99 luaL_checktype(L, 3, LUA_TFUNCTION);
100 lua_pushvalue(L, 3);
101 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
102 arg = lua_newuserdata(L, sizeof(*arg));
103 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
104 lua_setmetatable(L, -2);
106 arg->L = L;
107 arg->callbackRef = callbackRef;
108 /* Setup event... */
109 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
110 event_base_set(getEventBase(L), &arg->ev);
111 event_add(&arg->ev, NULL);
112 return 1;
115 static int luaevent_loop(lua_State* L) {
116 int ret = event_base_loop(getEventBase(L), 0);
117 lua_pushinteger(L, ret);
118 return 1;
121 static luaL_Reg funcs[] = {
122 { "addevent", luaevent_addevent },
123 { "loop", luaevent_loop },
124 { NULL, NULL }
127 typedef struct {
128 const char* name;
129 int value;
130 } namedInteger;
132 static namedInteger consts[] = {
133 {"LEAVE", -1},
134 {"EV_READ", EV_READ},
135 {"EV_WRITE", EV_WRITE},
136 {NULL, 0}
139 void setNamedIntegers(lua_State* L, namedInteger* p) {
140 while(p->name) {
141 lua_pushinteger(L, p->value);
142 lua_setfield(L, -2, p->name);
143 p++;
147 /* Verified ok */
148 int luaopen_luaevent_core(lua_State* L) {
149 /* Setup environ table */
150 lua_createtable(L, 1, 0);
151 lua_replace(L, LUA_ENVIRONINDEX);
152 /* Setup metatable */
153 luaL_newmetatable(L, EVENT_BASE_MT);
154 lua_pushcfunction(L, luaevent_base_gc);
155 lua_setfield(L, -2, "__gc");
156 lua_pop(L, 1);
157 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
158 lua_pushcfunction(L, luaevent_cb_gc);
159 lua_setfield(L, -2, "__gc");
160 lua_pop(L, 1);
162 setEventBase(L, event_init());
164 luaL_register(L, "luaevent.core", funcs);
165 setNamedIntegers(L, consts);
166 return 1;