feat(pbs): add linked collections and bidirectional snapshots

Implement SDK linked<T> with forward and reverse Cursor snapshots, trusted compiler integration, and neutral FIELDS_PUBLISH producer support. Pin the tested runtime revision and include specs, editor and installed knowledge coverage, conformance fixtures, and DSC-0091 lessons and evidence.
This commit is contained in:
2026-10-23 10:28:44 +01:00
parent c410e35001
commit 77aada6259
38 changed files with 3040 additions and 57 deletions
@@ -0,0 +1,223 @@
---
id: LSN-0091
ticket: pbs-sdk-linked
title: Private Linked Nodes and Atomic Publication
created: 2026-10-23
tags: [compiler-pbs, collections, snapshot-cursor, atomic-publication, runtime-conformance]
---
## Original problem and final result
Owner: `compiler/pbs`. Transversal owners: `compiler/general` for neutral IR/PBX
and the provider for typed storage, faults, ownership, GC and resource accounting.
DSC-0091 executed accepted DEC-0076 through PLN-0154, covering R1–R12. Their
workflow control artifacts are disposable; the [Linked specification](../../../docs/compiler/pbs/specs/sdk-linked-and-cursor.md)
is the durable normative reference.
PBS needed a dense sequence with private doubly linked nodes and useful end
operations, while preserving the shared snapshot traversal already delivered for
List. `linked<T>` now supplies that sequence through executable SDK source.
Forward and reverse captures both return the existing Cursor<T> contract. A
neutral FIELDS_PUBLISH operation provides complete publication across the collection
and its neighbors, without moving node algorithms into the VM.
The selected provider is clean commit `57992cf214e8a5f9de53a40dba9aaa7fab038c91`.
Studio changes were qualified over base `c410e3500150de5e6d55c1da8f9b22076ca84251`.
[Machine-readable evidence](runtime-conformance-evidence.json) records source/PBX
hashes, commands, configuration, lowered refusal limits and observed GC states.
## Dense positions do not change the optional API
A Linked contains one present value per private node. Its size counts nodes;
capacity, holes, reserve, trim, resize and clearAt do not exist. Valid indexed
reads/removals return a present optional, and invalid indices fault even when
empty. First/last and their removal operations return none for an empty collection.
This preserves the accepted API rather than changing return types because a
particular representation cannot contain holes.
Construction is empty and takes no capacity. Explicit and contextual applications
follow the existing generic struct rules. Directly optional elements are rejected,
including through grouping, just as in Array/List. An optional collection and an
otherwise valid element containing optional fields remain valid.
The executable API includes size, first/last, bracket read/write, add/addFirst,
insert, removeAt/removeFirst/removeLast, clear and both capture methods. Adding
appends, while addFirst prepends. Replacement preserves size; insertion/removal
changes following logical indices. Read/replace/remove require an index below
size; insert also accepts size. Long indices are not truncated, int arguments have
the same localized lossless promotion as List, and growth is checked before signed
long overflow without inheriting Array's structural size maximum.
Preserving inserted object identity is separate from choosing a comparison policy.
There is no remove(value), generic equality or uniqueness rule in this delivery.
Repeated occurrences, including repeated references to one instance, remain
independent sequence positions. List similarly leaves value search/removal outside
its current contract.
## Keep collection topology separate from traversal topology
Linked nodes have reciprocal previous/next links. The SDK locates indexed values
from the nearer end, so search remains O(n) in the worst case; end operations do
not search. A constant number of link updates does not make indexed access O(1).
Snapshot nodes are different objects: private immutable singly linked nodes with
no circular links or editable source association. Forward capture walks Linked
backwards and prepends; reverse capture walks it forwards and prepends. Both cost
O(n) work and additional nodes, and both finish before returning a Cursor.
For a source `[20, 22]`, forward starts at 20 and reverse starts at 22. Replacing
source values, removing elements or clearing the collection cannot change either
captured sequence. Referenced objects remain shared, so later field mutations
remain visible. Snapshots preserve occurrences and object identity, not deep copies.
The existing state table remains the only traversal protocol:
| State | hasNext() | current() | next() |
| --- | --- | --- | --- |
| Empty | false | none | No change |
| Current node has a successor | true | Present current value | Follow successor |
| Last node, including a singleton | false | Present final value | No change |
Process current before testing whether to advance. A hasNext-only loop omits the
last element; waiting for current to become absent never finishes a nonempty
snapshot. There is no removal/reset through Cursor.
The [distributed example](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/linked.pbs)
captures both orders, then replaces a source element and clears Linked. Both
traversals still sum to 42, retaining final values 22 and 20 respectively.
## Prepare every object before changing any object
Insertion can update the collection's head/tail/size and two existing neighbors.
Removal also prepares the returned optional value. Separate successful field
stores are insufficient: a later allocation or frame/operand refusal could leave
half of a neighbor pair updated.
The existing range-based STORAGE_PUBLISH operation covered an Array and two
fields of one owner. Its guarantee did not automatically extend to Linked.
The new generic FIELDS_PUBLISH uses a normal Struct TypeId as a schema: each
schema field names one destination Struct type. The stack supplies each nullable
destination followed by all its complete field groups. No request object, new
Registry category or public preparation token is needed.
All non-null destinations must have the exact concrete type and live provenance.
All supplied value groups are validated, including those paired with a null
(skipped) destination. The original operands stay rooted and unconsumed until the
whole attempt is admitted. The VM then commits in schema order without allocation,
validation, callbacks or GC/scheduler interleaving, and consumes the operands.
Repeated destinations have ordered last-write semantics using pre-captured inputs.
The SDK selects topology and values. It allocates and initializes a private
candidate node before publishing an insertion. Removal prepares its optional
result first. Private helper bindings lower those source-selected values to the
neutral operation. A refusal preserves the old roots, size, values and reciprocal
links; already evaluated caller argument effects remain ordinary source effects.
Publication itself needs no heap object or scratch. This does not make a source
mutation allocation-free: optional carriers, candidate nodes, packing and call
preparation still have their normal costs. Provider tests execute publication at
already exhausted allocation ceilings, preserve native String/reference identity
and multi-slot groups, and reject a late invalid group before every shared write.
## GC proofs require an actual collection, not only a safepoint
Removal disconnects a node from the collection. Clear removes the collection's
roots to the whole chain. Unreachable double-link cycles are handled by the normal
tracing collector; neither operation promises immediate collection or quota return.
A retained snapshot owns captured values through its own nodes and does not need
the editable Linked nodes.
A controlled fixture uses normal GC safepoints and independent non-owning storage
observations. With no snapshot, nodes and payloads fall from 1,200 to 1,197 after
three removals, then to zero after clear. A second fixture clears the source while
retaining its snapshot: original Linked nodes disappear, captured nodes/items go
from 1,200/1,200 to 1/1 after traversal, stay at 1/1 after repeated next, and reach
0/0 after the final Cursor owner is dropped.
A safepoint alone does not force this collector to run. The removal/clear fixture
therefore creates ordinary garbage pressure and asserts that the collection
counter advances at each observed round. Without that check, a test can mistake
an uncollected unreachable node for a retention bug. The observer introduces no
guest root or mutation.
## Compiler, tooling and distribution
Linked reuses ordinary generic specialization, methods, field groups and imports.
Origin-checked SDK metadata adds constructor/index integration and the same
localized int-to-long argument promotion as List. User homonyms or copied
attributes do not acquire those privileges.
Public projections show concrete Linked methods, optional results and source SDK
locations. They hide head/tail/length, private nodes and storage adapters, including
the internal nearest-end lookup method. Negative workspace cases reject optional
elements, capacity construction, unsupported APIs and private access. Invalid
constructor selection now reports diagnostics instead of throwing from lowering.
The normative English spec and example ship with installed knowledge. An isolated
installDist JVM, outside the checkout, loads the actual JAR resources and compiles
the Linked example. The production harness executes its emitted PBX and observes
both completed traversals. Imports/reexports are qualified separately.
The full harness also needed its original scalar/input corpus selector to exclude
the new Linked fixture prefix. Linked cases remain mandatory in their dedicated
validator; they are not silently treated as older scalar fixtures or skipped.
## Qualification and limits
| Gate | Observed result |
| --- | --- |
| Full Studio regression | 1,030 tests; no failures/errors; 13 existing skips |
| Installed knowledge | Both tests passed, including standalone Linked PBX generation |
| Provider bytecode/VM libraries | 6 + 305 passed; 2 existing measurement tests ignored |
| Linked source to production VM | 26 cases in each producer mode: 52 passed |
| Neutral storage/publication | Four common-IR fixtures executed, including ordinary/optimized FIELDS_PUBLISH |
| Whole-operation refusals | 231; all ten mutation/capture boundaries preserved previous source state |
| Linked removal/clear GC | 1,200 → 1,197 → 0 → 0 nodes and payloads |
| Snapshot GC | 1,200 → 1 → 1 → 0 captured nodes/payloads, with no retained Linked topology |
| Imports/installed traversal | Passed; installed forward/reverse final values 22/20 |
| Catalogs and canonical mirrors | Clean selected provider; catalog/exporter parity and all four mirrors match |
The refusal matrix observed GuestBytes, HandleSlots and AuxiliaryBytes refusals.
TemporaryBytes was checked against the measured setup/success peaks; no additional
peak required a lowered refusal case in this Linked corpus. FIELDS_PUBLISH has no
scratch allocation, and the direct provider test proves publication succeeds with
TemporaryBytes already capped at current usage. This is not a claim that general
source preparation never needs temporary resources.
Qualification used macOS 27.0.1 arm64, Rust 1.98.1/LLVM 22.1.8, the dev runtime
profile with resource-diagnostics, capabilities NONE, Gradle 9.3.1 and the Java 21
toolchain; the installed JVM was Corretto 21.0.4. No release or cross-platform claim
is made. Two pre-existing full-harness response-schema gaps, LifecycleRole and
NonFiniteValue, remain explicitly reported with original payloads.
## Pitfalls to preserve
- Do not implement a multi-object mutation as a sequence of fallible stores.
- Prepare the optional removed-value carrier before publishing the removal.
- Preserve every field group and native/reference identity, including unchanged
payloads when updating neighbor links.
- Never expose source nodes to support traversal. Snapshot and collection links
have different mutability and lifetime responsibilities.
- Keep optional results despite dense positions, and faults distinct from absence.
- Keep private SDK authority tied to origin through aliases and specialization.
- Check GC collection counters when asserting reclamation at a safepoint.
- Record clean provider provenance, explicit lowered limits and mandatory fixture
counts; metadata defaults or small unit tests alone do not qualify execution.
## References and reproduction
- [Linked specification](../../../docs/compiler/pbs/specs/sdk-linked-and-cursor.md),
[List/shared Cursor](../../../docs/compiler/pbs/specs/sdk-list-and-cursor.md),
[generic structs](../../../docs/compiler/pbs/specs/generic-structs-and-free-functions.md)
and [generic Registry/PBX publication](../../../docs/compiler/general/specs/typed-registry-and-pbx-v0.md).
- [SDK source](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs),
[source corpus](../../../prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsLinkedTest.java),
[neutral producer](../../../prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/registry/StoragePublicationPipelineTest.java)
and [production harness instructions](../../../tools/pbx-conformance/README.md).
- [Provider publication implementation](../../../../runtime/crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs)
and [previous List/Cursor lesson](../DSC-0090-pbs-sdk-list/LSN-0090-mutable-lists-and-stable-snapshot-cursors.md).
Run `./gradlew test :prometeu-app:installedKnowledgeTest --console=plain`, followed
by `python3 tools/pbx-conformance/run.py --runtime ../runtime`, after selecting the
recorded provider. The evidence file retains command/configuration details and
R1–R12 traceability after removal of the completed workflow controls.
File diff suppressed because it is too large Load Diff
@@ -244,3 +244,37 @@ before mutation. A refused preparation still charges the attempted work. Neither
operation is a safepoint. Consumers must prepare any potentially failing return
carrier and all call operands before publication; a series of ordinary stores is
not a substitute for whole-operation safety.
## Atomic publication of multiple Struct instances
FIELDS_PUBLISH (`0xAB`) has one little-endian u32 schema TypeId immediate. The
schema MUST be a Registry Struct whose ordered field types are all Struct types.
Names are descriptive only. The schema is declarative metadata; no request object,
new Registry category or preparation token is allocated or exposed.
For each schema entry, the operand stack supplies one nullable destination
reference followed by every destination field's complete typed value group, in
field declaration order. The stack effect is the sum, over entries, of one plus
the destination instance's initialization-slot count; no results are produced.
Null skips that destination, but every supplied field group is still validated.
Non-null destinations require exact concrete Struct identity and a live reference
of the current session/generation. Array, Trait, Fn and builtin schema entries are
invalid, including in unreachable instructions. The full schema and exact stack
height/groups are checked under normal Registry admission/work limits.
Validate all destinations and value groups before any shared write or operand
consumption. The original operands remain roots throughout preparation. Then write
all fields in schema order, without allocation, fallible validation, callbacks,
GC or scheduler interleaving, and consume the entire input. Input values are
captured before publication; repeated destinations use ordered last-write
semantics. Complete multi-slot/native/reference groups retain identity and normal
ownership. A failed attempt preserves every destination and the operand stack.
The operation allocates no publication scratch or guest object. Operand/frame and
source value preparation remain normally charged and may fail before publication.
Base cost is 6 cycles. After the stack-height check, precharge one cycle per schema
entry and one per destination field execution slot for the complete declared
attempt, including skipped destinations. Quotas are unchanged.
This is a generic typed storage mechanism; collection algorithms and node topology
remain source-owned, with no collection-specific VM instruction or syscall.
@@ -0,0 +1,127 @@
# SDK Linked and bidirectional snapshot capture
Owner: `compiler/pbs`. Linked algorithms belong to executable PBS SDK source.
Multi-object publication, storage, faults, ownership, GC and quotas remain generic
compiler/provider mechanisms. This contract builds on [generic structs](generic-structs-and-free-functions.md),
[arrays and optional](typed-arrays-and-optional-execution.md) and the
[shared Cursor protocol](sdk-list-and-cursor.md#cursor-protocol).
## Type, identity and construction
The collections SDK exports `linked<T>`. It is an ordinary invariant generic
struct with stable reference identity: aliases observe mutations of the same
collection. `new linked<T>()` creates an empty collection. Contextual construction
follows generic struct rules, requiring a concrete expected application; constructor
values do not infer T. There is no capacity argument.
A directly optional element, including through grouping, is invalid.
`optional (linked<T>)` remains valid, as do otherwise valid element types containing
optional fields. Values retain their complete groups and normal identities,
including tuples, native values, arrays, functions and contract views. No public
Dynamic, universal Element or wrapper node is introduced.
Storage consists of private doubly linked nodes, each containing one present
value. Logical size equals node count. Empty collections have no head or tail;
one-node collections have the same head and tail. Neighbor links are reciprocal,
forward and backward traversals agree, and there are no extra predecessor or
successor nodes beyond the ends. Nodes and private helpers are not a public API.
## Operations and bounds
| Operation | Result | Behavior |
| --- | --- | --- |
| `size()` | long | Number of elements |
| `first()` / `last()` | optional T | End value, or none when empty |
| `xs[i]` | optional T | Present value at a valid index |
| `xs[i] = value` | unit | Replace a valid position without changing size |
| `add(value)` | unit | Append |
| `addFirst(value)` | unit | Prepend |
| `insert(i, value)` | unit | Insert at `0 <= i <= size` |
| `removeAt(i)` | optional T | Remove and return a valid position |
| `removeFirst()` / `removeLast()` | optional T | Remove and return end value, or none when empty |
| `clear()` | unit | Remove all elements and set size to zero |
| `cursor()` | Cursor<T> | Capture first to last |
| `reverseCursor()` | Cursor<T> | Capture last to first |
Reading, replacing and removing by index require `0 <= i < size`; invalid indices
raise TRAP_OOB even when empty. Insertion accepts size, including zero in an empty
collection. Other insertion indices raise TRAP_OOB. Every valid position contains
an element, so indexed read/removal returns a present optional. Zero and false are
present values. Empty-end results do not suppress type or resource faults.
Insertion/removal changes the logical indices of following elements. Replacement
preserves size. There are no holes, clearAt, capacity, reserve, trim or resize.
Duplicate occurrences are preserved; no equality or uniqueness rule is imposed.
Value search/removal is outside this API, consistently with List.
Size is nonnegative long. Indices accept int and long with localized lossless
promotion, without general numeric conversion or truncation. The SDK checks the
current size against the maximum signed long before incrementing, using the generic
logical guard; an unrepresentable next size is rejected before arithmetic or
mutation. Array's u32 structural maximum is not a Linked size limit. Provider
quotas and applicable structural limits can refuse allocation much earlier.
Receiver/arguments evaluate once in source order, including named application.
## Snapshots and costs
Both capture methods return the existing Cursor<T> contract and preserve its
complete state table. A nonempty cursor starts on its first captured value;
hasNext asks whether a successor exists; next without a successor does nothing.
Only a snapshot born empty has an absent current. Repeated next retains the final
value. Capturing `[A, B, C]` produces `[A, B, C]` or `[C, B, A]` respectively.
Each capture completes before return and owns private immutable singly linked,
non-circular snapshot nodes, independent of the collection's doubly linked nodes.
It uses no snapshot array or live association with editable source nodes.
Subsequent insertion, replacement, removal and clear cannot change the captured
sequence. Shared objects retain identity and later field changes remain visible.
There is no removal/reset through Cursor or public node access.
End operations locate their nodes without traversal. Indexed operations search
from the nearer end and remain O(n) in the worst case. Both captures cost O(n)
work and O(n) additional nodes; next follows one link in O(1). Allocation,
optional-carrier preparation, ownership and eventual GC retain their normal
costs. Repeated indexed reads are not an efficient traversal strategy.
## Complete mutation attempts and lifetime
Before publication, prepare bounds, next size, candidate nodes, optional results,
all field values and call/operand resources. Private generic FIELDS_PUBLISH
publishes complete field groups of source-selected objects after validating all
receivers and groups. Null destinations represent missing neighbors. Algorithms
select the new head/tail/size and neighbor fields in SDK source; the VM has no
Linked opcode or syscall.
No fallible allocation, callback, scheduler or GC boundary may expose partial
publication. A refused mutation preserves all previous head/tail/size values,
elements and neighbor links. This includes optional return preparation for
removals and mutations without new nodes. Individually atomic stores do not prove
whole-operation atomicity. Previously evaluated argument effects are ordinary
source effects and are not rolled back.
Unpublished candidates follow normal ownership and quotas. Capture failure returns
no partial Cursor and does not change the source. Removal and clear release
collection references to discarded nodes and payloads. Doubly linked cycles are
collected by normal tracing when unreachable; no immediate reclamation is promised.
Snapshots retain payloads, not the editable source topology; consumed prefixes
become collectible while the last current value stays retained until released.
## Conformance and distribution
Qualification executes real PBS through common IR/PBX and the production verifier
and VM, with ordinary and optimized producer paths. Cover the full API in empty,
single and multi-node states, reciprocal links and size, nearest-end lookup, all
bounds, int/long and single/named evaluation, complete groups and identity,
optional-element diagnostics, both Cursor orders/states and mutation independence.
Independently observe all original fields, link carriers, nodes and groups around
quota refusals, including node/result/operand preparation. Exercise ordinary GC
safepoints for removed nodes/values, clear, snapshots and prefix/final release.
Generic publication also requires non-PBS producers and malformed/dead operand
coverage. Regress shared Array/List/generic/contract/Cursor behavior and editor/LSP
projections. Ship the specification and executable example with installed SDK
knowledge, compile the actual packaged example outside the checkout and execute
its emitted PBX with the production provider.
See [generic Registry/PBX publication](../../general/specs/typed-registry-and-pbx-v0.md)
for the language-neutral instruction contract.
@@ -152,11 +152,12 @@ faults. List itself rejects directly optional elements.
## Reuse, costs and conformance
The shared implementation is independent of List representation. Future Linked<T>
and Set<T> producers use Cursor<T>; Map<K,V> uses Cursor<keyval<K,V>>. Set/Map gain
The shared implementation is independent of List representation.
[Linked](sdk-linked-and-cursor.md) uses Cursor<T> for forward and reverse captures.
Future Set<T> producers use Cursor<T>; Map<K,V> uses Cursor<keyval<K,V>>. Set/Map gain
no ordering guarantee between captures. A captured keyval association is read-only
and does not follow later association replacement; referenced objects stay shared.
Those collections and keyval are separate deliveries.
Set/Map and keyval are separate deliveries.
For n logical List positions and k present occurrences, capture takes O(n) work
and O(k) additional nodes; next follows one link in O(1). Each node costs allocation,
+9
View File
@@ -202,3 +202,12 @@ The [Program Registry contract](../specs/runtime/02c-program-registry-and-pbx-v0
## VM-owned random core
See the [random contract](../specs/runtime/02e-vm-owned-random-core.md) for Random BuiltinId 0x0016, ObjectKind tag 9, intrinsics 0x4000–0x4003, atomic state mutation, bounded 4+8*N costs and rejection-limit trap 0x00000015. Random is managed by the VM heap and existing GC, without host entropy or PBX changes.
## Atomic publication of multiple Struct instances
FIELDS_PUBLISH (0xAB) atomically publishes complete typed fields to multiple
source-selected Struct destinations. Its schema is an ordinary Registry Struct;
no new category, request object, syscall or safepoint is introduced. Exact nullable
destination rules, stack effects, validation, costs and commit semantics are in the
[canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#atomic-publication-of-multiple-struct-instances).
+9
View File
@@ -198,3 +198,12 @@ STORAGE_PUBLISH (0xAA) provide generic logical validation, complete stored-group
transfer and prepared atomic publication. Their encoding, typed operands, exact
faults, overlap semantics, costs and preflight/commit guarantees are defined by
the [canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#logical-guards-and-atomic-storage-publication). No new Registry category, syscall, collection algorithm or safepoint is introduced.
## Atomic publication of multiple Struct instances
FIELDS_PUBLISH (0xAB) atomically publishes complete typed fields to multiple
source-selected Struct destinations. Its schema is an ordinary Registry Struct;
no new category, request object, syscall or safepoint is introduced. Exact nullable
destination rules, stack effects, validation, costs and commit semantics are in the
[canonical instruction contract](../specs/runtime/02-vm-instruction-set.md#atomic-publication-of-multiple-struct-instances).
+1 -1
View File
@@ -1,3 +1,3 @@
runtime.revision=d9b9eeb171d2861ec660d4cf5703b0795eac49a9
runtime.revision=57992cf214e8a5f9de53a40dba9aaa7fab038c91
INTRINSICS.csv.sha256=0c1bb78a9b81d1e3480ef5713f3d70f0b06772a7dfab2fd00c1f0a6364f98daa
BUILTIN_CONSTANTS.csv.sha256=e407e4718127aa7ef7b1097b5b4b67d86dd979235e73a551a7c36b628cddc039
@@ -49,6 +49,8 @@ class CompilerProjectKnowledgeTest {
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:quickstart")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:spec:sdk-list-and-cursor.md")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:example:game:1:src/collections.pbs")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:spec:sdk-linked-and-cursor.md")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:example:game:1:src/linked.pbs")));
assertTrue(language.referenceIds().contains("pbs:quickstart"));
assertFalse(result.limitations().stream().anyMatch(value -> value.contains("DOCUMENTATION_UNAVAILABLE")));
assertFalse(Files.exists(root.resolve(".workspace")));
@@ -29,6 +29,8 @@ public final class InstalledKnowledgeProbe {
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/generics.pbs")), "Missing installed generic struct/function example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:spec:sdk-list-and-cursor.md")), "Missing installed List/Cursor specification");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/collections.pbs")), "Missing installed List/Cursor example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:spec:sdk-linked-and-cursor.md")), "Missing installed Linked specification");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/linked.pbs")), "Missing installed Linked example");
for (var reference : docs) {
if (reference.id().startsWith("pbs:example:game:1:")) {
Path file = root.resolve(reference.id().substring("pbs:example:game:1:".length()));
@@ -75,6 +77,20 @@ public final class InstalledKnowledgeProbe {
Path fixtures=Path.of(System.getProperty("pbs.registryConformanceDirectory"));
Files.createDirectories(fixtures);Files.write(fixtures.resolve("pbs-list-installed.pbx"),listResult.bytecodeBytes());
System.out.println("Installed List/Cursor execution fixture generated");
Path linkedRoot=Files.createDirectory(Path.of("linked-example")).toRealPath();
Files.createDirectories(linkedRoot.resolve("src"));
Files.copy(root.resolve("prometeu.json"),linkedRoot.resolve("prometeu.json"));
Files.writeString(linkedRoot.resolve("src/mod.barrel"),"pub fn frame() -> void;");
Files.writeString(linkedRoot.resolve("src/main.pbs"),Files.readString(root.resolve("src/linked.pbs"))+"\n"+"""
[Frame] fn frame() -> void {
if linkedExample()!=42 {let failure=1/0;}
}
""");
var linkedResult=BuilderPipelineService.INSTANCE.compile(
BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false,linkedRoot.toString())),LogAggregator.stdout());
check(linkedResult.diagnostics().stream().noneMatch(issue->issue.isError()),"Installed Linked example must compile");
Files.write(fixtures.resolve("pbs-linked-installed.pbx"),linkedResult.bytecodeBytes());
System.out.println("Installed Linked execution fixture generated");
verifyExternalSources(provider.getClass().getClassLoader());
registration.unsubscribe();
System.out.println("Installed knowledge verified: " + docs.size() + " references; example compiled");
@@ -16,6 +16,7 @@ class InstalledKnowledgeTest {
assertTrue(log.contains("references; example compiled"), log);
assertTrue(log.contains("Packaged external sources verified"), log);
assertTrue(log.contains("Installed List/Cursor execution fixture generated"), log);
assertTrue(log.contains("Installed Linked execution fixture generated"), log);
}
@Test void installedCompilerAndSdkCarryTheSameVerifiedCatalog() throws Exception {
@@ -0,0 +1,274 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
class PbsLinkedTest {
static void compile(String name, String source) throws Exception {
PbsListCursorTest.compileCollection("linked", name, source);
}
static void probe(String name, String body) throws Exception {
compile(name,"declare service Probe {fn main()->int {"+body+"}}");
}
@Test void allOperationsPreserveBothDirectionsAndLogicalSize() throws Exception {
probe("basic","""
let values:linked<int>=new linked();let alias=values;
if values.first().hasSome() or values.last().hasSome() {return 0;}
if values.removeFirst().hasSome() or values.removeLast().hasSome() {return 0;}
values.clear();values.add(20);values.addFirst(1);values.insert(1,2);
values.insert(3,22);values[1]=42;
if (alias.size() as int)!=4 {return 0;}
if (values.first() else 0)!=1 or (values.last() else 0)!=22 {return 0;}
if (values.removeAt(1) else 0)!=42 {return 0;}
if (values.removeFirst() else 0)!=1 {return 0;}
if (values.removeLast() else 0)!=22 {return 0;}
if (values.first() else 0)!=20 or (values.last() else 0)!=20 {return 0;}
if (values.removeLast() else 0)!=20 {return 0;}
if (values.size() as int)!=0 or values.first().hasSome() {return 0;}
values.addFirst(42);if (values.removeFirst() else 0)!=42 {return 0;}
values.add(7);values.clear();values.add(42);return alias[0] else 0;
""");
probe("links","""
let values=new linked<int>();
for i:int from 0 until 16 {values.add(i);}
values.insert(8,90);values.insert(0,91);values.insert(18,92);
values.removeAt(9);values.removeFirst();values.removeLast();
let forward=values.cursor();let backward=values.reverseCursor();
for i:int from 0 until 16 {
if (values[i] else -1)!=i {return 0;}
if (forward.current() else -1)!=i {return 0;}
if (backward.current() else -1)!=(15-i) {return 0;}
forward.next();backward.next();
}
if forward.hasNext() or backward.hasNext() {return 0;}
if (values.size() as int)!=16 {return 0;}return 42;
""");
probe("construction","""
let values:linked<int>=new linked();
let diamond:linked<int>=new linked<>();
let maybe:optional (linked<int>)=some(new linked());
if let owner=maybe {if (owner.size() as int)!=0 {return 0;}} else {return 0;}
let index:int=0;values.insert(index,42);let at:long=0;return values[at] else 0;
""");
probe("long-size","""
let maximum:long=9223372036854775807;
if _linkedNextSize(maximum-1)!=maximum {return 0;}
let beyondArray:long=4294967296;
if _linkedNextSize(beyondArray-1)!=beyondArray {return 0;}
return 42;
""");
}
@Test void snapshotsHaveTheSharedStatesAndStayIndependentInBothOrders() throws Exception {
for (var method:new String[]{"cursor","reverseCursor"}) {
probe("empty-"+method,"let values=new linked<int>();let cursor=values."+method+"();values.add(42);cursor.next();cursor.next();if cursor.hasNext() or cursor.current().hasSome() {return 0;}return 42;");
probe("single-"+method,"let values=new linked<int>();values.add(42);let cursor=values."+method+"();values.clear();cursor.next();cursor.next();if cursor.hasNext() {return 0;}return cursor.current() else 0;");
}
probe("snapshot","""
let values=new linked<int>();values.add(20);values.add(22);values.add(20);
let forward=values.cursor();let reverse=values.reverseCursor();
values[0]=99;values.insert(1,7);values.removeAt(2);values.addFirst(3);
values.removeFirst();values.removeLast();values.clear();values.add(100);
if (forward.current() else 0)!=20 or (reverse.current() else 0)!=20 {return 0;}
if forward.hasNext()==false or reverse.hasNext()==false {return 0;}
forward.next();reverse.next();
if (forward.current() else 0)!=22 or (reverse.current() else 0)!=22 {return 0;}
forward.next();reverse.next();forward.next();reverse.next();
if forward.hasNext() or reverse.hasNext() {return 0;}
return (forward.current() else 0)+(reverse.current() else 0)+2;
""");
probe("false-present","""
let values=new linked<bool>();values.add(false);
let cursor=values.reverseCursor();values.clear();
if let value=cursor.current() {if value==false {return 42;}}return 0;
""");
}
@Test void identityAndFullGroupsSurviveAllMutations() throws Exception {
compile("optional-field","""
declare struct Payload(pub value:optional int);
declare service Probe {fn main()->int {
let values=new linked<Payload>();values.add(new Payload(some(42)));
let cursor=values.cursor();values.clear();
if let item=cursor.current() {return item.value else 0;}return 0;
}}
""");
compile("identity","""
declare struct Item(pub mut value:int);
declare service Probe {fn main()->int {
let item=new Item(20);let other=new Item(20);let values=new linked<Item>();
values.add(item);values.add(other);values.add(item);
let cursor=values.reverseCursor();values.removeAt(1);values.clear();item.value=22;
if let first=cursor.current() {if first!=item {return 0;}} else {return 0;}
cursor.next();if let middle=cursor.current() {if middle==item {return 0;}} else {return 0;}
cursor.next();if let last=cursor.current() {return last.value+other.value;}return 0;
}}
""");
probe("tuple","""
let values=new linked<(left:int,right:int)>();
values.add((left:1,right:2));values.addFirst((left:3,right:4));
values[1]=(left:20,right:22);values.removeFirst();
let cursor=values.reverseCursor();values.clear();
if let value=cursor.current() {return value.left+value.right;}return 0;
""");
probe("nested-array","""
let payload:array<int>=[20];let values=new linked<array<int>>();
values.add(payload);values.insert(0,payload);values.removeAt(1);
let cursor=values.cursor();values.clear();payload[0]=42;
if let value=cursor.current() {return value[0] else 0;}return 0;
""");
compile("function","""
fn answer()->int {return 42;}
declare service Probe {fn main()->int {
let values=new linked<Fn()->int>();values.add(answer);values.addFirst(answer);
values.removeLast();let cursor=values.reverseCursor();values.clear();
if let call=cursor.current() {return call();}return 0;
}}
""");
compile("contract","""
declare contract Reader {fn read()->int;}
declare struct Item(pub value:int);
implements Reader for Item using item {fn read()->int {return item.value;}}
declare service Probe {fn main()->int {
let item=new Item(42);let values=new linked<Reader>();values.add(item as Reader);
values.addFirst(item as Reader);values.removeAt(1);
let cursor=values.reverseCursor();values.clear();
if let value=cursor.current() {return value.read();}return 0;
}}
""");
compile("vec2","""
[BuiltinType(name="vec2",version=1)] declare builtin type Vector(pub x:float,pub y:float) {}
fn nativeProbe()->int {
let values=new linked<Vector>();values.add(new Vector(1.0,2.0));
values.addFirst(new Vector(20.0,22.0));values.removeLast();
let cursor=values.cursor();values.clear();
if let value=cursor.current() {return (value.x+value.y) as int;}return 0;
}
declare service Probe {fn main()->int {return nativeProbe();}}
""");
String color;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/core/color/main.pbs")) {
assertNotNull(stream);color=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
compile("pixel",color+"""
[BuiltinType(name="pixel",version=1)] declare builtin type Pixel(pub x:long,pub y:long,pub color:Color) {}
fn nativeProbe()->int {
let values=new linked<Pixel>();values.add(new Pixel(1,2,BLUE));
values.insert(0,new Pixel(20,22,RED));values.removeAt(1);
let cursor=values.reverseCursor();values.clear();
if let value=cursor.current() {if value.color!=RED {return 0;}return (value.x+value.y) as int;}return 0;
}
declare service Probe {fn main()->int {return nativeProbe();}}
""");
}
@Test void evaluationAndNamedApplicationPreserveSourceOrder() throws Exception {
compile("evaluation","""
declare struct Effects(pub mut count:int,pub values:linked<int>) {
fn receiver()->linked<int> {this.count=this.count*10+1;return this.values;}
fn index()->int {this.count=this.count*10+2;return 0;}
fn value()->int {this.count=this.count*10+3;return 42;}
}
declare service Probe {fn main()->int {
let values=new linked<int>();values.add(0);let effects=new Effects(0,values);
effects.receiver()[effects.index()]=effects.value();if effects.count!=123 {return 0;}
effects.count=0;effects.receiver().insert apply (value:effects.value(),index:effects.index());
if effects.count!=132 {return 0;}
let index:int=1;values.removeAt apply index;return values[0] else 0;
}}
""");
}
@Test void invalidIndicesAndGrowthCannotTruncateOrWrap() throws Exception {
var cases=java.util.Map.of(
"fault-read","let values=new linked<int>();return values[0] else 0;",
"fault-write","let values=new linked<int>();values[0]=42;return 0;",
"fault-remove","let values=new linked<int>();values.removeAt(0);return 0;",
"fault-negative","let values=new linked<int>();values.add(1);return values[-1] else 0;",
"fault-insert","let values=new linked<int>();values.insert(1,42);return 0;",
"fault-long-index","let values=new linked<int>();values.add(42);let index:long=4294967296;return values[index] else 0;",
"fault-overflow","_linkedNextSize(9223372036854775807);return 0;");
for(var item:cases.entrySet())probe(item.getKey(),item.getValue());
}
@Test void resourceFixturesExposeEveryMutationBoundary() throws Exception {
var operations=java.util.Map.of("insert","values.insert(12,42);","add","values.add(42);",
"addFirst","values.addFirst(42);","remove","let previous=values.removeAt(12);",
"removeFirst","let previous=values.removeFirst();","removeLast","let previous=values.removeLast();",
"replace","values[12]=42;","clear","values.clear();",
"cursor","let snapshot=values.cursor();","reverseCursor","let snapshot=values.reverseCursor();");
for(var operation:operations.entrySet())compile("refusal-"+operation.getKey(),"""
fn mutationMarker() {let marker:int=314160;}
fn completedMarker() {let marker:int=314161;}
fn performSecond(values:linked<int>) {OPERATION}
fn descend(values:linked<int>,depth:int) {if depth>0 {descend(values,depth-1);} else {performSecond(values);}}
fn perform(values:linked<int>) {descend(values,24);}
declare service Probe {fn main()->int {
let values=new linked<int>();for i:int from 0 until 32 {values.add(i);}
mutationMarker();perform(values);completedMarker();return 42;
}}
""".replace("OPERATION",operation.getValue()));
}
@Test void snapshotsReleaseOriginalNodesAndConsumedPrefixesAtNormalSafepoints() throws Exception {
compile("gc","""
declare struct Item(pub value:int);declare struct Garbage();
fn collect() {let marker:int=314159;}
fn makeCursor()->Cursor<Item> {
let values=new linked<Item>();for i:int from 0 until 1200 {values.add(new Item(i));}
let cursor=values.cursor();values.removeAt(600);values.removeFirst();values.removeLast();values.clear();
return cursor;
}
fn useCursor() {
let cursor=makeCursor();collect();
for i:int from 0 until 1199 {cursor.next();}collect();
for i:int from 0 until 1200 {cursor.next();}collect();
if let item=cursor.current() {if item.value!=1199 {let failure=1/0;}} else {let failure=1/0;}
}
declare service Probe {fn main()->int {
useCursor();for i:int from 0 until 1200 {let discarded=new Garbage();}collect();return 42;
}}
""");
}
@Test void removedPayloadsAndDoubleLinkedCyclesBecomeCollectible() throws Exception {
compile("gc-collection","""
declare struct Item(pub value:int);declare struct Garbage();
fn collect() {let marker:int=314159;}
fn pressure() {for i:int from 0 until 1200 {let garbage=new Garbage();}}
fn create()->linked<Item> {
let values=new linked<Item>();for i:int from 0 until 1200 {values.add(new Item(i));}return values;
}
fn discard(values:linked<Item>) {values.removeAt(600);values.removeFirst();values.removeLast();}
fn useCollection() {
let values=create();collect();discard(values);pressure();collect();values.clear();pressure();collect();
if (values.size() as int)!=0 {let failure=1/0;}
}
declare service Probe {fn main()->int {useCollection();pressure();collect();return 42;}}
""");
}
@Test void realSdkImportsAndReexportsCompile(@TempDir Path root) throws Exception {
Files.createDirectories(root.resolve("src/facade"));
Files.writeString(root.resolve("prometeu.json"),"""
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
""");
Files.writeString(root.resolve("src/mod.barrel"),"pub fn frame() -> void;");
Files.writeString(root.resolve("src/facade/mod.barrel"),"pub struct Items<T>;pub contract Walk<T>;");
Files.writeString(root.resolve("src/facade/main.pbs"),"import {linked as Items,Cursor as Walk} from @sdk:collections;");
Files.writeString(root.resolve("src/main.pbs"),"""
import {Items,Walk} from @app:facade;
[Frame] fn frame() -> void {
let values:Items<int>=new Items();values.add(20);values.add(22);
let cursor:Walk<int>=values.reverseCursor();values.clear();
let total:int=0;
while true {
if let value=cursor.current() {total=total+value;} else {break;}
if cursor.hasNext() {cursor.next();} else {break;}
}
if total!=42 {let failure=1/0;}
}
""");
var compiled=p.studio.compiler.workspaces.BuilderPipelineService.INSTANCE.compile(
p.studio.compiler.models.BuilderPipelineContext.fromConfig(new p.studio.compiler.messages.BuilderPipelineConfig(false,root.toString())),
p.studio.utilities.logs.LogAggregator.stdout());
assertFalse(compiled.diagnostics().stream().anyMatch(d->d.isError()),compiled.diagnostics().toString());
assertTrue(compiled.bytecodeBytes().length>0);
Files.write(Path.of(System.getProperty("pbs.registryConformanceDirectory")).resolve("pbs-linked-imports.pbx"),compiled.bytecodeBytes());
}
}
@@ -18,6 +18,9 @@ import static org.junit.jupiter.api.Assertions.*;
class PbsListCursorTest {
private static Path directory() { return Path.of(System.getProperty("pbs.registryConformanceDirectory")); }
static BytecodeModule compile(String name,String source) throws Exception {
return compileCollection("list", name, source);
}
static BytecodeModule compileCollection(String collection,String name,String source) throws Exception {
var diagnostics=DiagnosticSink.empty();var id=new FileId(0);var sdkId=new FileId(1);
String sdk;
try(var stream=PbsListCursorTest.class.getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
@@ -33,14 +36,14 @@ class PbsListCursorTest {
.intrinsicPool(file.intrinsicPool()).callableSignatures(file.callableSignatures()).build();
var program=new LowerToIRVMService().lower(backend);
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
if(name.equals("gc"))module=PbsStructuredValuesTest.withTestSafepoint(module);
if(name.startsWith("gc"))module=PbsStructuredValuesTest.withTestSafepoint(module);
new BytecodePreloadVerifierService().verify(module);
Files.createDirectories(directory());Files.write(directory().resolve("pbs-list-"+name+".pbx"),module.serialize());
if(!name.equals("gc")&&!name.startsWith("refusal-")) {
Files.createDirectories(directory());Files.write(directory().resolve("pbs-"+collection+"-"+name+".pbx"),module.serialize());
if(!name.startsWith("gc")&&!name.startsWith("refusal-")) {
var optimized=new OptimizeIRVMService().optimize(new LowerToIRVMService().lower(backend));
var optimizedModule=new BytecodeEmitter().emit(optimized.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(optimizedModule);
Files.write(directory().resolve("pbs-list-"+name+"-optimized.pbx"),optimizedModule.serialize());
Files.write(directory().resolve("pbs-"+collection+"-"+name+"-optimized.pbx"),optimizedModule.serialize());
}
return module;
}
@@ -237,8 +237,9 @@ public final class PbsGenericInstantiation {
error("ARITY", name(template)+" requires "+parameters(template).size()+" explicit type argument(s)", site);
return null;
}
if (PbsSdkCollections.isList(template) && PbsSdkCollections.directOptional(arguments.getFirst())) {
error("LIST_OPTIONAL_ELEMENT", "SDK list<T> does not permit a directly optional element", site);
if (PbsSdkCollections.isIndexedCollection(template) && PbsSdkCollections.directOptional(arguments.getFirst())) {
error(PbsSdkCollections.isLinked(template) ? "LINKED_OPTIONAL_ELEMENT" : "LIST_OPTIONAL_ELEMENT",
"SDK " + name(template) + "<T> does not permit a directly optional element", site);
return null;
}
var application = PbsAst.TypeRef.applied(name(template), arguments, template.span());
@@ -631,7 +632,12 @@ public final class PbsGenericInstantiation {
}
var concreteType = type(applied);
if (symbolicProbe) requiredShapes.add(concreteType.name());
var constructor = PbsSdkCollections.isList(s) && n.ctorName() == null ? "make" : n.ctorName();
var constructor = PbsSdkCollections.isIndexedCollection(s) && n.ctorName() == null ? "make" : n.ctorName();
if (constructor != null && s.ctors().stream().noneMatch(c -> c.name().equals(constructor)
&& c.parameters().size() == n.arguments().size())) {
error("CONSTRUCTOR_ARITY", "No constructor '" + constructor + "' with "
+ n.arguments().size() + " argument(s) for " + name(s), n.span());
}
var fields = constructorParameters(concreteType, constructor, n.arguments().size());
var arguments = new ArrayList<PbsAst.Expression>();
for (int i = 0; i < n.arguments().size(); i++) arguments.add(expression(n.arguments().get(i),
@@ -153,7 +153,7 @@ final class PbsGenericTemplateValidator {
context.error("CONTEXT", "Generic construction requires an expected application or explicit type arguments", n.span());
if (d instanceof PbsAst.StructDecl s) {
var env = bindings(s, result);
var constructor = PbsSdkCollections.isList(s) && n.ctorName()==null ? "make" : n.ctorName();
var constructor = PbsSdkCollections.isIndexedCollection(s) && n.ctorName()==null ? "make" : n.ctorName();
var expectedParameters = constructor == null ? map(s.fields(), f -> new PbsAst.Parameter(f.name(), PbsTypeSubstitution.substitute(f.typeRef(), env), f.span()))
: s.ctors().stream().filter(c -> c.name().equals(constructor) && c.parameters().size()==n.arguments().size()).findFirst().map(c ->
map(c.parameters(), p -> new PbsAst.Parameter(p.name(), PbsTypeSubstitution.substitute(p.typeRef(), env), p.span()))).orElse(ReadOnlyList.empty());
@@ -9,6 +9,12 @@ public final class PbsSdkCollections {
public static boolean isList(PbsAst.TopDecl declaration) {
return declaration instanceof PbsAst.StructDecl s && attribute(s.attributes(), "SdkList") != null;
}
public static boolean isLinked(PbsAst.TopDecl declaration) {
return declaration instanceof PbsAst.StructDecl s && attribute(s.attributes(), "SdkLinked") != null;
}
public static boolean isIndexedCollection(PbsAst.TopDecl declaration) {
return isList(declaration) || isLinked(declaration);
}
public static PbsAst.Attribute attribute(ReadOnlyList<PbsAst.Attribute> attributes, String name) {
return attributes.stream().filter(a -> a.trustedSdkOrigin() && a.name().equals(name)).findFirst().orElse(null);
}
@@ -1113,7 +1113,7 @@ final class PbsExecutableBodyLowerer {
var methods=declaration.methods().stream().filter(m->m.name().equals(member.memberName())&&m.parameters().size()==tuple.items().size()).toList();
if(methods.size()!=1)return false;
var method=methods.getFirst();
boolean listIndex=p.studio.compiler.pbs.PbsSdkCollections.isList(declaration)&&java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName());
boolean listIndex=p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(declaration)&&java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName());
var receiverName=c.nextLabel("named_receiver");
lowerExpected(member.receiver(),owner,c);
int receiver=c.declareLocalSlots(receiverName,1);c.localTypes.put(receiverName,owner);
@@ -1166,7 +1166,7 @@ final class PbsExecutableBodyLowerer {
var array = PbsRuntimeTypes.unwrap(expressionType(index.receiver(), c));
if(array==null)return null;
var declaration=c.types().structs.get(array.name());
return p.studio.compiler.pbs.PbsSdkCollections.isList(declaration)
return p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(declaration)
? declaration.methods().stream().filter(m->m.name().equals("_at")).findFirst().orElseThrow().returnType()
: PbsAst.TypeRef.optional(array.inner(),index.span());
}
@@ -1617,6 +1617,43 @@ final class PbsExecutableBodyLowerer {
var operation=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"name");
var span=function.span();
switch(operation) {
case "publishFields" -> {
var owners = new java.util.ArrayList<IRRegistry.TypeRef>();
int parameter = 0, offset = 0, pops = 0;
while (parameter < function.parameters().size()) {
var receiver = PbsRuntimeTypes.unwrap(function.parameters().get(parameter++).typeRef());
var owner = receiver.kind() == PbsAst.TypeRefKind.OPTIONAL
? PbsRuntimeTypes.unwrap(receiver.inner()) : receiver;
var declaration = c.types().structs.get(owner.name());
if (declaration == null) throw new IllegalArgumentException("Publication destination must be Struct");
owners.add(c.types().registryType(owner));
if (receiver.kind() == PbsAst.TypeRefKind.OPTIONAL) {
var absent = c.nextLabel("absent_publication_destination");
var ready = c.nextLabel("publication_destination_ready");
optionalPresence(offset, receiver, span, c);
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE, absent, span, c);
unwrapOptional(offset, receiver, span, c);
emitJump(IRBackendExecutableFunction.InstructionKind.JMP, ready, span, c);
emitLabel(absent, span, c);
emitDefault(c.types().registryType(owner), 1, span, c);
emitLabel(ready, span, c);
} else emitGetLocal(offset, span, c);
offset += c.types().slots(receiver);
pops++;
for (var field : declaration.fields()) {
if (parameter >= function.parameters().size()) throw new IllegalArgumentException("Missing publication field");
var value = function.parameters().get(parameter++).typeRef();
if (!c.types().key(value).equals(c.types().key(field.typeRef())))
throw new IllegalArgumentException("Publication field type mismatch");
int width = c.types().slots(value);
emitLoadLocalSlots(offset, width, span, c);
packStorage(value, span, c);
offset += width;
pops += storageWidth(value, c);
}
}
registry(FIELDS_PUBLISH, c.types().publicationSchema(owners), 0, pops, 0, span, c);
}
case "indexGuard" -> {
emitLoadLocalSlots(0,2,span,c);
registry(INDEX_GUARD,null,0,2,0,span,c);
@@ -1668,9 +1705,9 @@ final class PbsExecutableBodyLowerer {
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.RET,"",null,null,span));
}
private boolean lowerListIndex(PbsAst.IndexExpr index, PbsAst.Expression value, PbsAst.TypeRef type, PbsExecutableLoweringContext c) {
private boolean lowerCollectionIndex(PbsAst.IndexExpr index, PbsAst.Expression value, PbsAst.TypeRef type, PbsExecutableLoweringContext c) {
var declaration=type==null?null:c.types().structs.get(type.name());
if(!p.studio.compiler.pbs.PbsSdkCollections.isList(declaration))return false;
if(!p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(declaration))return false;
var name=value==null?"_at":"_put";
var method=declaration.methods().stream().filter(m->m.name().equals(name)).findFirst().orElseThrow();
lowerExpected(index.receiver(),type,c);
@@ -1723,7 +1760,7 @@ final class PbsExecutableBodyLowerer {
}
private void lowerArrayWrite(PbsAst.IndexExpr index,PbsAst.Expression value,PbsExecutableLoweringContext c) {
var type=PbsRuntimeTypes.unwrap(expressionType(index.receiver(),c));
if(lowerListIndex(index,value,type,c))return;
if(lowerCollectionIndex(index,value,type,c))return;
lowerExpected(index.receiver(),type,c);
lowerArrayIndex(index.index(),c);
lowerExpected(value,type.inner(),c);
@@ -1733,7 +1770,7 @@ final class PbsExecutableBodyLowerer {
}
private void lowerArrayRead(PbsAst.IndexExpr index,PbsExecutableLoweringContext c) {
var type=PbsRuntimeTypes.unwrap(expressionType(index.receiver(),c));
if(lowerListIndex(index,null,type,c))return;
if(lowerCollectionIndex(index,null,type,c))return;
var optional=PbsAst.TypeRef.optional(type.inner(),index.span());
lowerExpected(index.receiver(),type,c);
lowerArrayIndex(index.index(),c);
@@ -1839,9 +1876,14 @@ final class PbsExecutableBodyLowerer {
}
private void lowerStructNew(PbsAst.NewExpr expr,PbsExecutableLoweringContext c) {
var declaration=c.types().structs.get(expr.typeName());var ref=c.types().registryType(PbsRuntimeTypes.type(expr.typeName()));
boolean list=p.studio.compiler.pbs.PbsSdkCollections.isList(declaration);
boolean list=p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(declaration);
if(expr.ctorName()!=null) {
var ctor=declaration.ctors().stream().filter(f->f.name().equals(expr.ctorName()) && parametersMatch(f.parameters(),expr.arguments(),c,list)).findFirst().orElseThrow();
var ctor=declaration.ctors().stream().filter(f->f.name().equals(expr.ctorName()) && parametersMatch(f.parameters(),expr.arguments(),c,list)).findFirst().orElse(null);
if (ctor == null) {
reportUnsupportedLowering("No matching constructor '" + expr.ctorName() + "' for "
+ expr.typeName() + " with " + expr.arguments().size() + " argument(s)", expr.span(), c);
return;
}
var arguments=new java.util.ArrayList<Integer>();
for(int i=0;i<expr.arguments().size();i++){var type=PbsTypeSubstitution.replaceSelf(ctor.parameters().get(i).typeRef(),expr.typeName());if(list)lowerArrayIndex(expr.arguments().get(i),c);else lowerExpected(expr.arguments().get(i),type,c);arguments.add(save(c.types().slots(type),expr.span(),c));}
registry(STRUCT_NEW,ref,0,0,1,expr.span(),c);int instance=save(1,expr.span(),c);emitGetLocal(instance,expr.span(),c);
@@ -1916,7 +1958,7 @@ final class PbsExecutableBodyLowerer {
emitStructuredFunction(owner.name()+"."+method.name(),method.parameters().stream().map(PbsAst.Parameter::typeRef).toList(),PbsTypeSubstitution.replaceSelf(method.returnType(),owner.name()),pops,c.types().slots(method.returnType()),call.span(),c);return true;
}
var struct=c.types().structs.get(owner.name());if(struct==null)return false;
boolean listIndex=p.studio.compiler.pbs.PbsSdkCollections.isList(struct) && java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName());
boolean listIndex=p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(struct) && java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName());
var method=struct.methods().stream().filter(m->m.name().equals(member.memberName())&&parametersMatch(m.parameters(),call.arguments(),c,listIndex) && returnMatches(m.returnType(),owner.name(),c)).findFirst().orElse(null);
if(method==null)return false;
lowerExpected(member.receiver(),owner,c);int pops=1;
@@ -138,6 +138,15 @@ final class PbsRuntimeTypes {
used.putIfAbsent(ref.declarationKey(), new IRRegistry.TypeDeclaration(ref, null, new IRRegistry.Array(element)));
return ref;
}
IRRegistry.TypeRef publicationSchema(List<IRRegistry.TypeRef> owners) {
var ref = new IRRegistry.TypeRef("pbs:field-publication:" + owners.stream()
.map(IRRegistry.TypeRef::declarationKey).map(k -> k.length() + ":" + k)
.collect(java.util.stream.Collectors.joining()));
used.putIfAbsent(ref.declarationKey(), new IRRegistry.TypeDeclaration(ref, null,
new IRRegistry.Struct(ReadOnlyList.wrap(owners.stream()
.map(owner -> new IRRegistry.NamedType(owner, null)).toList()))));
return ref;
}
IRRegistry.TypeRef builtin(int id) {
var ref = IRRegistry.TypeRef.builtin(id);
used.putIfAbsent(ref.declarationKey(),new IRRegistry.TypeDeclaration(ref,null,new IRRegistry.Builtin(id)));
@@ -685,7 +685,7 @@ final class PbsDeclarationParser {
final String targetSurface) {
for (final var attribute : attributes) {
if ("Doc".equals(attribute.name()) || attribute.trustedSdkOrigin() &&
"SdkList".equals(attribute.name()) && "struct declarations".equals(targetSurface)) {
java.util.Set.of("SdkList", "SdkLinked").contains(attribute.name()) && "struct declarations".equals(targetSurface)) {
continue;
}
p.studio.compiler.source.diagnostics.Diagnostics.error(context.diagnostics(),
@@ -759,7 +759,7 @@ public final class PbsEditorialSupportService {
final Map<String, PbsEditorialCompletionCandidate> completions = new LinkedHashMap<>();
var symbolic = symbolicStruct(receiverType);
if (symbolic != null) {
for (var field : symbolic.fields()) if(!p.studio.compiler.pbs.PbsSdkCollections.isList(symbolic)||field.isPublic()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
for (var field : symbolic.fields()) if(!p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(symbolic)||field.isPublic()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
field.name(), PbsEditorialSymbolKind.FIELD, field.typeRef().surface(), symbolic.name()));
for (var method : symbolic.methods()) if(p.studio.compiler.pbs.PbsSdkCollections.attribute(method.attributes(),"SdkPrivate")==null) completions.put(method.name(), new PbsEditorialCompletionCandidate(
method.name(), PbsEditorialSymbolKind.METHOD, signatureFromFunction(method.name(), method.parameters(),
@@ -778,7 +778,7 @@ public final class PbsEditorialSupportService {
final StructInfo structInfo = model.structs.get(receiverType.name());
if (structInfo != null) {
for (final var field : structInfo.fields().entrySet()) {
if(model.isSdkList(receiverType)&&!field.getValue().isPublic())continue;
if(model.isSdkIndexedCollection(receiverType)&&!field.getValue().isPublic())continue;
completions.put(field.getKey(), new PbsEditorialCompletionCandidate(
field.getKey(),
PbsEditorialSymbolKind.FIELD,
@@ -786,7 +786,7 @@ public final class PbsEditorialSupportService {
receiverType.name()));
}
for (final var method : structInfo.methods().entrySet()) {
if(model.isSdkList(receiverType)&&java.util.Set.of("_at","_put").contains(method.getKey()))continue;
if(model.isSdkPrivateMember(receiverType, method.getKey()))continue;
completions.put(method.getKey(), new PbsEditorialCompletionCandidate(
method.getKey(),
PbsEditorialSymbolKind.METHOD,
@@ -51,7 +51,7 @@ final class PbsFlowCallableResolutionAnalyzer {
}
final boolean listIndexArgument = callExpr.callee() instanceof PbsAst.MemberExpr member
&& java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName())
&& context.model().isSdkList(analyzeValueExpression(member.receiver(),context,null).type());
&& context.model().isSdkIndexedCollection(analyzeValueExpression(member.receiver(),context,null).type());
final var callee = analyzeTargetExpression(callExpr.callee(), context, ExprUse.CALL_TARGET);
final var expectedInputs = callee.callables().size() == 1 ? callee.callables().getFirst().inputTypes() : java.util.List.<TypeView>of();
@@ -88,7 +88,7 @@ final class PbsFlowCallableResolutionAnalyzer {
final var callee = analyzeTargetExpression(applyExpr.callee(), context, ExprUse.APPLY_TARGET);
boolean listIndex=applyExpr.callee() instanceof PbsAst.MemberExpr member
&& java.util.Set.of("insert","clearAt","removeAt","resize","reserve").contains(member.memberName())
&& context.model().isSdkList(analyzeValueExpression(member.receiver(),context,null).type());
&& context.model().isSdkIndexedCollection(analyzeValueExpression(member.receiver(),context,null).type());
TypeView argument;
if (applyExpr.argument() instanceof PbsAst.TupleExpr tuple && !tuple.items().isEmpty()
&& tuple.items().getFirst().label() != null && callee.callables().size() == 1) {
@@ -209,7 +209,8 @@ final class PbsFlowCallableResolutionAnalyzer {
}
return ExprResult.type(field.type());
}
if(model.isSdkList(receiver) && java.util.Set.of("_at","_put").contains(memberExpr.memberName())) {
if(model.isSdkPrivateMember(receiver, memberExpr.memberName())
&& (receiverType == null || !receiver.name().equals(receiverType.name()))) {
Diagnostics.error(diagnostics,"E_SEM_SDK_PRIVATE_MEMBER","SDK storage adapters are private",memberExpr.span());
return ExprResult.type(TypeView.unknown());
}
@@ -198,10 +198,17 @@ final class PbsFlowSemanticSupport {
static final class Model {
p.studio.compiler.pbs.PbsContractTypes contractTypes;
boolean isSdkList(TypeView type) {
return type != null && p.studio.compiler.pbs.PbsSdkCollections.isList(contractTypes.declaration(
boolean isSdkIndexedCollection(TypeView type) {
return type != null && p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(contractTypes.declaration(
type.sourceType() == null ? PbsAst.TypeRef.simple(type.name(), Span.none()) : type.sourceType()));
}
boolean isSdkPrivateMember(TypeView type, String member) {
if (type == null) return false;
var declaration = contractTypes.declaration(type.sourceType() == null
? PbsAst.TypeRef.simple(type.name(), Span.none()) : type.sourceType());
return declaration instanceof PbsAst.StructDecl owner && owner.methods().stream()
.anyMatch(m -> m.name().equals(member) && p.studio.compiler.pbs.PbsSdkCollections.attribute(m.attributes(), "SdkPrivate") != null);
}
final Map<String, List<CallableSymbol>> topLevelCallables = new HashMap<>();
final Map<String, StructInfo> structs = new HashMap<>();
final Map<String, ServiceInfo> services = new HashMap<>();
@@ -53,7 +53,7 @@ final class PbsFlowStructuralExpressionAnalyzer {
if (expression instanceof PbsAst.IndexExpr index) {
final var receiver = analyzeValueExpression(index.receiver(), context, null).type();
checkArrayIndex(index.index(), context);
if (model.isSdkList(receiver)) {
if (model.isSdkIndexedCollection(receiver)) {
return ExprResult.type(model.structs.get(receiver.name()).methods().get("_at").getFirst().outputType());
}
if (receiver.kind() != Kind.ARRAY) {
@@ -154,3 +154,160 @@ implements<T> Cursor<T> for _SnapshotCursor<T> using snapshot {
fn next() { snapshot.next(); }
fn current() -> optional T { return snapshot.current(); }
}
// Complete-object publication is neutral; this source chooses the node topology.
[StoragePrimitive(name = "publishFields")]
fn _linkedPublish<T>(owner: linked<T>, head: optional _LinkedNode<T>, tail: optional _LinkedNode<T>, length: long,
previous: optional _LinkedNode<T>, previousValue: T, previousPrevious: optional _LinkedNode<T>, previousNext: optional _LinkedNode<T>,
next: optional _LinkedNode<T>, nextValue: T, nextPrevious: optional _LinkedNode<T>, nextNext: optional _LinkedNode<T>) {}
[StoragePrimitive(name = "publishFields")]
fn _linkedEmpty<T>(owner: linked<T>, head: optional _LinkedNode<T>, tail: optional _LinkedNode<T>, length: long) {}
[StoragePrimitive(name = "publishFields")]
fn _linkedReplace<T>(owner: _LinkedNode<T>, value: T, previous: optional _LinkedNode<T>, next: optional _LinkedNode<T>) {}
fn _linkedNextSize(length: long) -> long {
// Check before adding; Linked does not inherit Array's u32 structural bound.
let maximum: long = 9223372036854775807;
_bounds(length, maximum);
return length + 1;
}
declare struct _LinkedNode<T>(pub mut value: T, pub mut previous: optional _LinkedNode<T>, pub mut next: optional _LinkedNode<T>) {
ctor make(value: T, previous: optional _LinkedNode<T>, next: optional _LinkedNode<T>) {
this.value = value;
this.previous = previous;
this.next = next;
}
}
[SdkLinked]
declare struct linked<T>(head: optional _LinkedNode<T>, tail: optional _LinkedNode<T>, length: long) {
ctor make() {
this.head = none;
this.tail = none;
this.length = 0;
}
fn size() -> long { return this.length; }
fn first() -> optional T {
if let node = this.head { return some(node.value); }
return none;
}
fn last() -> optional T {
if let node = this.tail { return some(node.value); }
return none;
}
[SdkPrivate]
fn _node(index: long) -> optional _LinkedNode<T> {
_bounds(index, this.length);
let node: optional _LinkedNode<T> = none;
let remaining: long = 0;
if index < this.length / (2 as long) {
node = this.head;
remaining = index;
while remaining > (0 as long) {
if let current = node { node = current.next; }
remaining = remaining - 1;
}
} else {
node = this.tail;
remaining = this.length - index - 1;
while remaining > (0 as long) {
if let current = node { node = current.previous; }
remaining = remaining - 1;
}
}
return node;
}
[SdkPrivate]
fn _at(index: long) -> optional T {
if let node = this._node(index) { return some(node.value); }
return none;
}
[SdkPrivate]
fn _put(index: long, value: T) {
if let node = this._node(index) {
_linkedReplace<T>(node, value, node.previous, node.next);
}
}
fn add(value: T) { this.insert(this.length, value); }
fn addFirst(value: T) { this.insert(0, value); }
fn insert(index: long, value: T) {
if index != this.length { _bounds(index, this.length); }
let required: long = _linkedNextSize(this.length);
let next: optional _LinkedNode<T> = none;
let previous: optional _LinkedNode<T> = this.tail;
if index < this.length {
next = this._node(index);
if let node = next { previous = node.previous; }
}
let inserted: optional _LinkedNode<T> = some(new _LinkedNode<T>(value, previous, next));
let head: optional _LinkedNode<T> = this.head;
let tail: optional _LinkedNode<T> = this.tail;
if index == (0 as long) { head = inserted; }
if index == this.length { tail = inserted; }
let previousValue: T = value;
let previousPrevious: optional _LinkedNode<T> = none;
let nextValue: T = value;
let nextNext: optional _LinkedNode<T> = none;
if let node = previous { previousValue = node.value; previousPrevious = node.previous; }
if let node = next { nextValue = node.value; nextNext = node.next; }
_linkedPublish<T>(this, head, tail, required,
previous, previousValue, previousPrevious, inserted,
next, nextValue, inserted, nextNext);
}
fn removeAt(index: long) -> optional T {
if let removed = this._node(index) {
let removedValue: optional T = some(removed.value);
let previous: optional _LinkedNode<T> = removed.previous;
let next: optional _LinkedNode<T> = removed.next;
let head: optional _LinkedNode<T> = this.head;
let tail: optional _LinkedNode<T> = this.tail;
if previous.hasNone() { head = next; }
if next.hasNone() { tail = previous; }
let previousValue: T = removed.value;
let previousPrevious: optional _LinkedNode<T> = none;
let nextValue: T = removed.value;
let nextNext: optional _LinkedNode<T> = none;
if let node = previous { previousValue = node.value; previousPrevious = node.previous; }
if let node = next { nextValue = node.value; nextNext = node.next; }
_linkedPublish<T>(this, head, tail, this.length - 1,
previous, previousValue, previousPrevious, next,
next, nextValue, previous, nextNext);
return removedValue;
}
return none;
}
fn removeFirst() -> optional T {
if this.length == (0 as long) { return none; }
return this.removeAt(0);
}
fn removeLast() -> optional T {
if this.length == (0 as long) { return none; }
return this.removeAt(this.length - 1);
}
fn clear() { _linkedEmpty<T>(this, none, none, 0); }
fn cursor() -> Cursor<T> {
let captured: optional _SnapshotNode<T> = none;
let source: optional _LinkedNode<T> = this.tail;
while source.hasSome() {
if let node = source {
captured = some(new _SnapshotNode<T>(node.value, captured));
source = node.previous;
}
}
return new _SnapshotCursor<T>(captured) as Cursor<T>;
}
fn reverseCursor() -> Cursor<T> {
let captured: optional _SnapshotNode<T> = none;
let source: optional _LinkedNode<T> = this.head;
while source.hasSome() {
if let node = source {
captured = some(new _SnapshotNode<T>(node.value, captured));
source = node.next;
}
}
return new _SnapshotCursor<T>(captured) as Cursor<T>;
}
}
@@ -1,2 +1,3 @@
pub struct list<T>;
pub contract Cursor<T>;
pub struct linked<T>;
@@ -0,0 +1,27 @@
import { linked, Cursor } from @sdk:collections;
fn sumLinkedSnapshot(snapshot: Cursor<int>) -> int {
let total: int = 0;
while true {
if let value = snapshot.current() { total = total + value; }
else { break; }
if snapshot.hasNext() { snapshot.next(); }
else { break; }
}
return total;
}
fn linkedExample() -> int {
let values: linked<int> = new linked();
values.add(20);
values.add(22);
let forward: Cursor<int> = values.cursor();
let reverse: Cursor<int> = values.reverseCursor();
values[0] = 99;
values.clear(); // Completed snapshots still visit 20/22 and 22/20.
let forwardTotal: int = sumLinkedSnapshot(forward);
let reverseTotal: int = sumLinkedSnapshot(reverse);
if forwardTotal != 42 or reverseTotal != 42 { return 0; }
// next() retains 22 in forward.current(), and 20 in reverse.current().
return reverseTotal;
}
@@ -3,3 +3,4 @@ pub fn frame() -> void;
pub fn contractExample() -> int;
pub fn genericsExample() -> int;
pub fn collectionsExample() -> int;
pub fn linkedExample() -> int;
@@ -89,6 +89,41 @@ class PbsGateUSdkInterfaceConformanceTest {
assertTrue(copied.hasErrors(),"Project attributes cannot acquire SDK bindings");
}
@Test
void linkedRejectsOptionalElementsCapacityAndPrivateProjectAccess() throws IOException {
var cases=java.util.Map.ofEntries(
java.util.Map.entry("optional","let xs:linked<optional int>=new linked<optional int>();"),
java.util.Map.entry("grouped","let xs:linked<(optional int)>=new linked<(optional int)>();"),
java.util.Map.entry("capacity","let xs=new linked<int>(4);"),
java.util.Map.entry("context","let xs=new linked();"),
java.util.Map.entry("invariance","let xs:linked<long>=new linked<int>();"),
java.util.Map.entry("index","let xs=new linked<int>();let invalid=xs[1.5];"),
java.util.Map.entry("head","let xs=new linked<int>();let exposed=xs.head;"),
java.util.Map.entry("node","let xs=new linked<int>();let exposed=xs._node(0);"),
java.util.Map.entry("adapter","let xs=new linked<int>();let exposed=xs._at(0);"),
java.util.Map.entry("payload","let xs=new linked<long>();let value:int=1;xs.add(value);"),
java.util.Map.entry("holes","let xs=new linked<int>();xs.clearAt(0);"),
java.util.Map.entry("value-remove","let xs=new linked<int>();xs.remove(42);"));
for(var entry:cases.entrySet()) {
var result=compileWorkspaceModule(tempDir.resolve("linked-negative-"+entry.getKey()),
"import {linked} from @sdk:collections;[Frame] fn frame()->void {"+entry.getValue()+"}",
"pub fn frame()->void;",1,null);
assertTrue(result.diagnostics().hasErrors(),entry.getKey());
}
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(202),"""
declare struct linked<T>(value:T) {}
fn main() {let xs=new linked<int>(1);let invalid=xs[0];}
""",diagnostics,SourceKind.PROJECT);
assertTrue(diagnostics.stream().anyMatch(d->d.getCode().equals("E_SEM_ARRAY_RECEIVER")));
var copied=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(203),"""
[SdkLinked] declare struct linked<T>(value:T) {}
[StoragePrimitive(name="publishFields")] fn fake() {}
""",copied,SourceKind.PROJECT);
assertTrue(copied.hasErrors());
}
@Test
void removedInputObjectsExplainTheirMigration() throws IOException {
final var result = compileWorkspaceModule(tempDir.resolve("removed-input"), """
@@ -11,14 +11,16 @@ import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
class PbsListCursorEditorialTest {
@Test void concreteApiHidesStorageAndKeepsSdkDefinitionOrigins() throws Exception {
@org.junit.jupiter.params.ParameterizedTest
@org.junit.jupiter.params.provider.ValueSource(strings={"list","linked"})
void concreteApiHidesStorageAndKeepsSdkDefinitionOrigins(String collection) throws Exception {
var diagnostics=DiagnosticSink.empty();var sdkId=new FileId(1);
String sdk;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
var library=PbsParser.parse(PbsLexer.lex(sdk,sdkId,diagnostics),sdkId,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
var list=library.topDecls().stream().filter(d->d instanceof PbsAst.StructDecl s&&s.name().equals("list")).findFirst().orElseThrow();
var list=library.topDecls().stream().filter(d->d instanceof PbsAst.StructDecl s&&s.name().equals(collection)).findFirst().orElseThrow();
var cursor=library.topDecls().stream().filter(d->d instanceof PbsAst.ContractDecl c&&c.name().equals("Cursor")).findFirst().orElseThrow();
var source="""
import {list as Values,Cursor} from @sdk:collections;
@@ -26,19 +28,19 @@ class PbsListCursorEditorialTest {
let created:Values<int>=new Values(0);
values.add(42); cursor.hasNext(); return cursor.current();
}
""";
""".replace("{list as", "{"+collection+" as").replace("new Values(0)", collection.equals("list")?"new Values(0)":"new Values()");
var id=new FileId(2);var ast=PbsParser.parse(PbsLexer.lex(source,id,diagnostics),id,diagnostics);
assertFalse(diagnostics.hasErrors());
var supplemental=ReadOnlyList.<PbsAst.TopDecl>from(new PbsAst.TypeBindingDecl("Values",list,ast.span()),cursor);
var service=new PbsEditorialSupportService();int member=source.indexOf("values.add")+7;
var typeHover=service.hover(source,ast,supplemental,source.indexOf("Values<int>")).orElseThrow();
assertFalse(typeHover.signatures().stream().anyMatch(s->s.label().startsWith("_")));
var constructors=service.signatureHelp(source,ast,supplemental,source.indexOf("new Values(0")+11).orElseThrow().signatures();
assertEquals(2,constructors.size());
var constructors=service.signatureHelp(source,ast,supplemental,source.indexOf("new Values(")+11).orElseThrow().signatures();
assertEquals(collection.equals("list")?2:1,constructors.size());
assertTrue(constructors.stream().allMatch(s->!s.label().contains("storage")&&!s.label().contains("$generic$")));
var completions=service.completion(source,ast,supplemental,member);
assertTrue(completions.stream().anyMatch(c->c.label().equals("add")&&c.detail().contains("int")));
assertFalse(completions.stream().anyMatch(c->java.util.Set.of("storage","length","_at","_put").contains(c.label())));
assertFalse(completions.stream().anyMatch(c->java.util.Set.of("storage","length","head","tail","_at","_put","_node").contains(c.label())));
var definition=service.definition(source,ast,supplemental,member);
assertEquals(sdkId,definition.getFirst().span().getFileId());
int current=source.indexOf("cursor.current")+7;
@@ -8,7 +8,7 @@ final class BytecodeOpcodeLayout {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80,0xa7 -> 2;
case 0x16 -> 3;
case 0x02,0x03,0x04,0x10,0x17,0x18,0x40,0x41,0x42,0x43,0x50,0x53,0x57,0x70,0x71,0x72,
0x90,0x91,0x92,0xa0,0xa3,0xa4,0xa5,0xa6,0xa8,0xa9 -> 6;
0x90,0x91,0x92,0xa0,0xa3,0xa4,0xa5,0xa6,0xa8,0xa9,0xab -> 6;
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0xa1,0xa2 -> 10;
case 0xaa -> 18;
default -> throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
@@ -59,6 +59,10 @@ public class BytecodePreloadVerifierService {
case 0xa0 -> check(type(r,a).body() instanceof Struct,"Struct category");
case 0xa1,0xa2 -> field(r,a,b);
case 0xa3,0xa4,0xa5,0xa6,0xa8,0xa9 -> array(r,a);
case 0xab -> {
check(type(r,a).body() instanceof Struct,"Struct publication schema");
for (var entry : ((Struct)type(r,a).body()).fields()) check(r.type(entry.type()).body() instanceof Struct,"Struct publication destination");
}
case 0xaa -> {array(r,a);field(r,b,instruction.third());field(r,b,instruction.fourth());check(instruction.third()!=instruction.fourth(),"publication fields must differ");}
default -> { }
}
@@ -167,6 +171,14 @@ public class BytecodePreloadVerifierService {
case 0xa4 -> {consume(s,facts(r,type(r,a).identity()),typed);s.stack.add(carrier("i64"));}
case 0xa5 -> {consume(s,List.of(carrier("i64")),typed);consume(s,facts(r,type(r,a).identity()),typed);produce(s,facts(r,array(r,a).element()),i.pc());}
case 0xa6 -> {consume(s,facts(r,array(r,a).element()),typed);consume(s,List.of(carrier("i64")),typed);consume(s,facts(r,type(r,a).identity()),typed);}
case 0xab -> {
var expected=new ArrayList<Fact>();
for(var entry:((Struct)type(r,a).body()).fields()) {
expected.addAll(facts(r,entry.type()));
for(var field:((Struct)r.type(entry.type()).body()).fields()) expected.addAll(facts(r,field.type()));
}
consume(s,expected,typed);
}
case 0xa7 -> consume(s,List.of(carrier("i64"),carrier("i64")),typed);
case 0xa8 -> consume(s,List.of(carrier("i64")),typed);
case 0xa9,0xaa -> {
@@ -51,6 +51,14 @@ public final class RegistryInstructionLowerer {
pops=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_SET?1+width:1;pushes=instruction.kind()==IRRegistryInstruction.Kind.STRUCT_GET?width:0;
}
}
case FIELDS_PUBLISH -> {
if (!(body instanceof Struct schema)) throw new IllegalArgumentException("Struct publication schema required");
for (var entry : schema.fields()) {
if (!(registry.type(entry.type()).body() instanceof Struct owner)) throw new IllegalArgumentException("Struct publication destination required");
pops = Math.addExact(pops, 1);
for (var field : owner.fields()) pops = Math.addExact(pops, registry.width(field.type()));
}
}
case INDEX_GUARD -> pops=2;
case ARRAY_CHECK_LENGTH,ARRAY_COPY,STORAGE_PUBLISH -> {
if(!(body instanceof Array array))throw new IllegalArgumentException("Array category required");
@@ -77,7 +77,7 @@ class IRVMIntrinsicRegistryParityTest {
assertNotNull(input);
properties.load(input);
}
assertEquals("f7d7c167531bb662a1af9d73fc326bb476bf8aa7", properties.getProperty("runtime.revision"));
assertEquals("57992cf214e8a5f9de53a40dba9aaa7fab038c91", properties.getProperty("runtime.revision"));
for (var name : List.of("INTRINSICS.csv", "BUILTIN_CONSTANTS.csv")) {
assertEquals(properties.getProperty(name + ".sha256"),
HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes(name))));
@@ -31,6 +31,35 @@ class StoragePublicationPipelineTest {
}
private void number(long value){literal(PUSH_I64,Long.toString(value));}
@Test void multiObjectFieldsPublishThroughNeutralOrdinaryAndOptimizedProducers() throws Exception {
var schema = new TypeRef("synthetic:publication-schema");
var registry = new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(INTEGER,null,new Builtin(0x10)),
new TypeDeclaration(LONG,null,new Builtin(0x11)),
new TypeDeclaration(OWNER,"State",new Struct(ReadOnlyList.from(
new NamedType(LONG,"generation"),new NamedType(INTEGER,"answer")))),
new TypeDeclaration(schema,null,new Struct(ReadOnlyList.from(
new NamedType(OWNER,null),new NamedType(OWNER,null))))),null);
typed(STRUCT_NEW,OWNER,0);local(SET_LOCAL,0);
typed(STRUCT_NEW,OWNER,0);local(SET_LOCAL,1);
local(GET_LOCAL,0);number(7);literal(PUSH_I32,"20");
local(GET_LOCAL,1);number(9);literal(PUSH_I32,"42");
typed(FIELDS_PUBLISH,schema,0);
local(GET_LOCAL,1);typed(STRUCT_GET,OWNER,1);literal(RET,null);
var function=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"main",new CallableId(0),0,0,0,2,1,6,
ReadOnlyList.wrap(instructions),null,new FunctionMetadata(new Signature(null,ReadOnlyList.from(INTEGER)),null,Role.NONE));
var backend=IRBackend.builder().entryPointModuleId(new ModuleId(0)).entryPointCallableName("main")
.registry(registry).executableFunctions(ReadOnlyList.from(function)).build();
for(boolean optimize:new boolean[]{false,true}) {
var program=new LowerToIRVMService().lower(backend);
if(optimize)program=new OptimizeIRVMService().optimize(program);
var module=new BytecodeEmitter().emit(program.coherentEmissionPlan());
new BytecodePreloadVerifierService().verify(module);
var directory=Path.of("build/registry-conformance");Files.createDirectories(directory);
Files.write(directory.resolve("common-fields"+(optimize?"-optimized":"")+".pbx"),module.serialize());
}
}
@Test void guardsTransfersAndPublicationSurviveBothProducerPaths() throws Exception {
var registry=new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(INTEGER,null,new Builtin(0x10)),new TypeDeclaration(LONG,null,new Builtin(0x11)),
@@ -15,7 +15,7 @@ public record IRRegistryInstruction(Kind kind, IRRegistry.TypeRef type, IRRegist
REF_IS_EXACT(0x93), REF_IMPLEMENTS(0x94), REF_TO_TRAIT(0x95), REF_CAST(0x96),
STRUCT_NEW(0xa0), STRUCT_GET(0xa1), STRUCT_SET(0xa2),
ARRAY_NEW(0xa3), ARRAY_LEN(0xa4), ARRAY_GET(0xa5), ARRAY_SET(0xa6),
INDEX_GUARD(0xa7), ARRAY_CHECK_LENGTH(0xa8), ARRAY_COPY(0xa9), STORAGE_PUBLISH(0xaa);
INDEX_GUARD(0xa7), ARRAY_CHECK_LENGTH(0xa8), ARRAY_COPY(0xa9), STORAGE_PUBLISH(0xaa), FIELDS_PUBLISH(0xab);
private final int opcode;
Kind(int opcode) { this.opcode=opcode; }
public int opcode(){return opcode;}
+30
View File
@@ -264,3 +264,33 @@ Evidence prints compiler/provider source hashes, exact provider revision and dir
state, toolchain, platform/profile, capabilities, every default VM ceiling and each
explicitly lowered refusal limit. Development runs against a dirty provider are
identified as such; final catalog verification requires its clean selected commit.
## SDK Linked and multi-object publication
```sh
./gradlew :prometeu-compiler:prometeu-build-pipeline:test :prometeu-architecture-tests:test :prometeu-app:installedKnowledgeTest
python3 tools/pbx-conformance/run.py --runtime ../runtime --linked-only
```
The full harness requires this corpus too. Ordinary and optimized source programs
exercise the complete Linked API, both snapshot orders, empty/single/multiple
states, reciprocal links, identities and full native/tuple/contract/Fn groups,
logical and long bounds, contextual construction and source/named evaluation.
Neutral common-IR fixtures execute FIELDS_PUBLISH without PBS or collection types.
Ten explicit mutation boundaries qualify every mutation and both captures. The
harness lowers resource ceilings around measured setup/success peaks and compares
all prior Linked roots, optional-link carriers, nodes and complete groups after
each refusal. Node allocation, optional removed-value preparation, snapshot-node
allocation and frame/operand preparation must all produce observed refusals.
Publication itself uses no allocation or scratch; provider tests prove it succeeds
at already exhausted allocation ceilings and rejects late invalid groups before
any write, including for null destinations and aliased targets.
Normal GC safepoints qualify removal/clear without snapshots (1,200 → 1,197 → 0
nodes and payloads), and snapshot traversal (1,200 → 1 → 1 → 0). Fixtures introduce
ordinary garbage pressure so every asserted round performs an actual collection.
Snapshots retain no editable Linked nodes. Source imports/reexports and a separately
compiled installed-JAR example must execute; its forward/reverse traversals retain
final values 22/20 after later replacement and clear. Missing fixtures are failures.
+173 -16
View File
@@ -41,6 +41,7 @@ fn main() {
for resource in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes,Q::StringBytes,Q::StringAllocations,Q::IndividualStringBytes,Q::CallDepth] {
println!("DEFAULT VM LIMIT {resource:?}={}",limits.get(resource));
}
if std::env::args().any(|arg| arg == "--linked-only") { verify_common_storage_fixtures(&root); verify_pbs_linked_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--list-cursor-only") { verify_common_storage_fixtures(&root); verify_pbs_list_cursor_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--generic-contracts-only") { verify_pbs_generic_contract_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--generic-structs-only") { verify_pbs_generic_struct_fixtures(&root); return; }
@@ -55,6 +56,7 @@ fn main() {
verify_pbs_generic_struct_fixtures(&root);
verify_common_storage_fixtures(&root);
verify_pbs_list_cursor_fixtures(&root);
verify_pbs_linked_fixtures(&root);
verify_pbs_resource_refusals(&root);
verify_pbs_closure_frames(&root);
for name in [
@@ -491,7 +493,7 @@ fn verify_pbs_fixtures(root: &std::path::Path) {
for entry in std::fs::read_dir(root).unwrap() {
let path = entry.unwrap().path();
let name = path.file_stem().unwrap().to_str().unwrap();
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || name.starts_with("pbs-generic-contract-") || name.starts_with("pbs-generic-struct-") || name.starts_with("pbs-list-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
if name.starts_with("pbs-random-") || name.starts_with("pbs-math-") || name.starts_with("pbs-structured-") || name.starts_with("pbs-generic-contract-") || name.starts_with("pbs-generic-struct-") || name.starts_with("pbs-list-") || name.starts_with("pbs-linked-") || !name.starts_with("pbs-") || path.extension().and_then(|v| v.to_str()) != Some("pbx") { continue; }
count += 1;
let bytes = std::fs::read(&path).unwrap();
let image = ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(LoadLimits::default()), caps::NONE)
@@ -1168,10 +1170,12 @@ fn verify_list_cursor_refusals(root:&std::path::Path) {
assert!(operand_preparation,"source operation operand/frame preparation refusal");
}
fn verify_list_cursor_gc(root:&std::path::Path) {
fn verify_list_cursor_gc(root:&std::path::Path) { verify_collection_cursor_gc(root,"list"); }
fn verify_collection_cursor_gc(root:&std::path::Path,collection:&str) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
use prometeu_vm::resource_diagnostics::StorageValue as V;
let bytes=std::fs::read(root.join("pbs-list-gc.pbx")).expect("mandatory snapshot GC fixture");
let bytes=std::fs::read(root.join(format!("pbs-{collection}-gc.pbx"))).expect("mandatory snapshot GC fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),RuntimeLimits::default());vm.prepare_boot_call().unwrap();
let mut previous=0;
@@ -1184,9 +1188,10 @@ fn verify_list_cursor_gc(root:&std::path::Path) {
prometeu_bytecode::registry::TypeKind::Array(_))),"Cursor retains no source or snapshot array");
let named=|name:&str|objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some(name)).collect::<Vec<_>>();
assert_eq!(named("_SnapshotNode<Item>").len(),nodes,"prefix nodes round {round}");
if collection=="linked" {assert!(named("_LinkedNode<Item>").is_empty(),"snapshot does not retain original Linked nodes");}
let values=named("Item");assert_eq!(values.len(),items,"captured object roots round {round}");
if items==1 {assert_eq!(values[0].groups[0],[V::Int32(1199)],"sticky last payload remains live");}
println!("PBS CURSOR GC PASS round={round} nodes={nodes} items={items} collected={}",snapshot.gc.reclaimed);
println!("PBS {} CURSOR GC PASS round={round} nodes={nodes} items={items} collected={}",collection.to_uppercase(),snapshot.gc.reclaimed);
}
let report=vm.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::Halted|End::EndOfRom));assert_eq!(vm.operand_stack_top(1),vec![Value::Int32(42)]);
@@ -1194,7 +1199,7 @@ fn verify_list_cursor_gc(root:&std::path::Path) {
fn verify_common_storage_fixtures(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
for name in ["common-storage","common-storage-optimized"] {
for name in ["common-storage","common-storage-optimized","common-fields","common-fields-optimized"] {
let bytes=std::fs::read(root.join(format!("{name}.pbx"))).expect("mandatory non-PBS storage fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image,RuntimeLimits::default());vm.prepare_boot_call().unwrap();
@@ -1205,24 +1210,176 @@ fn verify_common_storage_fixtures(root:&std::path::Path) {
}
}
fn verify_list_cursor_distribution(root:&std::path::Path) {
fn verify_list_cursor_distribution(root:&std::path::Path) { verify_collection_distribution(root,"list",22); }
fn verify_collection_distribution(root:&std::path::Path,collection:&str,last:i32) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
for name in ["imports","installed"] {
let bytes=std::fs::read(root.join(format!("pbs-list-{name}.pbx"))).expect("mandatory workspace/installed List fixture");
let bytes=std::fs::read(root.join(format!("pbs-{collection}-{name}.pbx"))).expect("mandatory workspace/installed List fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),RuntimeLimits::default());vm.prepare_boot_call().unwrap();
let report=vm.run_budget(1_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::FrameSync|End::Halted|End::EndOfRom),"{name}: {report:?}");
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let cursor=objects.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_SnapshotCursor<int>"))
.expect("distributed frame actually created its shared snapshot Cursor");
let V::Reference(current)=cursor.groups[0][0] else {panic!("nonempty Cursor retains its current node carrier")};
let carrier=objects.iter().find(|o|o.reference==current).unwrap();
let V::Reference(node)=carrier.groups[0][0] else {panic!("current points at a snapshot node")};
let node=objects.iter().find(|o|o.reference==node).unwrap();
assert_eq!(node.groups[0],[V::Int32(22)],"{name}: complete traversal retains the final value");
assert_eq!(node.groups[1],[V::Null],"{name}: final node has no successor");
println!("PBS LIST/CURSOR DISTRIBUTION LOAD/EXEC PASS {name}");
let cursors=objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_SnapshotCursor<int>")).collect::<Vec<_>>();
assert!(!cursors.is_empty(),"distributed frame actually created its shared snapshot Cursor");
let mut retained=Vec::new();
for cursor in cursors {
let V::Reference(current)=cursor.groups[0][0] else {panic!("nonempty Cursor retains its current node carrier")};
let carrier=objects.iter().find(|o|o.reference==current).unwrap();
let V::Reference(node)=carrier.groups[0][0] else {panic!("current points at a snapshot node")};
let node=objects.iter().find(|o|o.reference==node).unwrap();
let V::Int32(value)=node.groups[0][0] else {panic!("typed captured value")};
retained.push(value);
assert_eq!(node.groups[1],[V::Null],"{name}: final node has no successor");
}
retained.sort_unstable();
let expected=if collection=="linked"&&name=="installed" {vec![20,22]} else {vec![last]};
assert_eq!(retained,expected,"{collection} {name}: complete traversal retains each final value");
println!("PBS {} DISTRIBUTION LOAD/EXEC PASS {name}",collection.to_uppercase());
}
}
fn verify_pbs_linked_fixtures(root: &std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End, resource_budget::{RuntimeLimits,RuntimeResource}};
use prometeu_bytecode::TRAP_OOB;
let successful=["basic","links","construction","long-size","empty-cursor","empty-reverseCursor",
"single-cursor","single-reverseCursor","snapshot","false-present","optional-field","identity","tuple","nested-array","function","evaluation","vec2","pixel","contract"];
let faults=[("fault-read",TRAP_OOB),("fault-write",TRAP_OOB),("fault-negative",TRAP_OOB),
("fault-insert",TRAP_OOB),("fault-remove",TRAP_OOB),("fault-overflow",TRAP_OOB),("fault-long-index",TRAP_OOB)];
let cases=successful.into_iter().map(|n|(n,None)).chain(faults.into_iter().map(|(n,c)|(n,Some(c))))
.flat_map(|(name,fault)|[(name.to_string(),fault),(format!("{name}-optimized"),fault)]);
for (name,fault) in cases {
let bytes=std::fs::read(root.join(format!("pbs-linked-{name}.pbx")))
.unwrap_or_else(|e|panic!("missing mandatory SDK Linked fixture {name}: {e}"));
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE)
.unwrap_or_else(|e|panic!("Linked {name}: {e:?}"));
let mut vm=VirtualMachine::from_image(image,RuntimeLimits::default());
let observer=vm.resource_observer();
vm.prepare_boot_call().unwrap();
let report=vm.run_budget(1_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
if let Some(code)=fault {
let End::Trap(info)=report.reason else {panic!("{name}: {report:?}")};
assert_eq!(info.code,code,"{name}: {info:?}");
} else {
assert!(matches!(report.reason,End::Halted|End::EndOfRom),"{name}: {report:?}");
assert_eq!(vm.operand_stack_top(1),vec![Value::Int32(42)],"{name}");
}
drop(vm);
for resource in [RuntimeResource::GuestBytes,RuntimeResource::HandleSlots,RuntimeResource::AuxiliaryBytes,
RuntimeResource::TemporaryBytes,RuntimeResource::StringBytes,RuntimeResource::StringAllocations] {
assert_eq!(observer.used(resource),0,"{name}: final owner release {resource:?}");
}
println!("PBS LINKED SOURCE/LOAD/EXEC PASS {name}");
}
verify_linked_collection_gc(root);
verify_linked_refusals(root);
verify_collection_cursor_gc(root,"linked");
verify_collection_distribution(root,"linked",20);
}
fn linked_state(vm:&VirtualMachine,image:&ValidatedProgramImage)->Vec<prometeu_vm::resource_diagnostics::StorageObject> {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let root=objects.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("linked<int>"))
.expect("source Linked remains observable");
let mut pending=vec![root.reference];let mut state=Vec::new();
while let Some(reference)=pending.pop() {
if state.iter().any(|o:&prometeu_vm::resource_diagnostics::StorageObject|o.reference==reference) {continue;}
let object=objects.iter().find(|o|o.reference==reference).expect("live complete Linked group");
for group in &object.groups {for value in group {if let V::Reference(r)=value {pending.push(*r);}}}
state.push(object.clone());
}
state.sort_by_key(|o|(o.reference.index,o.reference.generation));state
}
fn verify_linked_refusals(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::{RuntimeLimits,RuntimeResource as Q}};
use prometeu_vm::resource_diagnostics::StorageValue as V;
use prometeu_bytecode::{decode_next,TRAP_RESOURCE_LIMIT};
use prometeu_bytecode::isa::core::CoreOpCode as Op;
let mut node_allocation=false;let mut optional_return=false;let mut snapshot_allocation=false;let mut preparation=false;
for name in ["insert","add","addFirst","remove","removeFirst","removeLast","replace","clear","cursor","reverseCursor"] {
let bytes=std::fs::read(root.join(format!("pbs-linked-refusal-{name}.pbx"))).expect("mandatory Linked refusal fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let marker=|value:i32| {
let mut pc=0;let mut found=Vec::new();
while pc<image.code().len() {let instruction=decode_next(pc,image.code()).unwrap();
if instruction.opcode==Op::PushI32&&instruction.imm_i32().unwrap()==value {found.push(pc);}
pc=instruction.next_pc;
}
assert_eq!(found.len(),1);found[0]
};
let before_pc=marker(314160);let after_pc=marker(314161);
let prepare=|limits| {let mut vm=VirtualMachine::from_image(image.clone(),limits);vm.prepare_boot_call().unwrap();
vm.insert_breakpoint(before_pc);vm.insert_breakpoint(after_pc);vm};
let mut baseline=prepare(RuntimeLimits::default());
assert!(matches!(baseline.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap().reason,End::Breakpoint));
let before=baseline.resource_snapshot();let original=linked_state(&baseline,&image);
baseline.remove_breakpoint(before_pc);
assert!(matches!(baseline.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap().reason,End::Breakpoint));
let after=baseline.resource_snapshot();let changed=linked_state(&baseline,&image);
let owner=changed.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("linked<int>")).unwrap();
let expected=match name {"insert"|"add"|"addFirst"=>33,"remove"|"removeFirst"|"removeLast"=>31,"clear"=>0,_=>32};
assert_eq!(owner.groups[2],[V::Int64(expected)]);
if name=="cursor"||name=="reverseCursor" {assert_eq!(changed,original);}
let mut refused=0;
for q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes] {
let lower=before.budget.peaks[q as usize];let upper=after.budget.peaks[q as usize];if lower>=upper {continue;}
let mut limits=vec![lower,lower+(upper-lower)/4,lower+(upper-lower)/2,upper-1];
if q==Q::HandleSlots {limits.extend(lower..upper.min(lower+24));}limits.sort_unstable();limits.dedup();
for limit in limits {
let mut vm=prepare(RuntimeLimits::default().lowered(q,limit).unwrap());let observer=vm.resource_observer();
let setup=vm.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(setup.reason,End::Breakpoint),"{name} setup {q:?}={limit}: {setup:?}");
let old=linked_state(&vm,&image);vm.remove_breakpoint(before_pc);
let report=vm.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
if let End::Trap(info)=report.reason {
assert_eq!(info.code,TRAP_RESOURCE_LIMIT,"{name} {q:?}={limit}: {info:?}");
assert_eq!(linked_state(&vm,&image),old,"{name}: all original fields, groups and reciprocal links at {q:?}={limit}");
let instruction=decode_next(info.pc as usize,image.code()).unwrap();
preparation|=q==Q::AuxiliaryBytes;
if instruction.opcode==Op::StructNew {
let declaration=&image.registry().declarations().types[instruction.imm_u32().unwrap() as usize];
node_allocation|=declaration.name.as_deref()==Some("_LinkedNode<int>");
snapshot_allocation|=declaration.name.as_deref()==Some("_SnapshotNode<int>");
if name.starts_with("remove") {
if let prometeu_bytecode::registry::TypeKind::Struct(fields)=&declaration.kind {
optional_return|=fields.len()==1&&matches!(image.registry().declarations().types[fields[0].type_id.0 as usize].kind,
prometeu_bytecode::registry::TypeKind::Builtin(prometeu_bytecode::registry::BuiltinId::INT32));
}
}
}
refused+=1;println!("PBS LINKED ATOMIC REFUSAL {name} resource={q:?} limit={limit} opcode={:?}",instruction.opcode);
} else {assert!(matches!(report.reason,End::Breakpoint),"{name}: {report:?}");}
drop(vm);for resource in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes] {assert_eq!(observer.used(resource),0);}
}
}
assert!(refused>0,"{name}: whole-source-operation refusals required");
}
assert!(node_allocation,"new Linked node refusal");assert!(optional_return,"optional removed-value refusal");
assert!(snapshot_allocation,"snapshot node refusal");assert!(preparation,"frame/operand preparation refusal");
}
fn verify_linked_collection_gc(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeLimits};
let bytes=std::fs::read(root.join("pbs-linked-gc-collection.pbx")).expect("mandatory Linked removal/clear GC fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),RuntimeLimits::default());vm.prepare_boot_call().unwrap();
let mut previous=0;
for (round,count) in [1200,1197,0,0].into_iter().enumerate() {
let report=vm.run_budget(30_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::FrameSync),"collection GC {round}: {report:?}");
let collections=vm.resource_snapshot().gc.collections;assert!(collections>previous,"real collection at round {round}");previous=collections;
let objects=vm.heap().diagnostic_typed_storage();
for name in ["_LinkedNode<Item>","Item"] {
let actual=objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some(name)).count();
assert_eq!(actual,count,"{name} after removal/clear at round {round}");
}
println!("PBS LINKED COLLECTION GC PASS round={round} nodes={count} items={count}");
}
let report=vm.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert!(matches!(report.reason,End::Halted|End::EndOfRom));assert_eq!(vm.operand_stack_top(1),vec![Value::Int32(42)]);
}
+3 -1
View File
@@ -15,6 +15,7 @@ parser.add_argument("--release", action="store_true", help="Execute an optimized
parser.add_argument("--math-only", action="store_true", help="Run the complete PBS math corpus only")
parser.add_argument("--random-only", action="store_true", help="Run the complete PBS Random corpus only")
parser.add_argument("--generic-contracts-only", action="store_true", help="Run the mandatory generic-contract source corpus only")
parser.add_argument("--linked-only", action="store_true", help="Run mandatory SDK Linked corpus only")
parser.add_argument("--list-cursor-only", action="store_true", help="Run the mandatory SDK List/Cursor corpus only")
parser.add_argument("--generic-structs-only", action="store_true", help="Run the mandatory generic-struct/function source corpus only")
args = parser.parse_args()
@@ -32,6 +33,7 @@ for source in [pathlib.Path(__file__), pathlib.Path(__file__).with_name("main.rs
print("runtime", subprocess.check_output(["git", "-C", str(runtime), "rev-parse", "HEAD"], text=True).strip(), flush=True)
print("runtime working tree", subprocess.check_output(["git","-C",str(runtime),"status","--short"],text=True).strip() or "clean",flush=True)
for source in [pathlib.Path("prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsListCursorTest.java"),
pathlib.Path("prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsLinkedTest.java"),
pathlib.Path("prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs"),
runtime / "crates/console/prometeu-vm/src/virtual_machine/storage_publication.rs"]:
print("sha256",source,hashlib.sha256(source.read_bytes()).hexdigest(),flush=True)
@@ -50,4 +52,4 @@ with tempfile.TemporaryDirectory(prefix="pbx-conformance-") as directory:
(root / "Cargo.lock").write_text((runtime / "Cargo.lock").read_text())
(root / "src").mkdir()
(root / "src/main.rs").write_text(pathlib.Path(__file__).with_name("main.rs").read_text())
subprocess.run(["cargo", "run", *(["--release"] if args.release else []), *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json"), *(["--math-only"] if args.math_only else []), *(["--random-only"] if args.random_only else []), *(["--generic-contracts-only"] if args.generic_contracts_only else []), *(["--generic-structs-only"] if args.generic_structs_only else []), *(["--list-cursor-only"] if args.list_cursor_only else [])], check=True)
subprocess.run(["cargo", "run", *(["--release"] if args.release else []), *([] if args.online else ["--offline"]), "--manifest-path", str(root / "Cargo.toml"), "--", str(args.fixtures.resolve()), str(runtime / "devtools/headless-protocol/response.schema.json"), *(["--math-only"] if args.math_only else []), *(["--random-only"] if args.random_only else []), *(["--generic-contracts-only"] if args.generic_contracts_only else []), *(["--generic-structs-only"] if args.generic_structs_only else []), *(["--list-cursor-only"] if args.list_cursor_only else []), *(["--linked-only"] if args.linked_only else [])], check=True)