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"
25 struct lua_class_property
27 /** Name of the property */
29 /** Callback function called when the property is found in object creation. */
30 lua_class_propfunc_t
new;
31 /** Callback function called when the property is found in object __index. */
32 lua_class_propfunc_t index
;
33 /** Callback function called when the property is found in object __newindex. */
34 lua_class_propfunc_t newindex
;
37 DO_ARRAY(lua_class_t
*, lua_class
, DO_NOTHING
)
39 static lua_class_array_t luaA_classes
;
41 /** Convert a object to a udata if possible.
42 * \param L The Lua VM state.
43 * \param ud The index.
44 * \param class The wanted class.
45 * \return A pointer to the object, NULL otherwise.
48 luaA_toudata(lua_State
*L
, int ud
, lua_class_t
*class)
50 void *p
= lua_touserdata(L
, ud
);
51 if(p
&& lua_getmetatable(L
, ud
)) /* does it have a metatable? */
53 /* Get the lua_class_t that matches this metatable */
54 lua_rawget(L
, LUA_REGISTRYINDEX
);
55 lua_class_t
*metatable_class
= lua_touserdata(L
, -1);
57 /* remove lightuserdata (lua_class pointer) */
60 /* Now, check that the class given in argument is the same as the
61 * metatable's object, or one of its parent (inheritance) */
62 for(; metatable_class
; metatable_class
= metatable_class
->parent
)
63 if(metatable_class
== class)
69 /** Check for a udata class.
70 * \param L The Lua VM state.
71 * \param ud The object index on the stack.
72 * \param class The wanted class.
75 luaA_checkudata(lua_State
*L
, int ud
, lua_class_t
*class)
77 void *p
= luaA_toudata(L
, ud
, class);
79 luaA_typerror(L
, ud
, class->name
);
80 else if(class->checker
&& !class->checker(p
))
81 luaL_error(L
, "invalid object");
85 /** Get an object lua_class.
86 * \param L The Lua VM state.
87 * \param idx The index of the object on the stack.
90 luaA_class_get(lua_State
*L
, int idx
)
92 int type
= lua_type(L
, idx
);
94 if(type
== LUA_TUSERDATA
&& lua_getmetatable(L
, idx
))
96 /* Use the metatable has key to get the class from registry */
97 lua_rawget(L
, LUA_REGISTRYINDEX
);
98 lua_class_t
*class = lua_touserdata(L
, -1);
106 /** Enhanced version of lua_typename that recognizes setup Lua classes.
107 * \param L The Lua VM state.
108 * \param idx The index of the object on the stack.
111 luaA_typename(lua_State
*L
, int idx
)
113 int type
= lua_type(L
, idx
);
115 if(type
== LUA_TUSERDATA
)
117 lua_class_t
*lua_class
= luaA_class_get(L
, idx
);
119 return lua_class
->name
;
122 return lua_typename(L
, type
);
126 luaA_openlib(lua_State
*L
, const char *name
,
127 const struct luaL_Reg methods
[],
128 const struct luaL_Reg meta
[])
130 luaL_newmetatable(L
, name
); /* 1 */
131 lua_pushvalue(L
, -1); /* dup metatable 2 */
132 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
134 luaA_registerlib(L
, NULL
, meta
); /* 1 */
135 luaA_registerlib(L
, name
, methods
); /* 2 */
136 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
137 lua_setmetatable(L
, -2); /* set self as metatable 2 */
142 lua_class_property_cmp(const void *a
, const void *b
)
144 const lua_class_property_t
*x
= a
, *y
= b
;
145 return a_strcmp(x
->name
, y
->name
);
148 BARRAY_FUNCS(lua_class_property_t
, lua_class_property
, DO_NOTHING
, lua_class_property_cmp
)
151 luaA_class_add_property(lua_class_t
*lua_class
,
153 lua_class_propfunc_t cb_new
,
154 lua_class_propfunc_t cb_index
,
155 lua_class_propfunc_t cb_newindex
)
157 lua_class_property_array_insert(&lua_class
->properties
, (lua_class_property_t
)
162 .newindex
= cb_newindex
166 /** Garbage collect a Lua object.
167 * \param L The Lua VM state.
168 * \return The number of elements pushed on stack.
171 luaA_class_gc(lua_State
*L
)
173 lua_object_t
*item
= lua_touserdata(L
, 1);
174 signal_array_wipe(&item
->signals
);
175 /* Get the object class */
176 lua_class_t
*class = luaA_class_get(L
, 1);
178 /* Call the collector function of the class, and all its parent classes */
179 for(; class; class = class->parent
)
181 class->collector(item
);
185 /** Setup a new Lua class.
186 * \param L The Lua VM state.
187 * \param name The class name.
188 * \param parent The parent class (inheritance).
189 * \param allocator The allocator function used when creating a new object.
190 * \param Collector The collector function used when garbage collecting an
192 * \param checker The check function to call when using luaA_checkudata().
193 * \param index_miss_property Function to call when an object of this class
194 * receive a __index request on an unknown property.
195 * \param newindex_miss_property Function to call when an object of this class
196 * receive a __newindex request on an unknown property.
197 * \param methods The methods to set on the class table.
198 * \param meta The meta-methods to set on the class objects.
201 luaA_class_setup(lua_State
*L
, lua_class_t
*class,
204 lua_class_allocator_t allocator
,
205 lua_class_collector_t collector
,
206 lua_class_checker_t checker
,
207 lua_class_propfunc_t index_miss_property
,
208 lua_class_propfunc_t newindex_miss_property
,
209 const struct luaL_Reg methods
[],
210 const struct luaL_Reg meta
[])
212 /* Create the object metatable */
214 /* Register it with class pointer as key in the registry
215 * class-pointer -> metatable */
216 lua_pushlightuserdata(L
, class);
217 /* Duplicate the object metatable */
218 lua_pushvalue(L
, -2);
219 lua_rawset(L
, LUA_REGISTRYINDEX
);
220 /* Now register class pointer with metatable as key in the registry
221 * metatable -> class-pointer */
222 lua_pushvalue(L
, -1);
223 lua_pushlightuserdata(L
, class);
224 lua_rawset(L
, LUA_REGISTRYINDEX
);
226 /* Duplicate objects metatable */
227 lua_pushvalue(L
, -1);
228 /* Set garbage collector in the metatable */
229 lua_pushcfunction(L
, luaA_class_gc
);
230 lua_setfield(L
, -2, "__gc");
232 lua_setfield(L
, -2, "__index"); /* metatable.__index = metatable 1 */
234 luaA_registerlib(L
, NULL
, meta
); /* 1 */
235 luaA_registerlib(L
, name
, methods
); /* 2 */
236 lua_pushvalue(L
, -1); /* dup self as metatable 3 */
237 lua_setmetatable(L
, -2); /* set self as metatable 2 */
240 class->collector
= collector
;
241 class->allocator
= allocator
;
243 class->index_miss_property
= index_miss_property
;
244 class->newindex_miss_property
= newindex_miss_property
;
245 class->checker
= checker
;
246 class->parent
= parent
;
247 class->instances
= 0;
249 signal_add(&class->signals
, "new");
252 class->signals
.inherits_from
= &parent
->signals
;
254 lua_class_array_append(&luaA_classes
, class);
258 luaA_class_connect_signal(lua_State
*L
, lua_class_t
*lua_class
, const char *name
, lua_CFunction fn
)
260 lua_pushcfunction(L
, fn
);
261 luaA_class_connect_signal_from_stack(L
, lua_class
, name
, -1);
265 luaA_class_connect_signal_from_stack(lua_State
*L
, lua_class_t
*lua_class
,
266 const char *name
, int ud
)
268 luaA_checkfunction(L
, ud
);
269 signal_connect(&lua_class
->signals
, name
, luaA_object_ref(L
, ud
));
273 luaA_class_disconnect_signal_from_stack(lua_State
*L
, lua_class_t
*lua_class
,
274 const char *name
, int ud
)
276 luaA_checkfunction(L
, ud
);
277 void *ref
= (void *) lua_topointer(L
, ud
);
278 signal_disconnect(&lua_class
->signals
, name
, ref
);
279 luaA_object_unref(L
, (void *) ref
);
284 luaA_class_emit_signal(lua_State
*L
, lua_class_t
*lua_class
,
285 const char *name
, int nargs
)
287 signal_object_emit(L
, &lua_class
->signals
, name
, nargs
);
290 /** Try to use the metatable of an object.
291 * \param L The Lua VM state.
292 * \param idxobj The index of the object.
293 * \param idxfield The index of the field (attribute) to get.
294 * \return The number of element pushed on stack.
297 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
299 lua_class_t
*class = luaA_class_get(L
, idxobj
);
301 for(; class; class = class->parent
)
304 lua_pushlightuserdata(L
, class);
305 /* Get its metatable from registry */
306 lua_rawget(L
, LUA_REGISTRYINDEX
);
308 lua_pushvalue(L
, idxfield
);
309 /* Get the field in the metatable */
311 /* Do we have a field like that? */
312 if(!lua_isnil(L
, -1))
314 /* Yes, so remove the metatable and return it! */
318 /* No, so remove the metatable and its value */
325 static lua_class_property_t
*
326 lua_class_property_array_getbyname(lua_class_property_array_t
*arr
,
329 lua_class_property_t lookup_prop
= { .name
= name
};
330 return lua_class_property_array_lookup(arr
, &lookup_prop
);
333 /** Get a property of a object.
334 * \param L The Lua VM state.
335 * \param lua_class The Lua class.
336 * \param fieldidx The index of the field name.
337 * \return The object property if found, NULL otherwise.
339 static lua_class_property_t
*
340 luaA_class_property_get(lua_State
*L
, lua_class_t
*lua_class
, int fieldidx
)
342 /* Lookup the property using token */
343 const char *attr
= luaL_checkstring(L
, fieldidx
);
345 /* Look for the property in the class; if not found, go in the parent class. */
346 for(; lua_class
; lua_class
= lua_class
->parent
)
348 lua_class_property_t
*prop
=
349 lua_class_property_array_getbyname(&lua_class
->properties
, attr
);
358 /** Generic index meta function for objects.
359 * \param L The Lua VM state.
360 * \return The number of elements pushed on stack.
363 luaA_class_index(lua_State
*L
)
365 /* Try to use metatable first. */
366 if(luaA_usemetatable(L
, 1, 2))
369 lua_class_t
*class = luaA_class_get(L
, 1);
371 lua_class_property_t
*prop
= luaA_class_property_get(L
, class, 2);
373 /* Property does exist and has an index callback */
377 return prop
->index(L
, luaA_checkudata(L
, 1, class));
381 if(class->index_miss_property
)
382 return class->index_miss_property(L
, luaA_checkudata(L
, 1, class));
388 /** Generic newindex meta function for objects.
389 * \param L The Lua VM state.
390 * \return The number of elements pushed on stack.
393 luaA_class_newindex(lua_State
*L
)
395 /* Try to use metatable first. */
396 if(luaA_usemetatable(L
, 1, 2))
399 lua_class_t
*class = luaA_class_get(L
, 1);
401 lua_class_property_t
*prop
= luaA_class_property_get(L
, class, 2);
403 /* Property does exist and has a newindex callback */
407 return prop
->newindex(L
, luaA_checkudata(L
, 1, class));
411 if(class->newindex_miss_property
)
412 return class->newindex_miss_property(L
, luaA_checkudata(L
, 1, class));
418 /** Generic constructor function for objects.
419 * \param L The Lua VM state.
420 * \return The number of elements pushed on stack.
423 luaA_class_new(lua_State
*L
, lua_class_t
*lua_class
)
425 /* Check we have a table that should contains some properties */
426 luaA_checktable(L
, 2);
428 /* Create a new object */
429 void *object
= lua_class
->allocator(L
);
431 /* Push the first key before iterating */
433 /* Iterate over the property keys */
434 while(lua_next(L
, 2))
436 /* Check that the key is a string.
437 * We cannot call tostring blindly or Lua will convert a key that is a
438 * number TO A STRING, confusing lua_next() */
439 if(lua_isstring(L
, -2))
441 lua_class_property_t
*prop
= luaA_class_property_get(L
, lua_class
, -2);
443 if(prop
&& prop
->new)
444 prop
->new(L
, object
);
453 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80