Re: Question about number of values that a function may return
mjmouse9999 <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <[email protected]> |
On Wednesday, April 15, 2026 at 10:59:08AM UTC+10 Jure Bagić wrote:
> 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
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.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/fd969b6c-aec7-4861-b5bd-ec121ac9b48bn%40googlegroups.com.