@@ -138,7 +138,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
138138
139139
140140int luaD_rawrunprotected (lua_State * L , Pfunc f , void * ud ) {
141- unsigned short oldnCcalls = L -> nCcalls - L -> nci ;
141+ l_uint32 oldnCcalls = L -> nCcalls - L -> nci ;
142142 struct lua_longjmp lj ;
143143 lua_assert (L -> nCcalls >= L -> nci );
144144 lj .status = LUA_OK ;
@@ -513,12 +513,17 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
513513
514514
515515/*
516- ** Similar to 'luaD_call', but does not allow yields during the call
516+ ** Similar to 'luaD_call', but does not allow yields during the call.
517+ ** If there is a stack overflow, freeing all CI structures will
518+ ** force the subsequent call to invoke 'luaE_extendCI', which then
519+ ** will raise any errors.
517520*/
518521void luaD_callnoyield (lua_State * L , StkId func , int nResults ) {
519- L -> nny ++ ;
522+ incXCcalls (L );
523+ if (getCcalls (L ) >= LUAI_MAXCCALLS ) /* possible stack overflow? */
524+ luaE_freeCI (L );
520525 luaD_call (L , func , nResults );
521- L -> nny -- ;
526+ decXCcalls ( L ) ;
522527}
523528
524529
@@ -530,7 +535,7 @@ static void finishCcall (lua_State *L, int status) {
530535 CallInfo * ci = L -> ci ;
531536 int n ;
532537 /* must have a continuation and must be able to call it */
533- lua_assert (ci -> u .c .k != NULL && L -> nny == 0 );
538+ lua_assert (ci -> u .c .k != NULL && yieldable ( L ) );
534539 /* error status can only happen in a protected call */
535540 lua_assert ((ci -> callstatus & CIST_YPCALL ) || status == LUA_YIELD );
536541 if (ci -> callstatus & CIST_YPCALL ) { /* was inside a pcall? */
@@ -601,7 +606,6 @@ static int recover (lua_State *L, int status) {
601606 luaD_seterrorobj (L , status , oldtop );
602607 L -> ci = ci ;
603608 L -> allowhook = getoah (ci -> callstatus ); /* restore original 'allowhook' */
604- L -> nny = 0 ; /* should be zero to be yieldable */
605609 luaD_shrinkstack (L );
606610 L -> errfunc = ci -> u .c .old_errfunc ;
607611 return 1 ; /* continue running the coroutine */
@@ -622,13 +626,6 @@ static int resume_error (lua_State *L, const char *msg, int narg) {
622626}
623627
624628
625- /*
626- ** "Cost" in the C stack for a coroutine invocation.
627- */
628- #if !defined(LUAL_COROCSTK )
629- #define LUAL_COROCSTK 3
630- #endif
631-
632629/*
633630** Do the work for 'lua_resume' in protected mode. Most of the work
634631** depends on the status of the coroutine: initial state, suspended
@@ -664,7 +661,6 @@ static void resume (lua_State *L, void *ud) {
664661LUA_API int lua_resume (lua_State * L , lua_State * from , int nargs ,
665662 int * nresults ) {
666663 int status ;
667- unsigned short oldnny = L -> nny ; /* save "number of non-yieldable" calls */
668664 lua_lock (L );
669665 if (L -> status == LUA_OK ) { /* may be starting a coroutine */
670666 if (L -> ci != & L -> base_ci ) /* not in base level? */
@@ -675,11 +671,10 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
675671 if (from == NULL )
676672 L -> nCcalls = 1 ;
677673 else /* correct 'nCcalls' for this thread */
678- L -> nCcalls = from -> nCcalls - from -> nci + L -> nci + LUAL_COROCSTK ;
674+ L -> nCcalls = getCcalls ( from ) - from -> nci + L -> nci + CSTACKCF ;
679675 if (L -> nCcalls >= LUAI_MAXCCALLS )
680676 return resume_error (L , "C stack overflow" , nargs );
681677 luai_userstateresume (L , nargs );
682- L -> nny = 0 ; /* allow yields */
683678 api_checknelems (L , (L -> status == LUA_OK ) ? nargs + 1 : nargs );
684679 status = luaD_rawrunprotected (L , resume , & nargs );
685680 /* continue running after recoverable errors */
@@ -698,14 +693,13 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
698693 }
699694 * nresults = (status == LUA_YIELD ) ? L -> ci -> u2 .nyield
700695 : cast_int (L -> top - (L -> ci -> func + 1 ));
701- L -> nny = oldnny ; /* restore 'nny' */
702696 lua_unlock (L );
703697 return status ;
704698}
705699
706700
707701LUA_API int lua_isyieldable (lua_State * L ) {
708- return ( L -> nny == 0 );
702+ return yieldable ( L );
709703}
710704
711705
@@ -715,7 +709,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
715709 luai_userstateyield (L , nresults );
716710 lua_lock (L );
717711 api_checknelems (L , nresults );
718- if (unlikely (L -> nny > 0 )) {
712+ if (unlikely (! yieldable ( L ) )) {
719713 if (L != G (L )-> mainthread )
720714 luaG_runerror (L , "attempt to yield across a C-call boundary" );
721715 else
@@ -741,23 +735,21 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
741735
742736/*
743737** Call the C function 'func' in protected mode, restoring basic
744- ** thread information ('allowhook', 'nny', etc.) and in particular
738+ ** thread information ('allowhook', etc.) and in particular
745739** its stack level in case of errors.
746740*/
747741int luaD_pcall (lua_State * L , Pfunc func , void * u ,
748742 ptrdiff_t old_top , ptrdiff_t ef ) {
749743 int status ;
750744 CallInfo * old_ci = L -> ci ;
751745 lu_byte old_allowhooks = L -> allowhook ;
752- unsigned short old_nny = L -> nny ;
753746 ptrdiff_t old_errfunc = L -> errfunc ;
754747 L -> errfunc = ef ;
755748 status = luaD_rawrunprotected (L , func , u );
756749 if (unlikely (status != LUA_OK )) { /* an error occurred? */
757750 StkId oldtop = restorestack (L , old_top );
758751 L -> ci = old_ci ;
759752 L -> allowhook = old_allowhooks ;
760- L -> nny = old_nny ;
761753 status = luaF_close (L , oldtop , status );
762754 oldtop = restorestack (L , old_top ); /* previous call may change stack */
763755 luaD_seterrorobj (L , status , oldtop );
@@ -811,7 +803,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
811803 const char * mode ) {
812804 struct SParser p ;
813805 int status ;
814- L -> nny ++ ; /* cannot yield during parsing */
806+ incnny ( L ) ; /* cannot yield during parsing */
815807 p .z = z ; p .name = name ; p .mode = mode ;
816808 p .dyd .actvar .arr = NULL ; p .dyd .actvar .size = 0 ;
817809 p .dyd .gt .arr = NULL ; p .dyd .gt .size = 0 ;
@@ -822,7 +814,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
822814 luaM_freearray (L , p .dyd .actvar .arr , p .dyd .actvar .size );
823815 luaM_freearray (L , p .dyd .gt .arr , p .dyd .gt .size );
824816 luaM_freearray (L , p .dyd .label .arr , p .dyd .label .size );
825- L -> nny -- ;
817+ decnny ( L ) ;
826818 return status ;
827819}
828820
0 commit comments