feat(pbs): implement first-class functions and explicit captures
This commit is contained in:
@@ -7,7 +7,7 @@ From the Studio root:
|
||||
|
||||
```sh
|
||||
./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*LinkedRegistryTest' --tests '*PbxRegistryConformanceTest' --tests '*TypedCommonPipelineTest' --tests '*RuntimeCatalogPipelineTest'
|
||||
./gradlew :prometeu-architecture-tests:test --tests '*PbsRuntimeAlignmentTest' --tests '*PbsStructuredValuesTest'
|
||||
./gradlew :prometeu-architecture-tests:test --tests '*PbsRuntimeAlignmentTest' --tests '*PbsStructuredValuesTest' --tests '*PbsClosuresTest'
|
||||
python3 tools/pbx-conformance/run.py --runtime ../runtime
|
||||
```
|
||||
|
||||
@@ -112,3 +112,22 @@ nested references, tuples and strings remain readable. `resource-diagnostics`
|
||||
is enabled solely for observation. Default production ceilings and GC threshold
|
||||
are unchanged outside the explicitly lowered quota runs. This is semantic and
|
||||
resource conformance, not a performance benchmark or installed game qualification.
|
||||
|
||||
## PBS first-class functions and closure lifetime
|
||||
|
||||
`PbsClosuresTest` adds nine mandatory source/load/execute fixtures: named and
|
||||
anonymous functions, snapshots, shared objects/arrays, nested function captures,
|
||||
complete tuple groups and contract receivers, independent environments, Fn fields
|
||||
and overload selection, imported functions, and the installed example. Each
|
||||
returns 42. Six additional GuestBytes/HandleSlots runs at limits 0, 1 and 2 check
|
||||
closure allocation faults and session ownership, bringing structured quota runs
|
||||
to 24.
|
||||
|
||||
The tenth fixture, `closure-frame-lifecycle.pbx`, uses the existing production
|
||||
lifecycle assembler without bytecode patching. Its first frame stores a closure
|
||||
capturing an object; the next frame invokes it twice (41, then 42) and drops the
|
||||
reference. Each of three frames also discards 1,200 closures. The harness asserts
|
||||
threshold collections, retained closure/object reachability after frame one,
|
||||
and reclamation after release. The remaining root is the global array.
|
||||
PBS publishes FRAME_ROOT and the existing guarded wrapper returns normally.
|
||||
No sync syntax, new frame integration, suspension or coroutine is introduced.
|
||||
|
||||
@@ -40,6 +40,7 @@ fn main() {
|
||||
verify_pbs_fixtures(&root);
|
||||
verify_pbs_structured_fixtures(&root);
|
||||
verify_pbs_resource_refusals(&root);
|
||||
verify_pbs_closure_frames(&root);
|
||||
for name in [
|
||||
"minimal",
|
||||
"jump-end",
|
||||
@@ -542,7 +543,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
|
||||
|
||||
fn verify_pbs_structured_fixtures(root: &std::path::Path) {
|
||||
use prometeu_vm::LogicalFrameEndingReason as End;
|
||||
let success = ["array-cycle", "array-nested", "optional-flow", "struct-contract", "service-contract",
|
||||
let success = ["closure-import", "closure-installed", "closure-groups", "closure-isolation", "closure-fields", "closure-functions", "closure-snapshot", "closure-objects", "closure-nested", "array-cycle", "array-nested", "optional-flow", "struct-contract", "service-contract",
|
||||
"optional-tuple", "tuple-storage", "optional-intrinsics", "optional-lazy", "array-context",
|
||||
"binding-scope", "overloads", "single-evaluation", "long-boundaries", "gc-roots", "native-groups", "contract-elements", "imports", "global-array", "installed-example", "return-overloads", "inferred-optional-tuple", "inline-integers"];
|
||||
for name in success.into_iter().chain(["negative-index", "past-end", "negative-length", "large-length"]) {
|
||||
@@ -587,7 +588,7 @@ fn verify_pbs_structured_fixtures(root: &std::path::Path) {
|
||||
|
||||
fn verify_pbs_resource_refusals(root: &std::path::Path) {
|
||||
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits, RuntimeResource}};
|
||||
for name in ["array-cycle", "tuple-storage", "struct-contract"] {
|
||||
for name in ["closure-objects", "array-cycle", "tuple-storage", "struct-contract"] {
|
||||
for resource in [RuntimeResource::GuestBytes, RuntimeResource::HandleSlots] {
|
||||
for limit in [0, 1, 2] {
|
||||
let bytes = std::fs::read(root.join(format!("pbs-structured-{name}.pbx"))).unwrap();
|
||||
@@ -610,3 +611,22 @@ fn verify_pbs_resource_refusals(root: &std::path::Path) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_pbs_closure_frames(root: &std::path::Path) {
|
||||
use prometeu_vm::LogicalFrameEndingReason as End;
|
||||
let bytes = std::fs::read(root.join("closure-frame-lifecycle.pbx")).expect("mandatory closure frame fixture");
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE).unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
for frame in 0..3 {
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let report = vm.run_budget(2_000_000, &mut NoNative, &mut HostContext::new(None)).unwrap();
|
||||
assert!(matches!(report.reason, End::Halted | End::EndOfRom), "frame {frame}: {report:?}");
|
||||
assert!(vm.operand_stack_top(1).is_empty(), "frame root returns void");
|
||||
let snapshot = vm.resource_snapshot();
|
||||
assert!(snapshot.gc.collections >= frame + 1, "frame {frame}: {:?}", snapshot.gc);
|
||||
assert!(snapshot.gc.reclaimed >= (frame + 1) * 1200, "frame {frame}: {:?}", snapshot.gc);
|
||||
if frame == 0 { assert!(vm.heap().len() >= 3, "array, closure and capture survive root return"); }
|
||||
else { assert_eq!(vm.heap().len(), 1, "only the empty global array remains after closure release"); }
|
||||
}
|
||||
println!("PBS CLOSURE FRAME LIFECYCLE/RETENTION/GC PASS");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user