When sizeof(int) is 2 bytes I've encountered an issue in lparser.c.
Attempting to create a local variable produces the error message:
"too many local variables (limit is -1)".
I'm building using the v5.4 branch on github at
https://github.com/lua/lua,
commit 782ef85b.
Looking at the source code, it is connected with lparser.c line 201 in static
function new_localvar. Specifically the line:
luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1,
dyd->actvar.size, Vardesc, USHRT_MAX, "local variables");
Following the call through, it calls the function luaM_growaux_ via the
luaM_growvector macro.
The prototype of luaM_growaux_ is:
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
int size_elems, int limit, const char *what)
where the type of parameter 'limit' is int.
When sizeof(int) is 2 bytes the USHRT_MAX (65535) from the luaM_growvector
call is being passed through the int 'limit' parameter where it becomes -1.
This causes the luaG_runerror invocation within luaM_growaux_, where size is
0 and limit is -1:
if (size >= limit / 2) { /* cannot double it? */
if (l_unlikely(size >= limit)) /* cannot grow even a little? */
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
size = limit; /* still have at least one free place */
}
If I change the luaM_growvector invocation to use MAX_INT instead of USHRT_MAX
I no longer see the reported issue.