2 * luaclass.c - useful functions for handling Lua classes
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "common/luaclass.h"
23 #include "common/luaobject.h"
26 struct lua_class_property
28 /** Name of the property */
30 /** Callback function called when the property is found in object creation. */
31 lua_class_propfunc_t
new;
32 /** Callback function called when the property is found in object __index. */
33 lua_class_propfunc_t index
;
34 /** Callback function called when the property is found in object __newindex. */
35 lua_class_propfunc_t newindex
;
38 DO_ARRAY(lua_class_t
*, lua_class
, DO_NOTHING
)
40 static lua_class_array_t luaA_classes
;
42 /** Convert a object to a udata if possible.
43 * \param L The Lua VM state.
44 * \param ud The index.
45 * \param class The wanted class.
46 * \return A pointer to the object, NULL otherwise.
49 luaA_toudata(lua_State
*L
, int ud
, lua_class_t
*class)
51 void *p
= lua_touserdata(L
, ud
);
52 if(p
&& lua_getmetatable(L
, ud
)) /* does it have a metatable? */
54 /* Get the lua_class_t that matches this metatable */
55 lua_rawget(L
, LUA_REGISTRYINDEX
);
56 lua_class_t
*metatable_class
= lua_touserdata(L
, -1);
58 /* remove lightuserdata (lua_class pointer) */
61 /* Now, check that the class given in argument is the same as the
62 * metatable's object, or one of its parent (inheritance) */
63 for(; metatable_class
; metatable_class
= metatable_class
->parent
)
64 if(metatable_class
== class)
70 /** Check for a udata class.
71 * \param L The Lua VM state.
72 * \param ud The object index on the stack.
73 * \param class The wanted class.
76 luaA_checkudata(lua_State
*L
, int ud
, lua_class_t
*class)
78 void *p
= luaA_toudata(L
, ud
, class);
80 luaA_typerror(L
, ud
, class->name
);
81 else if(class->checker
&& !class->checker(p
))
82 luaL_error(L
, "invalid object");
86 /** Get an object lua_class.
87 * \param L The Lua VM state.
88 * \param idx The index of the object on the stack.
91 luaA_class_get(lua_State
*L
, int idx
)
93 int type
= lua_type(L
, idx
);
95 if(type
== LUA_TUSERDATA
&& lua_getmetatable(L
, idx
))
97 /* Use the metatable has key to get the class from registry */
98 lua_rawget(L
, LUA_REGISTRYINDEX
);
99 lua_class_t
*class = lua_touserdata(L
, -1);
107 /** Enhanced version of lua_typename that recognizes setup Lua classes.
108 * \param L The Lua VM state.
109 * \param idx The index of the object on the stack.
112 luaA_typename(lua_State
*L
, int idx
)
114 int type
= lua_type(L
, idx
);
116 if(type
== LUA_TUSERDATA
)
118 lua_class_t
*lua_class
= luaA_class_get(L
, idx
);
120 return lua_class
->name
;
123 return lua_typename(L
, type
);
127 luaA_openlib(lua_State
*L
, const char *name
,
128 const struct luaL_Reg methods
[],
129 const struct luaL_Reg meta
[])
131 luaL_newmetatable(L
, name
); /* 1 */
132 lua_pushvalue(L
, -1); /* dup metatable 2 */
133 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
135 luaA_registerlib(L
, NULL
, meta
); /* 1 */
136 luaA_registerlib(L
, name
, methods
); /* 2 */
137 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
138 lua_setmetatable(L
, -2); /* set self as metatable 2 */
143 lua_class_property_cmp(const void *a
, const void *b
)
145 const lua_class_property_t
*x
= a
, *y
= b
;
146 return a_strcmp(x
->name
, y
->name
);
149 BARRAY_FUNCS(lua_class_property_t
, lua_class_property
, DO_NOTHING
, lua_class_property_cmp
)
152 luaA_class_add_property(lua_class_t
*lua_class
,
154 lua_class_propfunc_t cb_new
,
155 lua_class_propfunc_t cb_index
,
156 lua_class_propfunc_t cb_newindex
)
158 lua_class_property_array_insert(&lua_class
->properties
, (lua_class_property_t
)
163 .newindex
= cb_newindex
167 /** Garbage collect a Lua object.
168 * \param L The Lua VM state.
169 * \return The number of elements pushed on stack.
172 luaA_class_gc(lua_State
*L
)
174 lua_object_t
*item
= lua_touserdata(L
, 1);
175 signal_array_wipe(&item
->signals
);
176 /* Get the object class */
177 lua_class_t
*class = luaA_class_get(L
, 1);
179 /* Call the collector function of the class, and all its parent classes */
180 for(; class; class = class->parent
)
182 class->collector(item
);
186 /** Setup a new Lua class.
187 * \param L The Lua VM state.
188 * \param name The class name.
189 * \param parent The parent class (inheritance).
190 * \param allocator The allocator function used when creating a new object.
191 * \param Collector The collector function used when garbage collecting an
193 * \param checker The check function to call when using luaA_checkudata().
194 * \param index_miss_property Function to call when an object of this class
195 * receive a __index request on an unknown property.
196 * \param newindex_miss_property Function to call when an object of this class
197 * receive a __newindex request on an unknown property.
198 * \param methods The methods to set on the class table.
199 * \param meta The meta-methods to set on the class objects.
202 luaA_class_setup(lua_State
*L
, lua_class_t
*class,
205 lua_class_allocator_t allocator
,
206 lua_class_collector_t collector
,
207 lua_class_checker_t checker
,
208 lua_class_propfunc_t index_miss_property
,
209 lua_class_propfunc_t newindex_miss_property
,
210 const struct luaL_Reg methods
[],
211 const struct luaL_Reg meta
[])
213 /* Create the object metatable */
215 /* Register it with class pointer as key in the registry
216 * class-pointer -> metatable */
217 lua_pushlightuserdata(L
, class);
218 /* Duplicate the object metatable */
219 lua_pushvalue(L
, -2);
220 lua_rawset(L
, LUA_REGISTRYINDEX
);
221 /* Now register class pointer with metatable as key in the registry
222 * metatable -> class-pointer */
223 lua_pushvalue(L
, -1);
224 lua_pushlightuserdata(L
, class);
225 lua_rawset(L
, LUA_REGISTRYINDEX
);
227 /* Duplicate objects metatable */
228 lua_pushvalue(L
, -1);
229 /* Set garbage collector in the metatable */
230 lua_pushcfunction(L
, luaA_class_gc
);
231 lua_setfield(L
, -2, "__gc");
233 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
235 luaA_registerlib(L
, NULL
, meta
); /* 1 */
236 luaA_registerlib(L
, name
, methods
); /* 2 */
237 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
238 lua_setmetatable(L
, -2); /* set self as metatable 2 */
241 class->collector
= collector
;
242 class->allocator
= allocator
;
244 class->index_miss_property
= index_miss_property
;
245 class->newindex_miss_property
= newindex_miss_property
;
246 class->checker
= checker
;
247 class->parent
= parent
;
248 class->instances
= 0;
250 signal_add(&class->signals
, "new");
253 class->signals
.inherits_from
= &parent
->signals
;
255 lua_class_array_append(&luaA_classes
, class);
259 luaA_class_connect_signal(lua_State
*L
, lua_class_t
*lua_class
, const char *name
, lua_CFunction fn
)
261 lua_pushcfunction(L
, fn
);
262 luaA_class_connect_signal_from_stack(L
, lua_class
, name
, -1);
266 luaA_class_connect_signal_from_stack(lua_State
*L
, lua_class_t
*lua_class
,
267 const char *name
, int ud
)
269 luaA_checkfunction(L
, ud
);
270 signal_connect(&lua_class
->signals
, name
, luaA_object_ref(L
, ud
));
274 luaA_class_disconnect_signal_from_stack(lua_State
*L
, lua_class_t
*lua_class
,
275 const char *name
, int ud
)
277 luaA_checkfunction(L
, ud
);
278 void *ref
= (void *) lua_topointer(L
, ud
);
279 signal_disconnect(&lua_class
->signals
, name
, ref
);
280 luaA_object_unref(L
, (void *) ref
);
285 luaA_class_emit_signal(lua_State
*L
, lua_class_t
*lua_class
,
286 const char *name
, int nargs
)
288 signal_object_emit(L
, &lua_class
->signals
, name
, nargs
);
291 /** Try to use the metatable of an object.
292 * \param L The Lua VM state.
293 * \param idxobj The index of the object.
294 * \param idxfield The index of the field (attribute) to get.
295 * \return The number of element pushed on stack.
298 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
300 lua_class_t
*class = luaA_class_get(L
, idxobj
);
302 for(; class; class = class->parent
)
305 lua_pushlightuserdata(L
, class);
306 /* Get its metatable from registry */
307 lua_rawget(L
, LUA_REGISTRYINDEX
);
309 lua_pushvalue(L
, idxfield
);
310 /* Get the field in the metatable */
312 /* Do we have a field like that? */
313 if(!lua_isnil(L
, -1))
315 /* Yes, so remove the metatable and return it! */
319 /* No, so remove the metatable and its value */
326 static lua_class_property_t
*
327 lua_class_property_array_getbyname(lua_class_property_array_t
*arr
,
330 lua_class_property_t lookup_prop
= { .name
= name
};
331 return lua_class_property_array_lookup(arr
, &lookup_prop
);
334 /** Get a property of a object.
335 * \param L The Lua VM state.
336 * \param lua_class The Lua class.
337 * \param fieldidx The index of the field name.
338 * \return The object property if found, NULL otherwise.
340 static lua_class_property_t
*
341 luaA_class_property_get(lua_State
*L
, lua_class_t
*lua_class
, int fieldidx
)
343 /* Lookup the property using token */
344 const char *attr
= luaL_checkstring(L
, fieldidx
);
346 /* Look for the property in the class; if not found, go in the parent class. */
347 for(; lua_class
; lua_class
= lua_class
->parent
)
349 lua_class_property_t
*prop
=
350 lua_class_property_array_getbyname(&lua_class
->properties
, attr
);
359 /** Generic index meta function for objects.
360 * \param L The Lua VM state.
361 * \return The number of elements pushed on stack.
364 luaA_class_index(lua_State
*L
)
366 /* Try to use metatable first. */
367 if(luaA_usemetatable(L
, 1, 2))
370 lua_class_t
*class = luaA_class_get(L
, 1);
372 lua_class_property_t
*prop
= luaA_class_property_get(L
, class, 2);
374 /* Property does exist and has an index callback */
378 return prop
->index(L
, luaA_checkudata(L
, 1, class));
382 if(class->index_miss_property
)
383 return class->index_miss_property(L
, luaA_checkudata(L
, 1, class));
389 /** Generic newindex meta function for objects.
390 * \param L The Lua VM state.
391 * \return The number of elements pushed on stack.
394 luaA_class_newindex(lua_State
*L
)
396 /* Try to use metatable first. */
397 if(luaA_usemetatable(L
, 1, 2))
400 lua_class_t
*class = luaA_class_get(L
, 1);
402 lua_class_property_t
*prop
= luaA_class_property_get(L
, class, 2);
404 /* Property does exist and has a newindex callback */
408 return prop
->newindex(L
, luaA_checkudata(L
, 1, class));
412 if(class->newindex_miss_property
)
413 return class->newindex_miss_property(L
, luaA_checkudata(L
, 1, class));
419 /** Generic constructor function for objects.
420 * \param L The Lua VM state.
421 * \return The number of elements pushed on stack.
424 luaA_class_new(lua_State
*L
, lua_class_t
*lua_class
)
426 /* Check we have a table that should contains some properties */
427 luaA_checktable(L
, 2);
429 /* Create a new object */
430 void *object
= lua_class
->allocator(L
);
432 /* Push the first key before iterating */
434 /* Iterate over the property keys */
435 while(lua_next(L
, 2))
437 /* Check that the key is a string.
438 * We cannot call tostring blindly or Lua will convert a key that is a
439 * number TO A STRING, confusing lua_next() */
440 if(lua_isstring(L
, -2))
442 lua_class_property_t
*prop
= luaA_class_property_get(L
, lua_class
, -2);
444 if(prop
&& prop
->new)
445 prop
->new(L
, object
);
454 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80