aboutsummaryrefslogtreecommitdiff
path: root/src/auxiliar.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-24 00:18:19 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-24 00:18:19 +0000
commit0c9f420a3549df3fb331bb24157b65a3301641d4 (patch)
treee1b6ce40b55a77ed2bc20493f10f8239b7c00071 /src/auxiliar.c
parent42e0e74487ca62b58a8a1fa06580154c632b4942 (diff)
downloadluasocket-0c9f420a3549df3fb331bb24157b65a3301641d4.tar.gz
luasocket-0c9f420a3549df3fb331bb24157b65a3301641d4.tar.bz2
luasocket-0c9f420a3549df3fb331bb24157b65a3301641d4.zip
New accept/connect code.
Better error checking. Better tests. __tostring implemented.
Diffstat (limited to 'src/auxiliar.c')
-rw-r--r--src/auxiliar.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c
index 6888f9c..9a249b6 100644
--- a/src/auxiliar.c
+++ b/src/auxiliar.c
@@ -5,6 +5,7 @@
5* RCS ID: $Id$ 5* RCS ID: $Id$
6\*=========================================================================*/ 6\*=========================================================================*/
7#include <string.h> 7#include <string.h>
8#include <stdio.h>
8 9
9#include "luasocket.h" 10#include "luasocket.h"
10#include "auxiliar.h" 11#include "auxiliar.h"
@@ -13,6 +14,27 @@
13* Exported functions 14* Exported functions
14\*=========================================================================*/ 15\*=========================================================================*/
15/*-------------------------------------------------------------------------*\ 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/*-------------------------------------------------------------------------*\
16* Initializes the module 38* Initializes the module
17\*-------------------------------------------------------------------------*/ 39\*-------------------------------------------------------------------------*/
18void aux_open(lua_State *L) 40void aux_open(lua_State *L)
@@ -26,14 +48,18 @@ void aux_open(lua_State *L)
26void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) 48void aux_newclass(lua_State *L, const char *classname, luaL_reg *func)
27{ 49{
28 luaL_newmetatable(L, classname); /* mt */ 50 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 */
29 lua_pushstring(L, "__index"); /* mt,"__index" */ 56 lua_pushstring(L, "__index"); /* mt,"__index" */
30 lua_newtable(L); /* mt,"__index",it */ 57 lua_newtable(L); /* mt,"__index",it */
31 luaL_openlib(L, NULL, func, 0); 58 luaL_openlib(L, NULL, func, 0);
32#ifdef LUASOCKET_DEBUG 59 /* put class name into class metatable */
33 lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ 60 lua_pushstring(L, "class"); /* mt,"__index",it,"class" */
34 lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ 61 lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */
35 lua_rawset(L, -3); /* mt,"__index",it */ 62 lua_rawset(L, -3); /* mt,"__index",it */
36#endif
37 /* get __gc method from class and use it for garbage collection */ 63 /* get __gc method from class and use it for garbage collection */
38 lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc" */ 64 lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc" */
39 lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc","__gc" */ 65 lua_pushstring(L, "__gc"); /* mt,"__index",it,"__gc","__gc" */