diff options
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 28 |
1 files changed, 26 insertions, 2 deletions
@@ -96,6 +96,29 @@ void luaE_setdebt (global_State *g, l_mem debt) { | |||
96 | } | 96 | } |
97 | 97 | ||
98 | 98 | ||
99 | LUA_API int lua_setCstacklimit (lua_State *L, unsigned int limit) { | ||
100 | global_State *g = G(L); | ||
101 | int ccalls; | ||
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 | } | ||
120 | |||
121 | |||
99 | /* | 122 | /* |
100 | ** Decrement count of "C calls" and check for overflows. In case of | 123 | ** Decrement count of "C calls" and check for overflows. In case of |
101 | ** a stack overflow, check appropriate error ("regular" overflow or | 124 | ** a stack overflow, check appropriate error ("regular" overflow or |
@@ -121,7 +144,7 @@ void luaE_enterCcall (lua_State *L) { | |||
121 | else if (ncalls >= CSTACKMARK) { | 144 | else if (ncalls >= CSTACKMARK) { |
122 | /* not in error-handling zone; raise the error now */ | 145 | /* not in error-handling zone; raise the error now */ |
123 | L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ | 146 | L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ |
124 | luaG_runerror(L, "C stack overflow1"); | 147 | luaG_runerror(L, "C stack overflow"); |
125 | } | 148 | } |
126 | /* else stack is in the error-handling zone; | 149 | /* else stack is in the error-handling zone; |
127 | allow message handler to work */ | 150 | allow message handler to work */ |
@@ -263,7 +286,7 @@ static void preinit_thread (lua_State *L, global_State *g) { | |||
263 | L->stacksize = 0; | 286 | L->stacksize = 0; |
264 | L->twups = L; /* thread has no upvalues */ | 287 | L->twups = L; /* thread has no upvalues */ |
265 | L->errorJmp = NULL; | 288 | L->errorJmp = NULL; |
266 | L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; | 289 | L->nCcalls = CSTACKTHREAD; |
267 | L->hook = NULL; | 290 | L->hook = NULL; |
268 | L->hookmask = 0; | 291 | L->hookmask = 0; |
269 | L->basehookcount = 0; | 292 | L->basehookcount = 0; |
@@ -365,6 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
365 | preinit_thread(L, g); | 388 | preinit_thread(L, g); |
366 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ | 389 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ |
367 | L->next = NULL; | 390 | L->next = NULL; |
391 | g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK; | ||
368 | g->frealloc = f; | 392 | g->frealloc = f; |
369 | g->ud = ud; | 393 | g->ud = ud; |
370 | g->warnf = NULL; | 394 | g->warnf = NULL; |