diff options
Diffstat (limited to 'lstate.c')
| -rw-r--r-- | lstate.c | 45 |
1 files changed, 25 insertions, 20 deletions
| @@ -97,25 +97,8 @@ void luaE_setdebt (global_State *g, l_mem debt) { | |||
| 97 | 97 | ||
| 98 | 98 | ||
| 99 | LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { | 99 | LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { |
| 100 | global_State *g = G(L); | 100 | UNUSED(L); UNUSED(limit); |
| 101 | int ccalls; | 101 | return LUAI_MAXCCALLS; /* warning?? */ |
| 102 | luaE_freeCI(L); /* release unused CIs */ | ||
| 103 | ccalls = getCcalls(L); | ||
| 104 | if (limit >= 40000) | ||
| 105 | return 0; /* out of bounds */ | ||
| 106 | limit += CSTACKERR; | ||
| 107 | if (L != g-> mainthread) | ||
| 108 | return 0; /* only main thread can change the C stack */ | ||
| 109 | else if (ccalls <= CSTACKERR) | ||
| 110 | return 0; /* handling overflow */ | ||
| 111 | else { | ||
| 112 | int diff = limit - g->Cstacklimit; | ||
| 113 | if (ccalls + diff <= CSTACKERR) | ||
| 114 | return 0; /* new limit would cause an overflow */ | ||
| 115 | g->Cstacklimit = limit; /* set new limit */ | ||
| 116 | L->nCcalls += diff; /* correct 'nCcalls' */ | ||
| 117 | return limit - diff - CSTACKERR; /* success; return previous limit */ | ||
| 118 | } | ||
| 119 | } | 102 | } |
| 120 | 103 | ||
| 121 | 104 | ||
| @@ -172,6 +155,28 @@ void luaE_shrinkCI (lua_State *L) { | |||
| 172 | } | 155 | } |
| 173 | 156 | ||
| 174 | 157 | ||
| 158 | /* | ||
| 159 | ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. | ||
| 160 | ** If equal, raises an overflow error. If value is larger than | ||
| 161 | ** LUAI_MAXCCALLS (which means it is handling an overflow) but | ||
| 162 | ** not much larger, does not report an error (to allow overflow | ||
| 163 | ** handling to work). | ||
| 164 | */ | ||
| 165 | void luaE_checkcstack (lua_State *L) { | ||
| 166 | if (getCcalls(L) == LUAI_MAXCCALLS) | ||
| 167 | luaG_runerror(L, "C stack overflow"); | ||
| 168 | else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) | ||
| 169 | luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | LUAI_FUNC void luaE_incCstack (lua_State *L) { | ||
| 174 | L->nCcalls++; | ||
| 175 | if (getCcalls(L) >= LUAI_MAXCCALLS) | ||
| 176 | luaE_checkcstack(L); | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 175 | static void stack_init (lua_State *L1, lua_State *L) { | 180 | static void stack_init (lua_State *L1, lua_State *L) { |
| 176 | int i; CallInfo *ci; | 181 | int i; CallInfo *ci; |
| 177 | /* initialize stack array */ | 182 | /* initialize stack array */ |
| @@ -357,7 +362,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
| 357 | preinit_thread(L, g); | 362 | preinit_thread(L, g); |
| 358 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ | 363 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ |
| 359 | L->next = NULL; | 364 | L->next = NULL; |
| 360 | g->Cstacklimit = L->nCcalls = 0; | 365 | L->nCcalls = 0; |
| 361 | incnny(L); /* main thread is always non yieldable */ | 366 | incnny(L); /* main thread is always non yieldable */ |
| 362 | g->frealloc = f; | 367 | g->frealloc = f; |
| 363 | g->ud = ud; | 368 | g->ud = ud; |
