12 #include "lel_debug.h"
14 #include "lel_instance.h"
17 /* ------------------------------------------------------------------------
18 * lua: x = eventloop.new() -- create a new eventloop object
20 static int l_new (lua_State
*L
)
22 struct lel_eventloop
*el
;
24 DBGF("** eventloop.new () **\n");
26 el
= (struct lel_eventloop
*)lua_newuserdata(L
, sizeof (struct lel_eventloop
));
28 luaL_getmetatable (L
, L_EVENTLOOP_MT
);
29 lua_setmetatable (L
, -2);
31 memset (el
, 0, sizeof(*el
));
32 FD_ZERO (&el
->all_fds
);
37 static int l_eventloop_gc (lua_State
*L
)
39 struct lel_eventloop
*el
;
41 el
= lel_checkeventloop (L
, 1);
43 DBGF("** eventloop:__gc (%p) **\n", el
);
48 /* ------------------------------------------------------------------------
49 * the class method table
51 static const luaL_reg class_table
[] =
58 /* ------------------------------------------------------------------------
59 * the instance method table
61 static const luaL_reg instance_table
[] =
63 { "__tostring", l_eventloop_tostring
},
64 { "__gc", l_eventloop_gc
},
66 { "add_exec", l_eventloop_add_exec
},
67 { "kill_exec", l_eventloop_kill_exec
},
69 { "run_loop", l_eventloop_run_loop
},
71 { "kill_all", l_eventloop_kill_all
},
76 /* ------------------------------------------------------------------------
79 static int lel_init_eventloop_class (lua_State
*L
)
81 luaL_newmetatable(L
, L_EVENTLOOP_MT
);
83 // setup the __index and __gc field
84 lua_pushstring (L
, "__index");
85 lua_pushvalue (L
, -2); // pushes the new metatable
86 lua_settable (L
, -3); // metatable.__index = metatable
88 luaL_openlib (L
, NULL
, instance_table
, 0);
89 luaL_openlib (L
, "eventloop", class_table
, 0);
92 luaL_register (L
, MYNAME
, R
);
93 lua_pushliteral (L
, "version");
94 lua_pushliteral (L
, MYVERSION
);
100 /* ------------------------------------------------------------------------
103 LUALIB_API
int luaopen_eventloop (lua_State
*L
)
105 return lel_init_eventloop_class (L
);