print("=== Upvalue Stress Tests ===")
print("")
print("1. Many upvalues (10 captured variables)")
local a, b, c, d, e, f, g, h, i, j = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
local sum_all = function()
return a + b + c + d + e + f + g + h + i + j
end
print("sum of 1-10 = " .. tostring(sum_all())) a = 100
print("after a=100, sum = " .. tostring(sum_all())) print("")
print("2. Deeply nested closures (5 levels)")
local outer = 1
local make_nested = function()
local level1 = 10
return function()
local level2 = 100
return function()
local level3 = 1000
return function()
local level4 = 10000
return function()
return outer + level1 + level2 + level3 + level4
end
end
end
end
end
local deep = make_nested()()()()
print("deeply nested sum = " .. tostring(deep())) outer = 2
print("after outer=2, sum = " .. tostring(deep())) print("")
print("3. Many closures sharing one upvalue")
local shared = 0
local closures = {}
local make_incrementer = function(idx)
return function()
shared = shared + idx
return shared
end
end
for i = 1, 5 do
closures[i] = make_incrementer(i)
end
print("shared starts at: " .. tostring(shared))
for i = 1, 5 do
print("closure " .. tostring(i) .. "() = " .. tostring(closures[i]()))
end
print("shared ends at: " .. tostring(shared)) print("")
print("4. Closure escaping creator (counter pattern)")
local make_counter = function(start)
local count = start
return {
inc = function() count = count + 1 return count end,
dec = function() count = count - 1 return count end,
get = function() return count end,
set = function(v) count = v end
}
end
local c1 = make_counter(0)
local c2 = make_counter(100)
print("c1: " .. tostring(c1.inc()) .. ", " .. tostring(c1.inc()) .. ", " .. tostring(c1.inc())) print("c2: " .. tostring(c2.dec()) .. ", " .. tostring(c2.dec())) print("c1.get() = " .. tostring(c1.get())) print("c2.get() = " .. tostring(c2.get())) c1.set(50)
print("after c1.set(50): c1.get() = " .. tostring(c1.get())) print("")
print("5. Upvalues from loop variables")
local funcs = {}
for i = 1, 3 do
local capture = i funcs[i] = function() return capture end
end
print("funcs[1]() = " .. tostring(funcs[1]())) print("funcs[2]() = " .. tostring(funcs[2]())) print("funcs[3]() = " .. tostring(funcs[3]())) print("")
print("6. Mutual recursion via upvalues")
local is_even, is_odd
is_even = function(n)
if n == 0 then return true end
return is_odd(n - 1)
end
is_odd = function(n)
if n == 0 then return false end
return is_even(n - 1)
end
print("is_even(10) = " .. tostring(is_even(10))) print("is_odd(10) = " .. tostring(is_odd(10))) print("is_even(7) = " .. tostring(is_even(7))) print("is_odd(7) = " .. tostring(is_odd(7))) print("")
print("7. Upvalue shadowing")
local x = "outer"
local get_outer = function() return x end
local test_shadow = function()
local x = "inner" local get_inner = function() return x end
return get_outer() .. ", " .. get_inner()
end
print("shadowing result: " .. test_shadow()) print("")
print("8. Closures in tables")
local base = 1000
local ops = {
add = function(x) return base + x end,
sub = function(x) return base - x end,
mul = function(x) return base * x end
}
print("ops.add(5) = " .. tostring(ops.add(5))) print("ops.sub(5) = " .. tostring(ops.sub(5))) print("ops.mul(5) = " .. tostring(ops.mul(5))) base = 2000
print("after base=2000:")
print("ops.add(5) = " .. tostring(ops.add(5))) print("")
print("9. Self-modifying closure")
local make_accumulator = function()
local total = 0
local count = 0
return function(x)
total = total + x
count = count + 1
return total, count
end
end
local acc = make_accumulator()
local t, c
t, c = acc(10)
print("acc(10): total=" .. tostring(t) .. ", count=" .. tostring(c))
t, c = acc(20)
print("acc(20): total=" .. tostring(t) .. ", count=" .. tostring(c))
t, c = acc(30)
print("acc(30): total=" .. tostring(t) .. ", count=" .. tostring(c))
print("")
print("=== All upvalue stress tests passed! ===")