src/lua_func.c
branchnew-lib-errors
changeset 219 cefec18b8268
parent 218 5229a5d098b2
--- a/src/lua_func.c	Thu May 28 00:35:02 2009 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,207 +0,0 @@
-#include "lua_func.h"
-#include "error.h"
-
-#include <lua5.1/lauxlib.h>
-
-/**
- * Pushes onto the stack the value at t[i]
- */
-static void lua_getindex (lua_State *L, int t, int i)
-{
-    lua_pushinteger(L, i);
-    lua_gettable(L, t);
-}
-
-/**
- * Pushes onto the stack either:
- *  * the value at t[name]
- *  * the value at t[index]
- *
- *  Returns the new index, or 0, if neither could be found
- */
-static int lua_arg_lookup (lua_State *L, int t, const char *name, int index)
-{
-    // try name
-    lua_getfield(L, t, name);
-
-    if (!lua_isnil(L, -1))
-        return lua_gettop(L);
-    else
-        lua_pop(L, 1);
-    
-    // try index
-    lua_getindex(L, t, index);
-    
-    if (!lua_isnil(L, -1))
-        return lua_gettop(L);
-
-    else
-        lua_pop(L, 1);
-    
-    // not found
-    return 0;
-}
-
-static const char *_lua_arg_string (lua_State *L, int index, const char *name, const char *def)
-{
-    const char *value;
-
-    // use default?
-    if (lua_isnoneornil(L, index) && def != (const char *) LUA_ARG_REQUIRED)
-        return def;
-    
-    // value given?
-    if ((value = lua_tostring(L, index)))
-        return value;
-   
-    // error
-    luaL_error(L, "missing value for required string argument <%d:%s>", index, name); return NULL;
-}
-
-static bool _lua_arg_bool (lua_State *L, int index, const char *name, int def)
-{
-    (void) name;
-
-    // use default?
-    if (lua_isnoneornil(L, index) && def != LUA_ARG_REQUIRED)
-        return def;
-   
-    // value given
-    return lua_toboolean(L, index);
-}
-
-static long _lua_arg_int (lua_State *L, int index, const char *name, long def)
-{
-    (void) name;
-
-    // use default?
-    if (lua_isnoneornil(L, index) && def != LUA_ARG_REQUIRED)
-        return def;
-   
-    // conver to integer
-    // XXX: check compatibility?
-    return lua_tointeger(L, index);
-}
-
-static void * _lua_arg_obj (lua_State *L, int index, const struct lua_type *type, bool optional)
-{
-    // not given?
-    if (!lua_isnoneornil(L, index))
-        return lua_type_get(L, type, index);
-
-    if (optional)
-        return NULL;
-    
-    luaL_error(L, "missing value for required object argument <%d:%s>", index, type->name);
-    return NULL;
-}
-
-/**
- * Look up the arg index to use for the given index/name.
- *
- * If no value is found for the corresponding index, returns zero.
- */
-static int lua_arg_index (lua_State *L, int nargs, int index, const char *name)
-{
-    // lookup from table?
-    if (nargs == 2 && lua_istable(L, 2) && name) {
-        // push the value from the named field onto the stack
-        lua_getfield(L, 2, name);
-        
-        // no named field?
-        if (lua_isnil(L, -1)) {
-            lua_pop(L, 1);
-
-            lua_getindex(L, 2, index - 1);
-        }
-
-        // no index field?
-        if (lua_isnil(L, -1)) {
-            lua_pop(L, 1);
-
-            return 0;
-        }
-        
-        // found either a named or indexed arg
-        return lua_gettop(L);
-
-    } else if (index <= nargs) {
-        // use the same index
-        return index;
-
-    } else {
-        // no index
-        return 0;
-    }
-}
-
-const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def)
-{
-    return _lua_arg_string(L, lua_arg_index(L, nargs, index, name), name, def);
-}
-
-bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def)
-{
-    return _lua_arg_bool(L, lua_arg_index(L, nargs, index, name), name, def);    
-}
-
-void* lua_arg_obj (lua_State *L, int nargs, int index, const struct lua_type *type, bool optional)
-{
-    return _lua_arg_obj(L, lua_arg_index(L, nargs, index, NULL), type, optional);
-}
-
-long lua_arg_int (lua_State *L, int nargs, int index, const char *name, long def)
-{
-    return _lua_arg_int(L, lua_arg_index(L, nargs, index, name), name, def);
-}
-
-void lua_args_parse (lua_State *L, const struct lua_func *func, void **obj_ptr, ...)
-{
-    int argidx = 1, argtbl = 0, idx;
-    const struct lua_func_arg *arg;
-    va_list vargs;
-
-    // first, the obj argument
-    if (func->type)
-        *obj_ptr = lua_type_get(L, func->type, argidx++);
-
-    // were we given a table of arguments?
-    if (lua_istable(L, argidx))
-        argtbl = argidx++;
-
-    // parse the args
-    va_start(vargs, obj_ptr);
-
-    for (arg = func->args, idx = 1; arg->name && arg->type; arg++, idx++) {
-        int index;
-
-        // map index
-        if (!argtbl)
-            // direct
-            index = argidx++;
-        
-        else
-            // lookup from table
-            index = lua_arg_lookup(L, argtbl, arg->name, idx);
-        
-        // apply
-        switch (arg->type) {
-            case LUA_ARG_STRING:
-                *va_arg(vargs, const char **) = _lua_arg_string(L, index, arg->name, arg->def.string);
-                break;
-
-            case LUA_ARG_BOOL:
-                *va_arg(vargs, bool *) = _lua_arg_bool(L, index, arg->name, arg->def.boolean);
-                break;
-            
-            case LUA_ARG_INT:
-                *va_arg(vargs, long *) = _lua_arg_int(L, index, arg->name, arg->def.integer);
-                break;
-
-            default:
-                NOT_REACHED();
-        };
-    }
-
-    va_end(vargs);
-}