implements PLN-0101
This commit is contained in:
+111
@@ -4,6 +4,8 @@ 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.IRBackendExecutableFunction;
|
||||
import p.studio.compiler.models.IRBackendFile;
|
||||
import p.studio.compiler.models.IRHiddenGlobalKind;
|
||||
import p.studio.compiler.models.IRGlobalVisibility;
|
||||
import p.studio.compiler.models.IRSyntheticCallableKind;
|
||||
@@ -52,6 +54,57 @@ class PbsFrontendCompilerTest {
|
||||
assertEquals(1, functions.get(1).parameterCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotLowerDocMetadataIntoRuntimeBackendArtifacts() {
|
||||
final var undocumentedSource = """
|
||||
declare global SCORE: int = 0;
|
||||
|
||||
fn add_one(value: int) -> int {
|
||||
return value + 1;
|
||||
}
|
||||
|
||||
[Init]
|
||||
fn boot() -> void {
|
||||
SCORE = add_one(41);
|
||||
return;
|
||||
}
|
||||
""";
|
||||
final var marker = "THIS_DOC_PAYLOAD_MUST_NOT_APPEAR_IN_RUNTIME_ARTIFACTS";
|
||||
final var documentedSource = """
|
||||
declare global SCORE: int = 0;
|
||||
|
||||
[Doc(markdown = \"""
|
||||
Adds one to a value.
|
||||
|
||||
This deliberately large documentation payload is editor metadata only:
|
||||
THIS_DOC_PAYLOAD_MUST_NOT_APPEAR_IN_RUNTIME_ARTIFACTS.
|
||||
\""")]
|
||||
fn add_one(value: int) -> int {
|
||||
return value + 1;
|
||||
}
|
||||
|
||||
[Doc(markdown = \"""
|
||||
Initializes the score global.
|
||||
\""")]
|
||||
[Init]
|
||||
fn boot() -> void {
|
||||
SCORE = add_one(41);
|
||||
return;
|
||||
}
|
||||
""";
|
||||
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
final var undocumentedDiagnostics = DiagnosticSink.empty();
|
||||
final var documentedDiagnostics = DiagnosticSink.empty();
|
||||
final var undocumentedBackend = compiler.compileFile(new FileId(20), undocumentedSource, undocumentedDiagnostics);
|
||||
final var documentedBackend = compiler.compileFile(new FileId(20), documentedSource, documentedDiagnostics);
|
||||
|
||||
assertTrue(undocumentedDiagnostics.isEmpty(), diagnosticsSummary(undocumentedDiagnostics));
|
||||
assertTrue(documentedDiagnostics.isEmpty(), diagnosticsSummary(documentedDiagnostics));
|
||||
assertEquals(runtimeFingerprint(undocumentedBackend), runtimeFingerprint(documentedBackend));
|
||||
assertFalse(String.valueOf(documentedBackend).contains(marker));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExposeGlobalsAndSyntheticLifecycleArtifactsInBackendFile() {
|
||||
final var source = """
|
||||
@@ -812,4 +865,62 @@ class PbsFrontendCompilerTest {
|
||||
assertEquals(1L, pixel.fields().get(1).slotStart());
|
||||
assertEquals(2L, pixel.fields().get(2).slotStart());
|
||||
}
|
||||
|
||||
private static String runtimeFingerprint(final IRBackendFile backend) {
|
||||
final var out = new StringBuilder();
|
||||
out.append("functions:");
|
||||
backend.functions().forEach(function -> out
|
||||
.append(function.name()).append('/')
|
||||
.append(function.parameterCount()).append('/')
|
||||
.append(function.hasExplicitReturnType()).append(';'));
|
||||
out.append("synthetic:");
|
||||
backend.syntheticFunctions().forEach(function -> out
|
||||
.append(function.moduleId()).append('/')
|
||||
.append(function.callableName()).append('/')
|
||||
.append(function.kind()).append(';'));
|
||||
out.append("globals:");
|
||||
backend.globals().forEach(global -> out
|
||||
.append(global.moduleId()).append('/')
|
||||
.append(global.name()).append('/')
|
||||
.append(global.declaredTypeSurface()).append('/')
|
||||
.append(global.slot()).append('/')
|
||||
.append(global.visibility()).append('/')
|
||||
.append(global.hiddenKind()).append(';'));
|
||||
out.append("executables:");
|
||||
backend.executableFunctions().forEach(function -> {
|
||||
out.append(function.moduleId()).append('/')
|
||||
.append(function.callableName()).append('/')
|
||||
.append(function.callableId().getIndex()).append('/')
|
||||
.append(function.paramSlots()).append('/')
|
||||
.append(function.localSlots()).append('/')
|
||||
.append(function.returnSlots()).append('/')
|
||||
.append(function.maxStackSlots()).append('{');
|
||||
function.instructions().forEach(instruction -> appendInstructionFingerprint(out, instruction));
|
||||
out.append("};");
|
||||
});
|
||||
out.append("modules:").append(backend.modulePool()).append(';');
|
||||
out.append("callables:").append(backend.callableSignatures()).append(';');
|
||||
out.append("intrinsics:").append(backend.intrinsicPool()).append(';');
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private static void appendInstructionFingerprint(
|
||||
final StringBuilder out,
|
||||
final IRBackendExecutableFunction.Instruction instruction) {
|
||||
out.append(instruction.kind()).append('(')
|
||||
.append(instruction.calleeModuleId()).append(',')
|
||||
.append(instruction.calleeCallableName()).append(',')
|
||||
.append(instruction.calleeCallableId() == null ? "" : instruction.calleeCallableId().getIndex()).append(',')
|
||||
.append(instruction.hostCall()).append(',')
|
||||
.append(instruction.intrinsicCall()).append(',')
|
||||
.append(instruction.label()).append(',')
|
||||
.append(instruction.targetLabel()).append(',')
|
||||
.append(instruction.expectedArgSlots()).append(',')
|
||||
.append(instruction.expectedRetSlots())
|
||||
.append(");");
|
||||
}
|
||||
|
||||
private static String diagnosticsSummary(final DiagnosticSink diagnostics) {
|
||||
return diagnostics.stream().map(d -> d.getCode() + ":" + d.getMessage()).toList().toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user