# HG changeset patch # User Thomas Harning Jr # Date 1295139081 18000 # Node ID f0f5046ba1b0391bcd3a226d01a2ff3020da665b # Parent 57662d96d8a8374d27b4153bb8d077fbdb9e7088# Parent 72cd8cb9b184716a2a8e82de92c25f5f9807bf5f Merge branch 'prosody-tree' diff -r 57662d96d8a8 -r f0f5046ba1b0 CHANGELOG --- a/CHANGELOG Sat Jan 15 19:46:55 2011 -0500 +++ b/CHANGELOG Sat Jan 15 19:51:21 2011 -0500 @@ -1,3 +1,15 @@ +luaevent-prosody 0.1.0 - 2010-02-15 + * Fixed stack slot leak in event callbacks + * Fixed stack slot leak when error occurs in callback + * Various compilation fixes for Windows/ANSI C + * Silence some compiler warnings + + Add base:loopexit() method, with timeout support + + Add base:method() to discover backend in use + + Add core.libevent_version() to detect libevent version + + Add _NAME and _VERSION fields to module table + + base:addevent() now accepts integer fd to watch + + Switched from premake to standard Makefile for building +====== 0.2.0 - 2007-09-21 + Reorganized project to better fit GIT + Refactored and cleaned sources diff -r 57662d96d8a8 -r f0f5046ba1b0 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Sat Jan 15 19:51:21 2011 -0500 @@ -0,0 +1,32 @@ +# Utilities +INSTALL = install +INSTALL_PROGRAM = $(INSTALL) +INSTALL_DATA = $(INSTALL) -m 644 + +# Flags +CFLAGS = -O3 -c -Wall -fpic +LDFLAGS = -shared + +# Directories +LUA_INC_DIR=/usr/include/lua5.1 +LUA_LIB_DIR=/usr/lib + +INSTALL_DIR_LUA=/usr/share/lua/5.1 +INSTALL_DIR_BIN=/usr/lib/lua/5.1 + +# Files +LUA_LIB = lua5.1 +LIB = core.so + +all: + $(CC) $(CFLAGS) -Iinclude -I$(LUA_INC_DIR) src/*.c + $(CC) $(LDFLAGS) -o $(LIB) *.o -L$(LUA_LIB_DIR) -l$(LUA_LIB) -levent + +install: all + $(INSTALL_DATA) -D lua/luaevent.lua $(DESTDIR)$(INSTALL_DIR_LUA)/luaevent.lua + $(INSTALL_PROGRAM) -D $(LIB) $(DESTDIR)$(INSTALL_DIR_BIN)/luaevent/$(LIB) + +clean: + rm *.so + rm *.o + diff -r 57662d96d8a8 -r f0f5046ba1b0 README --- a/README Sat Jan 15 19:46:55 2011 -0500 +++ b/README Sat Jan 15 19:51:21 2011 -0500 @@ -1,3 +1,13 @@ +luaevent-prosody is a (hopefully temporary) fork of the luaevent library. This fork is +maintained by Matthew Wild . + +Source repository: http://code.matthewwild.co.uk/luaevent-prosody + +This luaevent-prosody 0.1.0 release is based on luaevent 0.2.0. See CHANGELOG for more +information about the changes. + +=== Original README from luaevent 0.2.0 === + Description: This is a binding of libevent to Lua. It will serve as a drop-in replacement for copas, and eventually support more features (async DNS, HTTP, RPC...). diff -r 57662d96d8a8 -r f0f5046ba1b0 include/buffer_event.h --- a/include/buffer_event.h Sat Jan 15 19:46:55 2011 -0500 +++ b/include/buffer_event.h Sat Jan 15 19:51:21 2011 -0500 @@ -4,10 +4,6 @@ #define BUFFER_EVENT_H #include "luaevent.h" -#include -#include -#include -#include typedef struct { struct bufferevent* ev; diff -r 57662d96d8a8 -r f0f5046ba1b0 include/event_buffer.h --- a/include/event_buffer.h Sat Jan 15 19:46:55 2011 -0500 +++ b/include/event_buffer.h Sat Jan 15 19:51:21 2011 -0500 @@ -4,10 +4,6 @@ #define EVENT_BUFFER_H #include "luaevent.h" -#include -#include -#include -#include typedef struct { struct evbuffer* buffer; diff -r 57662d96d8a8 -r f0f5046ba1b0 include/event_callback.h --- a/include/event_callback.h Sat Jan 15 19:46:55 2011 -0500 +++ b/include/event_callback.h Sat Jan 15 19:51:21 2011 -0500 @@ -4,10 +4,6 @@ #define EVENT_CALLBACK #include "luaevent.h" -#include -#include -#include -#include typedef struct { struct event ev; diff -r 57662d96d8a8 -r f0f5046ba1b0 include/luaevent.h --- a/include/luaevent.h Sat Jan 15 19:46:55 2011 -0500 +++ b/include/luaevent.h Sat Jan 15 19:51:21 2011 -0500 @@ -5,7 +5,11 @@ #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include typedef struct { diff -r 57662d96d8a8 -r f0f5046ba1b0 lua/luaevent.lua --- a/lua/luaevent.lua Sat Jan 15 19:46:55 2011 -0500 +++ b/lua/luaevent.lua Sat Jan 15 19:51:21 2011 -0500 @@ -5,6 +5,9 @@ module("luaevent", package.seeall) require("luaevent.core") +_NAME = "luaevent-prosody"; +_VERSION = "0.1.0"; + local EV_READ = luaevent.core.EV_READ local EV_WRITE = luaevent.core.EV_WRITE local base = luaevent.core.new() diff -r 57662d96d8a8 -r f0f5046ba1b0 premake.lua --- a/premake.lua Sat Jan 15 19:46:55 2011 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -project.name = "luaevent.core" -project.libdir = "lib" -project.bindir = "bin" - -package = newpackage() -package.kind = "dll" -package.language = "c++" -package.targetprefix = "" -package.target = "core" - -package.links = { - "event" -} - -package.includepaths = { - "include", -} -if linux then - package.buildoptions = { "-Wall" } - package.config["Debug"].buildoptions = { "-O0" } - package.linkoptions = { "-Wall -L/usr/local/lib" } - package.postbuildcommands = { "mkdir -p test/luaevent", "cp bin/* test/luaevent", "cp lua/* test" } -else - print([[Other environements currently untested, may need tweaking]]) -end - -package.files = { - matchrecursive( - "src/*.c", - "include/*.h" - ) -} diff -r 57662d96d8a8 -r f0f5046ba1b0 src/buffer_event.c --- a/src/buffer_event.c Sat Jan 15 19:46:55 2011 -0500 +++ b/src/buffer_event.c Sat Jan 15 19:51:21 2011 -0500 @@ -1,7 +1,6 @@ /* LuaEvent - Copyright (C) 2007 Thomas Harning * Licensed as LGPL - See doc/COPYING for details */ #include "buffer_event.h" -#include "luaevent.h" #include "utility.h" #include #include @@ -49,7 +48,12 @@ /* func, bufferevent */ lua_pushinteger(L, what); /* What to do w/ errors...? */ - lua_pcall(L, 2, 0, 0); + if(!lua_pcall(L, 2, 0, 0)) + { + /* FIXME: Perhaps luaevent users should be + * able to set an error handler? */ + lua_pop(L, 1); /* Pop error message */ + } } static void buffer_event_readcb(struct bufferevent *ev, void *ptr) { diff -r 57662d96d8a8 -r f0f5046ba1b0 src/event_buffer.c --- a/src/event_buffer.c Sat Jan 15 19:46:55 2011 -0500 +++ b/src/event_buffer.c Sat Jan 15 19:51:21 2011 -0500 @@ -2,7 +2,6 @@ * Licensed as LGPL - See doc/COPYING for details */ #include "event_buffer.h" -#include "luaevent.h" #include #include @@ -201,6 +200,7 @@ } else if(lua_isuserdata(L, 2)) { ret = evbuffer_write(buf->buffer, getSocketFd(L, 2)); } else { + ret = 0; /* Shush uninitialized warning */ luaL_argerror(L, 2, "Unexpected data type. Expects: integer/lightuserdata/socket"); } lua_pushinteger(L, ret); @@ -222,6 +222,7 @@ } else if(lua_isuserdata(L, 2)) { ret = evbuffer_read(buf->buffer, getSocketFd(L, 2), len); } else { + ret = 0; /* Shush uninitialized warning */ luaL_argerror(L, 2, "Unexpected data type. Expects: integer/lightuserdata/socket"); } lua_pushinteger(L, ret); diff -r 57662d96d8a8 -r f0f5046ba1b0 src/event_callback.c --- a/src/event_callback.c Sat Jan 15 19:46:55 2011 -0500 +++ b/src/event_callback.c Sat Jan 15 19:51:21 2011 -0500 @@ -43,7 +43,7 @@ load_timeval(newTimeout, &cb->timeout); } } - lua_pop(L, 1); + lua_pop(L, 2); if(ret == -1) { freeCallbackArgs(cb, L); } else { diff -r 57662d96d8a8 -r f0f5046ba1b0 src/luaevent.c --- a/src/luaevent.c Sat Jan 15 19:46:55 2011 -0500 +++ b/src/luaevent.c Sat Jan 15 19:51:21 2011 -0500 @@ -1,21 +1,16 @@ /* LuaEvent - Copyright (C) 2007 Thomas Harning * Licensed as LGPL - See doc/COPYING for details */ -#include "luaevent.h" #include "event_callback.h" #include "event_buffer.h" #include "buffer_event.h" -#include #include #include +#include #define EVENT_BASE_MT "EVENT_BASE_MT" -#ifdef _WIN32 -#include -#endif - le_base* event_base_get(lua_State* L, int idx) { return (le_base*)luaL_checkudata(L, idx, EVENT_BASE_MT); } @@ -29,6 +24,11 @@ return 1; } +int luaevent_libevent_version(lua_State* L) { + lua_pushstring(L, event_get_version()); + return 1; +} + static int luaevent_base_gc(lua_State* L) { le_base *base = event_base_get(L, 1); if(base->base) { @@ -40,14 +40,18 @@ int getSocketFd(lua_State* L, int idx) { int fd; - luaL_checktype(L, idx, LUA_TUSERDATA); - lua_getfield(L, idx, "getfd"); - if(lua_isnil(L, -1)) - return luaL_error(L, "Socket type missing 'getfd' method"); - lua_pushvalue(L, idx); - lua_call(L, 1, 1); - fd = lua_tointeger(L, -1); - lua_pop(L, 1); + if(lua_isnumber(L, idx)) { + fd = lua_tonumber(L, idx); + } else { + luaL_checktype(L, idx, LUA_TUSERDATA); + lua_getfield(L, idx, "getfd"); + if(lua_isnil(L, -1)) + return luaL_error(L, "Socket type missing 'getfd' method"); + lua_pushvalue(L, idx); + lua_call(L, 1, 1); + fd = lua_tointeger(L, -1); + lua_pop(L, 1); + } return fd; } @@ -82,21 +86,47 @@ } static int luaevent_loop(lua_State* L) { + int ret; le_base *base = event_base_get(L, 1); base->loop_L = L; - int ret = event_base_loop(base->base, 0); + ret = event_base_loop(base->base, 0); lua_pushinteger(L, ret); return 1; } +static int luaevent_loopexit(lua_State*L) { + int ret; + le_base *base = event_base_get(L, 1); + struct timeval tv = { 0, 0 }; + if(lua_gettop(L) >= 2) /* Optional timeout before exiting the loop */ + load_timeval(luaL_checknumber(L, 2), &tv); + ret = event_base_loopexit(base->base, &tv); + lua_pushinteger(L, ret); + return 1; +} + +static int luaevent_method(lua_State* L) { + #ifdef _EVENT_VERSION + le_base *base = event_base_get(L, 1); + if(strcmp(_EVENT_VERSION, "1.3")<0) + lua_pushstring(L, event_base_get_method(base->base)); + else + #endif + lua_pushstring(L, event_get_method()); + return 1; +} + static luaL_Reg base_funcs[] = { { "addevent", luaevent_addevent }, { "loop", luaevent_loop }, + { "loopexit", luaevent_loopexit }, + { "method", luaevent_method }, { NULL, NULL } }; static luaL_Reg funcs[] = { { "new", luaevent_newbase }, + { "libevent_version", luaevent_libevent_version }, { NULL, NULL } }; diff -r 57662d96d8a8 -r f0f5046ba1b0 test/basic.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/basic.lua Sat Jan 15 19:51:21 2011 -0500 @@ -0,0 +1,8 @@ +require "luaevent" + +print("Version:", luaevent._NAME.." "..luaevent._VERSION) +print("libevent version:", luaevent.core.libevent_version()) +print("") +base = luaevent.core.new() +print("Testing creating base object:", type(base) == "userdata" and "OK" or "FAIL") +print("libevent backend:", base:method())