]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: convert field to lua type
authorThierry Fournier <tfournier@arpalert.org>
Wed, 16 Mar 2016 17:29:13 +0000 (18:29 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 30 Mar 2016 13:46:18 +0000 (15:46 +0200)
This function converts a field used by stats in lua type.

src/hlua_fcn.c

index 1e58f7ab2eaaea22784e3cef21e4190939fd6a9a..69d6ee652d7693ff91ac72730beb4834025bbf12 100644 (file)
 
 #include <types/hlua.h>
 
+#include <proto/dumpstats.h>
 #include <proto/proto_http.h>
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
 
+/* This function gets a struct field and convert it in Lua
+ * variable. The variable is pushed at the top of the stak.
+ */
+int hlua_fcn_pushfield(lua_State *L, struct field *field)
+{
+       /* The lua_Integer is always signed. Its length depends on
+        * compilation opions, so the followinfg code is conditionned
+        * by some macros. Windows maros are not supported.
+        * If the number cannot be represented as integer, we try to
+        * convert to float.
+        */
+       switch (field_format(field, 0)) {
+
+       case FF_EMPTY:
+               lua_pushnil(L);
+               return 1;
+
+       case FF_S32:
+               /* S32 is always supported. */
+               lua_pushinteger(L, field->u.s32);
+               return 1;
+
+       case FF_U32:
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
+               /* 64 bits case, U32 is always supported */
+               lua_pushinteger(L, field->u.u32);
+#else
+               /* 32 bits case, U32 is supported until INT_MAX. */
+               if (field->u.u32 > INT_MAX)
+                       lua_pushnumber(L, (lua_Number)field->u.u32);
+               else
+                       lua_pushinteger(L, field->u.u32);
+#endif
+               return 1;
+
+       case FF_S64:
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
+               /* 64 bits case, S64 is always supported */
+               lua_pushinteger(L, field->u.s64);
+#else
+               /* 64 bits case, S64 is supported beetween INT_MIN and INT_MAX */
+               if (field->u.s64 < INT_MIN || field->u.s64 > INT_MAX)
+                       lua_pushnumber(L, (lua_Number)field->u.s64);
+               else
+                       lua_pushinteger(L, (int)field->u.s64);
+#endif
+               return 1;
+
+       case FF_U64:
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && (__WORDSIZE == 64)))
+               /* 64 bits case, U64 is supported until LLONG_MAX */
+               if (field->u.u64 > LLONG_MAX)
+                       lua_pushnumber(L, (lua_Number)field->u.u64);
+               else
+                       lua_pushinteger(L, field->u.u64);
+#else
+               /* 64 bits case, U64 is supported until INT_MAX */
+               if (field->u.u64 > INT_MAX)
+                       lua_pushnumber(L, (lua_Number)field->u.u64);
+               else
+                       lua_pushinteger(L, (int)field->u.u64);
+#endif
+               return 1;
+
+       case FF_STR:
+               lua_pushstring(L, field->u.str);
+               return 1;
+
+       default:
+               break;
+       }
+
+       /* Default case, never reached. */
+       lua_pushnil(L);
+       return 1;
+}
+
 /* Some string are started or terminated by blank chars,
  * this function removes the spaces, tabs, \r and
  * \n at the begin and at the end of the string "str", and