implements PLN-0008
This commit is contained in:
+65
-2
@@ -9,12 +9,18 @@ import p.studio.compiler.models.IRGlobalVisibility;
|
||||
import p.studio.compiler.models.IRSyntheticCallableKind;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.lexer.LexErrors;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticPhase;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.identifiers.ModuleId;
|
||||
import p.studio.compiler.source.tables.NameTable;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -433,7 +439,8 @@ class PbsFrontendCompilerTest {
|
||||
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);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
@@ -462,7 +469,8 @@ class PbsFrontendCompilerTest {
|
||||
.anyMatch(h -> h.ownerName().equals("LowAssets")
|
||||
&& h.abiModule().equals("asset")
|
||||
&& h.abiMethod().equals("load")
|
||||
&& h.abiVersion() == 1));
|
||||
&& h.abiVersion() == 1
|
||||
&& java.util.Objects.equals(h.assetLoweringParam(), 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -493,6 +501,61 @@ class PbsFrontendCompilerTest {
|
||||
&& "37".equals(i.label())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRewriteAssetLoweringHostParameterToAssetId() {
|
||||
final var interfaceSource = """
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
}
|
||||
""";
|
||||
final var source = """
|
||||
fn boot() -> (status: int, loading_handle: int) {
|
||||
return LowAssets.load(assets.ui.panel, 2);
|
||||
}
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var interfaceBackend = compiler.compileFile(new FileId(16), interfaceSource, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
final var sourceFileId = new FileId(17);
|
||||
final var sourceTokens = PbsLexer.lex(source, sourceFileId, diagnostics);
|
||||
final var sourceAst = PbsParser.parse(sourceTokens, sourceFileId, diagnostics);
|
||||
final var interfaceTokens = PbsLexer.lex(interfaceSource, new FileId(18), diagnostics);
|
||||
final var interfaceAst = PbsParser.parse(interfaceTokens, new FileId(18), diagnostics, PbsParser.ParseMode.INTERFACE_MODULE);
|
||||
final var fileBackend = compiler.compileParsedFile(
|
||||
sourceFileId,
|
||||
sourceAst,
|
||||
diagnostics,
|
||||
SourceKind.PROJECT,
|
||||
ModuleId.none(),
|
||||
ReadOnlyList.empty(),
|
||||
HostAdmissionContext.permissiveDefault(),
|
||||
new FESurfaceContext(ReadOnlyList.from(new Addressable("assets.ui.panel", 55))),
|
||||
new NameTable(),
|
||||
interfaceAst.topDecls(),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.empty(),
|
||||
interfaceBackend.reservedMetadata(),
|
||||
Map.of());
|
||||
|
||||
assertEquals(0, diagnostics.errorCount(), 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
|
||||
&& "55".equals(i.label())));
|
||||
assertTrue(executableBoot.instructions().stream().anyMatch(i ->
|
||||
i.kind() == p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.CALL_HOST
|
||||
&& i.hostCall() != null
|
||||
&& "asset".equals(i.hostCall().module())
|
||||
&& "load".equals(i.hostCall().name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectUnresolvedBackendOwnedAssetReference() {
|
||||
final var source = """
|
||||
|
||||
+4
-2
@@ -332,7 +332,8 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
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);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
@@ -361,8 +362,9 @@ class PbsGateUSdkInterfaceConformanceTest {
|
||||
assertEquals("asset", backend.reservedMetadata().requiredCapabilities().getFirst());
|
||||
assertTrue(backend.reservedMetadata().hostMethodBindings().stream()
|
||||
.anyMatch(h -> h.ownerName().equals("LowAssets")
|
||||
&& java.util.Objects.equals(h.assetLoweringParam(), 0)
|
||||
&& h.abiModule().equals("asset")
|
||||
&& h.abiMethod().equals("cancel")
|
||||
&& h.abiMethod().equals("load")
|
||||
&& h.abiVersion() == 1));
|
||||
}
|
||||
|
||||
|
||||
+20
-1
@@ -126,7 +126,8 @@ class PbsInterfaceModuleSemanticsTest {
|
||||
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);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
|
||||
[Host(module = "asset", name = "status", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
@@ -140,6 +141,24 @@ class PbsInterfaceModuleSemanticsTest {
|
||||
assertTrue(diagnostics.isEmpty(), diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectMalformedAssetLoweringOnHostSurface() {
|
||||
final var source = """
|
||||
declare host LowAssets {
|
||||
[Host(module = "asset", name = "load", version = 1)]
|
||||
[Capability(name = "asset")]
|
||||
[AssetLowering(param = 1)]
|
||||
fn load(addressable: Addressable) -> int;
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(5), source, diagnostics, SourceKind.SDK_INTERFACE);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_MALFORMED_RESERVED_ATTRIBUTE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectBuiltinFieldWithNonAdmissibleLayoutType() {
|
||||
final var source = """
|
||||
|
||||
+2
-1
@@ -69,7 +69,8 @@ class InterfaceModuleLoaderTest {
|
||||
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);
|
||||
[AssetLowering(param = 0)]
|
||||
fn load(addressable: Addressable, slot: int) -> (status: int, loading_handle: int);
|
||||
}
|
||||
"""))),
|
||||
"pub host LowAssets;");
|
||||
|
||||
+2
-1
@@ -950,7 +950,8 @@ class PBSFrontendPhaseServiceTest {
|
||||
assertTrue(irBackend.getReservedMetadata().hostMethodBindings().stream()
|
||||
.anyMatch(h -> h.ownerName().equals("LowAssets")
|
||||
&& h.sourceMethodName().equals("load")
|
||||
&& h.abiModule().equals("asset")));
|
||||
&& h.abiModule().equals("asset")
|
||||
&& java.util.Objects.equals(h.assetLoweringParam(), 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user