function defer(call)
assert(type(call) == "function")
local a = {
__gc = function()
call()
end
}
return setmetatable(a, a)
end
function test()
print(1)
defer(function ()
print(4)
end)
defer(function ()
print(3)
end)
print(2)
end
test()
输出:
1
2
3
4