feat(pbs): add mutable lists and stable snapshot cursors
Implement SDK list<T> and Cursor<T> with trusted compiler bindings and generic storage publication across IR and PBX. Pin the runtime provider and include specs, conformance coverage, installed examples, and DSC-0090 lessons.
This commit is contained in:
+376
@@ -0,0 +1,376 @@
|
||||
package p.studio.compiler.backend.irvm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.backend.bytecode.*;
|
||||
import p.studio.compiler.models.*;
|
||||
import p.studio.compiler.pbs.*;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.*;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
import java.nio.file.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsListCursorTest {
|
||||
private static Path directory() { return Path.of(System.getProperty("pbs.registryConformanceDirectory")); }
|
||||
static BytecodeModule compile(String name,String source) throws Exception {
|
||||
var diagnostics=DiagnosticSink.empty();var id=new FileId(0);var sdkId=new FileId(1);
|
||||
String sdk;
|
||||
try(var stream=PbsListCursorTest.class.getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
|
||||
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
|
||||
}
|
||||
var ast=PbsParser.parse(PbsLexer.lex(sdk+"\n"+source,id,diagnostics),id,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
var file=new PbsFrontendCompiler().compileParsedFile(id,ast,diagnostics,source.contains("[BuiltinType")?SourceKind.SDK_INTERFACE:SourceKind.PROJECT,new ModuleId(0),
|
||||
ReadOnlyList.empty(),p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),
|
||||
p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
|
||||
assertFalse(diagnostics.hasErrors(),()->name+": "+diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()+" at "+d.getSpan().getStart()+":"+d.getSpan().getEnd()).toList());
|
||||
var backend=IRBackend.builder().entryPointCallableName("Probe.main").entryPointModuleId(new ModuleId(0))
|
||||
.executableFunctions(file.executableFunctions()).globals(file.globals()).registry(file.registry())
|
||||
.intrinsicPool(file.intrinsicPool()).callableSignatures(file.callableSignatures()).build();
|
||||
var program=new LowerToIRVMService().lower(backend);
|
||||
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
|
||||
if(name.equals("gc"))module=PbsStructuredValuesTest.withTestSafepoint(module);
|
||||
new BytecodePreloadVerifierService().verify(module);
|
||||
Files.createDirectories(directory());Files.write(directory().resolve("pbs-list-"+name+".pbx"),module.serialize());
|
||||
if(!name.equals("gc")&&!name.startsWith("refusal-")) {
|
||||
var optimized=new OptimizeIRVMService().optimize(new LowerToIRVMService().lower(backend));
|
||||
var optimizedModule=new BytecodeEmitter().emit(optimized.coherentEmissionPlan());
|
||||
new BytecodePreloadVerifierService().verify(optimizedModule);
|
||||
Files.write(directory().resolve("pbs-list-"+name+"-optimized.pbx"),optimizedModule.serialize());
|
||||
}
|
||||
return module;
|
||||
}
|
||||
@Test void defaultConstructionAndSourceAlgorithmsCompile() throws Exception {
|
||||
compile("basic","""
|
||||
declare service Probe { fn main() -> int {
|
||||
let values = new list<int>();
|
||||
values.add(20); values.add(22);
|
||||
return (values[0] else 0) + (values[1] else 0);
|
||||
} }
|
||||
""");
|
||||
}
|
||||
private static void probe(String name,String body) throws Exception {
|
||||
compile(name,"declare service Probe { fn main() -> int { "+body+" } }");
|
||||
}
|
||||
@Test void capacityPoliciesAndLogicalCleanup() throws Exception {
|
||||
probe("construction","""
|
||||
let values: list<int> = new list();
|
||||
if (values.size() as int) != 0 { return 0; }
|
||||
if (values.capacity() as int) != 16 { return 0; }
|
||||
let maybe:optional (list<int>)=some(new list());
|
||||
if let owner=maybe {if (owner.size() as int)!=0 {return 0;}} else {return 0;}
|
||||
let capacity: int = 0;
|
||||
let empty: list<int> = new list(capacity);
|
||||
if (empty.capacity() as int) != 0 { return 0; }
|
||||
empty.add(42);let first:long=0;return empty[first] else 0;
|
||||
""");
|
||||
probe("capacity","""
|
||||
let values = new list<int>(0);
|
||||
values.add(1); if (values.capacity() as int) != 1 { return 0; }
|
||||
values.add(2); if (values.capacity() as int) != 2 { return 0; }
|
||||
values.add(3); if (values.capacity() as int) != 3 { return 0; }
|
||||
values.add(4); if (values.capacity() as int) != 5 { return 0; }
|
||||
values.add(5); values.add(6);
|
||||
if (values.capacity() as int) != 8 { return 0; }
|
||||
values.reserve(9); if (values.capacity() as int) != 9 { return 0; }
|
||||
values.reserve(0); if (values.capacity() as int) != 9 { return 0; }
|
||||
values.resize(20); if (values.capacity() as int) != 20 { return 0; }
|
||||
values.resize(2); values.trim();
|
||||
if (values.capacity() as int) != 2 { return 0; }
|
||||
values.clear(); if (values.capacity() as int) != 2 { return 0; }
|
||||
values.trim(); if (values.capacity() as int) != 0 { return 0; }
|
||||
return 42;
|
||||
""");
|
||||
probe("holes","""
|
||||
let values = new list<int>(0);
|
||||
values.resize(3); values[0] = 0; values[2] = 7;
|
||||
if values[0].hasNone() { return 0; }
|
||||
if values[1].hasSome() { return 0; }
|
||||
values.add(9); values.insert(1, 42);
|
||||
if (values.size() as int) != 5 { return 0; }
|
||||
if values[2].hasSome() { return 0; }
|
||||
if (values[3] else 0) != 7 { return 0; }
|
||||
if values.removeAt(2).hasSome() { return 0; }
|
||||
if (values[2] else 0) != 7 { return 0; }
|
||||
let previous = values.removeAt(1);
|
||||
values.clearAt(0); if values[0].hasSome() { return 0; }
|
||||
if (values.size() as int) != 3 { return 0; }
|
||||
values.resize(1); values.resize(3);
|
||||
if values[1].hasSome() or values[2].hasSome() { return 0; }
|
||||
values.clear(); values.resize(3);
|
||||
if values[0].hasSome() or values[2].hasSome() { return 0; }
|
||||
return previous else 0;
|
||||
""");
|
||||
probe("structural-growth","""
|
||||
let maximum: long = 4294967295;
|
||||
if _grownCapacity(maximum - 1, maximum) != maximum { return 0; }
|
||||
if (_grownCapacity(0, 42) as int) != 42 { return 0; }
|
||||
return 42;
|
||||
""");
|
||||
}
|
||||
@Test void cursorStatesAndSnapshots() throws Exception {
|
||||
probe("cursor-empty","""
|
||||
let values = new list<int>(); values.resize(3);
|
||||
let cursor: Cursor<int> = values.cursor();
|
||||
values.add(42);
|
||||
cursor.next(); cursor.next();
|
||||
if cursor.hasNext() or cursor.current().hasSome() { return 0; }
|
||||
return 42;
|
||||
""");
|
||||
probe("cursor-single","""
|
||||
let values = new list<int>(); values.add(42);
|
||||
let cursor = values.cursor();
|
||||
if cursor.hasNext() { return 0; }
|
||||
cursor.next(); cursor.next(); cursor.next();
|
||||
if cursor.hasNext() { return 0; }
|
||||
return cursor.current() else 0;
|
||||
""");
|
||||
probe("cursor-many","""
|
||||
let values = new list<int>();
|
||||
values.add(20); values.add(22);
|
||||
let cursor = values.cursor();
|
||||
if (cursor.current() else 0) != 20 { return 0; }
|
||||
if (cursor.current() else 0) != 20 { return 0; }
|
||||
if cursor.hasNext() == false { return 0; }
|
||||
if cursor.hasNext() == false { return 0; }
|
||||
cursor.next();
|
||||
if cursor.hasNext() { return 0; }
|
||||
if (cursor.current() else 0) != 22 { return 0; }
|
||||
cursor.next(); cursor.next();
|
||||
if (cursor.current() else 0) != 22 { return 0; }
|
||||
return 42;
|
||||
""");
|
||||
probe("snapshot","""
|
||||
let values = new list<int>(0); values.resize(4);
|
||||
values[0]=0; values[2]=20; values[3]=22;
|
||||
let cursor=values.cursor();
|
||||
values[0]=9; values.insert(0,9); values.removeAt(1);
|
||||
values.add(99); values.resize(1); values.clear(); values.trim();
|
||||
let total:int=0; let count:int=0;
|
||||
while true {
|
||||
if let value=cursor.current() { total=total+value; count=count+1; }
|
||||
else { break; }
|
||||
if cursor.hasNext() {cursor.next();} else {break;}
|
||||
}
|
||||
if count!=3 {return 0;}
|
||||
return total;
|
||||
""");
|
||||
probe("false-present","""
|
||||
let values=new list<bool>();values.resize(2);values[1]=false;
|
||||
let cursor=values.cursor();
|
||||
if cursor.hasNext() {return 0;}
|
||||
if let value=cursor.current() {if value==false {return 42;}}
|
||||
return 0;
|
||||
""");
|
||||
probe("shared-optional","""
|
||||
let empty:Cursor<optional int>=new _SnapshotCursor<optional int>(none);
|
||||
if empty.current().hasSome() {return 0;}
|
||||
let node=new _SnapshotNode<optional int>(none,none);
|
||||
let cursor:Cursor<optional int>=new _SnapshotCursor<optional int>(some(node));
|
||||
if cursor.current().hasNone() {return 0;}
|
||||
if let payload=cursor.current() {if payload.hasSome() {return 0;}}
|
||||
else {return 0;}
|
||||
cursor.next();if cursor.current().hasNone() {return 0;}
|
||||
return 42;
|
||||
""");
|
||||
}
|
||||
@Test void aliasesAndCompletePayloads() throws Exception {
|
||||
compile("identity","""
|
||||
declare struct Item(pub mut value:int);
|
||||
declare service Probe {fn main()->int {
|
||||
let values=new list<Item>(0);let alias=values;
|
||||
let item=new Item(20);let other=new Item(20);
|
||||
values.add(item);alias.add(other);values.add(item);
|
||||
if (alias.size() as int)!=3 {return 0;}
|
||||
let cursor=values.cursor();values.clear();values.trim();item.value=22;
|
||||
if let first=cursor.current() {if first!=item {return 0;}}
|
||||
else {return 0;}
|
||||
cursor.next();if let middle=cursor.current() {if middle==item {return 0;}}
|
||||
else {return 0;}
|
||||
cursor.next();if let last=cursor.current() {return last.value+other.value;}
|
||||
return 0;
|
||||
}}
|
||||
""");
|
||||
probe("tuple","""
|
||||
let values=new list<(left:int,right:int)>(0);
|
||||
values.add((left:20,right:22));values.insert(0,(left:1,right:2));
|
||||
values.removeAt(0);let cursor=values.cursor();values.clear();
|
||||
if let value=cursor.current() {return value.left+value.right;}
|
||||
return 0;
|
||||
""");
|
||||
probe("nested-array","""
|
||||
let payload:array<int>=[20];let values=new list<array<int>>();
|
||||
values.add(payload);values.insert(0,payload);values.removeAt(0);
|
||||
let cursor=values.cursor();values.clear();payload[0]=42;
|
||||
if let value=cursor.current() {return value[0] else 0;}
|
||||
return 0;
|
||||
""");
|
||||
compile("function","""
|
||||
fn answer()->int {return 42;}
|
||||
declare service Probe {fn main()->int {
|
||||
let values=new list<Fn()->int>(0);values.add(answer);
|
||||
values.insert(0,answer);values.removeAt(1);
|
||||
let cursor=values.cursor();values.clear();
|
||||
if let call=cursor.current() {return call();}return 0;
|
||||
}}
|
||||
""");
|
||||
compile("evaluation","""
|
||||
declare struct Effects(pub mut count:int,pub values:list<int>) {
|
||||
fn receiver()->list<int> {this.count=this.count*10+1;return this.values;}
|
||||
fn index()->int {this.count=this.count*10+2;return 0;}
|
||||
fn value()->int {this.count=this.count*10+3;return 42;}
|
||||
}
|
||||
declare service Probe {fn main()->int {
|
||||
let values=new list<int>();values.resize(1);
|
||||
let effects=new Effects(0,values);
|
||||
effects.receiver()[effects.index()]=effects.value();
|
||||
if effects.count!=123 {return 0;}
|
||||
effects.count=0;
|
||||
effects.receiver().insert apply (value:effects.value(),index:effects.index());
|
||||
if effects.count!=132 {return 0;}
|
||||
let length:int=3;values.resize apply length;
|
||||
let index:int=2;values.insert apply (index,1);
|
||||
if (values.size() as int)!=4 {return 0;}
|
||||
return values[0] else 0;
|
||||
}}
|
||||
""");
|
||||
}
|
||||
@Test void nativeAndContractGroupsSurviveTransfersAndCapture() throws Exception {
|
||||
compile("vec2","""
|
||||
[BuiltinType(name="vec2",version=1)]
|
||||
declare builtin type Vector(pub x:float,pub y:float) {}
|
||||
fn nativeProbe()->int {
|
||||
let values=new list<Vector>(0);values.add(new Vector(20.0,22.0));
|
||||
values.insert(0,new Vector(1.0,2.0));values.removeAt(0);
|
||||
let cursor=values.cursor();values.clear();
|
||||
if let value=cursor.current() {
|
||||
let projected=(cursor.current() else new Vector(0.0,0.0)).x;
|
||||
return (projected+value.y) as int;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
declare service Probe {fn main()->int {return nativeProbe();}}
|
||||
""");
|
||||
String color;
|
||||
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/core/color/main.pbs")) {
|
||||
assertNotNull(stream);color=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
|
||||
}
|
||||
compile("pixel",color+"""
|
||||
[BuiltinType(name="pixel",version=1)]
|
||||
declare builtin type Pixel(pub x:long,pub y:long,pub color:Color) {}
|
||||
fn nativeProbe()->int {
|
||||
let values=new list<Pixel>(0);values.add(new Pixel(20,22,RED));
|
||||
values.insert(0,new Pixel(1,2,BLUE));values.removeAt(0);
|
||||
let cursor=values.cursor();values.clear();
|
||||
if let value=cursor.current() {
|
||||
if value.color!=RED {return 0;}
|
||||
return (value.x+value.y) as int;
|
||||
}return 0;
|
||||
}
|
||||
declare service Probe {fn main()->int {return nativeProbe();}}
|
||||
""");
|
||||
compile("contract","""
|
||||
declare contract Reader {fn read()->int;}
|
||||
declare struct Item(pub value:int);
|
||||
implements Reader for Item using item {fn read()->int {return item.value;}}
|
||||
declare service Probe {fn main()->int {
|
||||
let item=new Item(42);let values=new list<Reader>(0);
|
||||
values.add(item as Reader);values.insert(0,item as Reader);values.removeAt(0);
|
||||
let cursor=values.cursor();values.clear();
|
||||
if let value=cursor.current() {return value.read();}return 0;
|
||||
}}
|
||||
""");
|
||||
}
|
||||
@Test void faultsPreserveProviderClassification() throws Exception {
|
||||
probe("fault-read","let values=new list<int>(4);return values[0] else 0;");
|
||||
probe("fault-write","let values=new list<int>(4);values[0]=42;return 0;");
|
||||
probe("fault-negative","let values=new list<int>();values.add(1);return values[-1] else 0;");
|
||||
probe("fault-insert","let values=new list<int>();values.insert(1,42);return 0;");
|
||||
probe("fault-length","let values=new list<int>();values.resize(-1);return 0;");
|
||||
probe("fault-overflow","let values=new list<int>();let length:long=4294967296;values.reserve(length);return 0;");
|
||||
probe("fault-long-index","let values=new list<int>();values.add(42);let index:long=4294967296;return values[index] else 0;");
|
||||
probe("fault-constructor","let values=new list<int>(-1);return 0;");
|
||||
}
|
||||
@Test void resourceRefusalFixturesExposeIndependentMutationBoundaries() throws Exception {
|
||||
for(var operation:java.util.Map.of(
|
||||
"insert","values.insert(12,42);",
|
||||
"remove","let previous=values.removeAt(12);",
|
||||
"growth","values.add(42);",
|
||||
"cursor","let snapshot=values.cursor();").entrySet()) {
|
||||
int capacity=operation.getKey().equals("growth")?128:256;
|
||||
compile("refusal-"+operation.getKey(),"""
|
||||
fn mutationMarker() {let marker:int=314160;}
|
||||
fn completedMarker() {let marker:int=314161;}
|
||||
fn performSecond(values:list<int>) {OPERATION}
|
||||
fn perform(values:list<int>) {performSecond(values);}
|
||||
declare service Probe {fn main()->int {
|
||||
let values=new list<int>(CAPACITY);
|
||||
values.resize(128);
|
||||
for i:int from 0 until 128 {values[i]=i;}
|
||||
mutationMarker();
|
||||
perform(values);
|
||||
completedMarker();return 42;
|
||||
}}
|
||||
""".replace("CAPACITY",Integer.toString(capacity)).replace("OPERATION",operation.getValue()));
|
||||
}
|
||||
}
|
||||
@Test void snapshotPrefixAndStickyLastUseNormalSafepoints() throws Exception {
|
||||
compile("gc","""
|
||||
declare struct Item(pub value:int);
|
||||
declare struct Garbage();
|
||||
fn collect() {let marker:int=314159;}
|
||||
fn makeCursor()->Cursor<Item> {
|
||||
let values=new list<Item>(1200);
|
||||
for i:int from 0 until 1200 {values.add(new Item(i));}
|
||||
return values.cursor();
|
||||
}
|
||||
fn useCursor() {
|
||||
let cursor=makeCursor();collect();
|
||||
for i:int from 0 until 1199 {cursor.next();}
|
||||
collect();
|
||||
for i:int from 0 until 1200 {cursor.next();}
|
||||
collect();
|
||||
if let item=cursor.current() {
|
||||
if item.value!=1199 {let failure=1/0;}
|
||||
} else {let failure=1/0;}
|
||||
}
|
||||
declare service Probe {fn main()->int {
|
||||
useCursor();
|
||||
for i:int from 0 until 1200 {let discarded=new Garbage();}
|
||||
collect();return 42;
|
||||
}}
|
||||
""");
|
||||
}
|
||||
@Test void sdkAliasesAndReexportsExecuteThroughTheWorkspace(@org.junit.jupiter.api.io.TempDir Path root) throws Exception {
|
||||
Files.createDirectories(root.resolve("src/facade"));
|
||||
Files.writeString(root.resolve("prometeu.json"),"""
|
||||
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
|
||||
""");
|
||||
Files.writeString(root.resolve("src/mod.barrel"),"pub fn frame() -> void;");
|
||||
Files.writeString(root.resolve("src/facade/mod.barrel"),"pub struct Items<T>; pub contract Walk<T>;");
|
||||
Files.writeString(root.resolve("src/facade/main.pbs"),"import {list as Items,Cursor as Walk} from @sdk:collections;");
|
||||
Files.writeString(root.resolve("src/main.pbs"),"""
|
||||
import {Items,Walk} from @app:facade;
|
||||
[Frame] fn frame() -> void {
|
||||
let values:Items<int>=new Items(0);values.add(20);values.add(22);
|
||||
let cursor:Walk<int>=values.cursor();values.clear();
|
||||
let total:int=0;
|
||||
while true {
|
||||
if let value=cursor.current() {total=total+value;} else {break;}
|
||||
if cursor.hasNext() {cursor.next();} else {break;}
|
||||
}
|
||||
if total!=42 {let failure=1/0;}
|
||||
}
|
||||
""");
|
||||
var result=p.studio.compiler.workspaces.BuilderPipelineService.INSTANCE.compile(
|
||||
p.studio.compiler.models.BuilderPipelineContext.fromConfig(new p.studio.compiler.messages.BuilderPipelineConfig(false,root.toString())),
|
||||
p.studio.utilities.logs.LogAggregator.stdout());
|
||||
assertFalse(result.diagnostics().stream().anyMatch(d->d.isError()),result.diagnostics().toString());
|
||||
assertTrue(result.bytecodeBytes().length>0);
|
||||
Files.createDirectories(directory());Files.write(directory().resolve("pbs-list-imports.pbx"),result.bytecodeBytes());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user