lua-users home
lua-l archive

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


> Hi,
> 
> Inspired from a Reddit post, I see that the maximum number of local variables within a scope is 200 ( as defined in lparser.c ).
> Here's the snippet:
> /* maximum number of local variables per function (must be smaller than 250, due to the bytecode format) */
> #define MAXVARS   200
> 
> Question-1:
> I'd like to know why the comment states 250 and the value for MAXVARS is set to 200.
> 
> Question-2:
> As a layman, I'd assume the numbers to be powers of 2, like 256, but why's it 200 or 256?

Each function may use up to 255 virtual registers. These registers
are used both for local variables and temporary values. If Lua cannot
find enough temporary values to code an expression, compilation fails
with a message like 'function or expression needs too many registers'.
A limit of 200 variables leaves 55 registers free for expressions, good
enough even for long list constructors. A limit of 255 variables could
leave Lua in a state that even expressions like "a * b + c" would be
too complex to be coded.

-- Roberto