Merge branch 'release/0.4.6'
[luaevent.git] / src / event_callback.c
blob1cf67b01b1d671c40d2fad2e9ff5947bc1a2db9e
1 /* LuaEvent
2 Copyright (C) 2007,2012,2013 Thomas Harning <harningt@gmail.com>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
23 #include "event_callback.h"
24 #include <assert.h>
25 #include <lauxlib.h>
26 #include <string.h>
28 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
30 void freeCallbackArgs(le_callback* arg, lua_State* L) {
31 if(arg->base) {
32 arg->base = NULL;
33 event_del(&arg->ev);
34 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
37 /* le_callback is allocated at the beginning of the coroutine in which it
38 is used, no need to manually de-allocate */
40 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
41 void luaevent_callback(int fd, short event, void* p) {
42 le_callback* cb = p;
43 lua_State* L;
44 int ret, errfunc;
45 struct timeval new_tv = { 0, 0 };
46 le_base* base;
47 assert(cb);
48 if(!cb->base)
49 return; /* Event has already been collected + destroyed */
50 assert(cb->base->loop_L);
51 L = cb->base->loop_L;
53 errfunc = 0;
55 lua_getglobal(L, "debug");
57 if(lua_istable(L, -1)) {
58 lua_getfield(L, -1, "traceback");
60 if(lua_isfunction(L, -1)) {
61 lua_remove(L, -2);
62 errfunc = lua_gettop(L);
63 } else {
64 lua_pop(L, 2);
66 } else {
67 lua_pop(L, 1);
70 lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef);
71 lua_pushinteger(L, event);
72 /* cb->base may be NULL after the pcall, if the event is destroyed */
73 base = cb->base;
75 ret = lua_pcall(L, 1, 2, errfunc);
76 if(errfunc) {
77 lua_remove(L, errfunc);
79 if(ret)
81 base->errorMessage = luaL_ref(L, LUA_REGISTRYINDEX);
82 event_base_loopbreak(base->base);
83 lua_pop(L, 1);
84 return;
86 if(!cb->base) {
87 lua_pop(L, 2);
88 return; /* event was destroyed during callback */
90 /* If nothing is returned, re-use the old event value */
91 ret = luaL_optinteger(L, -2, event);
92 /* Clone the old timeout value in case a new one wasn't set */
93 memcpy(&new_tv, &cb->timeout, sizeof(new_tv));
94 if(lua_isnumber(L, -1)) {
95 double newTimeout = lua_tonumber(L, -1);
96 if(newTimeout >= 0) {
97 load_timeval(newTimeout, &new_tv);
100 lua_pop(L, 2);
101 if(ret == -1) {
102 freeCallbackArgs(cb, L);
103 } else {
104 struct event *ev = &cb->ev;
105 int newEvent = ret;
106 if( newEvent != event || (cb->timeout.tv_sec != new_tv.tv_sec || cb->timeout.tv_usec != new_tv.tv_usec) ) {
107 struct timeval *ptv = &cb->timeout;
108 cb->timeout = new_tv;
109 event_del(ev);
110 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, cb);
111 /* Assume cannot set a new timeout.. */
112 event_add(ev, ptv);
117 static int luaevent_cb_gc(lua_State* L) {
118 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
119 freeCallbackArgs(arg, L);
120 return 0;
123 le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) {
124 le_callback* cb;
125 le_base *base = event_base_get(L, baseIdx);
126 luaL_checktype(L, callbackIdx, LUA_TFUNCTION);
127 cb = lua_newuserdata(L, sizeof(*cb));
128 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
129 lua_setmetatable(L, -2);
131 lua_pushvalue(L, callbackIdx);
132 cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
133 cb->base = base;
134 memset(&cb->timeout, 0, sizeof(cb->timeout));
135 return cb;
138 void event_callback_register(lua_State* L) {
139 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
140 lua_pushcfunction(L, luaevent_cb_gc);
141 lua_setfield(L, -2, "__gc");
142 lua_newtable(L);
143 lua_pushcfunction(L, luaevent_cb_gc);
144 lua_setfield(L, -2, "close");
145 lua_setfield(L, -2, "__index");
146 lua_pop(L, 1);