moonbridge

view moonbridge_io.c @ 79:22dbb9d09f02

Stub for new non-blocking I/O library
author jbe
date Sun Apr 05 18:01:31 2015 +0200 (2015-04-05)
parents
children 1a0346580e6d
line source
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <sys/socket.h>
11 #include <sys/select.h>
13 #include <lua.h>
14 #include <lauxlib.h>
15 #include <lualib.h>
17 #define MOONBR_IO_HANDLE_MT_REGKEY "moonbridge_io_handle"
18 #define MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY "moonbridge_io_handle_public"
20 typedef struct {
21 int fd;
22 } moonbr_io_handle_t;
24 void moonbr_io_pushhandle(lua_State *L, int fd) {
25 moonbr_io_handle_t *handle;
26 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t));
27 handle->fd = fd;
28 luaL_getmetatable(L, MOONBR_IO_HANDLE_MT_REGKEY);
29 lua_setmetatable(L, -2);
30 lua_newtable(L); // uservalue
31 lua_newtable(L); // public
32 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
33 lua_setmetatable(L, -2);
34 lua_setfield(L, -2, "public");
35 lua_setuservalue(L, -2);
36 }
38 static int moonbr_io_handleindex(lua_State *L) {
39 lua_getuservalue(L, 1);
40 lua_getfield(L, -1, "public");
41 lua_pushvalue(L, 2);
42 lua_gettable(L, -2);
43 return 1;
44 }
46 static int moonbr_io_handlenewindex(lua_State *L) {
47 lua_getuservalue(L, 1);
48 lua_getfield(L, -1, "public");
49 lua_pushvalue(L, 2);
50 lua_pushvalue(L, 3);
51 lua_settable(L, -3);
52 return 0;
53 }
55 static int moonbr_io_getdummy(lua_State *L) {
56 moonbr_io_pushhandle(L, 1);
57 return 1;
58 }
60 static int moonbr_io_testmethod(lua_State *L) {
61 fprintf(stderr, "DEBUG\n");
62 return 0;
63 }
65 static const struct luaL_Reg moonbr_io_handle_methods[] = {
66 {"testmethod", moonbr_io_testmethod},
67 {NULL, NULL}
68 };
70 static const struct luaL_Reg moonbr_io_handle_metamethods[] = {
71 {"__index", moonbr_io_handleindex},
72 {"__newindex", moonbr_io_handlenewindex},
73 {NULL, NULL}
74 };
76 static const struct luaL_Reg moonbr_io_module_funcs[] = {
77 {"getdummy", moonbr_io_getdummy},
78 {NULL, NULL}
79 };
81 int luaopen_moonbridge_io(lua_State *L) {
83 lua_newtable(L); // public metatable
84 lua_newtable(L); // handle methods
85 luaL_setfuncs(L, moonbr_io_handle_methods, 0);
86 lua_setfield(L, -2, "__index");
87 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY);
89 lua_newtable(L); // handle metatable
90 luaL_setfuncs(L, moonbr_io_handle_metamethods, 0);
91 lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_HANDLE_MT_REGKEY);
93 lua_newtable(L); // module
94 luaL_setfuncs(L, moonbr_io_module_funcs, 0);
95 return 1;
97 }

Impressum / About Us