Question about number of values that a function may return

85 views
Skip to first unread message

Jure Bagić

unread,
Apr 14, 2026, 8:59:08 PMApr 14
to lua-l
Ola,

In the official documentation it says:
> There is a system-dependent limit on the number of values that a function may
> return. This limit is guaranteed to be at least 1000.
How come this specific value is 1000?

Lua code cannot encode more than 255 in OP_RETURN:
> luaY_checklimit(fs, nret + 1, MAXARG_B, "returns");
and for OP_CALL
> luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results");

C calls cannot (should not) return more than 250 results:
> #define MAXRESULTS 250
and the check before the call
> #define checkresults(L,na,nr) \
> (api_check(L, (nr) == LUA_MULTRET \
> || (L->ci->top.p - L->top.p >= (nr) - (na)), \
> "results from function overflow current stack size"), \
> api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
> "invalid number of results"))

I also don't understand why MAXRESULTS is 250 but the limit checks in the
parser/codgen are for 255 (maximum size that fits in respective registers).
I see no issue with CIST_NRESULTS encoding 255.

However, I do realize that you can temporarily push a lot of values
onto the Lua stack from C by calling 'lua_checkstack',
and then doing something like
> int i;
> lua_checkstack(L, 2000);
> for (i = 0; i < 2000; i++)
> lua_pushnil(L);
> return 2000;
But this is temporary; after the 'precallC' (C function returned) the
'luaD_poscall' is executed and so the 'genmoveresults' would in this case
remove any extra results.
This wanted number of results is no greater than MAXRESULTS.

The only thing that may see this stack state before the results are adjusted is
the C code executed through the return hook.
In the end,
the actual number of values a function may return is MAXRESULTS.
The "at least 1000" figure is a mistery to me.

-- Jure
signature.asc

mjmouse9999

unread,
Apr 14, 2026, 9:31:49 PMApr 14
to lua-l
You can't code it directly, but many returns (and passing as varargs into
another function) can be done like so:

#!/usr/bin/env lua

local function return_n(n)
        if n <= 0 then return end
        return n, return_n(n - 1)
end

print(select('#', return_n(3000)))
--> 3000


Testing on my Lua install shows that at least up to 300,000 works.

Jure Bagić

unread,
Apr 14, 2026, 10:01:50 PMApr 14
to lu...@googlegroups.com
> You can't code it directly, but many returns (and passing as varargs into
> another function) can be done like so:
> #!/usr/bin/env lua
> local function return_n(n)
> if n <= 0 then return end
> return n, return_n(n - 1)
> end
> print(select('#', return_n(3000)))
> --> 3000
> Testing on my Lua install shows that at least up to 300,000 works.

Thanks for the example you provided, it really made it obvious where to
look in the source code.
It seems I somehow missed the LUA_MULTRET case in 'moveresults':
> case LUA_MULTRET + 1:
> genmoveresults(L, res, nres, nres); /* we want all results */
> break;
Now I wonder why (how) is the number of results guaranteed to be at least 1000?

--
Jure
signature.asc

Roberto Ierusalimschy

unread,
Apr 15, 2026, 3:38:47 PMApr 15
to lu...@googlegroups.com
> Now I wonder why (how) is the number of results guaranteed to be at least 1000?

It is just a nice, round number smaller than any reasonable limit for the
stack size.

-- Roberto

Jure Bagić

unread,
Apr 15, 2026, 4:34:08 PMApr 15
to lu...@googlegroups.com
> It is just a nice, round number smaller than any reasonable limit for the
> stack size.

I see, thanks for addressing the question (as always).

-- Jure
signature.asc
Reply all
Reply to author
Forward
0 new messages