implements PLN-0122

This commit is contained in:
2026-07-15 13:02:09 +01:00
parent cf14039077
commit f42c83bdca
4 changed files with 205 additions and 3 deletions
@@ -559,6 +559,208 @@ class IRBackendExecutableContractTest {
assertEquals(1, backend.getCallableSignatures().get(1).moduleId().getIndex());
}
@Test
void aggregatorMustEmitDeterministicOrderedPublicContractShape() {
final var first = emitDeterministicAggregateFixture();
final var second = emitDeterministicAggregateFixture();
assertEquals(
List.of("alpha", "beta"),
first.getFunctions().asList().stream().map(IRFunction::name).toList());
assertEquals(
List.of("frameWrapper", "initWrapper"),
first.getSyntheticFunctions().asList().stream().map(IRSyntheticFunction::callableName).toList());
assertEquals(
List.of("state", "timer"),
first.getGlobals().asList().stream().map(IRGlobal::name).toList());
assertEquals(
List.of("frame", "init"),
first.getExecutableFunctions().asList().stream().map(IRBackendExecutableFunction::callableName).toList());
assertEquals(
List.of("app:alpha", "app:beta"),
first.getModulePool().asList().stream()
.map(module -> module.project() + ":" + String.join(".", module.pathSegments().asList()))
.toList());
assertEquals(
List.of("frame", "helper", "init"),
first.getCallableSignatures().asList().stream().map(CallableSignatureRef::callableName).toList());
assertEquals(
List.of("core.draw", "core.color.pack"),
first.getIntrinsicPool().asList().stream().map(IntrinsicReference::canonicalName).toList());
assertEquals(
List.of("draw", "play"),
first.getReservedMetadata().hostMethodBindings().asList().stream()
.map(IRReservedMetadata.HostMethodBinding::abiMethod)
.toList());
assertEquals(
List.of("Sprite", "Sound"),
first.getReservedMetadata().builtinTypeSurfaces().asList().stream()
.map(IRReservedMetadata.BuiltinTypeSurface::sourceTypeName)
.toList());
assertEquals(
List.of("WHITE", "BLACK"),
first.getReservedMetadata().builtinConstSurfaces().asList().stream()
.map(IRReservedMetadata.BuiltinConstSurface::sourceConstName)
.toList());
assertEquals(
List.of("gfx.draw", "audio.play", "input.read"),
first.getReservedMetadata().requiredCapabilities().asList());
assertEquals(first.toString(), second.toString());
assertEquals(
first.getReservedMetadata().requiredCapabilities().asList(),
second.getReservedMetadata().requiredCapabilities().asList());
assertEquals(
first.getExecutableFunctions().get(0).moduleId(),
second.getExecutableFunctions().get(0).moduleId());
assertEquals(
first.getExecutableFunctions().get(1).callableId(),
second.getExecutableFunctions().get(1).callableId());
}
private static IRBackend emitDeterministicAggregateFixture() {
final var aggregator = IRBackend.aggregator();
aggregator.merge(new IRBackendFile(
new FileId(10),
ReadOnlyList.from(new IRFunction(new FileId(10), "alpha", 0, false, new Span(new FileId(10), 0, 5))),
ReadOnlyList.from(new IRSyntheticFunction(
new ModuleId(0),
"frameWrapper",
IRSyntheticCallableKind.PUBLISHED_FRAME_WRAPPER,
new IRSyntheticOrigin(new FileId(10), "frame", new Span(new FileId(10), 0, 5)))),
ReadOnlyList.from(new IRGlobal(
new FileId(10),
new ModuleId(0),
"state",
"int",
new Span(new FileId(10), 1, 2))),
ReadOnlyList.from(new IRBackendExecutableFunction(
new FileId(10),
new ModuleId(0),
"frame",
new CallableId(0),
0,
10,
0,
0,
0,
1,
ReadOnlyList.from(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
new ModuleId(0),
"",
null,
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata("core.draw", 1, new IntrinsicId(0)),
Span.none())),
Span.none())),
new IRReservedMetadata(
ReadOnlyList.from(new IRReservedMetadata.HostMethodBinding(
"Graphics",
"draw",
"gfx",
"draw",
1,
0,
null,
true,
"gfx.draw",
Span.none())),
ReadOnlyList.from(new IRReservedMetadata.BuiltinTypeSurface(
"Sprite",
"core.Sprite",
1,
ReadOnlyList.from(new IRReservedMetadata.BuiltinFieldSurface(
"x",
"int",
0,
Span.none())),
ReadOnlyList.empty(),
Span.none())),
ReadOnlyList.from(new IRReservedMetadata.BuiltinConstSurface(
"WHITE",
"color",
"core.color.white",
1,
Span.none())),
ReadOnlyList.from("gfx.draw", "audio.play")),
ReadOnlyList.from(new ModuleReference("app", ReadOnlyList.from("alpha"))),
ReadOnlyList.from(
new CallableSignatureRef(new ModuleId(0), "frame", 0, "() -> unit"),
new CallableSignatureRef(new ModuleId(0), "helper", 0, "() -> unit")),
ReadOnlyList.from(new IntrinsicReference("core.draw", 1))));
aggregator.merge(new IRBackendFile(
new FileId(20),
ReadOnlyList.from(new IRFunction(new FileId(20), "beta", 1, true, new Span(new FileId(20), 0, 7))),
ReadOnlyList.from(new IRSyntheticFunction(
new ModuleId(0),
"initWrapper",
IRSyntheticCallableKind.PROJECT_INIT,
new IRSyntheticOrigin(new FileId(20), "init", new Span(new FileId(20), 0, 7)))),
ReadOnlyList.from(new IRGlobal(
new FileId(20),
new ModuleId(0),
"timer",
"int",
new Span(new FileId(20), 1, 3))),
ReadOnlyList.from(new IRBackendExecutableFunction(
new FileId(20),
new ModuleId(0),
"init",
new CallableId(0),
0,
8,
0,
0,
0,
1,
ReadOnlyList.from(new IRBackendExecutableFunction.Instruction(
IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
new ModuleId(0),
"",
null,
null,
new IRBackendExecutableFunction.IntrinsicCallMetadata("core.color.pack", 1, new IntrinsicId(0)),
Span.none())),
Span.none())),
new IRReservedMetadata(
ReadOnlyList.from(new IRReservedMetadata.HostMethodBinding(
"Audio",
"play",
"audio",
"play",
1,
0,
null,
true,
"audio.play",
Span.none())),
ReadOnlyList.from(new IRReservedMetadata.BuiltinTypeSurface(
"Sound",
"core.Sound",
1,
ReadOnlyList.empty(),
ReadOnlyList.from(new IRReservedMetadata.IntrinsicSurface(
"volume",
"core.sound.volume",
1,
1,
1,
Span.none())),
Span.none())),
ReadOnlyList.from(new IRReservedMetadata.BuiltinConstSurface(
"BLACK",
"color",
"core.color.black",
1,
Span.none())),
ReadOnlyList.from("gfx.draw", "input.read")),
ReadOnlyList.from(new ModuleReference("app", ReadOnlyList.from("beta"))),
ReadOnlyList.from(new CallableSignatureRef(new ModuleId(0), "init", 0, "() -> unit")),
ReadOnlyList.from(new IntrinsicReference("core.color.pack", 1))));
return aggregator.emit();
}
private static void inspectType(
final String owner,
final Type type,