GC checks stack space before running finalizer

If the stack does not have some minimum available space, the GC defers
calling a finalizer until the next cycle. That avoids errors while
running a finalizer that the programmer cannot control.
This commit is contained in:
Roberto I
2025-12-13 16:16:59 -03:00
parent 3d03ae5bd6
commit a5522f06d2
3 changed files with 16 additions and 3 deletions

11
ldo.c
View File

@@ -220,6 +220,17 @@ l_noret luaD_errerr (lua_State *L) {
}
/*
** Check whether stack has enough space to run a simple function (such
** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack and
** 2 slots in the C stack.
*/
int luaD_checkminstack (lua_State *L) {
return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) &&
(getCcalls(L) < LUAI_MAXCCALLS - 2));
}
/*
** In ISO C, any pointer use after the pointer has been deallocated is
** undefined behavior. So, before a stack reallocation, all pointers