Print errors from __gc finalizers instead of rethrowing them.

Finalizers are not supposed to throw errors -- this is undefined behavior.
Lua 5.1 - 5.3 and (previously) LuaJIT rethrow the error. This randomly
breaks some unrelated code that just happens to do an allocation. Bad.
Lua 5.4 catches the error and emits a warning instead. But warnings are
not enabled by default, so it fails silently. Even worse.
LuaJIT (now) catches the error and emits a VM event. The default event
handler function prints "ERROR in finalizer: ...".
Set a custom handler function with: jit.attach(handler, "errfin")
This commit is contained in:
Mike Pall
2023-04-16 18:13:48 +02:00
parent 8bbd58e534
commit 1c27912705
4 changed files with 64 additions and 28 deletions

View File

@@ -24,9 +24,10 @@
/* VM event IDs. */
typedef enum {
VMEVENT_DEF(BC, 0x00003883),
VMEVENT_DEF(TRACE, 0xb2d91467),
VMEVENT_DEF(RECORD, 0x9284bf4f),
VMEVENT_DEF(TEXIT, 0xb29df2b0),
VMEVENT_DEF(TRACE, 0x12d91467),
VMEVENT_DEF(RECORD, 0x1284bf4f),
VMEVENT_DEF(TEXIT, 0x129df2b0),
VMEVENT_DEF(ERRFIN, 0x12d93888),
LJ_VMEVENT__MAX
} VMEvent;