lua-users home
lua-l archive

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


Today I ran into another place where having access to the
lua_State->errfunc field would be *very* useful.  Specifically, if I
want to "defer" throwing of an error to a later time, I would prefer
to use lua_pcall (or xpcall if I'm in Lua code) with the current error
function.  Here is a bit more concrete example:

int C_callback(void* ud) {
    lua_State* L = (lua_State*)ud;
    /* Doing a longjump from this callback is NOT safe! */
    return lua_pcall(L, 1, 0, 0, LUA_CURRENT_ERRFUNC);
}

int L_runcallback(lua_State* L) {
    luaL_checktype(L, 1, LUA_TFUNCTION);
    int err = runcallback(L, &C_callback);
    /* Propagate the error message */
    if ( err ) lua_error(L);
    return 0;
}

Of course LUA_CURRENT_ERRFUNC is not valid, but if there was some way
to tell lua_pcall() that we want to preserve the current errfunc that
would be awesome... or if there was simply an API to push the current
error function on the stack, that would also work :-).

Thanks,
-Brian

On Sun, Mar 13, 2011 at 6:11 AM, GrayFace <sergroj@mail.ru> wrote:
> On 13.03.2011 18:41, Matthew Wild wrote:
>>
>>  Can you not throw a table, containing the message and additional
>>  information, then throw again just the message from your handler?
>
>
> I can get the additional information at any point in the program, but
> the problem is appending it to error message and still reporting all the
> information about error normally being reported in the target
> application. The standard error handling function can only be invoked if
> I call 'error' before or after xpcall, but if traceback is formed at
> that point it wouldn't contain any useful information, because
> some_function and functions it calls wouldn't be there. I cannot pass
> additional information to 'error' function either, because not only my
> code calls it, 'error' can be called from custom persistence functions
> called from my code.
>
> However, when I think more of it, what I really want is a hook that
> would only be called when lua_error is called, so I can change the
> message just before lua_error does its job.
>
>
> --
> Best regards,
> Sergey Rozhenko                 mailto:sergroj@mail.ru
>
>
>



-- 
Brian Maher >> Glory to God <<