Lua Ellipsis Expression Limited to 248

I am studying Lua for a university class in the field of mobile application development, and recently we have examined the ellipsis operator (...), which allows a dynamic number of arguments. Out of curiosity, I decided to try to find out if there is a limit to how many arguments he could handle, since it looks like 248.

For instance:

function pr(...)
    print(...)
end

pr(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1)

I thought that maybe it depends on the OS or is related to the size of the stack, so I tested this on both Linux and Windows, both on the 32-bit and 64-bit versions. The number of allowed items remained at 248. It would seem that this is a hard-coded limit. If I try> 248, then I will get the error that I get:

main.lua:30 function or expression too complex near <eof>

, - 248 , ? , 248? .

+4
1

, , . Lua.

. Lua 1 , 200 , 48 , :

local x001,x002,x003,x004,x005,x006,x007,x008,x009,x010
local x011,x012,x013,x014,x015,x016,x017,x018,x019,x020
local x021,x022,x023,x024,x025,x026,x027,x028,x029,x030
local x031,x032,x033,x034,x035,x036,x037,x038,x039,x040
local x041,x042,x043,x044,x045,x046,x047,x048,x049,x050
local x051,x052,x053,x054,x055,x056,x057,x058,x059,x060
local x061,x062,x063,x064,x065,x066,x067,x068,x069,x070
local x071,x072,x073,x074,x075,x076,x077,x078,x079,x080
local x081,x082,x083,x084,x085,x086,x087,x088,x089,x090
local x091,x092,x093,x094,x095,x096,x097,x098,x099,x100
local x101,x102,x103,x104,x105,x106,x107,x108,x109,x110
local x111,x112,x113,x114,x115,x116,x117,x118,x119,x120
local x121,x122,x123,x124,x125,x126,x127,x128,x129,x130
local x131,x132,x133,x134,x135,x136,x137,x138,x139,x140
local x141,x142,x143,x144,x145,x146,x147,x148,x149,x150
local x151,x152,x153,x154,x155,x156,x157,x158,x159,x160
local x161,x162,x163,x164,x165,x166,x167,x168,x169,x170
local x171,x172,x173,x174,x175,x176,x177,x178,x179,x180
local x181,x182,x183,x184,x185,x186,x187,x188,x189,x190
local x191,x192,x193,x194,x195,x196,x197,x198,x199,x200
print(
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
)

function or expression too complex too many arguments passed to function - .

248?

249. llimits.h Lua MAXSTACK 250 lcode.c, checkstack -, .

 if (newstack >= MAXSTACK)
      luaX_syntaxerror(fs->ls, "function or expression too complex");

, . - f(a1,a2,...,an) N+1 : , , N 248 + 1 = 249 <= 250. ( )

, 250 - . 256 , 8 , - Lua 2 3 , .

250

, 256 . , , - , , Lua, . (LUA_MAXSTACK) luaconf.h 1000000.

function rep(n)
  local t = {}
  for i = 1, n do
    t[i] = i
  end
  return t
end

function foo(...)
end

foo(table.unpack(rep(999986))) -- Pretty close to 1000000

1: , Lua script , , Lua "toplevel" - " ".

+6

Source: https://habr.com/ru/post/1651267/


All Articles