@@ -88,7 +88,7 @@ struct lua_longjmp {
8888};
8989
9090
91- static void seterrorobj (lua_State * L , int errcode , StkId oldtop ) {
91+ void luaD_seterrorobj (lua_State * L , int errcode , StkId oldtop ) {
9292 switch (errcode ) {
9393 case LUA_ERRMEM : { /* memory error? */
9494 setsvalue2s (L , oldtop , G (L )-> memerrmsg ); /* reuse preregistered msg. */
@@ -121,7 +121,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
121121 }
122122 else { /* no handler at all; abort */
123123 if (g -> panic ) { /* panic function? */
124- seterrorobj (L , errcode , L -> top ); /* assume EXTRA_STACK */
124+ luaD_seterrorobj (L , errcode , L -> top ); /* assume EXTRA_STACK */
125125 if (L -> ci -> top < L -> top )
126126 L -> ci -> top = L -> top ; /* pushing msg. can break this invariant */
127127 lua_unlock (L );
@@ -584,8 +584,8 @@ static int recover (lua_State *L, int status) {
584584 if (ci == NULL ) return 0 ; /* no recovery point */
585585 /* "finish" luaD_pcall */
586586 oldtop = restorestack (L , ci -> u2 .funcidx );
587- luaF_close (L , oldtop );
588- seterrorobj (L , status , oldtop );
587+ luaF_close (L , oldtop , status );
588+ luaD_seterrorobj (L , status , oldtop );
589589 L -> ci = ci ;
590590 L -> allowhook = getoah (ci -> callstatus ); /* restore original 'allowhook' */
591591 L -> nny = 0 ; /* should be zero to be yieldable */
@@ -678,7 +678,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
678678 }
679679 if (unlikely (errorstatus (status ))) { /* unrecoverable error? */
680680 L -> status = cast_byte (status ); /* mark thread as 'dead' */
681- seterrorobj (L , status , L -> top ); /* push error message */
681+ luaD_seterrorobj (L , status , L -> top ); /* push error message */
682682 L -> ci -> top = L -> top ;
683683 }
684684 else lua_assert (status == L -> status ); /* normal end or yield */
@@ -726,6 +726,11 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
726726}
727727
728728
729+ /*
730+ ** Call the C function 'func' in protected mode, restoring basic
731+ ** thread information ('allowhook', 'nny', etc.) and in particular
732+ ** its stack level in case of errors.
733+ */
729734int luaD_pcall (lua_State * L , Pfunc func , void * u ,
730735 ptrdiff_t old_top , ptrdiff_t ef ) {
731736 int status ;
@@ -737,11 +742,12 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u,
737742 status = luaD_rawrunprotected (L , func , u );
738743 if (unlikely (status != LUA_OK )) { /* an error occurred? */
739744 StkId oldtop = restorestack (L , old_top );
740- luaF_close (L , oldtop ); /* close possible pending closures */
741- seterrorobj (L , status , oldtop );
742745 L -> ci = old_ci ;
743746 L -> allowhook = old_allowhooks ;
744747 L -> nny = old_nny ;
748+ status = luaF_close (L , oldtop , status );
749+ oldtop = restorestack (L , old_top ); /* previous call may change stack */
750+ luaD_seterrorobj (L , status , oldtop );
745751 luaD_shrinkstack (L );
746752 }
747753 L -> errfunc = old_errfunc ;
0 commit comments