Set minimum required cmake version to 2.8.0
[awesome.git] / common / luaclass.c
blob53f6e85e3581293df0c637eb18d1e63403686ee3
1 /*
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 */
28 const char *name;
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.
47 void *
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) */
58 lua_pop(L, 1);
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)
64 return p;
66 return NULL;
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.
74 void *
75 luaA_checkudata(lua_State *L, int ud, lua_class_t *class)
77 void *p = luaA_toudata(L, ud, class);
78 if(!p)
79 luaL_typerror(L, ud, class->name);
80 else if(class->checker && !class->checker(p))
81 luaL_error(L, "invalid object");
82 return p;
85 /** Get an object lua_class.
86 * \param L The Lua VM state.
87 * \param idx The index of the object on the stack.
89 lua_class_t *
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);
99 lua_pop(L, 1);
100 return class;
103 return NULL;
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.
110 const char *
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);
118 if(lua_class)
119 return lua_class->name;
122 return lua_typename(L, type);
125 void
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 luaL_register(L, NULL, meta); /* 1 */
135 luaL_register(L, name, methods); /* 2 */
136 lua_pushvalue(L, -1); /* dup self as metatable 3 */
137 lua_setmetatable(L, -2); /* set self as metatable 2 */
138 lua_pop(L, 2);
141 static int
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)
150 void
151 luaA_class_add_property(lua_class_t *lua_class,
152 const char *name,
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)
159 .name = name,
160 .new = cb_new,
161 .index = cb_index,
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.
170 static int
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);
177 class->instances--;
178 /* Call the collector function of the class, and all its parent classes */
179 for(; class; class = class->parent)
180 if(class->collector)
181 class->collector(item);
182 return 0;
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
191 * object.
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.
200 void
201 luaA_class_setup(lua_State *L, lua_class_t *class,
202 const char *name,
203 lua_class_t *parent,
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 */
213 lua_newtable(L);
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 luaL_register(L, NULL, meta); /* 1 */
235 luaL_register(L, name, methods); /* 2 */
236 lua_pushvalue(L, -1); /* dup self as metatable 3 */
237 lua_setmetatable(L, -2); /* set self as metatable 2 */
238 lua_pop(L, 2);
240 class->collector = collector;
241 class->allocator = allocator;
242 class->name = name;
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");
251 /** @todo This is ugly :/ */
252 /* Copy all signals from the parent */
253 if (parent)
254 foreach(sig, parent->signals)
256 signal_t s = { .id = sig->id };
257 signal_array_insert(&class->signals, s);
260 lua_class_array_append(&luaA_classes, class);
263 void
264 luaA_class_connect_signal(lua_State *L, lua_class_t *lua_class, const char *name, lua_CFunction fn)
266 lua_pushcfunction(L, fn);
267 luaA_class_connect_signal_from_stack(L, lua_class, name, -1);
270 void
271 luaA_class_connect_signal_from_stack(lua_State *L, lua_class_t *lua_class,
272 const char *name, int ud)
274 luaA_checkfunction(L, ud);
275 signal_connect(&lua_class->signals, name, luaA_object_ref(L, ud));
278 void
279 luaA_class_disconnect_signal_from_stack(lua_State *L, lua_class_t *lua_class,
280 const char *name, int ud)
282 luaA_checkfunction(L, ud);
283 void *ref = (void *) lua_topointer(L, ud);
284 signal_disconnect(&lua_class->signals, name, ref);
285 luaA_object_unref(L, (void *) ref);
286 lua_remove(L, ud);
289 void
290 luaA_class_emit_signal(lua_State *L, lua_class_t *lua_class,
291 const char *name, int nargs)
293 signal_object_emit(L, &lua_class->signals, name, nargs);
296 /** Try to use the metatable of an object.
297 * \param L The Lua VM state.
298 * \param idxobj The index of the object.
299 * \param idxfield The index of the field (attribute) to get.
300 * \return The number of element pushed on stack.
303 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
305 lua_class_t *class = luaA_class_get(L, idxobj);
307 for(; class; class = class->parent)
309 /* Push the class */
310 lua_pushlightuserdata(L, class);
311 /* Get its metatable from registry */
312 lua_rawget(L, LUA_REGISTRYINDEX);
313 /* Push the field */
314 lua_pushvalue(L, idxfield);
315 /* Get the field in the metatable */
316 lua_rawget(L, -2);
317 /* Do we have a field like that? */
318 if(!lua_isnil(L, -1))
320 /* Yes, so remove the metatable and return it! */
321 lua_remove(L, -2);
322 return 1;
324 /* No, so remove the metatable and its value */
325 lua_pop(L, 2);
328 return 0;
331 static lua_class_property_t *
332 lua_class_property_array_getbyname(lua_class_property_array_t *arr,
333 const char *name)
335 lua_class_property_t lookup_prop = { .name = name };
336 return lua_class_property_array_lookup(arr, &lookup_prop);
339 /** Get a property of a object.
340 * \param L The Lua VM state.
341 * \param lua_class The Lua class.
342 * \param fieldidx The index of the field name.
343 * \return The object property if found, NULL otherwise.
345 static lua_class_property_t *
346 luaA_class_property_get(lua_State *L, lua_class_t *lua_class, int fieldidx)
348 /* Lookup the property using token */
349 const char *attr = luaL_checkstring(L, fieldidx);
351 /* Look for the property in the class; if not found, go in the parent class. */
352 for(; lua_class; lua_class = lua_class->parent)
354 lua_class_property_t *prop =
355 lua_class_property_array_getbyname(&lua_class->properties, attr);
357 if(prop)
358 return prop;
361 return NULL;
364 /** Generic index meta function for objects.
365 * \param L The Lua VM state.
366 * \return The number of elements pushed on stack.
369 luaA_class_index(lua_State *L)
371 /* Try to use metatable first. */
372 if(luaA_usemetatable(L, 1, 2))
373 return 1;
375 lua_class_t *class = luaA_class_get(L, 1);
377 lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
379 /* Property does exist and has an index callback */
380 if(prop)
382 if(prop->index)
383 return prop->index(L, luaA_checkudata(L, 1, class));
385 else
387 if(class->index_miss_property)
388 return class->index_miss_property(L, luaA_checkudata(L, 1, class));
391 return 0;
394 /** Generic newindex meta function for objects.
395 * \param L The Lua VM state.
396 * \return The number of elements pushed on stack.
399 luaA_class_newindex(lua_State *L)
401 /* Try to use metatable first. */
402 if(luaA_usemetatable(L, 1, 2))
403 return 1;
405 lua_class_t *class = luaA_class_get(L, 1);
407 lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
409 /* Property does exist and has a newindex callback */
410 if(prop)
412 if(prop->newindex)
413 return prop->newindex(L, luaA_checkudata(L, 1, class));
415 else
417 if(class->newindex_miss_property)
418 return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
421 return 0;
424 /** Generic constructor function for objects.
425 * \param L The Lua VM state.
426 * \return The number of elements pushed on stack.
429 luaA_class_new(lua_State *L, lua_class_t *lua_class)
431 /* Check we have a table that should contains some properties */
432 luaA_checktable(L, 2);
434 /* Create a new object */
435 void *object = lua_class->allocator(L);
437 /* Push the first key before iterating */
438 lua_pushnil(L);
439 /* Iterate over the property keys */
440 while(lua_next(L, 2))
442 /* Check that the key is a string.
443 * We cannot call tostring blindly or Lua will convert a key that is a
444 * number TO A STRING, confusing lua_next() */
445 if(lua_isstring(L, -2))
447 lua_class_property_t *prop = luaA_class_property_get(L, lua_class, -2);
449 if(prop && prop->new)
450 prop->new(L, object);
452 /* Remove value */
453 lua_pop(L, 1);
456 return 1;
459 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80