diff options
Diffstat (limited to 'src/auxiliar.c')
-rw-r--r-- | src/auxiliar.c | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c index b1f9203..9a37e10 100644 --- a/src/auxiliar.c +++ b/src/auxiliar.c | |||
@@ -7,7 +7,6 @@ | |||
7 | #include <string.h> | 7 | #include <string.h> |
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | 9 | ||
10 | #include "luasocket.h" | ||
11 | #include "auxiliar.h" | 10 | #include "auxiliar.h" |
12 | 11 | ||
13 | /*=========================================================================*\ | 12 | /*=========================================================================*\ |
@@ -16,16 +15,15 @@ | |||
16 | /*-------------------------------------------------------------------------*\ | 15 | /*-------------------------------------------------------------------------*\ |
17 | * Initializes the module | 16 | * Initializes the module |
18 | \*-------------------------------------------------------------------------*/ | 17 | \*-------------------------------------------------------------------------*/ |
19 | int aux_open(lua_State *L) | 18 | int aux_open(lua_State *L) { |
20 | { | ||
21 | return 0; | 19 | return 0; |
22 | } | 20 | } |
23 | 21 | ||
24 | /*-------------------------------------------------------------------------*\ | 22 | /*-------------------------------------------------------------------------*\ |
25 | * Creates a new class with given methods | 23 | * Creates a new class with given methods |
24 | * Methods whose names start with __ are passed directly to the metatable. | ||
26 | \*-------------------------------------------------------------------------*/ | 25 | \*-------------------------------------------------------------------------*/ |
27 | void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) | 26 | void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) { |
28 | { | ||
29 | luaL_newmetatable(L, classname); /* mt */ | 27 | luaL_newmetatable(L, classname); /* mt */ |
30 | /* create __index table to place methods */ | 28 | /* create __index table to place methods */ |
31 | lua_pushstring(L, "__index"); /* mt,"__index" */ | 29 | lua_pushstring(L, "__index"); /* mt,"__index" */ |
@@ -46,10 +44,30 @@ void aux_newclass(lua_State *L, const char *classname, luaL_reg *func) | |||
46 | } | 44 | } |
47 | 45 | ||
48 | /*-------------------------------------------------------------------------*\ | 46 | /*-------------------------------------------------------------------------*\ |
47 | * Prints the value of a class in a nice way | ||
48 | \*-------------------------------------------------------------------------*/ | ||
49 | int aux_tostring(lua_State *L) { | ||
50 | char buf[32]; | ||
51 | if (!lua_getmetatable(L, 1)) goto error; | ||
52 | lua_pushstring(L, "__index"); | ||
53 | lua_gettable(L, -2); | ||
54 | if (!lua_istable(L, -1)) goto error; | ||
55 | lua_pushstring(L, "class"); | ||
56 | lua_gettable(L, -2); | ||
57 | if (!lua_isstring(L, -1)) goto error; | ||
58 | sprintf(buf, "%p", lua_touserdata(L, 1)); | ||
59 | lua_pushfstring(L, "%s: %s", lua_tostring(L, -1), buf); | ||
60 | return 1; | ||
61 | error: | ||
62 | lua_pushstring(L, "invalid object passed to 'auxiliar.c:__tostring'"); | ||
63 | lua_error(L); | ||
64 | return 1; | ||
65 | } | ||
66 | |||
67 | /*-------------------------------------------------------------------------*\ | ||
49 | * Insert class into group | 68 | * Insert class into group |
50 | \*-------------------------------------------------------------------------*/ | 69 | \*-------------------------------------------------------------------------*/ |
51 | void aux_add2group(lua_State *L, const char *classname, const char *groupname) | 70 | void aux_add2group(lua_State *L, const char *classname, const char *groupname) { |
52 | { | ||
53 | luaL_getmetatable(L, classname); | 71 | luaL_getmetatable(L, classname); |
54 | lua_pushstring(L, groupname); | 72 | lua_pushstring(L, groupname); |
55 | lua_pushboolean(L, 1); | 73 | lua_pushboolean(L, 1); |
@@ -60,8 +78,7 @@ void aux_add2group(lua_State *L, const char *classname, const char *groupname) | |||
60 | /*-------------------------------------------------------------------------*\ | 78 | /*-------------------------------------------------------------------------*\ |
61 | * Make sure argument is a boolean | 79 | * Make sure argument is a boolean |
62 | \*-------------------------------------------------------------------------*/ | 80 | \*-------------------------------------------------------------------------*/ |
63 | int aux_checkboolean(lua_State *L, int objidx) | 81 | int aux_checkboolean(lua_State *L, int objidx) { |
64 | { | ||
65 | if (!lua_isboolean(L, objidx)) | 82 | if (!lua_isboolean(L, objidx)) |
66 | luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); | 83 | luaL_typerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); |
67 | return lua_toboolean(L, objidx); | 84 | return lua_toboolean(L, objidx); |
@@ -71,8 +88,7 @@ int aux_checkboolean(lua_State *L, int objidx) | |||
71 | * Return userdata pointer if object belongs to a given class, abort with | 88 | * Return userdata pointer if object belongs to a given class, abort with |
72 | * error otherwise | 89 | * error otherwise |
73 | \*-------------------------------------------------------------------------*/ | 90 | \*-------------------------------------------------------------------------*/ |
74 | void *aux_checkclass(lua_State *L, const char *classname, int objidx) | 91 | void *aux_checkclass(lua_State *L, const char *classname, int objidx) { |
75 | { | ||
76 | void *data = aux_getclassudata(L, classname, objidx); | 92 | void *data = aux_getclassudata(L, classname, objidx); |
77 | if (!data) { | 93 | if (!data) { |
78 | char msg[45]; | 94 | char msg[45]; |
@@ -86,8 +102,7 @@ void *aux_checkclass(lua_State *L, const char *classname, int objidx) | |||
86 | * Return userdata pointer if object belongs to a given group, abort with | 102 | * Return userdata pointer if object belongs to a given group, abort with |
87 | * error otherwise | 103 | * error otherwise |
88 | \*-------------------------------------------------------------------------*/ | 104 | \*-------------------------------------------------------------------------*/ |
89 | void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) | 105 | void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) { |
90 | { | ||
91 | void *data = aux_getgroupudata(L, groupname, objidx); | 106 | void *data = aux_getgroupudata(L, groupname, objidx); |
92 | if (!data) { | 107 | if (!data) { |
93 | char msg[45]; | 108 | char msg[45]; |
@@ -100,8 +115,7 @@ void *aux_checkgroup(lua_State *L, const char *groupname, int objidx) | |||
100 | /*-------------------------------------------------------------------------*\ | 115 | /*-------------------------------------------------------------------------*\ |
101 | * Set object class | 116 | * Set object class |
102 | \*-------------------------------------------------------------------------*/ | 117 | \*-------------------------------------------------------------------------*/ |
103 | void aux_setclass(lua_State *L, const char *classname, int objidx) | 118 | void aux_setclass(lua_State *L, const char *classname, int objidx) { |
104 | { | ||
105 | luaL_getmetatable(L, classname); | 119 | luaL_getmetatable(L, classname); |
106 | if (objidx < 0) objidx--; | 120 | if (objidx < 0) objidx--; |
107 | lua_setmetatable(L, objidx); | 121 | lua_setmetatable(L, objidx); |
@@ -111,8 +125,7 @@ void aux_setclass(lua_State *L, const char *classname, int objidx) | |||
111 | * Get a userdata pointer if object belongs to a given group. Return NULL | 125 | * Get a userdata pointer if object belongs to a given group. Return NULL |
112 | * otherwise | 126 | * otherwise |
113 | \*-------------------------------------------------------------------------*/ | 127 | \*-------------------------------------------------------------------------*/ |
114 | void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) | 128 | void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) { |
115 | { | ||
116 | if (!lua_getmetatable(L, objidx)) | 129 | if (!lua_getmetatable(L, objidx)) |
117 | return NULL; | 130 | return NULL; |
118 | lua_pushstring(L, groupname); | 131 | lua_pushstring(L, groupname); |
@@ -130,7 +143,6 @@ void *aux_getgroupudata(lua_State *L, const char *groupname, int objidx) | |||
130 | * Get a userdata pointer if object belongs to a given class. Return NULL | 143 | * Get a userdata pointer if object belongs to a given class. Return NULL |
131 | * otherwise | 144 | * otherwise |
132 | \*-------------------------------------------------------------------------*/ | 145 | \*-------------------------------------------------------------------------*/ |
133 | void *aux_getclassudata(lua_State *L, const char *classname, int objidx) | 146 | void *aux_getclassudata(lua_State *L, const char *classname, int objidx) { |
134 | { | ||
135 | return luaL_checkudata(L, objidx, classname); | 147 | return luaL_checkudata(L, objidx, classname); |
136 | } | 148 | } |