Deep self-recursive functions will stack overflow in Lua because each call adds a frame. Lazarus has no way to avoid this right now.
The fix is to detect tail calls — calls that are the last thing a function does before returning — and lower them to a `goto` loop in Lua instead of an actual call. Lua's `goto` makes this clean to implement and produces the same semantics with no stack growth.
This is mostly a codegen change. A tail call `return f(args)` where `f` is the current function gets replaced with an argument reassignment and a `goto` back to the top of the function body.
Deep self-recursive functions will stack overflow in Lua because each call adds a frame. Lazarus has no way to avoid this right now.
The fix is to detect tail calls — calls that are the last thing a function does before returning — and lower them to a `goto` loop in Lua instead of an actual call. Lua's `goto` makes this clean to implement and produces the same semantics with no stack growth.
This is mostly a codegen change. A tail call `return f(args)` where `f` is the current function gets replaced with an argument reassignment and a `goto` back to the top of the function body.