implement LUA_ARG_INT
authorTero Marttila <terom@fixme.fi>
Wed, 20 May 2009 22:49:03 +0300
changeset 201 5c34c5d90a3f
parent 200 c414343101df
child 202 210c43e6c088
implement LUA_ARG_INT
src/lua_func.c
src/lua_func.h
--- a/src/lua_func.c	Fri May 15 00:05:01 2009 +0300
+++ b/src/lua_func.c	Wed May 20 22:49:03 2009 +0300
@@ -70,6 +70,19 @@
     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?
@@ -132,11 +145,16 @@
     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)
+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;
@@ -175,6 +193,10 @@
             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();
--- a/src/lua_func.h	Fri May 15 00:05:01 2009 +0300
+++ b/src/lua_func.h	Wed May 20 22:49:03 2009 +0300
@@ -14,9 +14,15 @@
  */
 enum lua_arg_type {
     LUA_ARG_INVALID,
+    
+    /** A `const char *` pointing to lua-GC'd memory */
+    LUA_ARG_STRING,
 
-    LUA_ARG_STRING,
+    /** A c99 `bool` */
     LUA_ARG_BOOL,
+
+    /** A `long signed int` */
+    LUA_ARG_INT,
 };
 
 /**
@@ -33,6 +39,7 @@
     union {
         const char *string;
         int boolean;
+        long integer;
     } def;
 };
 
@@ -64,6 +71,7 @@
  */
 #define LUA_FUNC_ARG_STRING(name, def) { (name), LUA_ARG_STRING, { .string = (def) } }
 #define LUA_FUNC_ARG_BOOL(name, def) { (name), LUA_ARG_BOOL, { .boolean = (def) } }
+#define LUA_FUNC_ARG_INT(name, def) { (name), LUA_ARG_INT, { .integer = (def) } }
 #define LUA_FUNC_ARG_END { NULL, 0, { 0 } }
 
 /**
@@ -82,9 +90,14 @@
 bool lua_arg_bool (lua_State *L, int nargs, int index, const char *name, int def);
 
 /**
+ * Parse and return an integer argument
+ */
+long lua_arg_int (lua_State *L, int nargs, int index, const char *name, long def);
+
+/**
  * Return a userdata argument at the given fixed index
  */
-void * lua_arg_obj (lua_State *L, int nargs, int index, const struct lua_type *type, bool optional);
+void* lua_arg_obj (lua_State *L, int nargs, int index, const struct lua_type *type, bool optional);
 
 /**
  * Parse function arguments as defined