]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Improve error report for type missmatch in lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 13 Jun 2016 17:53:03 +0000 (18:53 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 13 Jun 2016 17:53:03 +0000 (18:53 +0100)
src/lua/lua_common.c
src/lua/lua_common.h

index 85dbfa37240bc5512f3004ecb08c5118f066ed8f..725160ee0c0147ed384fc6197992ac55cea82d3e 100644 (file)
@@ -68,36 +68,63 @@ rspamd_lua_new_class_full (lua_State *L,
        luaL_register (L, static_name, func);
 }
 
-gint
-rspamd_lua_class_tostring (lua_State * L)
+static const gchar *
+rspamd_lua_class_tostring_buf (lua_State *L, gboolean print_pointer, gint pos)
 {
-       gchar buf[32];
+       static gchar buf[64];
+       const gchar *ret = NULL;
+       gint pop = 0;
 
-       if (!lua_getmetatable (L, 1)) {
-               goto error;
+       if (!lua_getmetatable (L, pos)) {
+               goto err;
        }
+
        lua_pushstring (L, "__index");
        lua_gettable (L, -2);
+       pop ++;
 
        if (!lua_istable (L, -1)) {
-               goto error;
+               goto err;
        }
+
        lua_pushstring (L, "class");
        lua_gettable (L, -2);
+       pop ++;
 
        if (!lua_isstring (L, -1)) {
-               goto error;
+               goto err;
        }
 
-       snprintf (buf, sizeof (buf), "%p", lua_touserdata (L, 1));
+       if (print_pointer) {
+               rspamd_snprintf (buf, sizeof (buf), "%s(%p)", lua_tostring (L, -1),
+                               lua_touserdata (L, 1));
+       }
+       else {
+               rspamd_snprintf (buf, sizeof (buf), "%s", lua_tostring (L, -1));
+       }
 
-       lua_pushfstring (L, "%s: %s", lua_tostring (L, -1), buf);
+       ret = buf;
 
-       return 1;
+err:
+       lua_pop (L, pop);
+
+       return ret;
+}
+
+gint
+rspamd_lua_class_tostring (lua_State * L)
+{
+       const gchar *p;
+
+       p = rspamd_lua_class_tostring_buf (L, TRUE, 1);
+
+       if (!p) {
+               lua_pushstring (L, "invalid object passed to 'lua_common.c:__tostring'");
+               return lua_error (L);
+       }
+
+       lua_pushstring (L, p);
 
-error:
-       lua_pushstring (L, "invalid object passed to 'lua_common.c:__tostring'");
-       lua_error (L);
        return 1;
 }
 
@@ -804,11 +831,11 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos,
                                                g_set_error (err,
                                                                lua_error_quark (),
                                                                2,
-                                                               "invalid class for key %*.s, expected %s, got %s",
+                                                               "invalid class for key %.*s, expected %s, got %s",
                                                                (gint) keylen,
                                                                key,
                                                                classbuf,
-                                                               lua_tostring (L, idx));
+                                                               rspamd_lua_class_tostring_buf (L, FALSE, idx));
                                                va_end (ap);
 
                                                return FALSE;
index 63039533c8e7d27049ed3251d75089f33c64e9bd..ba389d7a637fd7cf4653742908602b2282a6484f 100644 (file)
@@ -302,6 +302,7 @@ struct rspamd_config * lua_check_config (lua_State * L, gint pos);
  * - V - size_t + const char *
  * - U{classname} - userdata of the following class (stored in gpointer)
  * - F - function
+ * - O - ucl_object_t *
  *
  * If any of keys is prefixed with `*` then it is treated as required argument
  * @param L lua state