luaevent/src/luaevent.c

Mon, 11 Jun 2007 01:52:16 +0000

author
Thomas Harning Jr <harningt@gmail.com>
date
Mon, 11 Jun 2007 01:52:16 +0000
changeset 4
4d0e9388214a
parent 3
5999243fab1d
child 7
e13f7c9cb6c7
permissions
-rw-r--r--

Fixed stack overflow issue. (Forgot to pop error/integer)

1
31c782cfe7fe * Adjusted licensing and added README.
Thomas Harning Jr <harningt@gmail.com>
parents: 0
diff changeset
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
31c782cfe7fe * Adjusted licensing and added README.
Thomas Harning Jr <harningt@gmail.com>
parents: 0
diff changeset
2 * Licensed as LGPL - See doc/COPYING for details */
31c782cfe7fe * Adjusted licensing and added README.
Thomas Harning Jr <harningt@gmail.com>
parents: 0
diff changeset
3
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
4 #include "luaevent.h"
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
5
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
6 #include <lua.h>
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
7 #include <lauxlib.h>
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
8
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
9 #define EVENT_BASE_MT "EVENT_BASE_MT"
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
10 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
11 #define EVENT_BASE_LOCATION 1
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
12
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
13 void setEventBase(lua_State* L, struct event_base* base) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
14 struct event_base** pbase = lua_newuserdata(L, sizeof(base));
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
15 *pbase = base;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
16 luaL_getmetatable(L, EVENT_BASE_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
17 lua_setmetatable(L, -2);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
18 lua_rawseti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
19 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
20 struct event_base* getEventBase(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
21 struct event_base* base;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
22 lua_rawgeti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
23 base = *(struct event_base**)lua_topointer(L, -1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
24 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
25 return base;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
26 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
27
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
28 void freeCallbackArgs(le_callback* arg) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
29 if(arg->L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
30 lua_State* L = arg->L;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
31 arg->L = NULL;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
32 event_del(&arg->ev);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
33 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
34 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
35 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
36 /* le_callback is allocated at the beginning of the coroutine in which it
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
37 is used, no need to manually de-allocate */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
38
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
39 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
40 static void luaevent_callback(int fd, short event, void* p) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
41 le_callback* arg = p;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
42 lua_State* L = arg->L;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
43 int ret;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
44 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
45 lua_pushinteger(L, event);
3
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
46 if(lua_pcall(L, 1, 1, 0) || !lua_isnumber(L, -1)) {
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
47 printf("ERROR IN CB: %s\n", lua_tostring(L, -1));
4
4d0e9388214a Fixed stack overflow issue. (Forgot to pop error/integer)
Thomas Harning Jr <harningt@gmail.com>
parents: 3
diff changeset
48 lua_pop(L, 1);
3
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
49 freeCallbackArgs(arg);
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
50 return;
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
51 }
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
52 ret = lua_tointeger(L, -1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
53 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
54 if(ret == -1) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
55 freeCallbackArgs(arg);
3
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
56 return;
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
57 }
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
58 if(ret != EV_READ && ret != EV_WRITE) {
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
59 printf("BAD RET_VAL: %i\n", ret);
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
60 }
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
61
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
62 struct event *ev = &arg->ev;
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
63 int newEvent = ret;
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
64 if(newEvent != event) { // Need to hook up new event...
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
65 event_del(ev);
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
66 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
67 event_add(ev, NULL);
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
68 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
69 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
70
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
71 static int luaevent_base_gc(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
72 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
73 if(*pbase) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
74 event_base_free(*pbase);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
75 *pbase = NULL;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
76 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
77 return 0;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
78 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
79
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
80 static int luaevent_cb_gc(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
81 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
82 freeCallbackArgs(arg);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
83 return 0;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
84 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
85
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
86 static int luaevent_cb_getfd(lua_State* L) {
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
87 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
88 lua_pushinteger(L, arg->ev.ev_fd);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
89 return 1;
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
90 }
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
91
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
92 int getSocketFd(lua_State* L, int idx) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
93 int fd;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
94 luaL_checktype(L, idx, LUA_TUSERDATA);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
95 lua_getfield(L, idx, "getfd");
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
96 if(lua_isnil(L, -1))
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
97 return luaL_error(L, "Socket type missing 'getfd' method");
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
98 lua_pushvalue(L, idx);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
99 lua_call(L, 1, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
100 fd = lua_tointeger(L, -1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
101 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
102 return fd;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
103 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
104
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
105 /* Expected to be called at the beginning of the coro that uses it..
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
106 Value must be kept until coro is complete....
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
107 */
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
108 /* sock, callback */
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
109 static int luaevent_addevent(lua_State* L) {
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
110 int fd, callbackRef;
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
111 int top, ret;
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
112 le_callback* arg;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
113 fd = getSocketFd(L, 1);
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
114 luaL_checktype(L, 2, LUA_TFUNCTION);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
115 top = lua_gettop(L);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
116 /* Preserve the callback function */
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
117 lua_pushvalue(L, 2);
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
118 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
119
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
120 /* Call the callback with all arguments after it to get the loop primed.. */
3
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
121 if(lua_pcall(L, top - 2, 1, 0) || !lua_isnumber(L, -1)) {
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
122 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1));
4
4d0e9388214a Fixed stack overflow issue. (Forgot to pop error/integer)
Thomas Harning Jr <harningt@gmail.com>
parents: 3
diff changeset
123 lua_pop(L, 1);
3
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
124 return 0;
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
125 }
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
126 ret = lua_tointeger(L, -1);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
127 lua_pop(L, 1);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
128 if(ret == -1) { /* Done, no need to setup event */
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
129 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
130 return 0;
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
131 }
3
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
132 if(ret != EV_READ && ret != EV_WRITE) {
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
133 printf("BAD RET_VAL IN INIT: %i\n", ret);
5999243fab1d * Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents: 2
diff changeset
134 }
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
135 arg = lua_newuserdata(L, sizeof(*arg));
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
136 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
137 lua_setmetatable(L, -2);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
138
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
139 arg->L = L;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
140 arg->callbackRef = callbackRef;
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
141
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
142 /* Setup event... */
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
143 event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg);
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
144 event_base_set(getEventBase(L), &arg->ev);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
145 event_add(&arg->ev, NULL);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
146 return 1;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
147 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
148
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
149 static int luaevent_loop(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
150 int ret = event_base_loop(getEventBase(L), 0);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
151 lua_pushinteger(L, ret);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
152 return 1;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
153 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
154
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
155 static luaL_Reg funcs[] = {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
156 { "addevent", luaevent_addevent },
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
157 { "loop", luaevent_loop },
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
158 { NULL, NULL }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
159 };
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
160
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
161 typedef struct {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
162 const char* name;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
163 int value;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
164 } namedInteger;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
165
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
166 static namedInteger consts[] = {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
167 {"LEAVE", -1},
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
168 {"EV_READ", EV_READ},
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
169 {"EV_WRITE", EV_WRITE},
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
170 {NULL, 0}
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
171 };
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
172
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
173 void setNamedIntegers(lua_State* L, namedInteger* p) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
174 while(p->name) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
175 lua_pushinteger(L, p->value);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
176 lua_setfield(L, -2, p->name);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
177 p++;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
178 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
179 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
180
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
181 /* Verified ok */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
182 int luaopen_luaevent_core(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
183 /* Setup environ table */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
184 lua_createtable(L, 1, 0);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
185 lua_replace(L, LUA_ENVIRONINDEX);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
186 /* Setup metatable */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
187 luaL_newmetatable(L, EVENT_BASE_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
188 lua_pushcfunction(L, luaevent_base_gc);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
189 lua_setfield(L, -2, "__gc");
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
190 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
191 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
192 lua_pushcfunction(L, luaevent_cb_gc);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
193 lua_setfield(L, -2, "__gc");
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
194 lua_pushcfunction(L, luaevent_cb_getfd);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
195 lua_setfield(L, -2, "getfd");
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
196 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
197
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
198 setEventBase(L, event_init());
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
199
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
200 luaL_register(L, "luaevent.core", funcs);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
201 setNamedIntegers(L, consts);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
202 return 1;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
203 }

mercurial