Hi Adam, Viacheslav
Adam: If this can't be relied upon, then Massimo is completely correct that the comparison is wrong.
Viacheslav: I did not understand what Massimo was trying to achieve.
Pls don't focus on that string comparison.
However you take the time to read and answer to me, so I have to
1) explain this detail;
2) explain again my concerns about Lua APIs.
1) string comparison
Lua into lstring.h defines:
#define MEMERRMSG "not enough memory"
There isn't any written contract
"In case of error, of type "not enough memory", the object pushed on the stack is exactly the pointer to the string literal MEMERRMSG."
A direct pointer comparison can fail. This test cannot fail:
if (ret != 0 && strcmp(ptr, MEMERRMSG) == 0)
ret = LUA_ERRMEM;
Do you agree on "better safe than sorry" ?
2) The main picture
Different Lua functions exit with different error messages and exit codes, even in case of the same error condition.
Examples in case of insufficient memory:
lstring.c:
LUA_YIELD : "not enough memory"
lauxlib.c: LUA_ERRRUN : "test.lua:10: not enough memory for buffer allocation"
lstrlib.c: LUA_ERRRUN : "test.lua:11: resulting string too large"
Lua defines a specific error code: LUA_ERRMEM.
When realloc failes, why do some Lua functions return LUA_ERRMEM other ones return LUA_YIELD or LUA_ERRRUN ?
I am talking about basic functions: lua_newstate, luaL_openlibs, luaL_requiref, luaL_loadfilex, lua_pcall.
I use a lot of libraries, I am used to get the same error code in case of the same cause.
Example: Iit is quite problematic to test for insufficient memory conditions, with all these combinations of return codes.
Does anybody recognize this inconsistent behavour of the APIs?
Am I the only one annoyed about it?
Thanks for your attention,
Massimo