diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-11-28 15:12:51 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-11-28 15:12:51 -0300 |
| commit | a07f6a824197d7dc01c321599d3bc71936a2590e (patch) | |
| tree | a3c4c0aff91e4175c31806727190803f250bf3e6 /ltm.c | |
| parent | f33cc4ddec886ea499d7d41dd60cac5ddc5687db (diff) | |
| download | lua-a07f6a824197d7dc01c321599d3bc71936a2590e.tar.gz lua-a07f6a824197d7dc01c321599d3bc71936a2590e.tar.bz2 lua-a07f6a824197d7dc01c321599d3bc71936a2590e.zip | |
Functions with vararg tables don't need hidden args.
Vararg functions with vararg tables don't use the arguments hidden in
the stack; therfore, it doesn't need to build/keep them.
Diffstat (limited to 'ltm.c')
| -rw-r--r-- | ltm.c | 39 |
1 files changed, 25 insertions, 14 deletions
| @@ -250,31 +250,42 @@ static void createvarargtab (lua_State *L, StkId f, int n) { | |||
| 250 | ** initial stack: func arg1 ... argn extra1 ... | 250 | ** initial stack: func arg1 ... argn extra1 ... |
| 251 | ** ^ ci->func ^ L->top | 251 | ** ^ ci->func ^ L->top |
| 252 | ** final stack: func nil ... nil extra1 ... func arg1 ... argn | 252 | ** final stack: func nil ... nil extra1 ... func arg1 ... argn |
| 253 | ** ^ ci->func ^ L->top | 253 | ** ^ ci->func |
| 254 | */ | 254 | */ |
| 255 | void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { | 255 | static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p, |
| 256 | int totalargs, int nfixparams, int nextra) { | ||
| 256 | int i; | 257 | int i; |
| 257 | int totalargs = cast_int(L->top.p - ci->func.p) - 1; | ||
| 258 | int nfixparams = p->numparams; | ||
| 259 | int nextra = totalargs - nfixparams; /* number of extra arguments */ | ||
| 260 | ci->u.l.nextraargs = nextra; | 258 | ci->u.l.nextraargs = nextra; |
| 261 | luaD_checkstack(L, p->maxstacksize + 1); | 259 | luaD_checkstack(L, p->maxstacksize + 1); |
| 262 | /* copy function to the top of the stack */ | 260 | /* copy function to the top of the stack, after extra arguments */ |
| 263 | setobjs2s(L, L->top.p++, ci->func.p); | 261 | setobjs2s(L, L->top.p++, ci->func.p); |
| 264 | /* move fixed parameters to the top of the stack */ | 262 | /* move fixed parameters to after the copied function */ |
| 265 | for (i = 1; i <= nfixparams; i++) { | 263 | for (i = 1; i <= nfixparams; i++) { |
| 266 | setobjs2s(L, L->top.p++, ci->func.p + i); | 264 | setobjs2s(L, L->top.p++, ci->func.p + i); |
| 267 | setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ | 265 | setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ |
| 268 | } | 266 | } |
| 269 | if (p->flag & PF_VATAB) /* does it need a vararg table? */ | 267 | ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */ |
| 268 | ci->top.p += totalargs + 1; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { | ||
| 273 | int totalargs = cast_int(L->top.p - ci->func.p) - 1; | ||
| 274 | int nfixparams = p->numparams; | ||
| 275 | int nextra = totalargs - nfixparams; /* number of extra arguments */ | ||
| 276 | if (p->flag & PF_VATAB) { /* does it need a vararg table? */ | ||
| 277 | lua_assert(!(p->flag & PF_VAHID)); | ||
| 270 | createvarargtab(L, ci->func.p + nfixparams + 1, nextra); | 278 | createvarargtab(L, ci->func.p + nfixparams + 1, nextra); |
| 271 | else { /* no table; set parameter to nil */ | 279 | /* move table to proper place (last parameter) */ |
| 272 | setnilvalue(s2v(L->top.p)); | 280 | setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1); |
| 273 | L->top.p++; | 281 | } |
| 282 | else { /* no table */ | ||
| 283 | lua_assert(p->flag & PF_VAHID); | ||
| 284 | buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra); | ||
| 285 | /* set vararg parameter to nil */ | ||
| 286 | setnilvalue(s2v(ci->func.p + nfixparams + 1)); | ||
| 287 | lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); | ||
| 274 | } | 288 | } |
| 275 | ci->func.p += totalargs + 1; | ||
| 276 | ci->top.p += totalargs + 1; | ||
| 277 | lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); | ||
| 278 | } | 289 | } |
| 279 | 290 | ||
| 280 | 291 | ||
