[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Change to Lua C API's handling of unprotected errors
- From: William Sumner <prestonsumner@...>
- Date: Fri, 22 Nov 2013 12:41:10 -0700
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?