implement table-as-argument for lua_arg_*
authorTero Marttila <terom@fixme.fi>
Sun, 19 Apr 2009 04:52:12 +0300
changeset 142 dc2bb09d412c
parent 141 0b850238c588
child 143 1edab39c88a8
implement table-as-argument for lua_arg_*
src/lua_irc.c
src/lua_objs.c
src/lua_objs.h
--- a/src/lua_irc.c	Sun Apr 19 04:35:29 2009 +0300
+++ b/src/lua_irc.c	Sun Apr 19 04:52:12 2009 +0300
@@ -149,16 +149,17 @@
 
 static int lua_client_set_defaults (lua_State *L)
 {
+    int nargs = lua_gettop(L);
     struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
 
     // required args
-    lua_client->defaults.register_info.nickname = lua_arg_string(L, 2, "nickname", LUA_ARG_STRING_REQUIRED);
-    lua_client->defaults.register_info.username = lua_arg_string(L, 3, "username", LUA_ARG_STRING_REQUIRED);
-    lua_client->defaults.register_info.realname = lua_arg_string(L, 4, "realname", LUA_ARG_STRING_REQUIRED);
+    lua_client->defaults.register_info.nickname = lua_arg_string(L, nargs, 2, "nickname", LUA_ARG_STRING_REQUIRED);
+    lua_client->defaults.register_info.username = lua_arg_string(L, nargs, 3, "username", LUA_ARG_STRING_REQUIRED);
+    lua_client->defaults.register_info.realname = lua_arg_string(L, nargs, 4, "realname", LUA_ARG_STRING_REQUIRED);
 
     // optional args
-    lua_client->defaults.service        = lua_arg_string(L, 5, "service", IRC_PORT);
-    lua_client->defaults.service_ssl    = lua_arg_string(L, 6, "service_ssl", IRC_SSL_PORT);
+    lua_client->defaults.service        = lua_arg_string(L, nargs, 5, "service", IRC_PORT);
+    lua_client->defaults.service_ssl    = lua_arg_string(L, nargs, 6, "service_ssl", IRC_SSL_PORT);
 
     // invoke
     irc_client_set_defaults(lua_client->client, &lua_client->defaults);
@@ -169,6 +170,7 @@
 
 static int lua_client_connect (lua_State *L)
 {
+    int nargs = lua_gettop(L);
     struct lua_client *lua_client = lua_obj_get_obj(L, __func__, "evirc.client");
     struct irc_net_info net_info;
     const char *ssl_cafile = NULL, *ssl_cert = NULL, *ssl_pkey = NULL;
@@ -180,18 +182,18 @@
     memset(&net_info, 0, sizeof(net_info));
 
     // required args
-    net_info.network    = lua_arg_string(L, 2, "network",       LUA_ARG_STRING_REQUIRED);
-    net_info.hostname   = lua_arg_string(L, 3, "hostname",      LUA_ARG_STRING_REQUIRED);
+    net_info.network    = lua_arg_string(L, nargs, 2, "network",    LUA_ARG_STRING_REQUIRED);
+    net_info.hostname   = lua_arg_string(L, nargs, 3, "hostname",   LUA_ARG_STRING_REQUIRED);
 
     // optional args
-    net_info.service    = lua_arg_string(L, 4, "service",       NULL);
+    net_info.service    = lua_arg_string(L, nargs, 4, "service",    NULL);
 
     // ssl stuff
-    use_ssl             = lua_arg_bool  (L, 5, "use_ssl",       false);
-    ssl_cafile          = lua_arg_string(L, 6, "ssl_cafile",    NULL);
-    ssl_verify          = lua_arg_bool  (L, 7, "ssl_verify",    false);
-    ssl_cert            = lua_arg_string(L, 8, "ssl_cert",      NULL);
-    ssl_pkey            = lua_arg_string(L, 9, "ssl_pkey",      NULL);
+    use_ssl             = lua_arg_bool  (L, nargs, 5, "use_ssl",    false);
+    ssl_cafile          = lua_arg_string(L, nargs, 6, "ssl_cafile", NULL);
+    ssl_verify          = lua_arg_bool  (L, nargs, 7, "ssl_verify", false);
+    ssl_cert            = lua_arg_string(L, nargs, 8, "ssl_cert",   NULL);
+    ssl_pkey            = lua_arg_string(L, nargs, 9, "ssl_pkey",   NULL);
 
     // SSL?
     if (use_ssl || ssl_cafile || ssl_verify || ssl_cert || ssl_pkey) {
--- 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)
--- a/src/lua_objs.h	Sun Apr 19 04:35:29 2009 +0300
+++ b/src/lua_objs.h	Sun Apr 19 04:52:12 2009 +0300
@@ -40,12 +40,12 @@
 /**
  * Parse and return a string argument
  */
-const char *lua_arg_string (lua_State *L, int index, const char *name, const char *def);
+const char *lua_arg_string (lua_State *L, int nargs, int index, const char *name, const char *def);
 
 /**
  * Parse and return a boolean argument
  */
-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);
 
 /**
  * Registers our lua runtime objects into the given lua state.