lua-users home
lua-l archive

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


I’ve seen a few solutions to handling Lua C API errors outside protected mode, some of them involving lua_pcall wrappers or C++ trickery. Thinking about this, a more universal solution would be to change the API so that functions which can raise an error return a status code and deliver information through out parameters. For example, it’s not explicitly stated in the reference manual that lua_compare() can generate a panic:

    // int lua_compare (lua_State *L, int index1, int index2, int op);

    lua_pushnumber(L, 123);
    lua_pushnil(L);
    int result = lua_compare(L, -2, -1, LUA_OPLT);

Instead, it might look like:

    // LUA_STATUS lua_compare (lua_State *L, int index1, int index2, int op, int *result);    

    lua_pushnumber(L, 123);
    lua_pushnil(L);
    int result;
    if (lua_compare(L, -2, -1, LUA_OPLT, &result) != LUA_OK) {
        // Handle error
    }

Prototypes for API functions that can generate an error would become self-documenting in this respect. Is this a good idea?