implement PBS low-level asset host surface

This commit is contained in:
2026-03-27 16:13:20 +00:00
parent 6150493e95
commit bd0b1924c2
14 changed files with 841 additions and 2 deletions
@@ -0,0 +1,17 @@
declare host LowAssets {
[Host(module = "asset", name = "load", version = 1)]
[Capability(name = "asset")]
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
[Host(module = "asset", name = "status", version = 1)]
[Capability(name = "asset")]
fn status(loading_handle: int) -> int;
[Host(module = "asset", name = "commit", version = 1)]
[Capability(name = "asset")]
fn commit(loading_handle: int) -> int;
[Host(module = "asset", name = "cancel", version = 1)]
[Capability(name = "asset")]
fn cancel(loading_handle: int) -> int;
}
@@ -420,6 +420,44 @@ class PbsFrontendCompilerTest {
assertEquals(1, fileBackend.reservedMetadata().builtinTypeSurfaces().getFirst().fields().size());
}
@Test
void shouldExtractLowAssetsReservedMetadataForInterfaceModuleSurface() {
final var source = """
declare host LowAssets {
[Host(module = "asset", name = "load", version = 1)]
[Capability(name = "asset")]
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
[Host(module = "asset", name = "status", version = 1)]
[Capability(name = "asset")]
fn status(loading_handle: int) -> int;
[Host(module = "asset", name = "commit", version = 1)]
[Capability(name = "asset")]
fn commit(loading_handle: int) -> int;
[Host(module = "asset", name = "cancel", version = 1)]
[Capability(name = "asset")]
fn cancel(loading_handle: int) -> int;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var compiler = new PbsFrontendCompiler();
final var fileBackend = compiler.compileFile(new FileId(11), source, diagnostics, SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(0, fileBackend.functions().size());
assertEquals(4, fileBackend.reservedMetadata().hostMethodBindings().size());
assertEquals(1, fileBackend.reservedMetadata().requiredCapabilities().size());
assertEquals("asset", fileBackend.reservedMetadata().requiredCapabilities().getFirst());
assertTrue(fileBackend.reservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowAssets")
&& h.abiModule().equals("asset")
&& h.abiMethod().equals("load")
&& h.abiVersion() == 1));
}
@Test
void shouldRejectHostBindingWithoutCapabilityAtHostAdmission() {
final var source = """
@@ -87,10 +87,11 @@ class PbsGateUSdkInterfaceConformanceTest {
import { Color } from @core:color;
import { Gfx } from @sdk:gfx;
import { Input, InputPad, InputButton } from @sdk:input;
import { LowAssets } from @sdk:asset;
import { Log } from @sdk:log;
declare contract Renderer {
fn render(gfx: Gfx, color: Color, input: Input, pad: InputPad, button: InputButton, log: Log) -> void;
fn render(gfx: Gfx, color: Color, input: Input, pad: InputPad, button: InputButton, assets: LowAssets, log: Log) -> void;
}
""",
"pub contract Renderer;",
@@ -122,6 +123,11 @@ class PbsGateUSdkInterfaceConformanceTest {
&& h.abiModule().equals("log")
&& h.abiMethod().equals("write_tag")
&& h.abiVersion() == 1));
assertTrue(positive.irBackend().getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowAssets")
&& h.abiModule().equals("asset")
&& h.abiMethod().equals("load")
&& h.abiVersion() == 1));
final var negative = compileWorkspaceModule(
tempDir.resolve("gate-u-reserved-import-negative"),
@@ -320,6 +326,46 @@ class PbsGateUSdkInterfaceConformanceTest {
DiagnosticPhase.HOST_ADMISSION);
}
@Test
void gateU_shouldAcceptLowAssetsInterfaceSurfaceAndExposeCanonicalAssetBindings() {
final var source = """
declare host LowAssets {
[Host(module = "asset", name = "load", version = 1)]
[Capability(name = "asset")]
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
[Host(module = "asset", name = "status", version = 1)]
[Capability(name = "asset")]
fn status(loading_handle: int) -> int;
[Host(module = "asset", name = "commit", version = 1)]
[Capability(name = "asset")]
fn commit(loading_handle: int) -> int;
[Host(module = "asset", name = "cancel", version = 1)]
[Capability(name = "asset")]
fn cancel(loading_handle: int) -> int;
}
""";
final var diagnostics = DiagnosticSink.empty();
final var backend = new PbsFrontendCompiler().compileFile(
new FileId(43),
source,
diagnostics,
SourceKind.SDK_INTERFACE);
assertFalse(diagnostics.hasErrors(), diagnostics.stream().map(Object::toString).toList().toString());
assertEquals(4, backend.reservedMetadata().hostMethodBindings().size());
assertEquals(1, backend.reservedMetadata().requiredCapabilities().size());
assertEquals("asset", backend.reservedMetadata().requiredCapabilities().getFirst());
assertTrue(backend.reservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowAssets")
&& h.abiModule().equals("asset")
&& h.abiMethod().equals("cancel")
&& h.abiVersion() == 1));
}
private WorkspaceCompileResult compileWorkspaceModule(
final Path projectRoot,
final String sourceContent,
@@ -120,6 +120,26 @@ class PbsInterfaceModuleSemanticsTest {
d.getCode().equals(PbsSemanticsErrors.E_SEM_INTERFACE_NON_DECLARATIVE_DECLARATION.name())));
}
@Test
void shouldAcceptLowAssetsHostSurfaceInInterfaceModule() {
final var source = """
declare host LowAssets {
[Host(module = "asset", name = "load", version = 1)]
[Capability(name = "asset")]
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
[Host(module = "asset", name = "status", version = 1)]
[Capability(name = "asset")]
fn status(loading_handle: int) -> int;
}
""";
final var diagnostics = DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(4), source, diagnostics, SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
}
@Test
void shouldRejectBuiltinFieldWithNonAdmissibleLayoutType() {
final var source = """
@@ -48,4 +48,39 @@ class InterfaceModuleLoaderTest {
"pub host Gfx;",
fileTable.get(module.barrelFiles().getFirst().fileId()).readUtf8().orElseThrow());
}
@Test
void shouldLoadAssetStdlibModuleWithPublicHostBarrel() {
final var projectTable = new ProjectTable();
final var projectId = projectTable.register(ProjectDescriptor.builder()
.name("app")
.version("1.0.0")
.rootPath(Path.of("/tmp/app"))
.sourceRoots(ReadOnlyList.wrap(java.util.List.of(Path.of("/tmp/app/src"))))
.build());
final var fileTable = new FileTable(1);
final var diagnostics = DiagnosticSink.empty();
final var moduleSource = new StdlibModuleSource(
"sdk",
ReadOnlyList.wrap(java.util.List.of("asset")),
ReadOnlyList.wrap(java.util.List.of(new StdlibModuleSource.SourceFile(
"main.pbs",
"""
declare host LowAssets {
[Host(module = "asset", name = "load", version = 1)]
[Capability(name = "asset")]
fn load(asset_id: int, slot: int) -> (status: int, loading_handle: int);
}
"""))),
"pub host LowAssets;");
final var module = new InterfaceModuleLoader().load(moduleSource, projectId, fileTable, diagnostics);
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
assertEquals(1, module.sourceFiles().size());
assertEquals(1, module.barrelFiles().size());
assertEquals(
"pub host LowAssets;",
fileTable.get(module.barrelFiles().getFirst().fileId()).readUtf8().orElseThrow());
}
}
@@ -783,6 +783,61 @@ class PBSFrontendPhaseServiceTest {
.anyMatch(h -> h.ownerName().equals("LowGfx") && h.sourceMethodName().equals("clear")));
}
@Test
void shouldResolveSdkAssetImportFromBootstrapStdlib() throws IOException {
final var projectRoot = tempDir.resolve("project-bootstrap-sdk-asset");
final var sourceRoot = projectRoot.resolve("src");
final var modulePath = sourceRoot.resolve("app");
Files.createDirectories(modulePath);
final var sourceFile = modulePath.resolve("source.pbs");
final var modBarrel = modulePath.resolve("mod.barrel");
Files.writeString(sourceFile, """
import { LowAssets } from @sdk:asset;
declare contract Loader {
fn load(assets: LowAssets) -> void;
}
""");
Files.writeString(modBarrel, "pub contract Loader;");
final var projectTable = new ProjectTable();
final var fileTable = new FileTable(1);
final var projectId = projectTable.register(ProjectDescriptor.builder()
.rootPath(projectRoot)
.name("app")
.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);
final var diagnostics = DiagnosticSink.empty();
final var irBackend = new PBSFrontendPhaseService().compile(
ctx,
diagnostics,
LogAggregator.empty(),
BuildingIssueSink.empty());
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_MODULE_NOT_FOUND.name())));
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_UNRESOLVED.name())));
assertTrue(diagnostics.stream().noneMatch(d ->
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_NOT_PUBLIC.name())));
assertEquals(0, irBackend.getFunctions().size());
assertTrue(irBackend.getReservedMetadata().hostMethodBindings().stream()
.anyMatch(h -> h.ownerName().equals("LowAssets")
&& h.sourceMethodName().equals("load")
&& h.abiModule().equals("asset")));
}
@Test
void shouldLowerSdkGfxServiceFacadeCallsWithoutHostCallableAmbiguity() throws IOException {
final var projectRoot = tempDir.resolve("project-bootstrap-sdk-gfx-facade-call");