Question about number of values that a function may return
Jure Bagić <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <ad7LGiW94MlzVQTW@archlinux> |
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 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/ad7LGiW94MlzVQTW%40archlinux.
signature.asc
(application/pgp-signature, 228 B)
-----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQTilOnPIAgk+VLLAS+FaQJ4QtXaNQUCad7i0gAKCRCFaQJ4QtXa NZRoAQC/ozc8DSMjBxyOGgXc068kTNEOwjeAH95C9n/Y8LIr7QEAnBG1i30hUR7f WAn8etfnZJPfYnIqoTcWDmVGE4XD1AM= =e/9v -----END PGP SIGNATURE-----