# HG changeset patch # User jbe # Date 1428249691 -7200 # Node ID 22dbb9d09f02bcb9122be8e7b6bc7e66a8ad121b # Parent 0ec070d6f5d9ba9d97c3b075c1b5c2493280281c Stub for new non-blocking I/O library diff -r 0ec070d6f5d9 -r 22dbb9d09f02 Makefile --- a/Makefile Sun Apr 05 15:15:06 2015 +0200 +++ b/Makefile Sun Apr 05 18:01:31 2015 +0200 @@ -50,11 +50,17 @@ MOONBR_LUA_CPATH_DEFINE = "-DMOONBR_LUA_CPATH=\"$(MOONBR_LUA_CPATH)\"" .endif -all:: moonbridge +all:: moonbridge moonbridge_io.so + +moonbridge: moonbridge.c moonbridge_io.h moonbridge_io.o + cc -Wall -Wno-unused-result -O2 -Wl,-E -I $(LUA_INCLUDE) -L $(LUA_LIBDIR) -o moonbridge $(MOONBR_LUA_PATH_DEFINE) $(MOONBR_LUA_CPATH_DEFINE) moonbridge.c -lm -l$(LUA_LIBRARY) $(UTIL_FLAGS) moonbridge_io.o -moonbridge: moonbridge.c - cc -Wall -Wno-unused-result -O2 -Wl,-E -I $(LUA_INCLUDE) -L $(LUA_LIBDIR) -o moonbridge $(MOONBR_LUA_PATH_DEFINE) $(MOONBR_LUA_CPATH_DEFINE) moonbridge.c -lm -l$(LUA_LIBRARY) $(UTIL_FLAGS) +moonbridge_io.o: moonbridge_io.c + cc -c -Wall -O2 -fPIC -I $(LUA_INCLUDE) -o moonbridge_io.o moonbridge_io.c + +moonbridge_io.so: moonbridge_io.o + ld -shared -o moonbridge_io.so moonbridge_io.o clean:: - rm -f moonbridge + rm -f moonbridge moonbridge_io.o moonbridge_io.so diff -r 0ec070d6f5d9 -r 22dbb9d09f02 moonbridge.c --- a/moonbridge.c Sun Apr 05 15:15:06 2015 +0200 +++ b/moonbridge.c Sun Apr 05 18:01:31 2015 +0200 @@ -67,6 +67,11 @@ #include +/*** Include directive for moonbridge_io library ***/ + +#include "moonbridge_io.h" + + /*** Constants ***/ /* Backlog option for listen() call */ @@ -2758,6 +2763,14 @@ lua_pushliteral(L, MOONBR_VERSION_STRING); lua_setglobal(L, "_MOONBRIDGE_VERSION"); luaL_openlibs(L); + luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 0); + lua_getglobal(L, "io"); + for (lua_pushnil(L); lua_next(L, -3); lua_pop(L, 1)) { + lua_pushvalue(L, -2); + lua_pushvalue(L, -2); + lua_settable(L, -5); + } + lua_pop(L, 2); #ifdef MOONBR_LUA_PATH moonbr_modify_path(L, "path", MOONBR_LUA_PATH); #endif diff -r 0ec070d6f5d9 -r 22dbb9d09f02 moonbridge_io.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moonbridge_io.c Sun Apr 05 18:01:31 2015 +0200 @@ -0,0 +1,98 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle" +#define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public" + +typedef struct { + int fd; +} moonbr_io_handle_t; + +void moonbr_io_pushhandle(lua_State *L, int fd) { + moonbr_io_handle_t *handle; + handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t)); + handle->fd = fd; + luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY); + lua_setmetatable(L, -2); + lua_newtable(L); // uservalue + lua_newtable(L); // public + luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); + lua_setmetatable(L, -2); + lua_setfield(L, -2, "public"); + lua_setuservalue(L, -2); +} + +static int moonbr_io_handleindex(lua_State *L) { + lua_getuservalue(L, 1); + lua_getfield(L, -1, "public"); + lua_pushvalue(L, 2); + lua_gettable(L, -2); + return 1; +} + +static int moonbr_io_handlenewindex(lua_State *L) { + lua_getuservalue(L, 1); + lua_getfield(L, -1, "public"); + lua_pushvalue(L, 2); + lua_pushvalue(L, 3); + lua_settable(L, -3); + return 0; +} + +static int moonbr_io_getdummy(lua_State *L) { + moonbr_io_pushhandle(L, 1); + return 1; +} + +static int moonbr_io_testmethod(lua_State *L) { + fprintf(stderr, "DEBUG\n"); + return 0; +} + +static const struct luaL_Reg moonbr_io_handle_methods[] = { + {"testmethod", moonbr_io_testmethod}, + {NULL, NULL} +}; + +static const struct luaL_Reg moonbr_io_handle_metamethods[] = { + {"__index", moonbr_io_handleindex}, + {"__newindex", moonbr_io_handlenewindex}, + {NULL, NULL} +}; + +static const struct luaL_Reg moonbr_io_module_funcs[] = { + {"getdummy", moonbr_io_getdummy}, + {NULL, NULL} +}; + +int luaopen_moonbridge_io(lua_State *L) { + + lua_newtable(L); // public metatable + lua_newtable(L); // handle methods + luaL_setfuncs(L, moonbr_io_handle_methods, 0); + lua_setfield(L, -2, "__index"); + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); + + lua_newtable(L); // handle metatable + luaL_setfuncs(L, moonbr_io_handle_metamethods, 0); + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY); + + lua_newtable(L); // module + luaL_setfuncs(L, moonbr_io_module_funcs, 0); + return 1; + +} + diff -r 0ec070d6f5d9 -r 22dbb9d09f02 moonbridge_io.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moonbridge_io.h Sun Apr 05 18:01:31 2015 +0200 @@ -0,0 +1,4 @@ + +void moonbr_io_pushhandle(lua_State *L, int fd); +int luaopen_moonbridge_io(lua_State *L); +