lua-users home
lua-l archive

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


You're right, Philipp: that should be `0`, not LUA_NOREF for the `msgh` parameter (there is no message handler). The result is still -1, however.

As for Chris Beck's point about this being an improper usage: that's how I (and others) initially spoke to the usage of a throw for the user compiling with C++ and wanting a thrown exception to be caught and properly handled without the trampoline. But, it still made me curious. I understand that losing exception information is bad (if Lua does have to do something like that): I would not mind if the behavior was well-defined for exceptions that derived from `std::exception`: e.g., `std::exception::what()` is called, the `c_str()` pushed as the error, and then the message handler called if it exists. (This could also be done for `const char*` throws as well, and then everything else captured with `...` and then written off as "unknown C++ exception handled").

This is really just musing at this point. This only affects a very small part of sol2, where someone directly serializes a `lua_CFunction` and does not do it through any of the typical sol2 mechanics. At this point I would assume if a person is willing to be exposed to the underlying C API in this manner, they're more or less ready to play ball with it.

I just wanted to know whether this was a by-design bug, a missed corner case, or something that the implementation did want to handle at some point. Knowing which it is helps me guide people that open bugs in my repo wondering why this behavior happens.

On Sat, Oct 15, 2016 at 12:37 AM, Philipp Janda <siffiejoe@gmx.net> wrote:
Am 15.10.2016 um 01:45 schröbte ThePhD:
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:

// Compile Lua 5.3.3 as C++
// no extern "C" { ... }
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int thrower(lua_State* L) {
    throw 0;
}

int main() {

    lua_State* L = luaL_newstate();

    lua_pushcclosure(L, thrower, 0);
    int top_before = lua_gettop(L);
    int code = lua_pcallk(L, 0, 0, LUA_NOREF, 0, NULL);
    int top_after  = lua_gettop(L);

    return 0;
}

`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.

It looks that way. `lua_resume` specifically handles a -1 status and converts it into `LUA_ERRRUN`, but this is missing for `lua_pcallk`. `LUA_ERRRUN` would probably be the correct return value in this situation, because there is no error message handler function at stack index -2 (which is the value of `LUA_NOREF`). You probably want to use a positive number (or `LUA_MULTRET` which is -1) there instead.

Philipp