diff options
Diffstat (limited to 'src/auxiliar.c')
-rw-r--r-- | src/auxiliar.c | 75 |
1 files changed, 52 insertions, 23 deletions
diff --git a/src/auxiliar.c b/src/auxiliar.c index 5e5ba1a..96138f1 100644 --- a/src/auxiliar.c +++ b/src/auxiliar.c | |||
@@ -3,12 +3,7 @@ | |||
3 | * | 3 | * |
4 | * RCS ID: $Id$ | 4 | * RCS ID: $Id$ |
5 | \*=========================================================================*/ | 5 | \*=========================================================================*/ |
6 | #include "aux.h" | 6 | #include "auxiliar.h" |
7 | |||
8 | /*=========================================================================*\ | ||
9 | * Internal function prototypes | ||
10 | \*=========================================================================*/ | ||
11 | static void *aux_getgroupudata(lua_State *L, const char *group, int objidx); | ||
12 | 7 | ||
13 | /*=========================================================================*\ | 8 | /*=========================================================================*\ |
14 | * Exported functions | 9 | * Exported functions |
@@ -20,18 +15,19 @@ static void *aux_getgroupudata(lua_State *L, const char *group, int objidx); | |||
20 | \*-------------------------------------------------------------------------*/ | 15 | \*-------------------------------------------------------------------------*/ |
21 | void aux_newclass(lua_State *L, const char *name, luaL_reg *func) | 16 | void aux_newclass(lua_State *L, const char *name, luaL_reg *func) |
22 | { | 17 | { |
23 | luaL_newmetatable(L, name); | 18 | lua_pushstring(L, name); |
19 | lua_newtable(L); | ||
24 | lua_pushstring(L, "__index"); | 20 | lua_pushstring(L, "__index"); |
25 | lua_newtable(L); | 21 | lua_newtable(L); |
26 | luaL_openlib(L, NULL, func, 0); | 22 | luaL_openlib(L, NULL, func, 0); |
27 | lua_pushstring(L, "class"); | 23 | lua_pushstring(L, "class"); |
28 | lua_pushstring(L, name); | 24 | lua_pushstring(L, name); |
29 | lua_settable(L, -3); | 25 | lua_rawset(L, -3); |
30 | lua_settable(L, -3); | ||
31 | lua_pushstring(L, "group"); | 26 | lua_pushstring(L, "group"); |
32 | lua_newtable(L); | 27 | lua_newtable(L); |
33 | lua_settable(L, -3); | 28 | lua_rawset(L, -3); |
34 | lua_pop(L, 1); | 29 | lua_rawset(L, -3); |
30 | lua_rawset(L, LUA_REGISTRYINDEX); | ||
35 | } | 31 | } |
36 | 32 | ||
37 | /*-------------------------------------------------------------------------*\ | 33 | /*-------------------------------------------------------------------------*\ |
@@ -39,13 +35,16 @@ void aux_newclass(lua_State *L, const char *name, luaL_reg *func) | |||
39 | \*-------------------------------------------------------------------------*/ | 35 | \*-------------------------------------------------------------------------*/ |
40 | void aux_add2group(lua_State *L, const char *name, const char *group) | 36 | void aux_add2group(lua_State *L, const char *name, const char *group) |
41 | { | 37 | { |
42 | luaL_getmetatable(L, name); | 38 | lua_pushstring(L, name); |
39 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
40 | lua_pushstring(L, "__index"); | ||
41 | lua_rawget(L, -2); | ||
43 | lua_pushstring(L, "group"); | 42 | lua_pushstring(L, "group"); |
44 | lua_gettable(L, -2); | 43 | lua_rawget(L, -2); |
45 | lua_pushstring(L, group); | 44 | lua_pushstring(L, group); |
46 | lua_pushnumber(L, 1); | 45 | lua_pushnumber(L, 1); |
47 | lua_settable(L, -3); | 46 | lua_rawset(L, -3); |
48 | lua_pop(L, 2); | 47 | lua_pop(L, 3); |
49 | } | 48 | } |
50 | 49 | ||
51 | /*-------------------------------------------------------------------------*\ | 50 | /*-------------------------------------------------------------------------*\ |
@@ -53,7 +52,7 @@ void aux_add2group(lua_State *L, const char *name, const char *group) | |||
53 | \*-------------------------------------------------------------------------*/ | 52 | \*-------------------------------------------------------------------------*/ |
54 | void *aux_checkclass(lua_State *L, const char *name, int objidx) | 53 | void *aux_checkclass(lua_State *L, const char *name, int objidx) |
55 | { | 54 | { |
56 | void *data = luaL_checkudata(L, objidx, name); | 55 | void *data = aux_getclassudata(L, name, objidx); |
57 | if (!data) { | 56 | if (!data) { |
58 | char msg[45]; | 57 | char msg[45]; |
59 | sprintf(msg, "%.35s expected", name); | 58 | sprintf(msg, "%.35s expected", name); |
@@ -81,7 +80,8 @@ void *aux_checkgroup(lua_State *L, const char *group, int objidx) | |||
81 | \*-------------------------------------------------------------------------*/ | 80 | \*-------------------------------------------------------------------------*/ |
82 | void aux_setclass(lua_State *L, const char *name, int objidx) | 81 | void aux_setclass(lua_State *L, const char *name, int objidx) |
83 | { | 82 | { |
84 | luaL_getmetatable(L, name); | 83 | lua_pushstring(L, name); |
84 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
85 | if (objidx < 0) objidx--; | 85 | if (objidx < 0) objidx--; |
86 | lua_setmetatable(L, objidx); | 86 | lua_setmetatable(L, objidx); |
87 | } | 87 | } |
@@ -92,17 +92,47 @@ void aux_setclass(lua_State *L, const char *name, int objidx) | |||
92 | /*-------------------------------------------------------------------------*\ | 92 | /*-------------------------------------------------------------------------*\ |
93 | * Get a userdata if object belongs to a given group. | 93 | * Get a userdata if object belongs to a given group. |
94 | \*-------------------------------------------------------------------------*/ | 94 | \*-------------------------------------------------------------------------*/ |
95 | static void *aux_getgroupudata(lua_State *L, const char *group, int objidx) | 95 | void *aux_getgroupudata(lua_State *L, const char *group, int objidx) |
96 | { | 96 | { |
97 | if (!lua_getmetatable(L, objidx)) return NULL; | 97 | if (!lua_getmetatable(L, objidx)) |
98 | return NULL; | ||
99 | lua_pushstring(L, "__index"); | ||
100 | lua_rawget(L, -2); | ||
101 | if (!lua_istable(L, -1)) { | ||
102 | lua_pop(L, 2); | ||
103 | return NULL; | ||
104 | } | ||
98 | lua_pushstring(L, "group"); | 105 | lua_pushstring(L, "group"); |
99 | lua_gettable(L, -2); | 106 | lua_rawget(L, -2); |
107 | if (!lua_istable(L, -1)) { | ||
108 | lua_pop(L, 3); | ||
109 | return NULL; | ||
110 | } | ||
111 | lua_pushstring(L, group); | ||
112 | lua_rawget(L, -2); | ||
100 | if (lua_isnil(L, -1)) { | 113 | if (lua_isnil(L, -1)) { |
114 | lua_pop(L, 4); | ||
115 | return NULL; | ||
116 | } | ||
117 | lua_pop(L, 4); | ||
118 | return lua_touserdata(L, objidx); | ||
119 | } | ||
120 | |||
121 | /*-------------------------------------------------------------------------*\ | ||
122 | * Get a userdata if object belongs to a given class. | ||
123 | \*-------------------------------------------------------------------------*/ | ||
124 | void *aux_getclassudata(lua_State *L, const char *group, int objidx) | ||
125 | { | ||
126 | if (!lua_getmetatable(L, objidx)) | ||
127 | return NULL; | ||
128 | lua_pushstring(L, "__index"); | ||
129 | lua_rawget(L, -2); | ||
130 | if (!lua_istable(L, -1)) { | ||
101 | lua_pop(L, 2); | 131 | lua_pop(L, 2); |
102 | return NULL; | 132 | return NULL; |
103 | } | 133 | } |
104 | lua_pushstring(L, group); | 134 | lua_pushstring(L, "class"); |
105 | lua_gettable(L, -2); | 135 | lua_rawget(L, -2); |
106 | if (lua_isnil(L, -1)) { | 136 | if (lua_isnil(L, -1)) { |
107 | lua_pop(L, 3); | 137 | lua_pop(L, 3); |
108 | return NULL; | 138 | return NULL; |
@@ -110,4 +140,3 @@ static void *aux_getgroupudata(lua_State *L, const char *group, int objidx) | |||
110 | lua_pop(L, 3); | 140 | lua_pop(L, 3); |
111 | return lua_touserdata(L, objidx); | 141 | return lua_touserdata(L, objidx); |
112 | } | 142 | } |
113 | |||