]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow direct userdata extraction from a table
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 27 Aug 2020 21:42:39 +0000 (22:42 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 27 Aug 2020 21:42:39 +0000 (22:42 +0100)
src/lua/lua_common.c
src/lua/lua_common.h

index d27bf05298c6e4295bd98a72c7fee49eca6893a1..b7fcc2034ca78116340f40233a35930bf349f8ab 100644 (file)
@@ -1348,7 +1348,7 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
                read_semicolon
        } state = read_key;
        gsize keylen = 0, *valuelen, clslen;
-       gint idx = 0, t;
+       gint idx = 0, t, direct_userdata = 0;
 
        g_assert (extraction_pattern != NULL);
 
@@ -1403,7 +1403,7 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
 
                        t = lua_type (L, idx);
 
-                       switch (g_ascii_toupper (*p)) {
+                       switch (*p) {
                        case 'S':
                                if (t == LUA_TSTRING) {
                                        *(va_arg (ap, const gchar **)) = lua_tostring (L, idx);
@@ -1728,6 +1728,39 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
 
                                state = read_class_start;
                                clslen = 0;
+                               direct_userdata = 0;
+                               cls = NULL;
+                               p ++;
+                               continue;
+                       case 'u':
+                               if (t == LUA_TNIL || t == LUA_TNONE) {
+                                       failed = TRUE;
+
+                                       if (how != RSPAMD_LUA_PARSE_ARGUMENTS_IGNORE_MISSING) {
+                                               *(va_arg (ap, void **)) = NULL;
+                                       }
+                                       else {
+                                               (void)va_arg (ap, void **);
+                                       }
+                               }
+                               else if (t != LUA_TUSERDATA) {
+                                       g_set_error (err,
+                                                       lua_error_quark (),
+                                                       1,
+                                                       "bad type for key:"
+                                                       " %.*s: '%s', '%s' is expected",
+                                                       (gint) keylen,
+                                                       key,
+                                                       lua_typename (L, lua_type (L, idx)),
+                                                       "int64");
+                                       va_end (ap);
+
+                                       return FALSE;
+                               }
+
+                               state = read_class_start;
+                               clslen = 0;
+                               direct_userdata = 1;
                                cls = NULL;
                                p ++;
                                continue;
@@ -1805,7 +1838,14 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
                                 */
                                if (!failed && (!is_table ||
                                                rspamd_lua_check_class (L, idx, classbuf))) {
-                                       *(va_arg (ap, void **)) = *(void **)lua_touserdata (L, idx);
+                                       if (direct_userdata) {
+                                               void **arg_p = (va_arg (ap, void **));
+                                               *arg_p = lua_touserdata (L, idx);
+                                       }
+                                       else {
+                                               *(va_arg (ap,
+                                               void **)) = *(void **) lua_touserdata (L, idx);
+                                       }
                                }
                                else {
                                        if (!failed) {
index 4c9e9dd4f8441ffd351b67c2f920ad22cbb26ee2..737c4b817cefc68709174d70d613e16524065b71 100644 (file)
@@ -427,6 +427,7 @@ enum rspamd_lua_parse_arguments_flags {
  * - F - function
  * - O - ucl_object_t *
  * - D - same as N but argument is set to NAN not to 0.0
+ * - u{classname} - userdata of the following class (stored directly)
  *
  * If any of keys is prefixed with `*` then it is treated as required argument
  * @param L lua state