2009-06-08 17:28:20 +02:00
|
|
|
/*
|
|
|
|
* luaclass.c - useful functions for handling Lua classes
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/luaclass.h"
|
|
|
|
#include "common/luaobject.h"
|
|
|
|
|
2009-06-16 11:44:28 +02:00
|
|
|
struct lua_class_property
|
|
|
|
{
|
2010-09-01 15:41:41 +02:00
|
|
|
/** Name of the property */
|
|
|
|
const char *name;
|
2009-06-16 11:44:28 +02:00
|
|
|
/** Callback function called when the property is found in object creation. */
|
|
|
|
lua_class_propfunc_t new;
|
|
|
|
/** Callback function called when the property is found in object __index. */
|
|
|
|
lua_class_propfunc_t index;
|
|
|
|
/** Callback function called when the property is found in object __newindex. */
|
|
|
|
lua_class_propfunc_t newindex;
|
|
|
|
};
|
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
DO_ARRAY(lua_class_t *, lua_class, DO_NOTHING)
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
static lua_class_array_t luaA_classes;
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
/** Convert a object to a udata if possible.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param ud The index.
|
|
|
|
* \param class The wanted class.
|
|
|
|
* \return A pointer to the object, NULL otherwise.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
luaA_toudata(lua_State *L, int ud, lua_class_t *class)
|
|
|
|
{
|
|
|
|
void *p = lua_touserdata(L, ud);
|
2009-09-30 14:21:25 +02:00
|
|
|
if(p && lua_getmetatable(L, ud)) /* does it have a metatable? */
|
|
|
|
{
|
|
|
|
/* Get the lua_class_t that matches this metatable */
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
lua_class_t *metatable_class = lua_touserdata(L, -1);
|
|
|
|
|
|
|
|
/* remove lightuserdata (lua_class pointer) */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
/* Now, check that the class given in argument is the same as the
|
|
|
|
* metatable's object, or one of its parent (inheritance) */
|
|
|
|
for(; metatable_class; metatable_class = metatable_class->parent)
|
|
|
|
if(metatable_class == class)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
2009-08-17 15:52:41 +02:00
|
|
|
}
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
/** Check for a udata class.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param ud The object index on the stack.
|
|
|
|
* \param class The wanted class.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
luaA_checkudata(lua_State *L, int ud, lua_class_t *class)
|
|
|
|
{
|
|
|
|
void *p = luaA_toudata(L, ud, class);
|
|
|
|
if(!p)
|
2012-06-07 08:31:21 +02:00
|
|
|
luaA_typerror(L, ud, class->name);
|
2009-09-30 11:55:43 +02:00
|
|
|
else if(class->checker && !class->checker(p))
|
|
|
|
luaL_error(L, "invalid object");
|
2009-08-17 15:52:41 +02:00
|
|
|
return p;
|
|
|
|
}
|
2009-06-15 17:57:04 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
/** Get an object lua_class.
|
2009-06-15 17:57:04 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param idx The index of the object on the stack.
|
|
|
|
*/
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_class_t *
|
|
|
|
luaA_class_get(lua_State *L, int idx)
|
2009-06-15 17:57:04 +02:00
|
|
|
{
|
|
|
|
int type = lua_type(L, idx);
|
|
|
|
|
2009-09-30 14:21:25 +02:00
|
|
|
if(type == LUA_TUSERDATA && lua_getmetatable(L, idx))
|
|
|
|
{
|
|
|
|
/* Use the metatable has key to get the class from registry */
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
lua_class_t *class = lua_touserdata(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return class;
|
|
|
|
}
|
2009-06-15 17:57:04 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
return NULL;
|
2009-06-15 17:57:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Enhanced version of lua_typename that recognizes setup Lua classes.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param idx The index of the object on the stack.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
luaA_typename(lua_State *L, int idx)
|
|
|
|
{
|
2009-08-17 15:52:41 +02:00
|
|
|
int type = lua_type(L, idx);
|
2009-06-15 17:57:04 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
if(type == LUA_TUSERDATA)
|
|
|
|
{
|
|
|
|
lua_class_t *lua_class = luaA_class_get(L, idx);
|
|
|
|
if(lua_class)
|
|
|
|
return lua_class->name;
|
|
|
|
}
|
2009-06-15 17:57:04 +02:00
|
|
|
|
|
|
|
return lua_typename(L, type);
|
|
|
|
}
|
|
|
|
|
2009-06-15 17:24:36 +02:00
|
|
|
void
|
|
|
|
luaA_openlib(lua_State *L, const char *name,
|
2012-06-06 23:07:07 +02:00
|
|
|
const struct luaL_Reg methods[],
|
|
|
|
const struct luaL_Reg meta[])
|
2009-06-15 17:24:36 +02:00
|
|
|
{
|
|
|
|
luaL_newmetatable(L, name); /* 1 */
|
|
|
|
lua_pushvalue(L, -1); /* dup metatable 2 */
|
|
|
|
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
|
|
|
|
|
2012-06-12 13:32:40 +02:00
|
|
|
luaA_registerlib(L, NULL, meta); /* 1 */
|
|
|
|
luaA_registerlib(L, name, methods); /* 2 */
|
2009-06-15 17:24:36 +02:00
|
|
|
lua_pushvalue(L, -1); /* dup self as metatable 3 */
|
|
|
|
lua_setmetatable(L, -2); /* set self as metatable 2 */
|
|
|
|
lua_pop(L, 2);
|
|
|
|
}
|
|
|
|
|
2009-06-16 11:44:28 +02:00
|
|
|
static int
|
|
|
|
lua_class_property_cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const lua_class_property_t *x = a, *y = b;
|
2010-09-01 15:41:41 +02:00
|
|
|
return a_strcmp(x->name, y->name);
|
2009-06-16 11:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BARRAY_FUNCS(lua_class_property_t, lua_class_property, DO_NOTHING, lua_class_property_cmp)
|
|
|
|
|
|
|
|
void
|
|
|
|
luaA_class_add_property(lua_class_t *lua_class,
|
2010-09-01 15:41:41 +02:00
|
|
|
const char *name,
|
2009-06-16 11:44:28 +02:00
|
|
|
lua_class_propfunc_t cb_new,
|
|
|
|
lua_class_propfunc_t cb_index,
|
|
|
|
lua_class_propfunc_t cb_newindex)
|
|
|
|
{
|
|
|
|
lua_class_property_array_insert(&lua_class->properties, (lua_class_property_t)
|
|
|
|
{
|
2010-09-01 15:41:41 +02:00
|
|
|
.name = name,
|
2009-06-16 11:44:28 +02:00
|
|
|
.new = cb_new,
|
|
|
|
.index = cb_index,
|
|
|
|
.newindex = cb_newindex
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-10-02 15:48:32 +02:00
|
|
|
/** Garbage collect a Lua object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_class_gc(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_object_t *item = lua_touserdata(L, 1);
|
|
|
|
signal_array_wipe(&item->signals);
|
|
|
|
/* Get the object class */
|
|
|
|
lua_class_t *class = luaA_class_get(L, 1);
|
2010-09-17 17:10:53 +02:00
|
|
|
class->instances--;
|
2009-10-02 15:48:32 +02:00
|
|
|
/* Call the collector function of the class, and all its parent classes */
|
|
|
|
for(; class; class = class->parent)
|
|
|
|
if(class->collector)
|
|
|
|
class->collector(item);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-30 14:21:25 +02:00
|
|
|
/** Setup a new Lua class.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param name The class name.
|
|
|
|
* \param parent The parent class (inheritance).
|
|
|
|
* \param allocator The allocator function used when creating a new object.
|
2009-10-02 15:48:32 +02:00
|
|
|
* \param Collector The collector function used when garbage collecting an
|
|
|
|
* object.
|
2009-09-30 14:21:25 +02:00
|
|
|
* \param checker The check function to call when using luaA_checkudata().
|
|
|
|
* \param index_miss_property Function to call when an object of this class
|
|
|
|
* receive a __index request on an unknown property.
|
|
|
|
* \param newindex_miss_property Function to call when an object of this class
|
|
|
|
* receive a __newindex request on an unknown property.
|
|
|
|
* \param methods The methods to set on the class table.
|
|
|
|
* \param meta The meta-methods to set on the class objects.
|
|
|
|
*/
|
2009-06-15 17:24:36 +02:00
|
|
|
void
|
|
|
|
luaA_class_setup(lua_State *L, lua_class_t *class,
|
|
|
|
const char *name,
|
2009-09-30 14:21:25 +02:00
|
|
|
lua_class_t *parent,
|
2009-06-15 17:24:36 +02:00
|
|
|
lua_class_allocator_t allocator,
|
2009-10-02 15:48:32 +02:00
|
|
|
lua_class_collector_t collector,
|
2009-09-30 11:55:43 +02:00
|
|
|
lua_class_checker_t checker,
|
2009-08-20 17:37:46 +02:00
|
|
|
lua_class_propfunc_t index_miss_property,
|
|
|
|
lua_class_propfunc_t newindex_miss_property,
|
2012-06-06 23:07:07 +02:00
|
|
|
const struct luaL_Reg methods[],
|
|
|
|
const struct luaL_Reg meta[])
|
2009-06-15 17:24:36 +02:00
|
|
|
{
|
2009-10-02 15:48:32 +02:00
|
|
|
/* Create the object metatable */
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_newtable(L);
|
2009-09-30 14:21:25 +02:00
|
|
|
/* Register it with class pointer as key in the registry
|
|
|
|
* class-pointer -> metatable */
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_pushlightuserdata(L, class);
|
2009-10-02 15:48:32 +02:00
|
|
|
/* Duplicate the object metatable */
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2009-09-30 14:21:25 +02:00
|
|
|
/* Now register class pointer with metatable as key in the registry
|
|
|
|
* metatable -> class-pointer */
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_pushlightuserdata(L, class);
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
2009-08-17 15:52:41 +02:00
|
|
|
|
2009-10-02 15:48:32 +02:00
|
|
|
/* Duplicate objects metatable */
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
/* Set garbage collector in the metatable */
|
|
|
|
lua_pushcfunction(L, luaA_class_gc);
|
|
|
|
lua_setfield(L, -2, "__gc");
|
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2012-06-12 13:32:40 +02:00
|
|
|
luaA_registerlib(L, NULL, meta); /* 1 */
|
|
|
|
luaA_registerlib(L, name, methods); /* 2 */
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_pushvalue(L, -1); /* dup self as metatable 3 */
|
|
|
|
lua_setmetatable(L, -2); /* set self as metatable 2 */
|
|
|
|
lua_pop(L, 2);
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2009-10-02 15:48:32 +02:00
|
|
|
class->collector = collector;
|
2009-06-15 17:24:36 +02:00
|
|
|
class->allocator = allocator;
|
|
|
|
class->name = name;
|
2009-08-20 17:37:46 +02:00
|
|
|
class->index_miss_property = index_miss_property;
|
|
|
|
class->newindex_miss_property = newindex_miss_property;
|
2009-09-30 11:55:43 +02:00
|
|
|
class->checker = checker;
|
2009-09-30 14:21:25 +02:00
|
|
|
class->parent = parent;
|
2014-12-06 18:16:05 +01:00
|
|
|
class->tostring = NULL;
|
2010-09-17 17:10:53 +02:00
|
|
|
class->instances = 0;
|
2009-06-15 17:24:36 +02:00
|
|
|
|
2010-08-25 23:00:36 +02:00
|
|
|
signal_add(&class->signals, "new");
|
|
|
|
|
|
|
|
if (parent)
|
2014-03-07 14:38:28 +01:00
|
|
|
class->signals.inherits_from = &parent->signals;
|
2010-08-25 23:00:36 +02:00
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
lua_class_array_append(&luaA_classes, class);
|
2009-06-15 17:24:36 +02:00
|
|
|
}
|
|
|
|
|
2009-06-08 17:28:20 +02:00
|
|
|
void
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_class_connect_signal(lua_State *L, lua_class_t *lua_class, const char *name, lua_CFunction fn)
|
2009-10-09 20:32:33 +02:00
|
|
|
{
|
|
|
|
lua_pushcfunction(L, fn);
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_class_connect_signal_from_stack(L, lua_class, name, -1);
|
2009-10-09 20:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_class_connect_signal_from_stack(lua_State *L, lua_class_t *lua_class,
|
|
|
|
const char *name, int ud)
|
2009-06-08 17:28:20 +02:00
|
|
|
{
|
|
|
|
luaA_checkfunction(L, ud);
|
2010-08-25 20:48:42 +02:00
|
|
|
signal_connect(&lua_class->signals, name, luaA_object_ref(L, ud));
|
2009-06-08 17:28:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-09 20:39:55 +02:00
|
|
|
luaA_class_disconnect_signal_from_stack(lua_State *L, lua_class_t *lua_class,
|
|
|
|
const char *name, int ud)
|
2009-06-08 17:28:20 +02:00
|
|
|
{
|
|
|
|
luaA_checkfunction(L, ud);
|
|
|
|
void *ref = (void *) lua_topointer(L, ud);
|
2010-08-25 20:48:42 +02:00
|
|
|
signal_disconnect(&lua_class->signals, name, ref);
|
2009-06-08 17:28:20 +02:00
|
|
|
luaA_object_unref(L, (void *) ref);
|
|
|
|
lua_remove(L, ud);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
luaA_class_emit_signal(lua_State *L, lua_class_t *lua_class,
|
|
|
|
const char *name, int nargs)
|
|
|
|
{
|
2009-08-18 18:11:42 +02:00
|
|
|
signal_object_emit(L, &lua_class->signals, name, nargs);
|
2009-06-08 17:28:20 +02:00
|
|
|
}
|
|
|
|
|
2009-06-16 15:42:32 +02:00
|
|
|
/** Try to use the metatable of an object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param idxobj The index of the object.
|
|
|
|
* \param idxfield The index of the field (attribute) to get.
|
|
|
|
* \return The number of element pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
|
|
|
|
{
|
2009-09-30 14:21:25 +02:00
|
|
|
lua_class_t *class = luaA_class_get(L, idxobj);
|
|
|
|
|
|
|
|
for(; class; class = class->parent)
|
2009-06-16 15:42:32 +02:00
|
|
|
{
|
2009-09-30 14:21:25 +02:00
|
|
|
/* Push the class */
|
|
|
|
lua_pushlightuserdata(L, class);
|
|
|
|
/* Get its metatable from registry */
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
/* Push the field */
|
|
|
|
lua_pushvalue(L, idxfield);
|
|
|
|
/* Get the field in the metatable */
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
/* Do we have a field like that? */
|
|
|
|
if(!lua_isnil(L, -1))
|
|
|
|
{
|
|
|
|
/* Yes, so remove the metatable and return it! */
|
|
|
|
lua_remove(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* No, so remove the metatable and its value */
|
|
|
|
lua_pop(L, 2);
|
2009-06-16 15:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static lua_class_property_t *
|
2010-09-01 15:41:41 +02:00
|
|
|
lua_class_property_array_getbyname(lua_class_property_array_t *arr,
|
|
|
|
const char *name)
|
2009-06-16 15:42:32 +02:00
|
|
|
{
|
2010-09-01 15:41:41 +02:00
|
|
|
lua_class_property_t lookup_prop = { .name = name };
|
2009-06-16 15:42:32 +02:00
|
|
|
return lua_class_property_array_lookup(arr, &lookup_prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a property of a object.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param lua_class The Lua class.
|
|
|
|
* \param fieldidx The index of the field name.
|
|
|
|
* \return The object property if found, NULL otherwise.
|
|
|
|
*/
|
|
|
|
static lua_class_property_t *
|
|
|
|
luaA_class_property_get(lua_State *L, lua_class_t *lua_class, int fieldidx)
|
|
|
|
{
|
|
|
|
/* Lookup the property using token */
|
2010-09-01 15:41:41 +02:00
|
|
|
const char *attr = luaL_checkstring(L, fieldidx);
|
2009-06-16 15:42:32 +02:00
|
|
|
|
2009-09-30 14:21:25 +02:00
|
|
|
/* Look for the property in the class; if not found, go in the parent class. */
|
|
|
|
for(; lua_class; lua_class = lua_class->parent)
|
|
|
|
{
|
|
|
|
lua_class_property_t *prop =
|
2010-09-01 15:41:41 +02:00
|
|
|
lua_class_property_array_getbyname(&lua_class->properties, attr);
|
2009-09-30 14:21:25 +02:00
|
|
|
|
|
|
|
if(prop)
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2009-06-16 15:42:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Generic index meta function for objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_class_index(lua_State *L)
|
|
|
|
{
|
|
|
|
/* Try to use metatable first. */
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
lua_class_t *class = luaA_class_get(L, 1);
|
|
|
|
|
2015-02-15 12:16:03 +01:00
|
|
|
/* Is this the special 'valid' property? This is the only property
|
|
|
|
* accessible for invalid objects and thus needs special handling. */
|
|
|
|
const char *attr = luaL_checkstring(L, 2);
|
|
|
|
if (A_STREQ(attr, "valid"))
|
|
|
|
{
|
|
|
|
void *p = luaA_toudata(L, 1, class);
|
|
|
|
if (class->checker)
|
|
|
|
lua_pushboolean(L, p != NULL && class->checker(p));
|
|
|
|
else
|
|
|
|
lua_pushboolean(L, p != NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-06-16 15:42:32 +02:00
|
|
|
lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
|
|
|
|
|
|
|
|
/* Property does exist and has an index callback */
|
2009-08-20 17:37:46 +02:00
|
|
|
if(prop)
|
|
|
|
{
|
|
|
|
if(prop->index)
|
|
|
|
return prop->index(L, luaA_checkudata(L, 1, class));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(class->index_miss_property)
|
|
|
|
return class->index_miss_property(L, luaA_checkudata(L, 1, class));
|
|
|
|
}
|
2009-06-16 15:42:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generic newindex meta function for objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_class_newindex(lua_State *L)
|
|
|
|
{
|
|
|
|
/* Try to use metatable first. */
|
|
|
|
if(luaA_usemetatable(L, 1, 2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
lua_class_t *class = luaA_class_get(L, 1);
|
|
|
|
|
|
|
|
lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
|
|
|
|
|
|
|
|
/* Property does exist and has a newindex callback */
|
2009-08-20 17:37:46 +02:00
|
|
|
if(prop)
|
|
|
|
{
|
|
|
|
if(prop->newindex)
|
|
|
|
return prop->newindex(L, luaA_checkudata(L, 1, class));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(class->newindex_miss_property)
|
|
|
|
return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
|
|
|
|
}
|
2009-06-16 15:42:32 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-25 11:21:51 +02:00
|
|
|
/** Generic constructor function for objects.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_class_new(lua_State *L, lua_class_t *lua_class)
|
|
|
|
{
|
|
|
|
/* Check we have a table that should contains some properties */
|
|
|
|
luaA_checktable(L, 2);
|
|
|
|
|
|
|
|
/* Create a new object */
|
|
|
|
void *object = lua_class->allocator(L);
|
|
|
|
|
|
|
|
/* Push the first key before iterating */
|
|
|
|
lua_pushnil(L);
|
|
|
|
/* Iterate over the property keys */
|
|
|
|
while(lua_next(L, 2))
|
|
|
|
{
|
|
|
|
/* Check that the key is a string.
|
|
|
|
* We cannot call tostring blindly or Lua will convert a key that is a
|
|
|
|
* number TO A STRING, confusing lua_next() */
|
|
|
|
if(lua_isstring(L, -2))
|
|
|
|
{
|
2009-09-30 14:21:25 +02:00
|
|
|
lua_class_property_t *prop = luaA_class_property_get(L, lua_class, -2);
|
2009-06-25 11:21:51 +02:00
|
|
|
|
|
|
|
if(prop && prop->new)
|
|
|
|
prop->new(L, object);
|
|
|
|
}
|
|
|
|
/* Remove value */
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|