forked from brimworks/lua-ev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua_ev.c
167 lines (137 loc) · 4.11 KB
/
lua_ev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <assert.h>
#include <ev.h>
#include <lauxlib.h>
#include <lua.h>
#include "lua_ev.h"
static char lua_ev_loop_mt[] = "ev{loop}";
static char lua_ev_io_mt[] = "ev{io}";
static char lua_ev_timer_mt[] = "ev{timer}";
static char lua_ev_signal_mt[] = "ev{signal}";
static char lua_ev_idle_mt[] = "ev{idle}";
static char lua_ev_child_mt[] = "ev{child}";
static char lua_ev_stat_mt[] = "ev{stat}";
/* We make everything static, so we just include all *.c files in a
* single compilation unit. */
#include "obj_lua_ev.c"
#include "loop_lua_ev.c"
#include "watcher_lua_ev.c"
#include "io_lua_ev.c"
#include "timer_lua_ev.c"
#include "signal_lua_ev.c"
#include "idle_lua_ev.c"
#include "child_lua_ev.c"
#include "stat_lua_ev.c"
static const luaL_reg R[] = {
{"version", version},
{NULL, NULL},
};
static char *traceback_key = "LUA_EV_TRACEBACK_KEY";
static void push_traceback(lua_State *L) {
lua_pushlightuserdata(L, traceback_key);
lua_gettable(L, LUA_REGISTRYINDEX); /* registry[<traceback_key>] */
}
static void save_traceback(lua_State *L) {
lua_pushlightuserdata(L, traceback_key);
/* save reference to 'debug.traceback' */
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if ( !lua_istable(L, -1) ) {
luaL_error(L, "Can't get global 'debug'");
}
lua_getfield(L, -1, "traceback");
if ( !lua_isfunction(L, -1) ) {
luaL_error(L, "Can't get 'debug.trackeback' function");
}
lua_remove(L, -2); /* remove 'debug' table from stack. */
lua_settable(L, LUA_REGISTRYINDEX); /* registry[<traceback_key>] = debug.traceback */
}
/**
* Entry point into the 'ev' lua library. Validates that the
* dynamically linked libev version matches, creates the object
* registry, and creates the table returned by require().
*/
LUALIB_API int luaopen_ev(lua_State *L) {
assert(ev_version_major() == EV_VERSION_MAJOR &&
ev_version_minor() >= EV_VERSION_MINOR);
save_traceback(L);
luaL_register(L, "ev", R);
#define CONSTANT(def, name) do { \
lua_pushinteger(L, def); \
lua_setfield(L, -2, name); \
} while(0);
#if EV_VERSION_MAJOR >= 4
CONSTANT(EVRUN_NOWAIT, "NOWAIT");
CONSTANT(EVRUN_ONCE, "ONCE");
CONSTANT(EVBREAK_CANCEL, "CANCEL");
CONSTANT(EVBREAK_ONE, "ONE");
CONSTANT(EVBREAK_ALL, "ALL");
#else
/* use names from 4.x */
CONSTANT(EVLOOP_NONBLOCK, "NOWAIT");
CONSTANT(EVLOOP_ONESHOT, "ONCE");
CONSTANT(EVUNLOOP_CANCEL, "CANCEL");
CONSTANT(EVUNLOOP_ONE, "ONE");
CONSTANT(EVUNLOOP_ALL, "ALL");
#endif
#undef CONSTANT
luaopen_ev_loop(L);
lua_setfield(L, -2, "Loop");
luaopen_ev_timer(L);
lua_setfield(L, -2, "Timer");
luaopen_ev_io(L);
lua_setfield(L, -2, "IO");
luaopen_ev_signal(L);
lua_setfield(L, -2, "Signal");
luaopen_ev_idle(L);
lua_setfield(L, -2, "Idle");
luaopen_ev_child(L);
lua_setfield(L, -2, "Child");
luaopen_ev_stat(L);
lua_setfield(L, -2, "Stat");
#define CONSTANT(name) do { \
lua_pushinteger(L, EV_ ## name); \
lua_setfield(L, -2, #name); \
} while(0);
CONSTANT(READ);
CONSTANT(WRITE);
CONSTANT(TIMEOUT);
CONSTANT(SIGNAL);
CONSTANT(IDLE);
CONSTANT(CHILD);
CONSTANT(STAT);
CONSTANT(MINPRI);
CONSTANT(MAXPRI);
#undef CONSTANT
return 1;
}
/**
* Push the major and minor version of libev onto the stack.
*
* [+2, -0, -]
*/
static int version(lua_State *L) {
lua_pushnumber(L, ev_version_major());
lua_pushnumber(L, ev_version_minor());
return 2;
}
/**
* Taken from lua.c out of the lua source distribution. Use this
* function when doing lua_pcall().
*/
static int traceback(lua_State *L) {
if ( !lua_isstring(L, 1) ) return 1;
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if ( !lua_istable(L, -1) ) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if ( !lua_isfunction(L, -1) ) {
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1); /* pass error message */
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
return 1;
}
/* vi:set expandtab ts=4: */