-
Notifications
You must be signed in to change notification settings - Fork 55
/
lua_ev.c
163 lines (137 loc) · 3.76 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
#include <assert.h>
#include <ev.h>
#include <lauxlib.h>
#include <lua.h>
#include <signal.h>
#include "lua_ev.h"
/* 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 "async_lua_ev.c"
#include "child_lua_ev.c"
#include "stat_lua_ev.c"
static const luaL_Reg R[] = {
{"version", version},
{"object_count", obj_count},
{NULL, NULL},
};
/**
* 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);
create_obj_registry(L);
#if LUA_VERSION_NUM > 501
luaL_newlib(L, R);
#else
luaL_register(L, "ev", R);
#endif
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_async(L);
lua_setfield(L, -2, "Async");
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 EV_SETCONST(state, prefix, C) \
lua_pushnumber(L, prefix ## C); \
lua_setfield(L, -2, #C)
EV_SETCONST(L, EV_, CHILD);
EV_SETCONST(L, EV_, IDLE);
EV_SETCONST(L, EV_, ASYNC);
EV_SETCONST(L, EV_, MINPRI);
EV_SETCONST(L, EV_, MAXPRI);
EV_SETCONST(L, EV_, READ);
EV_SETCONST(L, EV_, SIGNAL);
EV_SETCONST(L, EV_, STAT);
EV_SETCONST(L, EV_, TIMEOUT);
EV_SETCONST(L, EV_, WRITE);
EV_SETCONST(L, , SIGABRT);
EV_SETCONST(L, , SIGALRM);
EV_SETCONST(L, , SIGBUS);
EV_SETCONST(L, , SIGCHLD);
EV_SETCONST(L, , SIGCONT);
EV_SETCONST(L, , SIGFPE);
EV_SETCONST(L, , SIGHUP);
EV_SETCONST(L, , SIGINT);
EV_SETCONST(L, , SIGIO);
EV_SETCONST(L, , SIGIOT);
EV_SETCONST(L, , SIGKILL);
EV_SETCONST(L, , SIGPIPE);
#ifdef SIGPOLL
EV_SETCONST(L, , SIGPOLL);
#endif
EV_SETCONST(L, , SIGPROF);
#ifdef SIGPWR
EV_SETCONST(L, , SIGPWR);
#endif
EV_SETCONST(L, , SIGQUIT);
EV_SETCONST(L, , SIGSEGV);
#ifdef SIGSTKFLT
EV_SETCONST(L, , SIGSTKFLT);
#endif
EV_SETCONST(L, , SIGSYS);
EV_SETCONST(L, , SIGTERM);
EV_SETCONST(L, , SIGTRAP);
EV_SETCONST(L, , SIGTSTP);
EV_SETCONST(L, , SIGTTIN);
EV_SETCONST(L, , SIGTTOU);
EV_SETCONST(L, , SIGURG);
EV_SETCONST(L, , SIGUSR1);
EV_SETCONST(L, , SIGUSR2);
EV_SETCONST(L, , SIGVTALRM);
EV_SETCONST(L, , SIGWINCH);
EV_SETCONST(L, , SIGXCPU);
EV_SETCONST(L, , SIGXFSZ);
#undef EV_SETCONST
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_getglobal(L, "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;
}