lua-users home
lua-l archive

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


> I have a strange bug I came across while trying to help someone with an
> issue with Sol. Lua says it can compile as C++. So, I tried to compile the
> following with C++ and I get a strange error after throwing an error that
> gets caught by the LUA_TRY macro and then returned:
> 
> [...]
> `code == -1`, which isn't a valid return for `lua_pcallk`, and `top_before
> == 1 `, and `top_after == 1` as well (the function is not cleaned off the
> stack).
> 
> It seems like a bug in the Lua implementation.

(Got time to look into it.)

Status is -1 on purpose, although I cannot remember why :-) See ldo.c:

    /* C++ exceptions */
    #define LUAI_THROW(L,c)         throw(c)
    #define LUAI_TRY(L,c,a) \
            try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
                                                                       ^^

That is, in C++, Lua explicitly sets the status to -1 when there are no
other status (which means the error was raised directly by C++ code,
not going through Lua). Probably it was a way to tell that the error
is not one known by Lua.

About top_after == 1, this is the correct behavior (sort of): Lua
pops the function and pushes the error object. (As there was no error
object, the function ends there anyway.) If you provide an error
object, it will be there at index 1:

int thrower (lua_State* L) {
    lua_pushliteral(L, "error message");
    throw 0;
}

(Maybe for that kind of errors we should assume that there is no
error object?)

-- Roberto