diff options
| author | Cliff Click <cliffc@acm.org> | 2026-11-15 16:11:39 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-11-15 16:11:39 -0800 |
| commit | 9fe7c6f9697b5bbf3041261fdc60040e75cc674f (patch) | |
| tree | d92a8ff20ed984938d90ad8917b4d67fba9fb745 /chapter32/src/test/java/com/seaofnodes/simple/MethodAccessTest.java | |
| parent | f0a6c6757a795b8e186de0076836a87acef59fa7 (diff) | |
| download | Simple-ch32.tar.gz Simple-ch32.zip | |
Split off chapter 31ch32
Ch 32 will be focused on compiler performance issues.
Diffstat (limited to 'chapter32/src/test/java/com/seaofnodes/simple/MethodAccessTest.java')
| -rw-r--r-- | chapter32/src/test/java/com/seaofnodes/simple/MethodAccessTest.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/MethodAccessTest.java b/chapter32/src/test/java/com/seaofnodes/simple/MethodAccessTest.java new file mode 100644 index 00000000..7275b151 --- /dev/null +++ b/chapter32/src/test/java/com/seaofnodes/simple/MethodAccessTest.java @@ -0,0 +1,118 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.parser.Parser; +import com.seaofnodes.simple.type.TypeInteger; +import com.seaofnodes.simple.util.Ary; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Test; +import static org.junit.Assert.*; + +public class MethodAccessTest { + @Test public void readersAcceptReadOnlyReceivers() throws Exception { + String source = """ + struct Child { int n = 7; }; + struct Box { + !Child child = new Child; + int[] values = new int[]{3,5}; + int count = 2; + { i64 } read = { -> count+child.n+values[1]; }; + val delegate = { -> read(); }; + val recurse = { i64 n -> n == 0 ? read() : recurse(n-1); }; + val draw = { !Child canvas -> canvas.n = count; }; + val outer = { -> later(count); }; + }; + val later = { int n -> n+1; }; + val check = { Box box -> + val canvas = new Child; + box.draw(canvas); + return box.read()+box.delegate()+box.recurse(3)+canvas.n+box.outer(); + }; + return check(new Box); + """; + assertEquals("47",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.LocalSched),0)); + TestC.runSF("val main = { -> "+source.replace("return check(new Box);","return check(new Box) != 47;")+" };", + "readonlyMethods","",-1); + } + + @Test public void writesThroughReceiverRequireAccess() { + String[] methods = { + "val act = { -> x = 2; };", + "val act = { -> self.x++; };", + "val act = { -> val nested = { -> x++; }; nested(); };", + "val act = { -> val alias = self; alias.x += 1; };", + "val act = { -> child.n = 2; };", + "val act = { -> values[0]++; };", + "val act = { -> write(); }; val write = { -> x++; };", + "val act = { -> val f = x == 0 ? left : right; f(); }; val left = { -> x++; }; val right = { -> x--; };", + "val act = { -> child.write(); };", + "val act = { -> write(self); };", + "{ int } act = \"C\";" + }; + for( String method : methods ) { + String source = """ + struct Child { int n; val write = { -> n++; }; }; + struct Box { + int x; + !Child child = new Child; + int[] values = new int[1]; + """+method+""" + }; + val write = { !Box box -> box.x++; }; + val check = { Box box -> box.act(); return 0; }; + return check(new Box); + """; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Accepted writer: "+method); + } catch( Parser.ParseException e ) { + assertTrue(method+": "+e.getMessage(),e.getMessage().contains("read-only")); + } + new CodeGen(source.replace("val check = { Box box", "val check = { !Box box")) + .driver(CodeGen.Phase.TypeCheck); + } + } + + @Test public void returningWritableAliasesRequiresAccess() { + String source = """ + struct Box { int x; val alias = { -> self; }; }; + val check = { Box box -> box.alias(); }; + return check(new Box); + """; + try { + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + fail("Read-only receiver exposed as writable"); + } catch( Parser.ParseException e ) { + assertTrue(e.getMessage(),e.getMessage().contains("read-only")); + } + source = source.replace("val alias = { -> self; }", "val alias = { -> Box view = self; view; }"); + new CodeGen(source).driver(CodeGen.Phase.TypeCheck); + } + + @Test public void importedReceiverAccess() throws Exception { + var root = Files.createDirectories(Path.of("build/objs/separate")); + var lib = Files.createTempDirectory(root,"method-access-"); + new CodeGen(null,lib.toString(),null,"Counters",""" + struct Counter { + int n = 7; + val read = { -> n; }; + val write = { -> n++; }; + }; + val make = { -> new Counter; }; + """,126,true,TypeInteger.BOT) + .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + var paths = new Ary<>(new String[]{lib.toString()}); + String source = "val box = Counters.make(); val check = { Counters.Counter c -> c.read(); }; return check(box);"; + var code = new CodeGen(null,"build/objs",paths,"CounterReader",source,126,true, + TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck); + assertEquals("7",Eval2.eval(code,0)); + try { + new CodeGen(null,"build/objs",paths,"CounterWriter",source.replace("c.read()","c.write()"),126,true, + TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck); + fail("Imported writer accepted a read-only receiver"); + } catch( Parser.ParseException e ) { + assertTrue(e.getMessage(),e.getMessage().contains("read-only")); + } + } +} |
