Only in lua-5.1.1/src: #ldo.c# diff -ur lua-5.1.1/src/lapi.c lua-5.1.1-copatch/src/lapi.c --- lua-5.1.1/src/lapi.c 2006-06-07 05:37:17.000000000 -0700 +++ lua-5.1.1-copatch/src/lapi.c 2007-11-25 14:41:23.000000000 -0800 @@ -119,6 +119,7 @@ for (i = 0; i < n; i++) { setobj2s(to, to->top++, from->top + i); } + to->nresumes = from->nresumes; lua_unlock(to); } diff -ur lua-5.1.1/src/ldo.c lua-5.1.1-copatch/src/ldo.c --- lua-5.1.1/src/ldo.c 2006-06-05 12:36:14.000000000 -0700 +++ lua-5.1.1-copatch/src/ldo.c 2007-11-25 14:44:33.000000000 -0800 @@ -8,6 +8,7 @@ #include #include #include +#include #define ldo_c #define LUA_CORE @@ -367,16 +368,17 @@ ** function position. */ void luaD_call (lua_State *L, StkId func, int nResults) { - if (++L->nCcalls >= LUAI_MAXCCALLS) { - if (L->nCcalls == LUAI_MAXCCALLS) + int effCalls = ++L->nCcalls + L->nresumes; + if (effCalls >= LUAI_MAXCCALLS) { + if (effCalls == LUAI_MAXCCALLS) luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ - } - if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ - luaV_execute(L, 1); /* call it */ - L->nCcalls--; - luaC_checkGC(L); + else if (effCalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + } + if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ + luaV_execute(L, 1); /* call it */ + L->nCcalls--; + luaC_checkGC(L); } @@ -401,7 +403,15 @@ else /* yielded inside a hook: just continue its execution */ L->base = L->ci->base; } + int effCalls = L->nCcalls + ++L->nresumes; + if (effCalls >= LUAI_MAXCCALLS) { + if (effCalls == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (effCalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + } luaV_execute(L, cast_int(L->ci - L->base_ci)); + --L->nresumes; } @@ -454,6 +464,7 @@ ptrdiff_t old_top, ptrdiff_t ef) { int status; unsigned short oldnCcalls = L->nCcalls; + unsigned short oldnresumes = L->nresumes; ptrdiff_t old_ci = saveci(L, L->ci); lu_byte old_allowhooks = L->allowhook; ptrdiff_t old_errfunc = L->errfunc; @@ -464,6 +475,7 @@ luaF_close(L, oldtop); /* close eventual pending closures */ luaD_seterrorobj(L, status, oldtop); L->nCcalls = oldnCcalls; + L->nresumes = oldnresumes; L->ci = restoreci(L, old_ci); L->base = L->ci->base; L->savedpc = L->ci->savedpc; diff -ur lua-5.1.1/src/lstate.c lua-5.1.1-copatch/src/lstate.c --- lua-5.1.1/src/lstate.c 2006-05-24 07:15:50.000000000 -0700 +++ lua-5.1.1-copatch/src/lstate.c 2007-11-25 14:30:43.000000000 -0800 @@ -94,6 +94,7 @@ L->openupval = NULL; L->size_ci = 0; L->nCcalls = 0; + L->nresumes = 0; L->status = 0; L->base_ci = L->ci = NULL; L->savedpc = NULL; @@ -206,6 +207,7 @@ L->ci = L->base_ci; L->base = L->top = L->ci->base; L->nCcalls = 0; + L->nresumes = 0; } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); lua_assert(G(L)->tmudata == NULL); luai_userstateclose(L); diff -ur lua-5.1.1/src/lstate.h lua-5.1.1-copatch/src/lstate.h --- lua-5.1.1/src/lstate.h 2006-02-06 10:27:59.000000000 -0800 +++ lua-5.1.1-copatch/src/lstate.h 2007-11-25 14:30:15.000000000 -0800 @@ -112,6 +112,7 @@ int stacksize; int size_ci; /* size of array `base_ci' */ unsigned short nCcalls; /* number of nested C calls */ + unsigned short nresumes; /* number of coroutine resumes */ lu_byte hookmask; lu_byte allowhook; int basehookcount;