tag: Improve tag property::index support (FS#1229)
[awesome.git] / common / luaclass.c
blob68be33065f5adde6f76cfa134e6f52878e8770e5
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 luaA_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 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 */
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 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 */
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 if (parent)
252 class->signals.inherits_from = &parent->signals;
254 lua_class_array_append(&luaA_classes, class);
257 void
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);
264 void
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));
272 void
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);
280 lua_remove(L, ud);
283 void
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)
303 /* Push the class */
304 lua_pushlightuserdata(L, class);
305 /* Get its metatable from registry */
306 lua_rawget(L, LUA_REGISTRYINDEX);
307 /* Push the field */
308 lua_pushvalue(L, idxfield);
309 /* Get the field in the metatable */
310 lua_rawget(L, -2);
311 /* Do we have a field like that? */
312 if(!lua_isnil(L, -1))
314 /* Yes, so remove the metatable and return it! */
315 lua_remove(L, -2);
316 return 1;
318 /* No, so remove the metatable and its value */
319 lua_pop(L, 2);
322 return 0;
325 static lua_class_property_t *
326 lua_class_property_array_getbyname(lua_class_property_array_t *arr,
327 const char *name)
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);
351 if(prop)
352 return prop;
355 return NULL;
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))
367 return 1;
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 */
374 if(prop)
376 if(prop->index)
377 return prop->index(L, luaA_checkudata(L, 1, class));
379 else
381 if(class->index_miss_property)
382 return class->index_miss_property(L, luaA_checkudata(L, 1, class));
385 return 0;
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))
397 return 1;
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 */
404 if(prop)
406 if(prop->newindex)
407 return prop->newindex(L, luaA_checkudata(L, 1, class));
409 else
411 if(class->newindex_miss_property)
412 return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
415 return 0;
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 */
432 lua_pushnil(L);
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);
446 /* Remove value */
447 lua_pop(L, 1);
450 return 1;
453 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80