Browse Source

[Feature] Improve error report for type missmatch in lua

tags/1.3.0
Vsevolod Stakhov 8 years ago
parent
commit
a1347ca2e2
2 changed files with 43 additions and 15 deletions
  1. 42
    15
      src/lua/lua_common.c
  2. 1
    0
      src/lua/lua_common.h

+ 42
- 15
src/lua/lua_common.c View 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;

+ 1
- 0
src/lua/lua_common.h View 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

Loading…
Cancel
Save