luaevent/src/luaevent.c

Mon, 11 Jun 2007 01:08:59 +0000

author
Thomas Harning Jr <harningt@gmail.com>
date
Mon, 11 Jun 2007 01:08:59 +0000
changeset 2
01b3a96ae760
parent 1
31c782cfe7fe
child 3
5999243fab1d
permissions
-rw-r--r--

* Completed mostly working version
* Moved to a mode where addevent calls a callback rather than
it being instantiated within.
* If the callback returns -1, then no event is ever setup
* Otherwise the integer value is used to setup the event
* This allows for using coroutine.wrap rather than a cooked-up wrapper
* Tests work, although there are a few remaining issues:
* Need to figure a good way of preserving the event object,
not sure if current method is good enough, since the socket
is the only anchor, and it is only held inside the coro..
circular reference, something that Lua 'handles' well.
* Doing more than the maximum sockets the process is allows
causes strangeness to occur in libevent.. somehow
it is getting around to epoll_add which is causing valgrind
to barf.

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);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
46 lua_call(L, 1, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
47 ret = lua_tointeger(L, -1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
48 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
49 if(ret == -1) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
50 freeCallbackArgs(arg);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
51 } else {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
52 struct event *ev = &arg->ev;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
53 int newEvent = ret;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
54 if(newEvent != event) { // Need to hook up new event...
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
55 event_del(ev);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
56 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
57 event_add(ev, NULL);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
58 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
59 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
60 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
61
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
62 static int luaevent_base_gc(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
63 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
64 if(*pbase) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
65 event_base_free(*pbase);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
66 *pbase = NULL;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
67 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
68 return 0;
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_cb_gc(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
72 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
73 freeCallbackArgs(arg);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
74 return 0;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
75 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
76
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
77 static int luaevent_cb_getfd(lua_State* L) {
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
78 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
79 lua_pushinteger(L, arg->ev.ev_fd);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
80 return 1;
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
81 }
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
82
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
83 int getSocketFd(lua_State* L, int idx) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
84 int fd;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
85 luaL_checktype(L, idx, LUA_TUSERDATA);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
86 lua_getfield(L, idx, "getfd");
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
87 if(lua_isnil(L, -1))
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
88 return luaL_error(L, "Socket type missing 'getfd' method");
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
89 lua_pushvalue(L, idx);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
90 lua_call(L, 1, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
91 fd = lua_tointeger(L, -1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
92 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
93 return fd;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
94 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
95
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
96 /* 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
97 Value must be kept until coro is complete....
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
98 */
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
99 /* sock, callback */
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
100 static int luaevent_addevent(lua_State* L) {
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
101 int fd, callbackRef;
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
102 int top, ret;
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
103 le_callback* arg;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
104 fd = getSocketFd(L, 1);
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
105 luaL_checktype(L, 2, LUA_TFUNCTION);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
106 top = lua_gettop(L);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
107 /* Preserve the callback function */
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
108 lua_pushvalue(L, 2);
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
109 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
110
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
111 /* Call the callback with all arguments after it to get the loop primed.. */
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
112 lua_call(L, top - 2, 1);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
113 ret = lua_tointeger(L, -1);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
114 lua_pop(L, 1);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
115 if(ret == -1) { /* Done, no need to setup event */
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
116 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
117 return 0;
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
118 }
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
119 arg = lua_newuserdata(L, sizeof(*arg));
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
120 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
121 lua_setmetatable(L, -2);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
122
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
123 arg->L = L;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
124 arg->callbackRef = callbackRef;
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
125
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
126 /* Setup event... */
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
127 event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg);
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
128 event_base_set(getEventBase(L), &arg->ev);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
129 event_add(&arg->ev, NULL);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
130 return 1;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
131 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
132
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
133 static int luaevent_loop(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
134 int ret = event_base_loop(getEventBase(L), 0);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
135 lua_pushinteger(L, ret);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
136 return 1;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
137 }
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 static luaL_Reg funcs[] = {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
140 { "addevent", luaevent_addevent },
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
141 { "loop", luaevent_loop },
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
142 { NULL, NULL }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
143 };
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
144
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
145 typedef struct {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
146 const char* name;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
147 int value;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
148 } namedInteger;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
149
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
150 static namedInteger consts[] = {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
151 {"LEAVE", -1},
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
152 {"EV_READ", EV_READ},
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
153 {"EV_WRITE", EV_WRITE},
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
154 {NULL, 0}
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
155 };
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
156
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
157 void setNamedIntegers(lua_State* L, namedInteger* p) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
158 while(p->name) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
159 lua_pushinteger(L, p->value);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
160 lua_setfield(L, -2, p->name);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
161 p++;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
162 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
163 }
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
164
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
165 /* Verified ok */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
166 int luaopen_luaevent_core(lua_State* L) {
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
167 /* Setup environ table */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
168 lua_createtable(L, 1, 0);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
169 lua_replace(L, LUA_ENVIRONINDEX);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
170 /* Setup metatable */
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
171 luaL_newmetatable(L, EVENT_BASE_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
172 lua_pushcfunction(L, luaevent_base_gc);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
173 lua_setfield(L, -2, "__gc");
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
174 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
175 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
176 lua_pushcfunction(L, luaevent_cb_gc);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
177 lua_setfield(L, -2, "__gc");
2
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
178 lua_pushcfunction(L, luaevent_cb_getfd);
01b3a96ae760 * Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents: 1
diff changeset
179 lua_setfield(L, -2, "getfd");
0
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
180 lua_pop(L, 1);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
181
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
182 setEventBase(L, event_init());
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
183
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
184 luaL_register(L, "luaevent.core", funcs);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
185 setNamedIntegers(L, consts);
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
186 return 1;
f2e807614be9 Initial commit:
Thomas Harning Jr <harningt@gmail.com>
parents:
diff changeset
187 }

mercurial