lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It is the role of lua_pcall()'s third parameter to specify a function
which adds a full stack traceback, not the role of a library (this is
not the formal role of lua_pcall, but is one of the most common uses).
For example, the default Lua interpreter:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> table.sort({1, {2}})
attempt to compare table with number
stack traceback:
        [C]: in function 'sort'
        stdin:1: in main chunk
        [C]: ?

On Wed, Feb 4, 2009 at 11:42 PM, Justin Pease <jspease@gmail.com> wrote:
> The following Lua code causes an error, as expected, but (as of Lua
> 5.1.4) the error message is missing any file or line number
> information:
>
>  table.sort({1,{2}}) --> "attempt to compare table with number"
>
> This lack of information can be quite confusing for someone who
> happens to sort the wrong table somewhere in a big script and doesn't
> know what to make of the error message.
>
> I'm not sure if this is an internal error in ltablib.c, but regardless
> of that, I think Lua should be able to handle situations like this
> better; even internal errors in a library should at least point to the
> line of Lua code that triggered the error.
>
> A small addition to the addinfo function in ldebug.c seems to greatly
> improve the situation, something like:
>
>  static void addinfo (lua_State *L, const char *msg) {
>   CallInfo *ci = L->ci;
>   if (isLua(ci)) {  /* is Lua code? */
>     char buff[LUA_IDSIZE];  /* add file:line information */
>     int line = currentline(L, ci);
>     luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);
>     luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
>   }
> +  else { /* it's not Lua code, but we can find the Lua code that called it */
> +    lua_Debug ar;
> +    if (lua_getstack(L, 1, &ar)) {  /* check function at level 1 */
> +      lua_getinfo(L, "Sl", &ar);  /* get info about it */
> +      if (ar.currentline > 0) {  /* is there info? */
> +        luaO_pushfstring(L, "%s:%d: internal error: %s",
> ar.short_src, ar.currentline, msg);
> +      }
> +    }
> +  }
>  }
>