@@ -474,26 +474,16 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
474474
475475
476476/*
477- ** Prepare a function for a tail call, building its call info on top
478- ** of the current call info. 'narg1' is the number of arguments plus 1
479- ** (so that it includes the function itself).
477+ ** In a tail call, move function and parameters to previous call frame.
478+ ** (This is done only when no more errors can occur before entering the
479+ ** new function, to keep debug information always consistent.)
480480*/
481- void luaD_pretailcall (lua_State * L , CallInfo * ci , StkId func , int narg1 ) {
482- Proto * p = clLvalue (s2v (func ))-> p ;
483- int fsize = p -> maxstacksize ; /* frame size */
484- int nfixparams = p -> numparams ;
481+ static void moveparams (lua_State * L , StkId prevf , StkId func , int narg ) {
485482 int i ;
486- for (i = 0 ; i < narg1 ; i ++ ) /* move down function and arguments */
487- setobjs2s (L , ci -> func + i , func + i );
488- checkstackGC (L , fsize );
489- func = ci -> func ; /* moved-down function */
490- for (; narg1 <= nfixparams ; narg1 ++ )
491- setnilvalue (s2v (func + narg1 )); /* complete missing arguments */
492- ci -> top = func + 1 + fsize ; /* top for new function */
493- lua_assert (ci -> top <= L -> stack_last );
494- ci -> u .l .savedpc = p -> code ; /* starting point */
495- ci -> callstatus |= CIST_TAIL ;
496- L -> top = func + narg1 ; /* set top */
483+ narg ++ ; /* function itself will be moved, too */
484+ for (i = 0 ; i < narg ; i ++ ) /* move down function and arguments */
485+ setobjs2s (L , prevf + i , func + i );
486+ L -> top = prevf + narg ; /* correct top */
497487}
498488
499489
@@ -504,8 +494,12 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) {
504494** to be executed, if it was a Lua function. Otherwise (a C function)
505495** returns NULL, with all the results on the stack, starting at the
506496** original function position.
497+ ** For regular calls, 'delta1' is 0. For tail calls, 'delta1' is the
498+ ** 'delta' (correction of base for vararg functions) plus 1, so that it
499+ ** cannot be zero. Like 'moveparams', this correction can only be done
500+ ** when no more errors can occur in the call.
507501*/
508- CallInfo * luaD_precall (lua_State * L , StkId func , int nresults ) {
502+ CallInfo * luaD_precall (lua_State * L , StkId func , int nresults , int delta1 ) {
509503 lua_CFunction f ;
510504 retry :
511505 switch (ttypetag (s2v (func ))) {
@@ -542,12 +536,18 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) {
542536 int nfixparams = p -> numparams ;
543537 int fsize = p -> maxstacksize ; /* frame size */
544538 checkstackGCp (L , fsize , func );
545- L -> ci = ci = next_ci (L );
546- ci -> nresults = nresults ;
539+ if (delta1 ) { /* tail call? */
540+ ci = L -> ci ; /* reuse stack frame */
541+ ci -> func -= delta1 - 1 ; /* correct 'func' */
542+ moveparams (L , ci -> func , func , narg );
543+ }
544+ else { /* regular call */
545+ L -> ci = ci = next_ci (L ); /* new frame */
546+ ci -> func = func ;
547+ ci -> nresults = nresults ;
548+ }
547549 ci -> u .l .savedpc = p -> code ; /* starting point */
548550 ci -> top = func + 1 + fsize ;
549- ci -> func = func ;
550- L -> ci = ci ;
551551 for (; narg < nfixparams ; narg ++ )
552552 setnilvalue (s2v (L -> top ++ )); /* complete missing arguments */
553553 lua_assert (ci -> top <= L -> stack_last );
@@ -572,7 +572,7 @@ static void ccall (lua_State *L, StkId func, int nResults, int inc) {
572572 L -> nCcalls += inc ;
573573 if (l_unlikely (getCcalls (L ) >= LUAI_MAXCCALLS ))
574574 luaE_checkcstack (L );
575- if ((ci = luaD_precall (L , func , nResults )) != NULL ) { /* Lua function? */
575+ if ((ci = luaD_precall (L , func , nResults , 0 )) != NULL ) { /* Lua function? */
576576 ci -> callstatus = CIST_FRESH ; /* mark that it is a "fresh" execute */
577577 luaV_execute (L , ci ); /* call it */
578578 }
0 commit comments