aboutsummaryrefslogtreecommitdiff
path: root/src/auxiliar.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/auxiliar.c')
-rw-r--r--src/auxiliar.c38
1 files changed, 7 insertions, 31 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c
index fe21d08..b1f9203 100644
--- a/src/auxiliar.c
+++ b/src/auxiliar.c
@@ -14,27 +14,6 @@
14* Exported functions 14* Exported functions
15\*=========================================================================*/ 15\*=========================================================================*/
16/*-------------------------------------------------------------------------*\ 16/*-------------------------------------------------------------------------*\
17* Prints the value of a class in a nice way
18\*-------------------------------------------------------------------------*/
19int aux_meth_tostring(lua_State *L)
20{
21 char buf[32];
22 if (!lua_getmetatable(L, 1)) goto error;
23 lua_pushstring(L, "__index");
24 lua_gettable(L, -2);
25 if (!lua_istable(L, -1)) goto error;
26 lua_pushstring(L, "class");
27 lua_gettable(L, -2);
28 if (!lua_isstring(L, -1)) goto error;
29 sprintf(buf, "%p", lua_touserdata(L, 1));
30 lua_pushfstring(L, "socket: %s: %s", lua_tostring(L, -1), buf);
31 return 1;
32error:
33 lua_pushnil(L);
34 return 1;
35}
36
37/*-------------------------------------------------------------------------*\
38* Initializes the module 17* Initializes the module
39\*-------------------------------------------------------------------------*/ 18\*-------------------------------------------------------------------------*/
40int aux_open(lua_State *L) 19int aux_open(lua_State *L)
@@ -48,23 +27,20 @@ int aux_open(lua_State *L)
48void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) 27void aux_newclass(lua_State *L, const char *classname, luaL_reg *func)
49{ 28{
50 luaL_newmetatable(L, classname); /* mt */ 29 luaL_newmetatable(L, classname); /* mt */
51 /* set __tostring metamethod */
52 lua_pushstring(L, "__tostring");
53 lua_pushcfunction(L, aux_meth_tostring);
54 lua_rawset(L, -3);
55 /* create __index table to place methods */ 30 /* create __index table to place methods */
56 lua_pushstring(L, "__index"); /* mt,"__index" */ 31 lua_pushstring(L, "__index"); /* mt,"__index" */
57 lua_newtable(L); /* mt,"__index",it */ 32 lua_newtable(L); /* mt,"__index",it */
58 luaL_openlib(L, NULL, func, 0);
59 /* put class name into class metatable */ 33 /* put class name into class metatable */
60 lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ 34 lua_pushstring(L, "class"); /* mt,"__index",it,"class" */
61 lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ 35 lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */
62 lua_rawset(L, -3); /* mt,"__index",it */ 36 lua_rawset(L, -3); /* mt,"__index",it */
63 /* get __gc method from class and use it for garbage collection */ 37 /* pass all methods that start with _ to the metatable, and all others
64 lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc" */ 38 * to the index table */
65 lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc","__gc" */ 39 for (; func->name; func++) { /* mt,"__index",it */
66 lua_rawget(L, -3); /* mt,"__index",it,"__gc",fn */ 40 lua_pushstring(L, func->name);
67 lua_rawset(L, -5); /* mt,"__index",it */ 41 lua_pushcfunction(L, func->func);
42 lua_rawset(L, func->name[0] == '_' ? -5: -3);
43 }
68 lua_rawset(L, -3); /* mt */ 44 lua_rawset(L, -3); /* mt */
69 lua_pop(L, 1); 45 lua_pop(L, 1);
70} 46}