[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua_next() crashing on former LUA_TNONE arguments
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 15 Dec 2017 17:02:39 +0200
2017-12-15 12:50 GMT+02:00 Viacheslav Usov <via.usov@gmail.com>:
> Because, as I said above, the number of functions requiring a very strict
> stack discipline (stricter than the valid/acceptable index requirement) is
> not large, I think it would be good to document that explicitly. Even if
> that does not prevent making those mistakes, it will be much easier to
> understand that it is indeed a mistake and require no research as to whether
> this is a mistaken use of the API or a bug in Lua.
On the contrary, the number of functions that can recover without crashing
from receiving a value of the wrong type is very small, and _this_ fact
deserves documentation. E.g. lua_tonumberx (which is the actual function
sitting under lua_tonumber) has a manual entry "The Lua value must be
a number or a string convertible to a number (see §3.4.3); otherwise,
lua_tonumberx returns 0."
If one codes in the style of the standard library, using luaL_check* functions,
then these crashes are rare, but the moment one starts doing cute stuff,
they just keep coming.
I can't even count the number of crashes I have had with lua_gettable,
lua_geti etc because I was writing some clever code with a lot of
stack manipulations and the table itself was not where I thought it was.
My API code has inside almost every file:
static void stackprint(lua_State *L, int from) {
int top=lua_gettop(L);
printf("Stack:");
while (from<=top) {
printf(" %s",luaL_tolstring(L,from++,NULL));
lua_pop(L,1); }
printf("\n");
}
static void arrayprint(lua_State *L, int from, int to) {
printf("Array:");
while (from<=to) { lua_rawgeti(L,1,from++);
printf(" %s",luaL_tolstring(L,-1,NULL));
lua_pop(L,2); }
printf("\n");
}
to be called while debugging.
Of course, nothing but hubris stops one from using the luaL_check*
functions later down in the code :-)