feat(pbs): implement typed arrays, optional binding and contract execution

This commit is contained in:
2026-10-16 16:21:33 +01:00
parent c4e1b9055d
commit 39df63581c
51 changed files with 1902 additions and 68 deletions
@@ -0,0 +1,471 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.models.IRBackend;
import p.studio.compiler.models.IRBackendExecutableFunction;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.models.SourceKind;
import java.nio.file.*;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.utilities.structures.ReadOnlyList;
import p.studio.compiler.source.identifiers.ModuleId;
import static org.junit.jupiter.api.Assertions.*;
class PbsStructuredValuesTest {
private static BytecodeModule compile(String name,String source) throws Exception {
var diagnostics=DiagnosticSink.empty();
var fileId = new FileId(0);
var ast = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(source,fileId,diagnostics),fileId,diagnostics,p.studio.compiler.pbs.parser.PbsParser.ParseMode.INTERFACE_MODULE);
var file=new PbsFrontendCompiler().compileParsedFile(fileId,ast,diagnostics,source.contains("[BuiltinType") ? SourceKind.SDK_INTERFACE : SourceKind.PROJECT,
new p.studio.compiler.source.identifiers.ModuleId(0),p.studio.utilities.structures.ReadOnlyList.empty(),
p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
assertFalse(diagnostics.hasErrors(), ()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
var functions = new java.util.ArrayList<>(file.executableFunctions().asList());
var signatures = new java.util.ArrayList<>(file.callableSignatures().asList());
String entry = "Probe.main";
var initializer = functions.stream().filter(f -> f.callableName().startsWith("__pbs.file_init$")).findFirst();
if (initializer.isPresent()) {
// The fixture driver performs the same ordered file-init call as the production lifecycle wrapper.
var main = functions.stream().filter(f -> f.callableName().equals("Probe.main")).findFirst().orElseThrow();
var init = initializer.get();
var id = new p.studio.compiler.source.identifiers.CallableId(signatures.size());
entry = "$test.entry";
signatures.add(new p.studio.compiler.source.tables.CallableSignatureRef(new ModuleId(0),entry,0,"()->simple:int"));
var kind = IRBackendExecutableFunction.InstructionKind.RET;
var instructions = ReadOnlyList.from(
new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
new ModuleId(0),init.callableName(),init.callableId(),null,null,0,0,init.span()),
new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.CALL_FUNC,
new ModuleId(0),main.callableName(),main.callableId(),null,null,0,1,main.span()),
new IRBackendExecutableFunction.Instruction(kind,"",null,null,main.span()));
functions.add(new IRBackendExecutableFunction(fileId,new ModuleId(0),entry,id,0,0,0,0,1,1,instructions,main.span(),main.metadata()));
}
var backend=IRBackend.builder().entryPointCallableName(entry).entryPointModuleId(new ModuleId(0)).executableFunctions(ReadOnlyList.wrap(functions))
.globals(file.globals()).registry(file.registry()).intrinsicPool(file.intrinsicPool()).callableSignatures(ReadOnlyList.wrap(signatures)).build();
var program=new LowerToIRVMService().lower(backend);
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
if (name.equals("gc-roots")) module = withTestSafepoint(module);
new BytecodePreloadVerifierService().verify(module);
var directory=Path.of(System.getProperty("pbs.registryConformanceDirectory"));Files.createDirectories(directory);
Files.write(directory.resolve("pbs-structured-"+name+".pbx"),module.serialize());
return module;
}
// PBS sync/closure syntax belongs to AGD82. This test-only driver hook replaces
// an isolated marker assignment with FRAME_SYNC + NOPs, preserving every offset.
// All allocations, roots, field/array access and post-GC assertions are compiled PBS.
private static BytecodeModule withTestSafepoint(BytecodeModule module) {
byte[] code = module.code().clone();
var marker = java.nio.ByteBuffer.allocate(6).order(java.nio.ByteOrder.LITTLE_ENDIAN)
.putShort((short) 0x17).putInt(314159).array();
int found = 0;
for (int offset = 0; offset + 12 <= code.length; offset++) {
if (!java.util.Arrays.equals(marker, java.util.Arrays.copyOfRange(code, offset, offset + 6))) continue;
assertEquals(0x43, code[offset + 6]);
assertEquals(0, code[offset + 7]);
java.util.Arrays.fill(code, offset, offset + 12, (byte) 0);
code[offset] = (byte) 0x80;
found++;
}
assertEquals(1, found, "exactly one test safepoint marker");
return new BytecodeModule(module.version(), module.constPool(), module.functions(), code,
module.debugInfo(), module.exports(), module.syscalls(), module.registry(), module.intrinsicSignatures());
}
@Test void arraysAndOptionalReachProductionPbx() throws Exception {
compile("array-cycle", """
declare service Probe {
fn main() -> int {
let count: int = 3;
let values: array int = new array(count);
let index: int = 1;
values[index] = 42;
let answer = values[index] else 0;
values.clear(index);
if let absent = values[index] { return 999; }
return answer;
}
}
""");
}
@Test void literalsAndNestingReachProductionPbx() throws Exception {
compile("array-nested", """
declare service Probe {
fn main() -> int {
let groups: array[2] int = new array(3);
groups[0] = [10, 42];
if let group = groups[0] {
if let value = group[1] { return value; }
}
return 0;
}
}
""");
}
@Test void optionalFunctionAndLazyFallbackReachProductionPbx() throws Exception {
compile("optional-flow", """
declare service Probe {
fn present() -> optional int { return some(42); }
fn absent() -> optional int { return none; }
fn main() -> int {
let value: optional int = Probe.present();
if let answer = value { return answer; }
return Probe.absent() else 0;
}
}
""");
}
@Test void structsConstructorsAndContractsReachProductionPbx() throws Exception {
compile("struct-contract", """
declare contract Target { fn health() -> int; }
declare struct Enemy(pub mut hp: int) {
ctor create(hp: int) { this.hp = hp; }
fn damage(amount: int) { this.hp -= amount; }
fn health() -> int { return this.hp; }
}
implements Target for Enemy using enemy { fn health() -> int { return enemy.health(); } }
declare service Probe {
fn main() -> int {
let enemies: array Enemy = new array(2);
let enemy = new Enemy.create(50);
enemies[0] = enemy;
if let selected = enemies[0] { selected.damage(8); }
let target: Target = enemy;
return target.health();
}
}
""");
}
@Test void serviceContractAndOptionalGroupsReachProductionPbx() throws Exception {
compile("service-contract", """
declare contract Answer { fn value() -> int; }
declare service Source { fn value() -> int { return 42; } }
implements Answer for Source using source { fn value() -> int { return source.value(); } }
declare service Probe {
fn main() -> int {
let first: Answer = Source;
let second: Answer = Source;
return first.value();
}
}
""");
compile("optional-tuple", """
declare service Probe {
fn pair() -> optional (left: int, right: int) { return some((left: 20, right: 22)); }
fn main() -> int {
if let pair = Probe.pair() { return pair.left + pair.right; }
return 0;
}
}
""");
}
@Test void tupleStorageAndOptionalIntrinsicsReachProductionPbx() throws Exception {
compile("tuple-storage", """
declare struct Pair(pub mut value: (left: int, right: int)) {}
declare service Probe {
fn main() -> int {
let values: array (left: int, right: int) = [(left: 20, right: 22)];
if let pair = values[0] {
let holder = new Pair(pair);
holder.value = (left: 21, right: 21);
return holder.value.left + holder.value.right;
}
return 0;
}
}
""");
compile("optional-intrinsics", """
declare service Probe {
fn absent() -> optional int {}
fn main() -> int {
let values: array int = [0];
if values[0].hasSome() {
if Probe.absent().hasNone() { return 42; }
}
return 0;
}
}
""");
compile("optional-lazy", """
declare service Probe {
fn forbidden() -> int {
let empty: array int = [];
return empty[0] else 999;
}
fn main() -> int {
let value: optional int = some(42);
return value else Probe.forbidden();
}
}
""");
}
@Test void contextualArraysAndAliasReachProductionPbx() throws Exception {
compile("array-context", """
declare service Probe {
fn make() -> array int { return [20, 22]; }
fn sum(values: array int) -> int { return (values[0] else 0) + (values[1] else 0); }
fn main() -> int {
let values: array int = { [1, 2] };
let alias = values;
alias[0] = 20;
alias[1] = 22;
let answer = Probe.sum(values);
if answer != Probe.sum([20, 22]) { return 999; }
if answer != Probe.sum(Probe.make()) { return 998; }
return answer;
}
}
""");
compile("binding-scope", """
declare service Probe {
fn main() -> int {
let answer = 42;
let value: optional int = some(8);
if let answer = value { let ignored = answer; }
return answer;
}
}
""");
}
@Test void overloadsAndEvaluationOrderReachProductionPbx() throws Exception {
compile("overloads", """
declare struct Enemy(pub hp: int) {
ctor create(value: int) { this.hp = value; }
ctor create(value: bool) { this.hp = 999; }
fn add(value: int) -> int { return this.hp + value; }
fn add(value: bool) -> int { return 999; }
}
declare service Probe {
fn main() -> int { return new Enemy.create(40).add(2); }
}
""");
compile("single-evaluation", """
declare struct Counter(pub mut count: int) {
fn next() -> int { this.count += 1; return this.count - 1; }
fn present() -> optional int { this.count += 1; return some(42); }
}
declare service Probe {
fn main() -> int {
let counter = new Counter(0);
let values: array int = new array(2);
values[counter.next()] = counter.next();
if counter.count != 2 { return 999; }
if (values[0] else 99) != 1 { return 998; }
if let value = counter.present() {
if counter.count == 3 { return value; }
}
return 997;
}
}
""");
}
@Test void boundsFixturesReachProductionPbx() throws Exception {
for (var entry : java.util.Map.of(
"negative-index", "let a: array int = new array(1); return a[-1] else 42;",
"past-end", "let a: array int = new array(1); return a[1] else 42;",
"negative-length", "let size: int = -1; let a: array int = new array(size); return 42;",
"large-length", "let size: long = 4294967296; let a: array int = new array(size); return 42;"
).entrySet()) compile(entry.getKey(), "declare service Probe { fn main() -> int { " + entry.getValue() + " } }");
compile("long-boundaries", """
declare service Probe {
fn main() -> int {
let size: long = 2;
let a: array int = new array(size);
let first: long = 0;
let last: int = 1;
a[first] = 20;
a[last] = 22;
if a.length != size { return 999; }
return (a[first] else 0) + (a[last] else 0);
}
}
""");
}
@Test void sourceOwnedGroupsSurviveRuntimeSafepoint() throws Exception {
compile("gc-roots", """
declare struct Enemy(pub hp: int) {}
declare service Probe {
fn garbage() { let discarded: array int = new array(1); }
fn gcCollect() { let marker = 314159; }
fn main() -> int {
let keep: array Enemy = [new Enemy(42)];
let tuples: array (left: int, right: int) = [(left: 20, right: 22)];
let names: array str = new array(2);
names[0] = "enemy";
for i: int from 0 until 1100 { Probe.garbage(); }
Probe.gcCollect();
if let unexpected = names[1] { return 997; }
if (names[0] else "missing") != "enemy" { return 996; }
if let enemy = keep[0] {
if let pair = tuples[0] {
if pair.left + pair.right == enemy.hp { return enemy.hp; }
}
}
return 999;
}
}
""");
}
@Test void nativeGroupsAndContractElementsReachProductionPbx() throws Exception {
compile("native-groups", """
[BuiltinType(name = "vec2", version = 1)]
declare builtin type Vector(pub x: float, pub y: float) {}
declare service Probe {
fn main() -> int {
let vectors: array Vector = [new Vector(20.0, 22.0)];
let vector = vectors[0] else new Vector(0.0, 0.0);
return (vector.x + vector.y) as int;
}
}
""");
compile("contract-elements", """
declare contract Target { fn health() -> int; }
declare struct Enemy(pub hp: int) { fn same() -> Self { return this; } }
implements Target for Enemy using enemy { fn health() -> int { return enemy.hp; } }
declare service Probe {
fn main() -> int {
let enemy = new Enemy(42);
let targets: array Target = [enemy as Target];
if let target = targets[0] { return target.health(); }
return 999;
}
}
""");
}
@Test void importedStructAndServiceUseProviderIdentity() throws Exception {
var diagnostics = DiagnosticSink.empty();
String providerSource = """
declare contract Answer { fn answer() -> int; }
declare struct Enemy(pub hp: int) {
ctor create(hp: int) { this.hp = hp; }
fn health() -> int { return this.hp; }
}
implements Answer for Enemy using enemy { fn answer() -> int { return enemy.hp; } }
declare service Source { fn value() -> int { return 42; } }
""";
String clientSource = """
declare service Probe {
fn main() -> int {
let values: array Enemy = [new Enemy.create(42)];
let first = Source;
let second = Source;
if first != second { return 999; }
if let enemy = values[0] {
let target: Answer = enemy as Answer;
if enemy.health() == first.value() { return target.answer(); }
}
return 998;
}
}
""";
var compiler = new PbsFrontendCompiler();
var host = p.studio.compiler.messages.HostAdmissionContext.permissiveDefault();
var surface = p.studio.compiler.messages.FESurfaceContext.empty();
var names = new p.studio.compiler.source.tables.NameTable();
var providerId = new FileId(10);
var providerAst = p.studio.compiler.pbs.parser.PbsParser.parse(
p.studio.compiler.pbs.lexer.PbsLexer.lex(providerSource, providerId, diagnostics), providerId, diagnostics,
p.studio.compiler.pbs.parser.PbsParser.ParseMode.INTERFACE_MODULE);
var modules = ReadOnlyList.from(new p.studio.compiler.source.tables.ModuleReference("test",ReadOnlyList.from("provider")),
new p.studio.compiler.source.tables.ModuleReference("test",ReadOnlyList.from("client")));
var provider = compiler.compileParsedFile(providerId, providerAst, diagnostics, SourceKind.PROJECT,
new ModuleId(0), modules, host, surface, names);
var imported = new java.util.ArrayList<PbsFrontendCompiler.ImportedCallableSurface>();
for (var signature : provider.callableSignatures()) {
var executable = provider.executableFunctions().stream().filter(f -> f.callableName().equals(signature.callableName())).findFirst().orElseThrow();
var type = signature.callableName().endsWith("$instance") ? PbsAst.TypeRef.simple("Source",providerAst.span())
: executable.returnSlots() == 0 ? PbsAst.TypeRef.unit(providerAst.span()) : PbsAst.TypeRef.simple("int",providerAst.span());
imported.add(new PbsFrontendCompiler.ImportedCallableSurface(new ModuleId(0),signature.callableName(), signature.arity(),
executable.returnSlots(), executable.returnSlots()==0?PbsAst.ReturnKind.EXPLICIT_UNIT:PbsAst.ReturnKind.PLAIN, type, signature.typeShape()));
}
var clientId = new FileId(11);
var clientAst = p.studio.compiler.pbs.parser.PbsParser.parse(
p.studio.compiler.pbs.lexer.PbsLexer.lex(clientSource, clientId, diagnostics), clientId, diagnostics,
p.studio.compiler.pbs.parser.PbsParser.ParseMode.INTERFACE_MODULE);
var client = compiler.compileParsedFile(clientId, clientAst, diagnostics, SourceKind.PROJECT, new ModuleId(1),modules,
host,surface,names,providerAst.topDecls(),ReadOnlyList.wrap(imported),ReadOnlyList.empty(),
p.studio.compiler.models.IRReservedMetadata.empty(),java.util.Map.of());
assertFalse(diagnostics.hasErrors(), () -> diagnostics.stream().map(d -> d.getCode()+":"+d.getMessage()).toList().toString());
var aggregator = IRBackend.aggregator();
aggregator.merge(provider); aggregator.merge(client);
var backend = aggregator.entryPointCallableName("Probe.main").entryPointModuleId(new ModuleId(1)).emit();
var program = new LowerToIRVMService().lower(backend);
var module = new BytecodeEmitter().emit(program.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(module);
Files.write(Path.of(System.getProperty("pbs.registryConformanceDirectory"),"pbs-structured-imports.pbx"),module.serialize());
}
@Test void globalArraysAreInitializedBeforeUse() throws Exception {
compile("global-array", """
declare global values: array int = [20, 22];
declare global empty: array int = new array(3);
declare service Probe {
fn main() -> int {
if empty.length != 3 as long { return 999; }
if let unexpected = empty[0] { return 998; }
return (values[0] else 0) + (values[1] else 0);
}
}
""");
}
@Test void installedArrayExampleExecutes() throws Exception {
try (var stream = PbsFrontendCompiler.class.getResourceAsStream("/pbs-knowledge/examples/game/1/src/arrays.pbs")) {
assertNotNull(stream);
var source = new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8);
compile("installed-example",source+"\ndeclare service Probe { fn main() -> int { return arrayExample(); } }");
}
}
@Test void returnOverloadsAndInferredOptionalGroupsExecute() throws Exception {
compile("return-overloads", """
declare struct Box(pub n: int) {
fn get() -> int { return this.n; }
fn get() -> long { return 999; }
fn same() -> Self { return this; }
}
declare service Probe {
fn main() -> int {
let box = new Box(42);
let answer: int = box.same().get();
return answer;
}
}
""");
compile("inferred-optional-tuple", """
declare service Probe {
fn main() -> int {
let value = some((left: 20, right: 22));
if let pair = value { return pair.left + pair.right; }
return 999;
}
}
""");
}
@Test void integerStorageNeedsOnlyTheArrayHandle() throws Exception {
compile("inline-integers", """
declare service Probe {
fn main() -> int {
let values: array int = [10, 20, 30];
return (values.length as int) + 39;
}
}
""");
}
@Test void invalidArraySurfacesAreRejected() {
for(var body : new String[] {
"let values = [1, 2];", "let values = [];", "let values = new array(3);",
"let values: array (optional int) = new array(3);",
"let values: array[2147483647] int = new array(3);", "let values: array[0] int = new array(3);", "let values: array[-1] int = new array(3);",
"let values: array int = new array(3); let x = values[true];",
"let values: array int = new array(3); values[0] = true;",
"if let value = 42 { let x = value; }",
"let value: optional int = some(1); if let found = value {} let bad = found;",
"let value: optional int = some(1); if let found = value {} else { let bad = found; }",
"let values: array int = new array(3); let wrong: array bool = values;"
}) {
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),"fn main() { "+body+" }",diagnostics);
assertTrue(diagnostics.hasErrors(), body);
}
}
}