src/lua_objs.c
changeset 142 dc2bb09d412c
parent 141 0b850238c588
child 143 1edab39c88a8
--- a/src/lua_objs.c	Sun Apr 19 04:35:29 2009 +0300
+++ b/src/lua_objs.c	Sun Apr 19 04:52:12 2009 +0300
@@ -66,9 +66,57 @@
     }
 }
 
-const char *lua_arg_string (lua_State *L, int index, const char *name, const char *def)
+static void lua_getindex (lua_State *L, int t, int i)
+{
+    lua_pushinteger(L, i);
+    lua_gettable(L, t);
+}
+
+/**
+ * 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 (name && lua_istable(L, 2)) {
+        // push the value from the 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)
 {
     const char *value;
+
+    // map index
+    index = lua_arg_index(L, nargs, index, name);
  
     // use default?
     if (lua_isnoneornil(L, index) && def != (const char *) LUA_ARG_REQUIRED)
@@ -82,9 +130,12 @@
     luaL_error(L, "missing value for required string argument <%d:%s>", index, name);
 }
 
-bool lua_arg_bool (lua_State *L, int index, const char *name, int def)
+bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def)
 {
     bool value;
+    
+    // map index
+    index = lua_arg_index(L, nargs, index, name);
  
     // use default?
     if (lua_isnoneornil(L, index) && def != LUA_ARG_REQUIRED)