feat(pbs): implement first-class functions and explicit captures
This commit is contained in:
+238
@@ -0,0 +1,238 @@
|
||||
package p.studio.compiler.backend.irvm;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PbsClosuresTest {
|
||||
@Test void namedAndAnonymousFunctionsShareSignature() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-functions", """
|
||||
fn twice(x: int) -> int { return x * 2; }
|
||||
fn invoke(x: int, f: Fn(int) -> int) -> int { return f(x); }
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let f: Fn(int) -> int = twice;
|
||||
let g = fn [](x: int) -> int { return x + 2; };
|
||||
return invoke(20, f) + g(0);
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void capturesSnapshotAndEscape() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-snapshot", """
|
||||
fn create(n: int) -> Fn(int) -> int {
|
||||
let factor = n;
|
||||
let f = fn [factor](x: int) -> int { return x * factor; };
|
||||
factor = 100;
|
||||
return f;
|
||||
}
|
||||
declare service Probe {
|
||||
fn main() -> int { let f = create(3); return f(14); }
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void closuresShareObjectsAndTravelThroughArrays() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-objects", """
|
||||
declare struct Counter(pub mut value: int);
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let counter = new Counter(40);
|
||||
let increment = fn [counter]() -> void { counter.value += 1; };
|
||||
let read = fn [counter]() -> int { return counter.value; };
|
||||
let reads: array (Fn() -> int) = [read];
|
||||
increment(); increment();
|
||||
if let f = reads[0] { return f(); }
|
||||
return 999;
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void nestedClosuresCaptureFunctions() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-nested", """
|
||||
fn create(n: int) -> Fn() -> Fn(int) -> int {
|
||||
let add = fn [n](x: int) -> int { return n + x; };
|
||||
return fn [add]() -> Fn(int) -> int { return add; };
|
||||
}
|
||||
declare service Probe {
|
||||
fn main() -> int { let factory = create(20); let f = factory(); return f(22); }
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void capturedGroupsAndContractReceiverPreserveTheirLayouts() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-groups", """
|
||||
declare contract Answer { fn get() -> int; }
|
||||
declare struct Box(pub value: int) { fn get() -> int { return this.value; } }
|
||||
implements Answer for Box using box { fn get() -> int { return box.get(); } }
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let pair: (left: int, right: int) = (20, 21);
|
||||
let target: Answer = new Box(1);
|
||||
let f = fn [pair, target]() -> (left: int, right: int) {
|
||||
return (pair.left, pair.right + target.get());
|
||||
};
|
||||
let value = f();
|
||||
return value.left + value.right;
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void separateFactoriesKeepIndependentState() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-isolation", """
|
||||
declare struct Counter(pub mut n: int);
|
||||
fn create(start: int) -> Fn() -> int {
|
||||
let state = new Counter(start);
|
||||
return fn [state]() -> int { state.n += 1; return state.n; };
|
||||
}
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let a = create(10);
|
||||
let b = create(18);
|
||||
let first = a();
|
||||
let second = a();
|
||||
return first + second + b();
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
@Test void functionFieldsAndOverloadsUseExpectedSignature() throws Exception {
|
||||
PbsStructuredValuesTest.compile("closure-fields", """
|
||||
fn value(x: int) -> int { return x + 2; }
|
||||
fn value(x: bool) -> int { return 999; }
|
||||
declare struct Behavior(pub run: Fn(int) -> int);
|
||||
declare service Probe {
|
||||
fn main() -> int {
|
||||
let behavior = new Behavior(value);
|
||||
return behavior.run(40);
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
@Test void importedFunctionValuesLinkToProvider() throws Exception {
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var compiler = new PbsFrontendCompiler();
|
||||
var names = new p.studio.compiler.source.tables.NameTable();
|
||||
var host = p.studio.compiler.messages.HostAdmissionContext.permissiveDefault();
|
||||
var surface = p.studio.compiler.messages.FESurfaceContext.empty();
|
||||
var modules = p.studio.utilities.structures.ReadOnlyList.from(
|
||||
new p.studio.compiler.source.tables.ModuleReference("test",p.studio.utilities.structures.ReadOnlyList.from("provider")),
|
||||
new p.studio.compiler.source.tables.ModuleReference("test",p.studio.utilities.structures.ReadOnlyList.from("client")));
|
||||
var providerId = new FileId(10);
|
||||
var providerAst = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(
|
||||
"fn twice(x: int) -> int { return x * 2; }",providerId,diagnostics),providerId,diagnostics);
|
||||
var provider = compiler.compileParsedFile(providerId,providerAst,diagnostics,p.studio.compiler.models.SourceKind.PROJECT,
|
||||
new p.studio.compiler.source.identifiers.ModuleId(0),modules,host,surface,names);
|
||||
var function = providerAst.functions().getFirst();
|
||||
var signature = provider.callableSignatures().getFirst();
|
||||
var imported = new PbsFrontendCompiler.ImportedCallableSurface(new p.studio.compiler.source.identifiers.ModuleId(0),
|
||||
signature.callableName(),signature.arity(),1,function.returnKind(),function.returnType(),signature.typeShape());
|
||||
var clientId = new FileId(11);
|
||||
var source = "declare service Probe { fn main() -> int { let f: Fn(int) -> int = twice; return f(21); } }";
|
||||
var ast = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(source,clientId,diagnostics),clientId,diagnostics);
|
||||
var client = compiler.compileParsedFile(clientId,ast,diagnostics,p.studio.compiler.models.SourceKind.PROJECT,
|
||||
new p.studio.compiler.source.identifiers.ModuleId(1),modules,host,surface,names,providerAst.topDecls(),
|
||||
p.studio.utilities.structures.ReadOnlyList.from(imported),p.studio.utilities.structures.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 = p.studio.compiler.models.IRBackend.aggregator();
|
||||
aggregator.merge(provider); aggregator.merge(client);
|
||||
var backend = aggregator.entryPointCallableName("Probe.main").entryPointModuleId(new p.studio.compiler.source.identifiers.ModuleId(1)).emit();
|
||||
var program = new LowerToIRVMService().lower(backend);
|
||||
var output = new p.studio.compiler.backend.bytecode.BytecodeEmitter().emit(program.coherentEmissionPlan());
|
||||
new p.studio.compiler.backend.bytecode.BytecodePreloadVerifierService().verify(output);
|
||||
java.nio.file.Files.write(java.nio.file.Path.of(System.getProperty("pbs.registryConformanceDirectory"),"pbs-structured-closure-import.pbx"),output.serialize());
|
||||
}
|
||||
|
||||
@Test void installedExampleProducesExecutableClosures() throws Exception {
|
||||
try(var resource = PbsFrontendCompiler.class.getResourceAsStream("/pbs-knowledge/examples/game/1/src/closures.pbs")) {
|
||||
assertNotNull(resource);
|
||||
var source = new String(resource.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8);
|
||||
PbsStructuredValuesTest.compile("closure-installed", source + "\ndeclare service Probe { fn main() -> int { return closureExample(); } }");
|
||||
}
|
||||
}
|
||||
|
||||
@Test void closuresSurviveExistingFrameLifecycleAndAreCollectible() throws Exception {
|
||||
var source = """
|
||||
declare struct Counter(pub mut value: int);
|
||||
declare global callbacks: array (Fn() -> int) = new array(1);
|
||||
declare global frames: int = 0;
|
||||
[Frame]
|
||||
fn frame() -> void {
|
||||
if frames == 0 {
|
||||
let counter = new Counter(40);
|
||||
callbacks[0] = fn [counter]() -> int {
|
||||
counter.value += 1;
|
||||
return counter.value;
|
||||
};
|
||||
} else {
|
||||
if let action = callbacks[0] {
|
||||
let first = action();
|
||||
let second = action();
|
||||
if first != 41 { callbacks[-1] = action; }
|
||||
if second != 42 { callbacks[-1] = action; }
|
||||
callbacks.clear(0);
|
||||
}
|
||||
}
|
||||
for i: int from 0 until 1200 {
|
||||
let discarded = fn [i]() -> int { return i; };
|
||||
}
|
||||
frames += 1;
|
||||
}
|
||||
""";
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
var id = new FileId(0);
|
||||
var moduleId = new p.studio.compiler.source.identifiers.ModuleId(0);
|
||||
var ast = p.studio.compiler.pbs.parser.PbsParser.parse(p.studio.compiler.pbs.lexer.PbsLexer.lex(source,id,diagnostics),id,diagnostics);
|
||||
var modules = p.studio.utilities.structures.ReadOnlyList.from(new p.studio.compiler.source.tables.ModuleReference("test",
|
||||
p.studio.utilities.structures.ReadOnlyList.from("main")));
|
||||
var file = new PbsFrontendCompiler().compileParsedFile(id,ast,diagnostics,p.studio.compiler.models.SourceKind.PROJECT,
|
||||
moduleId,modules,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 aggregator = p.studio.compiler.models.IRBackend.aggregator();
|
||||
aggregator.merge(file);
|
||||
var backend = aggregator.lifecycleModuleOrder(p.studio.utilities.structures.ReadOnlyList.from(moduleId)).emit();
|
||||
var issues = p.studio.compiler.messages.BuildingIssueSink.empty();
|
||||
var assembled = new p.studio.compiler.lifecycle.LifecycleAssemblerService().assemble(backend,issues);
|
||||
assertFalse(issues.hasErrors(), issues.asCollection().toString());
|
||||
var program = new LowerToIRVMService().lower(assembled);
|
||||
var output = new p.studio.compiler.backend.bytecode.BytecodeEmitter().emit(program.coherentEmissionPlan());
|
||||
new p.studio.compiler.backend.bytecode.BytecodePreloadVerifierService().verify(output);
|
||||
java.nio.file.Files.write(java.nio.file.Path.of(System.getProperty("pbs.registryConformanceDirectory"),"closure-frame-lifecycle.pbx"),output.serialize());
|
||||
}
|
||||
|
||||
@Test void retiredCallbackDeclarationsAndExportsReject() {
|
||||
var source = "declare callback Tick(x: int) -> int; fn probe() -> void {}";
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0),source,diagnostics);
|
||||
assertTrue(diagnostics.hasErrors());
|
||||
var barrelDiagnostics = DiagnosticSink.empty();
|
||||
var id = new FileId(0);
|
||||
p.studio.compiler.pbs.parser.PbsBarrelParser.parse(
|
||||
p.studio.compiler.pbs.lexer.PbsLexer.lex("pub callback Tick;",id,barrelDiagnostics),id,barrelDiagnostics);
|
||||
assertTrue(barrelDiagnostics.hasErrors());
|
||||
}
|
||||
|
||||
@Test void invalidCapturesAndSignaturesReject() {
|
||||
for (var body : new String[] {
|
||||
"declare callback Old() -> void;",
|
||||
"let x = bind(ctx, handler);",
|
||||
"let n = 1; let f = fn []() -> int { return n; };",
|
||||
"let n = 1; let f = fn [n]() -> void { n = 2; };",
|
||||
"let f = fn [missing]() -> int { return missing; };",
|
||||
"let n = 1; let f = fn [n,n]() -> int { return n; };",
|
||||
"let f: Fn(int) -> bool = fn [](x: int) -> int { return x; };",
|
||||
"let f = fn (x: int) -> int { return x; };",
|
||||
"let f = fn [](x: int) { return x; };",
|
||||
"let f = fn [](x) -> int { return x; };",
|
||||
"let f = fn [](x: int) -> int { x };",
|
||||
"let f = fn [](x: int) -> int { return x; }; let x = f(true);"
|
||||
}) {
|
||||
var diagnostics = DiagnosticSink.empty();
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), "fn probe() { " + body + " }", diagnostics);
|
||||
assertTrue(diagnostics.hasErrors(), body);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -15,7 +15,7 @@ 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 {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user