If I understand your problem, your call frame stack ends up looking like:
1. body
2. withx
3. withx
4. withx
...
Frames 2/3/4 only exist to restore the value of globalx, and frames
2/3 are redundant as the value they restore will be overridden by
frame 4. If this understanding is correct, then the following might be
a solution:
function restorex(oldx, ...)
globalx = oldx
return ...
end
function withx(x, body)
local oldx = restorex(x, globalx)
if (debug.getinfo(2, "f") or {}).func == withx then return body() end
return restorex(oldx, body())
end
On Fri, Nov 4, 2016 at 9:18 PM, Brandon Bloom <brandon.d.bloom@xxxxxxxxx> wrote:
Just for curiosity, is x always the same in your recursive calls or you
have different x?
Nope. The recursive calls necessarily create a stack of x values.