diff --git a/common/space_lua/eval.ts b/common/space_lua/eval.ts index b91fd4cc..ca3d703b 100644 --- a/common/space_lua/eval.ts +++ b/common/space_lua/eval.ts @@ -588,12 +588,12 @@ export async function evalStatement( const start = await evalExpression(s.start, env, sf); const end = await evalExpression(s.end, env, sf); const step = s.step ? await evalExpression(s.step, env, sf) : 1; - const localEnv = new LuaEnv(env); for ( let i = start; step > 0 ? i <= end : i >= end; i += step ) { + const localEnv = new LuaEnv(env); localEnv.setLocal(s.name, i); try { await evalStatement(s.block, localEnv, sf); diff --git a/common/space_lua/language_test.lua b/common/space_lua/language_test.lua index 3ccddb5c..836ff012 100644 --- a/common/space_lua/language_test.lua +++ b/common/space_lua/language_test.lua @@ -308,3 +308,233 @@ assert_equal(data[2].name, "John") -- os functions assert(os.time() > 0) assert(os.date("%Y-%m-%d", os.time({ year = 2020, month = 1, day = 1 })) == "2020-01-01") + +-- Closure tests +local function make_counter() + local count = 0 + return function() + count = count + 1 + return count + end +end + +local counter1 = make_counter() +local counter2 = make_counter() +assert(counter1() == 1, "First counter first call") +assert(counter1() == 2, "First counter second call") +assert(counter2() == 1, "Second counter should be independent") +assert(counter1() == 3, "First counter maintains state") + +-- Test nested closures +local function make_adder(x) + return function(y) + return function(z) + return x + y + z + end + end +end + +local add5 = make_adder(5) +local add5and2 = add5(2) +assert(add5and2(3) == 10, "Nested closure should maintain all scopes") + +-- Test closure variable independence +local function make_value_keeper() + local value = 0 + return { + set = function(v) value = v end, + get = function() return value end + } +end + +local keeper1 = make_value_keeper() +local keeper2 = make_value_keeper() +keeper1.set(5) +keeper2.set(10) +assert(keeper1.get() == 5, "First keeper maintains its own value") +assert(keeper2.get() == 10, "Second keeper maintains its own value") + +-- Test closure over loop variables +local functions = {} +for i = 1, 3 do + functions[i] = function() return i end +end +assert(functions[1]() == 1, "Closure should capture loop variable value at creation time") +assert(functions[2]() == 2, "Each closure should have its own value") +assert(functions[3]() == 3, "Each closure should have its own value") + +-- Test closure over mutating variables +local function make_accumulator(initial) + local sum = initial + return { + add = function(x) sum = sum + x end, + get = function() return sum end + } +end + +local acc = make_accumulator(5) +acc.add(3) +acc.add(2) +assert(acc.get() == 10, "Accumulator should maintain state through multiple calls") + +-- Test closures with upvalues modified in nested scopes +local function make_counter_with_reset() + local count = 0 + return { + increment = function() + local old = count + count = count + 1 + return old + end, + reset = function() + local old = count + count = 0 + return old + end + } +end + +local counter = make_counter_with_reset() +assert(counter.increment() == 0) +assert(counter.increment() == 1) +local final = counter.reset() +assert(final == 2, "Reset should return last value") +assert(counter.increment() == 0, "Counter should start fresh after reset") + +print("All closure tests passed!") + +-- Advanced closure tests with multiple functions sharing state +local function test_advanced_closures() + -- Counter that can count by custom steps + local function make_counter_with_step() + local count = 0 + return { + increment = function(step) + count = count + (step or 1) + return count + end, + decrement = function(step) + count = count - (step or 1) + return count + end, + get = function() + return count + end + } + end + + local counter = make_counter_with_step() + assert(counter.increment(5) == 5, "Counter should increment by 5") + assert(counter.decrement(2) == 3, "Counter should decrement by 2") + assert(counter.get() == 3, "Counter should maintain state") + assert(counter.increment() == 4, "Counter should default to 1") + + -- Test multiple independent counters + local c1 = make_counter_with_step() + local c2 = make_counter_with_step() + c1.increment(10) + c2.increment(5) + assert(c1.get() == 10, "First counter should be independent") + assert(c2.get() == 5, "Second counter should be independent") +end + +-- Test closures with shared upvalues +local function test_shared_closures() + local function make_shared_counter() + local count = 0 + local function inc() + count = count + 1 + return count + end + local function dec() + count = count - 1 + return count + end + local function get() + return count + end + return inc, dec, get + end + + local inc, dec, get = make_shared_counter() + assert(inc() == 1, "First increment") + assert(inc() == 2, "Second increment") + assert(dec() == 1, "First decrement") + assert(get() == 1, "Get should return current value") +end + +-- Test varargs handling +local function test_varargs() + -- Basic varargs sum function + local function sum(...) + local args = { ... } + local total = 0 + for _, v in ipairs(args) do + total = total + v + end + return total + end + + assert(sum(1, 2, 3, 4, 5) == 15, "Sum should handle multiple arguments") + assert(sum() == 0, "Sum should handle no arguments") + assert(sum(42) == 42, "Sum should handle single argument") + + -- Test varargs propagation + local function pass_varargs(...) + return sum(...) + end + + assert(pass_varargs(1, 2, 3) == 6, "Should propagate varargs") + assert(pass_varargs() == 0, "Should propagate empty varargs") + + -- Test mixing regular args with varargs + local function first_plus_sum(first, ...) + local args = { ... } + local total = first or 0 + for _, v in ipairs(args) do + total = total + v + end + return total + end + + assert(first_plus_sum(10, 1, 2, 3) == 16, "Should handle mixed arguments") + assert(first_plus_sum(5) == 5, "Should handle only first argument") +end + +-- Test closure edge cases +local function test_closure_edge_cases() + -- Test closure over loop variables + local closures = {} + for i = 1, 3 do + closures[i] = function() return i end + end + + assert(closures[1]() == 1, "Should capture loop variable") + assert(closures[2]() == 2, "Should capture loop variable") + assert(closures[3]() == 3, "Should capture loop variable") + + -- Test nested closure scopes + local function make_nested_counter(start) + local count = start + return function() + local function increment() + count = count + 1 + return count + end + return increment() + end + end + + local counter1 = make_nested_counter(5) + local counter2 = make_nested_counter(10) + assert(counter1() == 6, "First nested counter") + assert(counter1() == 7, "First nested counter increment") + assert(counter2() == 11, "Second nested counter independent") +end + +-- Run the new tests +test_advanced_closures() +test_shared_closures() +test_varargs() +test_closure_edge_cases() +print("All closure and varargs tests passed!") diff --git a/common/space_lua/runtime.ts b/common/space_lua/runtime.ts index 1cb806db..67113587 100644 --- a/common/space_lua/runtime.ts +++ b/common/space_lua/runtime.ts @@ -138,12 +138,16 @@ export function singleResult(value: any): any { } export class LuaFunction implements ILuaFunction { - constructor(readonly body: LuaFunctionBody, private closure: LuaEnv) { + private capturedEnv: LuaEnv; + + constructor(readonly body: LuaFunctionBody, closure: LuaEnv) { + // Don't create a new environment, just store the reference to the closure environment + this.capturedEnv = closure; } call(sf: LuaStackFrame, ...args: LuaValue[]): Promise | LuaValue { - // Create a new environment for this function call - const env = new LuaEnv(this.closure); + // Create a new environment that chains to the captured environment + const env = new LuaEnv(this.capturedEnv); if (!sf) { console.trace(sf); }