implements PLN-0006

This commit is contained in:
2026-03-27 17:16:34 +00:00
parent e0c57814e1
commit 4a6cc35989
20 changed files with 430 additions and 14 deletions
@@ -1,6 +1,8 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.messages.Addressable;
import p.studio.compiler.messages.FESurfaceContext;
import p.studio.compiler.messages.HostAdmissionContext;
import p.studio.compiler.models.IRHiddenGlobalKind;
import p.studio.compiler.models.IRGlobalVisibility;
@@ -458,6 +460,58 @@ class PbsFrontendCompilerTest {
&& h.abiVersion() == 1));
}
@Test
void shouldLowerBackendOwnedAssetReferenceToAssetId() {
final var source = """
fn boot() -> Addressable {
return assets.ui.panel;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(
new FileId(12),
source,
diagnostics,
SourceKind.PROJECT,
HostAdmissionContext.permissiveDefault(),
new FESurfaceContext(ReadOnlyList.from(new Addressable("assets.ui.panel", 37))));
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
final var executableBoot = fileBackend.executableFunctions().stream()
.filter(fn -> fn.callableName().equals("boot"))
.findFirst()
.orElseThrow();
assertTrue(executableBoot.instructions().stream().anyMatch(i ->
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_I32
&& "37".equals(i.label())));
}
@Test
void shouldRejectUnresolvedBackendOwnedAssetReference() {
final var source = """
fn boot() -> Addressable {
return assets.ui.missing;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(
new FileId(13),
source,
diagnostics,
SourceKind.PROJECT,
HostAdmissionContext.permissiveDefault(),
new FESurfaceContext(ReadOnlyList.from(new Addressable("assets.ui.panel", 37))));
assertTrue(diagnostics.stream().anyMatch(d ->
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_MEMBER_ACCESS.name())
&& d.getMessage().contains("assets.ui.missing")));
assertEquals(0, fileBackend.executableFunctions().size());
}
@Test
void shouldRejectHostBindingWithoutCapabilityAtHostAdmission() {
final var source = """
@@ -2,7 +2,9 @@ package p.studio.compiler.services;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.Addressable;
import p.studio.compiler.messages.BuildingIssueSink;
import p.studio.compiler.messages.FESurfaceContext;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.IRGlobalVisibility;
import p.studio.compiler.models.BuildStack;
@@ -40,6 +42,61 @@ class PBSFrontendPhaseServiceTest {
@TempDir
Path tempDir;
@Test
void shouldPropagateBackendOwnedAssetSurfaceIntoPbsFrontendCompilation() throws IOException {
final var projectRoot = tempDir.resolve("project-assets");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("main");
Files.createDirectories(modulePath);
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
Files.writeString(sourceFile, """
[Frame]
fn frame() -> void {
let asset: Addressable = assets.ui.panel;
return;
}
""");
Files.writeString(modBarrel, "pub fn frame() -> void;");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("core")
.version("1.0.0")
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
.build());
registerFile(projectId, projectRoot, sourceFile, fileTable);
registerFile(projectId, projectRoot, modBarrel, fileTable);
final var ctx = new FrontendPhaseContext(
projectTable,
fileTable,
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
1,
p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),
new FESurfaceContext(ReadOnlyList.from(new Addressable("assets.ui.panel", 91))));
final var diagnostics = DiagnosticSink.empty();
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
final var frameFn = irBackend.getExecutableFunctions().stream()
.filter(function -> "frame".equals(function.callableName()))
.findFirst()
.orElseThrow();
assertTrue(frameFn.instructions().stream().anyMatch(instruction ->
instruction.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.PUSH_I32
&& "91".equals(instruction.label())));
}
@Test
void shouldReportInvalidBarrelFilename() throws IOException {
final var projectRoot = tempDir.resolve("project");