From: Vsevolod Stakhov Date: Mon, 13 Jun 2016 17:53:03 +0000 (+0100) Subject: [Feature] Improve error report for type missmatch in lua X-Git-Tag: 1.3.0~354 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a1347ca2e256817a3829416088de0ee304f44711;p=rspamd.git [Feature] Improve error report for type missmatch in lua --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 85dbfa372..725160ee0 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -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; diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 63039533c..ba389d7a6 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -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