From 7b19c504459e2368ceb42540691abf664db935c6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 20 Nov 2023 22:50:04 +0000 Subject: Support capture of vars in effect handler methods --- .../patio/PatioCodeGenDefineFunctionTests.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java index eeb17968..696876b9 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.patio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.shedlang.compiler.ast.typed.FreeVariables; +import org.shedlang.compiler.patio.ast.types.PatioTypes; import org.shedlang.compiler.patio.instructions.Patio; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; @@ -90,6 +91,33 @@ public class PatioCodeGenDefineFunctionTests { snapshotter.assertSnapshot(serialize(context)); } + @Test + public void pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException { + var instruction = new DefineFunctionInstruction( + "f", + List.of(), + List.of(), + FreeVariables.builder() + .plus("a", Sources.NULL) + .plus("b", Sources.NULL) + .build(), + List.of( + Instructions.localLoad("a"), + Instructions.return_() + ), + Sources.NULL + ); + var context = PatioCodeGenContext.EMPTY + .declareStackIrVal("a", PatioTypes.DATA_POINTER).context() + .initStackIrLocal("a") + .declareStackIrVal("b", PatioTypes.DATA_POINTER).context() + .initStackIrLocal("b"); + + var result = PatioCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + @Test public void positionalAndNamedParamsAreDefined(Snapshotter snapshotter) throws IOException { var instruction = new DefineFunctionInstruction( -- cgit v1.2.3