** Try to grow the stack by at least 'n' elements. When 'raiseerror' ** is true, raises any error; otherwise, return 0 in case of errors. */
| 351 | ** is true, raises any error; otherwise, return 0 in case of errors. |
| 352 | */ |
| 353 | int luaD_growstack (lua_State *L, int n, int raiseerror) { |
| 354 | int size = stacksize(L); |
| 355 | if (l_unlikely(size > MAXSTACK)) { |
| 356 | /* if stack is larger than maximum, thread is already using the |
| 357 | extra space reserved for errors, that is, thread is handling |
| 358 | a stack error; cannot grow further than that. */ |
| 359 | lua_assert(stacksize(L) == ERRORSTACKSIZE); |
| 360 | if (raiseerror) |
| 361 | luaD_errerr(L); /* stack error inside message handler */ |
| 362 | return 0; /* if not 'raiseerror', just signal it */ |
| 363 | } |
| 364 | else if (n < MAXSTACK) { /* avoids arithmetic overflows */ |
| 365 | int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ |
| 366 | int needed = cast_int(L->top.p - L->stack.p) + n; |
| 367 | if (newsize > MAXSTACK) /* cannot cross the limit */ |
| 368 | newsize = MAXSTACK; |
| 369 | if (newsize < needed) /* but must respect what was asked for */ |
| 370 | newsize = needed; |
| 371 | if (l_likely(newsize <= MAXSTACK)) |
| 372 | return luaD_reallocstack(L, newsize, raiseerror); |
| 373 | } |
| 374 | /* else stack overflow */ |
| 375 | /* add extra size to be able to handle the error message */ |
| 376 | luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); |
| 377 | if (raiseerror) |
| 378 | luaG_runerror(L, "stack overflow"); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | /* |
no test coverage detected