Compare commits

...
2 Commits
Author SHA1 Message Date
bquarkz 01a1968689 feat(pbs): add Map and read-only Keyval associations
Intrepid/Prometeu/Studio/pipeline/pr-master This commit looks good
Test / Tests and coverage
2026-10-26 20:37:06 +00:00
bquarkz 8105604f92 feat(pbs): add Set and reusable identity hashing 2026-10-25 12:15:13 +00:00
82 changed files with 8229 additions and 43 deletions
@@ -0,0 +1,218 @@
---
id: LSN-0093
ticket: pbs-sdk-set
title: Reusable identity and source Set buckets
created: 2026-10-25
tags: [compiler-pbs, collections, hashing, identity, runtime-integration]
---
## Original problem and ownership
Owner: `compiler/pbs`; propagation: `compiler` and `vm-arch`.
Set needed a precise equality policy before membership could become an executable
SDK algorithm. Mutable objects must retain instance identity by default, while an
explicit strategy can intentionally merge distinct objects by content. Importing
hash metadata alone did not make source hashing, typed reference queries or Set
available to PBS consumers.
AGD-0094 converged into accepted DEC-0078 and implementation PLN-0156. The user
accepted the recommendations, authorized the decision and plan, and requested
implementation. These identifiers record provenance; normative behavior now lives
in the [Set/Identity specification](../../../docs/compiler/pbs/specs/sdk-set-and-identity.md).
Completed workflow scaffolding is removed by the discussion CLI.
The runtime provider is `dcf6561ea73446e0a3e1cf1c73eda20923b38df0`, consumed
unchanged. No runtime source, documentation, catalog, manifest, lockfile or commit
was changed. Cargo projects/targets used for qualification live outside its
checkout. Ordinary Studio builds use versioned local SDK/catalog resources.
## Consolidated decision and final result
`@sdk:collections` exports invariant ordinary `set<T>` and nominal `Identity<T>`.
Identity supplies `hash(T)->long` and `equals(T,T)->bool`. Set exports
add/contains/remove/clear/size/isEmpty/cursor, with either a default constructor or
an explicit service/struct strategy. Constructor arguments do not infer T.
Defaults use value equality for int/long/bool, exact UTF-8 content for str, finite
Float equality with normalized signed zeros, and instance identity for
struct/array/Fn/Random. Contract identity uses its fully validated receiver,
independent of the view/implementation. Tuples and other native values require an
explicit strategy. Direct optional elements, including grouped optionals, are
invalid; optional Sets and optional fields within elements remain valid.
```pbs
import {set, Cursor} from @sdk:collections;
declare struct Enemy(pub mut health: int);
fn sample() -> long {
let a = new Enemy(20);
let b = new Enemy(20);
let enemies: set<Enemy> = new set();
enemies.add(a);
enemies.add(b); // A distinct instance, despite equal fields.
enemies.add(a); // False; keeps the first stored reference.
a.health = 42; // Instance identity is stable across mutation.
let saved: Cursor<Enemy> = enemies.cursor();
enemies.clear(); // The snapshot still retains the shared Enemy references.
return enemies.size();
}
```
An explicit content strategy may collapse a and b. It must be an equivalence
relation, remain stable while stored, and satisfy equal values => equal hashes.
Remove from every affected collection before changing content used by that policy,
then reinsert. Cached hashes do not freeze mutable equality. The compiler does
not prove purity or coherence and does not deep-copy payloads.
The shared public policy is reusable by Map. Map/keyval API and representation
remain a separate discussion; Set does not impose its physical table on Map.
## Patterns and algorithms
### Integrate neutral identity before the collection
Common IR now represents REF_IDENTITY_HASH 0x97 and REF_IDENTITY_EQ 0x98 with static
source TypeIds. Lowering derives complete group widths; encoding uses one/two
little-endian TypeIds. Producer verification checks eligible categories even in
dead code and consumes the complete operands. Existing conversion instructions
retain their narrower categories.
The exact eligible categories are Struct, Array, Fn, Trait and Random. Trait
queries validate receiver plus ImplementationId, including valid zero, before
using receiver identity. Equality validates both operands left-to-right: an absent
or different left operand cannot hide an invalid right operand. Invalid/stale
values remain faults. Digests neither root objects nor provide persistent IDs.
The new `@sdk:hash` facade exposes i64/f64/text plus explicitly applied
referenceHash/referenceEquals. Origin-checked private bindings select default
hash/equality at concrete logical types. Unsupported default construction is a
localized diagnostic; the unreachable default branch of an explicitly configured
Set does not reject valid tuple/native strategies. User declarations with matching
names or attributes receive no SDK privilege.
The source-to-PBX-to-production-VM hash gate ran before Set algorithms were
written. The initial gate covered 60 scalar/String oracle executions and 14
reference executions; later qualification expanded admission/right-side and
discarded-reference cases. Temporal gate provenance and retained final logs are
distinguished in [the evidence record](evidence/README.md).
### Cache full hashes and build independent growth candidates
Private `_SetNode<T>` stores a full signed long hash, complete T and next link.
Buckets are an optional array. Empty construction has no table; the first
successful inclusion allocates eight buckets. Above 75% next occupancy, capacity
doubles, clamped to Array's u32 maximum. At that maximum, chains may continue
within checked signed-long size and resource limits. Removal does not shrink;
clear releases the table and returns to lazy empty state.
Use signed remainder followed by correction for negative values. Taking abs of
Long.MIN_VALUE or narrowing a digest to int breaks valid inputs, and clamped
capacity is not necessarily a power of two.
Duplicate detection precedes growth. Rehash copies independent nodes from cached
hashes, preserving original links and shared payload references until installation.
It invokes no strategy callbacks. Ordinary insertion does not copy the whole
table. Capture likewise never calls hash/equals.
The six-to-seven growth case simultaneously retained six old nodes, six copied
nodes and the inserted node, plus both tables. GuestBytes high-water rose from
21,304 to 44,192 in the measured fixture. These are charged fixture/session peaks,
including generic carriers and call preparation, not a universal per-Set bound
or a timing benchmark. Detailed resource vectors are retained with the evidence.
### Separate guard writes from structural mutation
add/contains/remove install a guard before invoking Identity. Reentrant
add/remove/clear/contains on the same Set, including aliases, terminate through
TRAP_EXPLICIT. size/isEmpty/cursor may inspect the consistent pre-mutation
structure during callbacks and do not invoke Identity. Normal return releases the
guard; a callback fault may leave it installed.
Prepare checks, callbacks, next size, nodes/tables and complete values before
structural stores. Then apply ordinary sequential stores without user callbacks.
The [shared failure-phase contract](../../../docs/compiler/pbs/specs/sdk-set-and-identity.md#guard-mutation-phases-and-faults)
is essential:
| Outcome | Meaning |
| --- | --- |
| Refusal before structural writes | Old elements/topology survive; guard, callback effects and provisional allocations are not rolled back |
| Successful return | Size, table, groups, links and guard satisfy all invariants |
| Fault after structural writes | Earlier writes and incomplete invariants may remain; no recovery, safe-reuse or rollback guarantee |
The natural quota corpus observed 282 refusals across paired producer variants,
all before structural mutation. That observation does not prove post-write faults
impossible. A separately named test-only in-memory SDK variant inserts an Array
allocation after the real table installation. Its quota fault leaves capacity 16,
seven reachable nodes, old size six and the active guard. The failed instruction
itself preserves its entry state. Production SDK source contains no such probe.
Budget/debugger pauses are resumable instruction boundaries. They do not make a
source method an atomic observation region.
### Reuse the entire snapshot protocol
Capture builds one private immutable singly linked snapshot node per element.
It scans B buckets and N nodes, costs O(B+N) work and O(N) extra nodes, and returns
only a complete Cursor. Payload references stay shared; later Set mutation cannot
rewrite the captured sequence. There is no iteration-order promise.
A nonempty Cursor starts at its first value, hasNext tests a successor, and next
without a successor retains the final value. Only empty-born cursors have absent
current. Real GC safepoints verified snapshot node/payload retention
1,200 -> 1 -> 1 -> 0, and source Set node reclamation 64 -> 61 -> 0 -> 0.
### Preserve logical identity across specialization and modules
This consumer exposed several reusable compiler pitfalls. Concrete implementations
from the same generic family share a source span; deduplicating by span alone
loses later applications. Deduplication now includes logical owner and contract
keys. Raw TypeRef equality would instead distinguish incidental spans and produce
duplicates.
An already specialized type used as another module's generic argument still needs
its defining dependency. Scoped if-let bindings, member receiver shapes and field
assignment paths must also carry their symbolic types through specialization.
Expected value contexts now reject mismatched nominal generic applications, such
as list<int> assigned to list<long>; Set and ordinary user generic structs obey
the same invariant rule. Negative integer literals retain contextual long typing
during overload selection instead of falling through to an incorrect call path.
## Validation and limits of evidence
The [evidence record](evidence/README.md) preserves commands, raw compressed logs,
SHA-256 manifests, generated fixture fingerprints and the regression report.
Source fixtures execute through the production loader/VM with ordinary and
optimized PBS output, independently of Rust debug/release profiles. Installed
examples compile outside the checkout using installDist JARs and their PBX runs
on the same unchanged provider. LSP projections expose concrete signatures and
hide fields/private helpers.
Qualification is local macOS/arm64. It does not claim other hosts, installed game
deployment, adversarial hash resistance or a performance threshold. Existing
upstream headless-schema gaps are printed explicitly by the harness; their
historical exceptions are not counted as schema conformance. Existing disabled
JUnit cases are listed separately from the mandatory runtime corpus.
## Pitfalls to avoid
- Do not compare carrier handles to implement logical tuple/value equality.
- Do not reinterpret valid zero/false payloads as absence.
- Do not make dead generic default branches reject explicit strategies.
- Do not invoke strategy callbacks during rehash/capture or structural updates.
- Do not translate resource faults into false/none or silently reset a faulted guard.
- Do not infer whole-method rollback from single-instruction refusal preservation.
- Do not present test-only fault injection as a natural production allocation.
- Do not feed new named corpora through a legacy catch-all fixture verifier;
each mandatory corpus has its own assertions and count.
## References
- [Set/Identity/hash specification](../../../docs/compiler/pbs/specs/sdk-set-and-identity.md).
- [Shared Cursor specification](../../../docs/compiler/pbs/specs/sdk-list-and-cursor.md).
- [Common Registry/PBX specification](../../../docs/compiler/general/specs/typed-registry-and-pbx-v0.md).
- [SDK collection source](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs).
- [Set source fixtures](../../../prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsSetTest.java).
- [Conformance tooling and reproduction](../../../tools/pbx-conformance/README.md).
- [Sequential mutation predecessor](../DSC-0096-runtime-storage-isa-consumer-migration/LSN-0092-sequential-collection-mutations-need-explicit-failure-boundaries.md).
@@ -0,0 +1,156 @@
# Set and reusable identity qualification
Owner: compiler/pbs. Captured 2026-10-25 for accepted DEC-0078 / PLN-0156.
Studio baseline HEAD: `632ba6d0d6988a5e5020f0d70e4fb3993739bc85`.
Runtime: clean `dcf6561ea73446e0a3e1cf1c73eda20923b38df0`, unchanged throughout.
No commit was created in either repository. Runtime builds used external targets.
## Commands and outcomes
All commands ran from Studio unless a runtime working directory is specified.
The compressed raw outputs and SHA-256 manifest are alongside this file.
```sh
./gradlew test :prometeu-app:installedKnowledgeTest --console=plain
python3 tools/runtime-catalog/verify.py --runtime ../runtime
CARGO_TARGET_DIR=/tmp/pln156-pbx-target python3 tools/pbx-conformance/run.py --runtime ../runtime
CARGO_TARGET_DIR=/tmp/pln156-pbx-target python3 tools/pbx-conformance/run.py --runtime ../runtime --release
```
All four commands returned zero. Gradle completed in 46 seconds: 94 actionable
tasks, 21 executed and 73 up-to-date. Aggregated JUnit reports contain 1,062 cases,
zero failures/errors and 13 existing skipped cases (1,049 executed cases). Skips
belong to BackendGateIIntegrationTest (8), MainProjectPipelineIntegrationTest (4)
and ExternalSourceDevelopmentHostTest (1); individual names and suite counts are
in `gradle-results.json`. None belongs to the new identity/Set tests. Installed
knowledge validation additionally completed outside the checkout using the built
distribution JARs and produced the mandatory hash/Set PBX examples.
Focused class sizes in the full regression: IdentityHashingPipelineTest 3,
PbsIdentityHashingTest 3, PbsSetTest 15. PBS frontend totals 300; architecture totals
115; build-pipeline totals 200 including its 12 existing skips. The complete
regression also covers editor/LSP, Array, List, Linked, generic structs/contracts,
native groups, Random, compiler architecture and ordinary packaging.
Both complete production VM harness commands passed. Each includes the following
new mandatory observations (no missing fixture is skipped):
| Corpus | Count per Rust profile |
| --- | ---: |
| Scalar/String independent oracle + exact attempted cost + zero fuel | 60 |
| Complete reference/common/source groups, aliases, costs, unused results | 16 |
| Neutral category/TypeId/dead-code admission rejects | 8 |
| Partial group / truncated instruction admission rejects | 2 |
| Null views / complete invalid right operand / cost / fuel | 3 |
| Set success and expected callback/overflow faults, paired producers | 52 |
| Natural Set quota refusals with instruction-prefix comparison | 282 |
| Separate test-only post-publication quota injection | 2 |
| Snapshot GC observations / source Set GC observations | 4 / 4 |
| Real Set import/reexport and installed-example executions | 2 |
| Real installed hash-example execution | 1 |
The full harness additionally requires the existing common Registry, math, Random,
input, structured values, generic contracts/structs, storage/copy, List/Linked,
closure/lifecycle and loader/schema corpora. Historical upstream schema exceptions
remain explicit: LifecycleRole and NonFiniteValue are absent from the selected
provider's published headless enum. The harness preserves the original payload
and does not call those two cases unmodified-schema conformance.
Provider supplement (runtime working directory; external target only):
```sh
cargo test --locked --offline --target-dir /tmp/pln156-provider-target -p prometeu-vm --test identity_hashing
cargo test --locked --offline --target-dir /tmp/pln156-provider-target -p prometeu-vm --lib identity_tests
cargo test --locked --offline --target-dir /tmp/pln156-provider-target -p prometeu-vm --lib 'hashing::'
```
The three suites passed 7 + 6 + 2 tests in debug and again with `--release`.
These provider tests cover stale handles/generation reuse, complete invalid
groups, fault order, non-finite injected Float bits, foreign/resident String
owners, quota-free borrowed queries, GC non-rooting and all independent reference
hash vectors. They supplement actual Studio-produced PBX; they do not substitute
for it. Two exploratory `--lib hash_intrinsics::` filters in the retained combined
log selected zero tests and provide no additional qualification.
The explicit catalog exporter check passed one provider test and compared both
CSV artifacts byte-for-byte against committed provider source and Studio mirrors.
## Resource observations
Ordinary and optimized producers produced identical resource vectors. These are
whole-fixture/session high-water measurements, including earlier setup, complete
carriers and call preparation, not standalone object layout sizes.
| Operation | GuestBytes peak before → after | HandleSlots peak before → after | Maximum Set nodes / bucket tables | Natural refusals per producer |
| --- | --- | --- | --- | ---: |
| Growth 6 → 7 | 21,304 → 44,192 | 68 → 140 | 13 / 2 | 38 |
| Add without growth 5 → 6 | 17,288 → 22,216 | 55 → 71 | 6 / 1 | 28 |
| Duplicate add | 17,288 → 19,440 | 55 → 62 | 5 / 1 | 13 |
| Remove | 17,288 → 20,680 | 55 → 66 | 5 / 1 | 21 |
| Clear | 17,288 → 17,592 | 55 → 56 | 5 / 1 | 5 |
| Cursor | 17,288 → 31,808 | 55 → 102 | 5 / 1 | 36 |
Growth's AuxiliaryBytes peak was 9,968 → 17,648, TemporaryBytes 3,840 → 7,680
and CallDepth 5 → 7. The eight raw peak entries are GuestBytes, HandleSlots,
AuxiliaryBytes, TemporaryBytes, StringBytes, StringAllocations,
IndividualStringBytes and CallDepth. Detailed limits, fault opcodes and complete
vectors are printed in both profile logs. Every natural refusal was before a
structural write; an installed guard may remain. Snapshot failures always
preserved the source Set and returned no partial result.
The independently named `pbs-set-injected-refusal-growth*` fixtures use an
in-memory SDK override that inserts one controlled Array allocation after the
real candidate table store. A GuestBytes quota refuses the probe: the source now
references a 16-bucket table with seven nodes, still reports old size six and
retains its guard. The failing instruction preserves its own entry state. This
is explicitly test-only post-write evidence, not production refusal frequency.
The production SDK has no injected allocation. Real safepoints measured snapshot
nodes/payloads 1,200 → 1 → 1 → 0 without retained source Set nodes, and source
collection nodes 64 → 61 → 0 → 0 after removal/clear/release. Captures and their
final values assume no iteration order.
## Hashing-first chronology and scope
The plan recorded a successful gate on 2026-10-24 before any Set algorithm edits:
60 scalar/String oracle executions plus 14 common/source reference executions.
At that stage IdentityHashingPipelineTest had two tests and
PbsIdentityHashingTest three. The original `/tmp/pln156-hashing-*` log names were
subsequently reused for expanded requalification. Retained focused logs therefore
show the later 89-observation corpus; they are not represented as untouched raw
outputs from the initial temporal gate. This chronology preserves the original
plan's contemporaneous record without fabricating an earlier log.
Final scope: local macOS 27.0.1, arm64; Rust 1.98.1; Java/Gradle toolchain recorded
in `environment.txt`; VM resource-diagnostics feature and default production
ceilings except explicitly lowered quotas. Source optimizer paths are independent
of Rust runtime profiles. Cross-host, installed game deployment, performance and
adversarial hash resistance are not qualified by these runs.
`source.sha256` fingerprints 39 changed/new Studio production, test and documentation
files. `fixtures.sha256.gz` fingerprints 1,301 generated PBX artifacts available to
the full harness; regenerate them with the Gradle command. `gradle-results.json`
preserves all suite counts and skipped names. `SHA256SUMS` fingerprints retained
evidence files. Logs can be read with `gzip -dc <file>.gz`.
## Workflow closure
`discussion mark-plan PLN-0156 done` completed successfully, followed by
`discussion housekeep DSC-0092 --lesson discussion/lessons/DSC-0092-pbs-sdk-set/LSN-0093-reusable-identity-and-source-set-buckets.md`.
The CLI removed three completed control artifacts and registered LSN-0093.
`discussion validate` passed afterward; DSC-0092 is done with only its lesson
registered. AGD-0089 and AGD-0095 reference the delivered result and keep Map's
remaining questions open. Final read-only workflow state is in `workflow.txt`.
## Accepted requirement coverage
| Requirements | Final evidence |
| --- | --- |
| R1 / R3 | Source API/default/explicit/type/privacy tests, generic invariance, concrete editor projections and installed examples |
| R2 / R5 | Neutral IR instruction/width/encoding tests, oracle and complete-group source execution, malformed instructions and source-order fixtures |
| R4 | Topology assertions, 8 → 16 threshold, duplicate/no-shrink/clear, signed-long/u32 arithmetic and cached rehash counters |
| R6 / R7 | Alias reentrancy faults, permitted callback reads, callback-fault guard observations and natural/injected instruction-prefix resource evidence |
| R8 | Empty/single/multiple complete Cursor state table, payload sharing, independent capture and real GC lifetime |
| R9 / R10 | Hashing-first chronology, paired producer/profile runs, provider supplement, collision/callback counters and measured coexistence |
| R11 | Public Identity/hash handoff to Map without Map API/layout implementation |
| R12 | Clean fixed provider, external targets, English specs/lesson, packaged SDK/examples, full regression and CLI validation/housekeeping |
@@ -0,0 +1,19 @@
01b966b6a3f710c150c960c6a5b49f1b45d1db93fe7d5fa536db761f3489dfad README.md
fb48082c878378f7fa01009361380b38be074a0b711edc605c531c9fb575ff5a conformance-debug.log.gz
4e5b52584c70bca57c3b715558102b663daf51cf84bebf549600bdf11c88cab4 conformance-release.log.gz
774b5aa6f29a3e4d986bab805427b7d0a0c53445749e03a93db4320cee376302 distribution-tests.log.gz
1510a88944e6a632e4373d35e3e5855a06a1273f7f0be3b590d811faa209d830 environment.txt
5b136af3ee037ba0879c02cc57c135350ed446853dedfd41a7455a532e4852e6 fixtures.sha256.gz
b7a05d6c252b869cd2a909edd40ef16a094279d6aee4379daa67b607753cfe2b gradle-results.json
3e4e33aa6338142b79a4740945c50b149c572980ad108219d930e3909dd73f8a hashing-conformance.log.gz
6c4a1723e53ad40a2315a509a43a6a0058db145cc006ee4bc7a346cd12333fee hashing-tests.log.gz
79b81e38a9ecf948c7381796c77aa30929dbcfcd2f4eb0a5ead71032fbb5bf04 provider-catalog.log.gz
2f53ddd02d054c4c801aeaf20981f83eb28961b9b973fd025c10aed6673dd254 provider-hash-remaining.log.gz
0d2a846ee52a7a40662c1720055ffdf36179cdbaf846696e07d7ab0149d4367d provider-identity-debug.log.gz
0255d42eb28c98566741fa17987480969ab89b35a113c568481240713f69aa1e provider-identity-private-debug.log.gz
f6ca7e0c4865b83031d8f21e166248209089a91466103d193cb14ffd8e533df7 regression.log.gz
9f9bf7af13da11147a1388ea86a41f8e59202b4d08333dca24d4385d5329dc21 set-conformance.log.gz
60b879014ec61aad58eb6aba651e28fb017077c3c17a6f06b2851f99be50ca72 set-tests.log.gz
2d0b3b19a447fa29a4ae127842db26d4adacb400af77837c09edd8ea0c9874c3 source.sha256
0e245cc1d83d697f9ffb3fb0a8dd18800cdc0892c23def0d4e14737c3cb42cbc workflow.txt
8ee423550b4716622600a4a943084837961ff9587259f2ff485e25d30005e434 ../LSN-0093-reusable-identity-and-source-set-buckets.md
@@ -0,0 +1,32 @@
macOS-27.0.1-arm64-arm-64bit-Mach-O
632ba6d0d6988a5e5020f0d70e4fb3993739bc85
dcf6561ea73446e0a3e1cf1c73eda20923b38df0
runtime working tree: clean
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: aarch64-apple-darwin
release: 1.98.1
LLVM version: 22.1.8
------------------------------------------------------------
Gradle 9.3.1
------------------------------------------------------------
Build time: 2026-01-29 14:15:01 UTC
Revision: 44f4e8d3122ee6e7cbf5a248d7e20b4ca666bda3
Kotlin: 2.2.21
Groovy: 4.0.29
Ant: Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM: 21.0.4 (Amazon.com Inc. 21.0.4+7-LTS)
Daemon JVM: /Users/niltonconstantino/.sdkman/candidates/java/21.0.4-amzn (no Daemon JVM specified, using current Java home)
OS: Mac OS X 27.0.1 aarch64
Java toolchain: buildSrc/src/main/kotlin/gradle.java-common-conventions.gradle.kts sets 21
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,39 @@
b86f24c8ce609b0994b9fcfabfff0ec2a78a0fafaf242b931db10ba2325d30fb docs/compiler/general/specs/runtime-catalog-support.md
cbaec6777d4a41bff62486763a084aaec4fd5a7c9cacbf7c34ec83b50e8c208c docs/compiler/general/specs/typed-registry-and-pbx-v0.md
e61f3a1a2d3ebe17f680dddee805de56f8e57356a98ea3821885da1ba70d1d98 docs/compiler/pbs/specs/generic-contracts.md
da61fca1efa1410ea1e57452724c022a492356836ca1a113e2668b8255596d85 docs/compiler/pbs/specs/sdk-list-and-cursor.md
73de7f4148daf4f40050c503bed8c118f9422a14239082b3e1501ea66fbf509b docs/compiler/pbs/specs/sdk-set-and-identity.md
ca7103dbe6613b4501f9a8c8ca210a84b7ce37eb0e1b27e0321dc1b31588e427 docs/specs/compiler-languages/pbs/README.md
9e6eb38e60753b9e4b092f14d0aeec9debb7023ee706a8ff4b81d3380334e741 prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeProbe.java
0d29ca282e27488852f35ec36ac858cc23dc69e4acc31812f1c0cb4a49188357 prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsIdentityHashingTest.java
9cbdfc340dba2467388c09958bb436fc319091b2948ca8b353b0d714b56e7f5e prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsListCursorTest.java
980b429d3c3c823a03e3aa6d63affc7773eda8f260fe72501999bc13aab0682c prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsSetTest.java
a863486eaea14ab1a010f076f355eed0d5738207dad3651f7d10e92fc09f74d4 prometeu-architecture-tests/src/test/resources/hash/hash-conformance.tsv
9a7d614a6f50b2368d66c2bf7c8b7d706ca609c555c2ed8f1bf447c2568edca8 prometeu-architecture-tests/src/test/resources/hash/provenance.txt
3ab546b5e93345442550e385e271b7152aa6a749f1d2b7a7cab3b9b29521c76d prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsContractTypes.java
dae77c30a768f709a95b10a909e6b17c82fbf5ed81108c475f8ed07921eb2168 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsGenericInstantiation.java
3d771f40ebf91919a6e9f2c0df54aa1de49d7f4daa992fb8ceb44a9db4faaf3e prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsGenericTemplateValidator.java
dd0202c2867f9175778bab0476d91dc75fff1f6ac2b56546016461485f3cb9ad prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsSdkCollections.java
0f5a2a8a1a23519ee315d87703e83db33c1737b1d2213be18387ee64ee2b84be prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsExecutableBodyLowerer.java
3f713dfc25a34b6b1cdc40ebf74b73d66292ff55704844b81058650a88838bef prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsRuntimeTypes.java
09b812332999c41161741debb61e509777e5c35661526b14ce5ac1b8ea0db69c prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/parser/PbsDeclarationParser.java
6c2f87d0f4409823474df16704d6a3394ad386d8c04402d36e15970799b47150 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/semantics/PbsEditorialSupportService.java
d3d656a09c5b422b04b69fb8e6178be925e368b39dd52a48ca79d174dad0eb18 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/semantics/PbsFlowExpressionAnalyzer.java
af43f155ac52d777f0ce33d7abe178175493cf215cdb05fb58c00faeea7d5f64 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/semantics/PbsFlowSemanticSupport.java
0a812aa055a0584a2994cf7e03432e7a793ed3dafa46fb0d99649aa87b61e5fa prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs
282bf48949e9541ac0c2b8ac8f22c32c88c2b989db47e735d93986e1be5aa35d prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/mod.barrel
5fefadf0c171849db43a84a8e96ee9b65f9085630c54fc2605b478b052202f3e prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/hash/main.pbs
96c5efc77fa829cd1ada41551455d5ab702af7d93003bff166a917cfe94d5c91 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/hash/mod.barrel
51a535c87ea1f6cfe63921c16116325e98ff5b792c52d29c554708f6f747fbbe prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/hashing.pbs
f41f867226127c663a4aff3429c482bab5d685dbdeede7dbb156fd31e8056cb4 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/mod.barrel
af301f0399bbf960efb011bc9a64bc203198b5dd808cfce4e726d28460f081a5 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/sets.pbs
b2f68ef00a5d2f2b2959ca7b35293b3ebbddaccac6c7983800a3ce2686dfd315 prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsGenericStructsTest.java
b012912e78dac60d7f87b0133c4a40802acbe8f8470a64aa599e01a745da894f prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/semantics/PbsListCursorEditorialTest.java
6924220bb90f36d8e14a7e58a9b645863661fa5244ec2984f380dcdcc135a458 prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodeOpcodeLayout.java
ff70c90b3803325c21a9c8b7c9614d18df6e70950e2ad04f182e0d02a3ee11bf prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/bytecode/BytecodePreloadVerifierService.java
ad66da924ae8a41358f3ccfe31fab2e69e961892bc24dab2dbeb8aa148cfb20e prometeu-compiler/prometeu-build-pipeline/src/main/java/p/studio/compiler/backend/registry/RegistryInstructionLowerer.java
b78a89315c3335d13a14e4fae67025844ca06ebdd6cd6450333550a7ffe63670 prometeu-compiler/prometeu-build-pipeline/src/test/java/p/studio/compiler/backend/registry/IdentityHashingPipelineTest.java
5b65afa2b540d9322e66e51618e3b30ef48773c21b3b22c449f96151fabff4a4 prometeu-compiler/prometeu-frontend-api/src/main/java/p/studio/compiler/models/registry/IRRegistryInstruction.java
a97ccf7cf1ef9cc955b4c8149cc4617a0c5d1e7d5bbb1568bf747ed6c8c901a1 tools/pbx-conformance/README.md
7034976755c6231001c716190dee2bb45a8fc5123dfe99cbb88eff3cddce6a4a tools/pbx-conformance/main.rs
314882419dcab726c9944450e114e8da92348a82976cfb64a4157c47485ddb83 tools/pbx-conformance/run.py
@@ -0,0 +1,10 @@
$ discussion validate
validation passed
$ discussion inspect DSC-0092
DSC-0092 done pbs-sdk-set
PBS SDK Set
tags: compiler-pbs, collections, umbrella-agd-0089
lessons:
- LSN-0093 done exists /Users/niltonconstantino/personal/workspace.personal/intrepid/prometeu/studio/discussion/lessons/DSC-0092-pbs-sdk-set/LSN-0093-reusable-identity-and-source-set-buckets.md
@@ -0,0 +1,194 @@
---
id: LSN-0094
ticket: pbs-sdk-map-keyval
title: Map presence, key identity and captured associations
created: 2026-10-26
tags: [compiler-pbs, collections, map, keyval, identity, optional, cursor]
---
# Map presence, key identity and captured associations
**Worker:** Lesson Worker. **Owner domain:** `compiler/pbs`.
**Discussion:** DSC-0093, accepted DEC-0079 implemented by PLN-0157 (R1–R12).
The normative contract is [SDK Map and Keyval](../../../docs/compiler/pbs/specs/sdk-map-and-keyval.md).
The [evidence record](evidence/README.md) preserves reproduction commands, source
and fixture fingerprints, raw logs and requirement coverage after housekeeping.
## Original problem
Map needed to distinguish association existence from the content of its value,
define which object represents an equivalent key, and transport read-only entries
without exposing mutable hash storage. Set had already delivered reusable Identity
and hashing, while List supplied a shared snapshot Cursor. Neither delivery
automatically determined Map's API, representation or mutation guarantees.
The accepted result is a source SDK Map with its own bucket nodes, an ordinary
generic Keyval association, optional lookup/previous-value results and independent
entry snapshots. The implementation uses existing typed Registry/PBX/VM contracts;
the runtime checkout was not modified.
## Separate presence from payload
`m[key]`, `put(entry)` and `remove(key)` return optional V. The outer optional
answers whether an entry existed. V may itself be optional:
| Stored state | Lookup or previous-value result |
| --- | --- |
| No association | none |
| Present association, V=none | some(none) |
| Present association, V=some(42) | some(some(42)) |
For example, inside a function:
```pbs
let scores = new map<str, optional int>();
scores["pending"] = none;
if scores.containsKey("pending") {
if let payload = scores["pending"] {
// payload is optional int and is currently none.
}
}
```
Assigning none does not remove an entry. remove is the structural operation.
Zero/false are present values, and faults never become none. K directly optional
is rejected consistently with Set; independent `keyval<K,V>` remains an ordinary
generic struct and can hold optional components. The restriction on
`array<optional T>` does not prohibit a V field in a private node.
## Preserve the stored key representative
Default Identity follows the [Set policy](../../../docs/compiler/pbs/specs/sdk-set-and-identity.md):
scalar/String values use their specified value domains; eligible objects use
instance identity; contracts use their validated receiver. Unsupported defaults
require an explicit Identity<K>. V needs no equality/hash capability.
A content strategy can consider distinct objects equivalent. If a map already
stores object A and a write supplies equivalent object B, replacement changes
only V. A remains the key returned by subsequent snapshots; size and capacity do
not change. To make B the representative, remove and reinsert. This rule avoids
silently replacing object identity while updating an association's value.
An explicit strategy must remain coherent and stable while keys are stored.
Remove keys from affected collections before changing relevant content or policy
configuration, then reinsert. Cached hashes do not repair unstable equality.
## Keyval is an association, not a storage node
`new keyval<K,V>(key, value)` constructs a normal generic SDK struct. It has private
fields and only key()/value() accessors, with no mutators. Assignment shares its
instance and payload references. Separately constructed keyvals remain distinct
under default instance identity even when their components match.
Map.put reads those components and returns the previous V. It does not retain the
input Keyval as an editable table node. Internal Map nodes store cached long hash,
K, mutable V and next link. This keeps storage mutation private and avoids one
public Keyval allocation per internally stored association.
Keyval is shallowly read-only: its association is fixed, but referenced objects
remain shared and mutable. An old captured value does not follow replacement
with a new object; mutating the old object itself is still visible through aliases.
## Share policies and helpers without a universal table
Map and Set share Identity, hash primitives, private bucket/size/growth arithmetic
and the Cursor implementation. They retain separate node types and algorithms.
Map is not implemented over Set or Linked, and there is no universal Element.
Empty Map construction is lazy. First inclusion creates 8 buckets, growth occurs
above 3/4 occupancy and doubles up to Array's u32 structural maximum. Chaining may
continue above that bucket count within checked signed-long size and provider
quotas. Removal does not shrink; clear releases the table and restores lazy state.
Full signed-long hashes select buckets using remainder plus positive correction,
without abs or int narrowing. Rehash copies independent topology using cached
hashes and complete K/V groups; it never calls Identity or edits old published
links during preparation. Old and candidate tables/nodes coexist under normal
quota accounting. Search still has a linear worst case.
## Capture associations independently of later mutation
Map.cursor returns `Cursor<keyval<K,V>>`. Each association costs a new Keyval and
an immutable private snapshot node, plus applicable carriers. Capture takes
O(B+N) work and O(N) extra storage, returns only when complete, calls no strategies
and retains no editable Map node or bucket position.
The [shared Cursor table](../../../docs/compiler/pbs/specs/sdk-list-and-cursor.md#cursor-protocol)
is unchanged: current starts on the first captured entry; hasNext tests for a
successor; next without one retains the last entry. Only an empty-born Cursor has
absent current. Consume current before checking whether to advance.
Later insertion, replacement, removal, clear or rehash cannot rewrite captured
associations. Order is unspecified. Consumed prefixes may become unreachable,
while the final current remains retained until its Cursor releases it. The real
GC corpus observes 1,200 -> 1 -> 1 -> 0 snapshot nodes and Keyvals, retaining
2,400 -> 2 -> 2 -> 0 distinct key/value objects. The Map-node count is zero after
capture ownership leaves the source collection, confirming no source-node link.
## Prepare even typed absence before structural writes
The quota corpus exposed an easy source-level mistake: `return none` can lower
to instructions that allocate a private typed-default carrier. Returning it after
inserting a node therefore introduced a fallible allocation after publication,
despite the source text looking like a trivial constant.
The insertion path now prepares the typed absent previous value before structural
writes and returns that prepared local afterward. Existing-entry put/remove also
prepare their complete optional previous-value carriers before replacement or
unlinking. Optimized and ordinary producers qualify these phases independently.
This preparation is not a transaction. Read/write indexing, containsKey, put and
remove use a guard against Identity-driven reentry on the same Map, including
aliases. clear is also blocked during callbacks; size/isEmpty/cursor may inspect
the consistent prior structure. Normal returns release the guard. Callback faults
may leave it installed while associations remain unchanged.
Before structural writes, refusal preserves old associations/topology but does
not undo callbacks, arguments, guard writes or provisional allocation. After
structural writes start, faults may retain earlier writes and incomplete
invariants, with no rollback or safe-reuse guarantee. A separate test-only quota
probe after candidate-table installation demonstrates table=16/nodes=7 while old
size=6 and guard=true. It does not ship in the SDK or represent a natural refusal
frequency. Budget/debugger pauses remain resumable instruction boundaries.
## Compiler and editor integration pitfalls
- Map keys are K, not positional long indices. Semantic analysis and lowering must
select Map handling before Array/List/Linked promotion, passing every key slot.
- Indexed reads are optional V while indexed assignment expects V. Contextual
generic construction on a key/value expression must use that operand's expected
application, not the outer lookup optional.
- A chained `optionalCall().hasSome()` has a boolean outer context, but that must
not erase the uniquely resolved optional return or change the receiver's lowering
context. This front corrected both resolution and receiver lowering.
- Keyval privacy must hold in completion, hover, navigation and signature help,
while private `this` fields remain available inside their owner. Unknown editor
receivers must produce no invented members or null-name resolution failures.
- Contextual constructors record application provenance on the entire NewExpr
span; signature help must resolve that concrete application as well as explicit
angular type applications. Imported aliases keep original SDK definitions.
- Generic default constructors must not invalidate explicit-strategy applications
for keys without default Identity. Keep K and V restrictions independent.
## Qualification and maintenance references
The complete regression and installed-JAR gates passed. Both production runtime
profiles executed all 88 mandatory Map PBX fixtures: 64 paired behavior/expected
fault cases, 16 natural-resource fixtures, 2 separate injected-fault fixtures,
4 GC fixtures covering both producers and 2 import/installed examples. Each profile
observed 328 natural refusals before structural writes, 2 post-publication test
injections, 8 snapshot GC observations and 10 collection GC observations.
Qualification is local macOS/arm64 against unchanged provider
`dcf6561ea73446e0a3e1cf1c73eda20923b38df0`. It does not establish a performance
threshold, other-host qualification or installed game readiness. Existing unrelated
disabled JUnit cases and upstream headless-schema gaps remain explicit in the
evidence and are not counted as new Map conformance.
- [Normative Map/Keyval specification](../../../docs/compiler/pbs/specs/sdk-map-and-keyval.md).
- [Executable installed example](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/maps.pbs).
- [SDK algorithms](../../../prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs).
- [Source-to-PBX qualification](../../../prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsMapKeyvalTest.java).
- [Harness reproduction](../../../tools/pbx-conformance/README.md).
- [Set/Identity predecessor](../DSC-0092-pbs-sdk-set/LSN-0093-reusable-identity-and-source-set-buckets.md).
- [Sequential mutation boundaries](../DSC-0096-runtime-storage-isa-consumer-migration/LSN-0092-sequential-collection-mutations-need-explicit-failure-boundaries.md).
@@ -0,0 +1,126 @@
# Map/Keyval implementation and qualification evidence
Owner: `compiler/pbs`. Captured 2026-10-26 for accepted DEC-0079 / PLN-0157,
with complete coverage of R1–R12. Studio baseline HEAD and host/tool versions are
in [environment.json](environment.json). Runtime remained clean at
`dcf6561ea73446e0a3e1cf1c73eda20923b38df0`; no runtime write or commit was made.
## Reproduction and results
From the Studio root:
```sh
./gradlew test :prometeu-app:installedKnowledgeTest --console=plain
CARGO_TARGET_DIR=/tmp/studio-pbx-map-target python3 tools/pbx-conformance/run.py --runtime ../runtime
CARGO_TARGET_DIR=/tmp/studio-pbx-map-target python3 tools/pbx-conformance/run.py --runtime ../runtime --release
git diff --check
discussion validate
```
All returned zero. The two complete VM runs include all Map cases and the existing
Registry, catalog, math, Random, input, Array/optional, Struct/contract, generic,
closure, storage/copy, List, Linked, Set, Identity/hash, admission and schema corpora.
Focused `--map-only` runs were used during development; final evidence is from
the complete harness built from the final source fingerprints.
Aggregated standard JUnit reports contain 1,079 cases: zero failures/errors,
1,066 executed and 13 existing skips. The independent installed-knowledge task
adds two passing cases outside the checkout using installed JARs. The final
Gradle invocation completed in 15 seconds with 2 executed and 92 up-to-date tasks;
unchanged suites retained valid prior successful results. Reported suites and
individual skips are preserved in [gradle-results.json](gradle-results.json).
Existing skips belong to BackendGateIIntegrationTest (8),
MainProjectPipelineIntegrationTest (4) and ExternalSourceDevelopmentHostTest (1).
None is a new Map test. Frontend totals are 305 and architecture totals are 127.
Each Rust profile uses both ordinary and optimized source producers, including
the GC fixtures. Eighty-eight mandatory Map files are fingerprinted: 64 behavior
and expected-fault cases, 16 resource fixtures, 2 distinct injection fixtures,
4 GC fixtures and 2 real import/installed examples. Missing mandatory fixtures
fail qualification. The full fixture directory contains 1,389 fingerprinted PBX
files; a directory count is not itself a count of executed corpus cases.
| New observation per Rust profile | Count |
| --- | ---: |
| Behavior / expected callback and overflow faults | 64 |
| Natural quota refusals with successful-prefix comparison | 328 |
| Separate test-only post-publication quota injections | 2 |
| Snapshot GC observations, ordinary + optimized | 8 |
| Collection/replacement/removal GC observations, ordinary + optimized | 10 |
| Import/reexport and installed-JAR example executions | 2 |
Known upstream headless-schema gaps for LifecycleRole and NonFiniteValue remain
printed explicitly. Original provider payloads are preserved and those cases are
not called conformance to the unmodified published enum. No new exception was
introduced for Map. Qualification is local macOS/arm64, not a timing benchmark or
other-host/deployed-game qualification.
## Resource and failure-phase observations
The paired producer paths and Rust profiles produced the same peak vectors.
These are measured whole-fixture/session peaks including setup, wrappers and call
preparation, not standalone Map object sizes.
| Operation | GuestBytes before → after | HandleSlots before → after | Peak Map nodes / tables | Natural refusals per producer |
| --- | --- | --- | --- | ---: |
| Growth 6 → 7 | 26,776 → 50,576 | 86 → 161 | 13 / 2 | 38 |
| Add without growth | 21,848 → 27,688 | 70 → 89 | 6 / 1 | 25 |
| Index replacement | 21,848 → 24,912 | 70 → 80 | 5 / 1 | 16 |
| put replacement | 21,848 → 25,516 | 70 → 82 | 5 / 1 | 18 |
| Remove | 21,848 → 25,540 | 70 → 82 | 5 / 1 | 16 |
| Clear | 21,848 → 22,152 | 70 → 71 | 5 / 1 | 5 |
| Cursor capture | 21,848 → 37,888 | 70 → 122 | 5 / 1 | 30 |
| Read | 21,848 → 25,212 | 70 → 81 | 5 / 1 | 16 |
All 328 natural refusals in each final profile occurred before structural writes;
an installed guard may remain. This does not prove post-write faults impossible
or permit automatic safe reuse. The separate test-only probe after actual table
installation retains 16 buckets, 7 nodes, old size 6 and active guard when the
probe allocation fails, while the refused instruction preserves its own entry state.
Production SDK source contains no such probe. The source prepares even typed none
before publication because lowering that absence can materialize a private carrier.
Snapshot GC observes 1,200 -> 1 -> 1 -> 0 nodes and Keyvals and
2,400 -> 2 -> 2 -> 0 distinct key/value Items, without retaining the source Map
table/nodes. Collection GC observes 64 -> 64 -> 61 -> 0 -> 0 Map nodes and
128 -> 128 -> 122 -> 0 -> 0 Items: overwritten values are reclaimed, removals
release keys/values and clear releases all associations. Both producer variants
pass these observations in both runtime profiles.
The eight peak vector components are GuestBytes, HandleSlots, AuxiliaryBytes,
TemporaryBytes, StringBytes, StringAllocations, IndividualStringBytes and CallDepth.
The complete vectors, refusal operation/resource/opcode records and GC counts are
in [map-results.json](map-results.json) and the compressed raw logs.
## Requirement coverage retained after housekeeping
| Requirement | Final evidence |
| --- | --- |
| R1 Construction, nominal origin, invariance | Frontend Map tests, SDK interface gate, alias/reexport frame and installed source |
| R2 API and evaluation | basic/scalars/evaluation source cases, typed string/object/tuple/native keys |
| R3 Optional presence | optional source case, nested payloads, stored none/zero/false, actual installed example |
| R4 Representative and previous values | content/basic cases, no-growth replacement resource baseline, snapshot independence |
| R5 Identity policies | scalars/objects/references/random/tuple/native/pixel cases and nominal/default-strategy frontend negatives |
| R6 Read-only Keyval | content case with distinct association instances and shared payloads; private-field semantic/editorial gates |
| R7 Buckets, bounds, cleanup | collisions/arithmetic/overflow, structural validation of resource fixtures, collection GC |
| R8 Reuse, rehash, memory | collision strategy counters, 13-node/2-table growth peak and full existing Set corpus |
| R9 Entry snapshot | empty/single/multiple states, snapshots/content, callback reads, paired snapshot GC |
| R10 Guard and failure phases | hash and equals callback fault matrices, successful-prefix quota comparison, separate post-publication injection |
| R11 Groups, owners, LSP | tuple/native/pixel/contract/reference payloads, source-order checks, real GC and SDK provenance/privacy editor tests |
| R12 Production qualification and distribution | Complete debug/release logs, both producer paths, isolated installed-JAR compilation/execution and regression reports |
## Files and workflow mechanics
- `regression.log.gz`, `conformance-debug.log.gz`, `conformance-release.log.gz`:
complete final raw outputs.
- `source.sha256`: all changed/new version-controlled surfaces at qualification.
- `fixtures.sha256.gz`: actual generated PBX fingerprints, including installed examples.
- `gradle-results.json`, `map-results.json`, `environment.json`: machine-readable results.
- `workflow.txt`: completed plan, validation and housekeeping output.
- `SHA256SUMS`: evidence file integrity manifest.
The CLI has no standalone lesson creation/next-ID query. LSN-0094 was read through
the framework's read-only model accessor; `discussion housekeep` performs its
registration, counter advancement, index updates and workflow artifact removal.
The index was not edited manually. The English lesson preserves the operational
knowledge; completed agenda/decision/plan files are disposable workflow scaffolding.
@@ -0,0 +1,10 @@
e00bd96cd1e689654be4d563b550e488265c8eaf2038734013219019c508e004 README.md
08b3115f4dc03a2a637b4133fd4a7ec6a27395183b8de1d778953e71c2e7df9f conformance-debug.log.gz
5efbde06fc26364560cf5e4e717d8023769ce322edb624b01d80e957dfe1a701 conformance-release.log.gz
325cb8c0638609340e398352e1c7a006b232ad24c7f860958f84017193266576 environment.json
8e8453dbd3b4e336d6fcf5d49337cb03ef2a3365d8e905f4db52d7ce6a8d1000 fixtures.sha256.gz
effff1a1552e7210bdb0486e44996d668dfb1d1e1cd1c0f076fe7a47a837f0f6 gradle-results.json
be95e4f4aa5c8d2718c8cc356b3eaa68cce050632f0ea6d1ed5051f482013b4e map-results.json
5af47b805bcf2c794d95bfaf517fe592e6b24116ba87d1c46a490fbbc18fbce1 regression.log.gz
8569d628c2e0305e403197ef8b7c92cb9156bae817529a01f828c7ef340ce3ea source.sha256
e93ad3067041f75297210adbf1f6cfd6a30a090c44d74afaf0059b6a1bee1faf workflow.txt
@@ -0,0 +1,13 @@
{
"captured": "2026-10-26",
"owner": "compiler/pbs",
"decision": "DEC-0079",
"plan": "PLN-0157",
"studio_baseline_head": "8105604f9210468fc61950b124cdd7adf58daaf0",
"runtime_head": "dcf6561ea73446e0a3e1cf1c73eda20923b38df0",
"runtime_working_tree": "",
"host": "macOS-27.0.1-arm64-arm-64bit-Mach-O",
"rust": "rustc 1.98.1 (48a229cea 2026-09-01)\nbinary: rustc\ncommit-hash: 48a229ceaefd4985c50990b14116b6d856af0985\ncommit-date: 2026-09-01\nhost: aarch64-apple-darwin\nrelease: 1.98.1\nLLVM version: 22.1.8",
"map_fixture_count": 88,
"all_fixture_count": 1389
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,992 @@
{
"debug": {
"behavior_and_expected_faults": 64,
"phase_refusals": [
{
"operation": "growth",
"refusals": 38,
"after_structural": 0
},
{
"operation": "growth-optimized",
"refusals": 38,
"after_structural": 0
},
{
"operation": "add",
"refusals": 25,
"after_structural": 0
},
{
"operation": "add-optimized",
"refusals": 25,
"after_structural": 0
},
{
"operation": "replace",
"refusals": 16,
"after_structural": 0
},
{
"operation": "replace-optimized",
"refusals": 16,
"after_structural": 0
},
{
"operation": "put",
"refusals": 18,
"after_structural": 0
},
{
"operation": "put-optimized",
"refusals": 18,
"after_structural": 0
},
{
"operation": "remove",
"refusals": 16,
"after_structural": 0
},
{
"operation": "remove-optimized",
"refusals": 16,
"after_structural": 0
},
{
"operation": "clear",
"refusals": 5,
"after_structural": 0
},
{
"operation": "clear-optimized",
"refusals": 5,
"after_structural": 0
},
{
"operation": "cursor",
"refusals": 30,
"after_structural": 0
},
{
"operation": "cursor-optimized",
"refusals": 30,
"after_structural": 0
},
{
"operation": "read",
"refusals": 16,
"after_structural": 0
},
{
"operation": "read-optimized",
"refusals": 16,
"after_structural": 0
}
],
"peaks": [
{
"operation": "growth",
"nodes": 13,
"tables": 2,
"before": [
26776,
86,
10688,
3840,
0,
0,
0,
6
],
"after": [
50576,
161,
18368,
7680,
0,
0,
0,
8
]
},
{
"operation": "growth-optimized",
"nodes": 13,
"tables": 2,
"before": [
26776,
86,
10688,
3840,
0,
0,
0,
6
],
"after": [
50576,
161,
18368,
7680,
0,
0,
0,
8
]
},
{
"operation": "add",
"nodes": 6,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
27688,
89,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "add-optimized",
"nodes": 6,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
27688,
89,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "replace",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
24912,
80,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "replace-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
24912,
80,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "put",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25516,
82,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "put-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25516,
82,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "remove",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25540,
82,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "remove-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25540,
82,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "clear",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
22152,
71,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "clear-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
22152,
71,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "cursor",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
37888,
122,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "cursor-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
37888,
122,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "read",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25212,
81,
10688,
3840,
0,
0,
0,
7
]
},
{
"operation": "read-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25212,
81,
10688,
3840,
0,
0,
0,
7
]
}
],
"test_only_postpublication_injections": 2,
"snapshot_gc_observations": 8,
"collection_gc_observations": 10,
"distribution_executions": 2,
"known_upstream_schema_gaps": [
"LifecycleRole missing from published enum; original payload preserved",
"NonFiniteValue missing from published enum; original payload preserved"
]
},
"release": {
"behavior_and_expected_faults": 64,
"phase_refusals": [
{
"operation": "growth",
"refusals": 38,
"after_structural": 0
},
{
"operation": "growth-optimized",
"refusals": 38,
"after_structural": 0
},
{
"operation": "add",
"refusals": 25,
"after_structural": 0
},
{
"operation": "add-optimized",
"refusals": 25,
"after_structural": 0
},
{
"operation": "replace",
"refusals": 16,
"after_structural": 0
},
{
"operation": "replace-optimized",
"refusals": 16,
"after_structural": 0
},
{
"operation": "put",
"refusals": 18,
"after_structural": 0
},
{
"operation": "put-optimized",
"refusals": 18,
"after_structural": 0
},
{
"operation": "remove",
"refusals": 16,
"after_structural": 0
},
{
"operation": "remove-optimized",
"refusals": 16,
"after_structural": 0
},
{
"operation": "clear",
"refusals": 5,
"after_structural": 0
},
{
"operation": "clear-optimized",
"refusals": 5,
"after_structural": 0
},
{
"operation": "cursor",
"refusals": 30,
"after_structural": 0
},
{
"operation": "cursor-optimized",
"refusals": 30,
"after_structural": 0
},
{
"operation": "read",
"refusals": 16,
"after_structural": 0
},
{
"operation": "read-optimized",
"refusals": 16,
"after_structural": 0
}
],
"peaks": [
{
"operation": "growth",
"nodes": 13,
"tables": 2,
"before": [
26776,
86,
10688,
3840,
0,
0,
0,
6
],
"after": [
50576,
161,
18368,
7680,
0,
0,
0,
8
]
},
{
"operation": "growth-optimized",
"nodes": 13,
"tables": 2,
"before": [
26776,
86,
10688,
3840,
0,
0,
0,
6
],
"after": [
50576,
161,
18368,
7680,
0,
0,
0,
8
]
},
{
"operation": "add",
"nodes": 6,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
27688,
89,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "add-optimized",
"nodes": 6,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
27688,
89,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "replace",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
24912,
80,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "replace-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
24912,
80,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "put",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25516,
82,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "put-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25516,
82,
10688,
3840,
0,
0,
0,
8
]
},
{
"operation": "remove",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25540,
82,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "remove-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25540,
82,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "clear",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
22152,
71,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "clear-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
22152,
71,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "cursor",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
37888,
122,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "cursor-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
37888,
122,
10688,
3840,
0,
0,
0,
6
]
},
{
"operation": "read",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25212,
81,
10688,
3840,
0,
0,
0,
7
]
},
{
"operation": "read-optimized",
"nodes": 5,
"tables": 1,
"before": [
21848,
70,
10688,
3840,
0,
0,
0,
6
],
"after": [
25212,
81,
10688,
3840,
0,
0,
0,
7
]
}
],
"test_only_postpublication_injections": 2,
"snapshot_gc_observations": 8,
"collection_gc_observations": 10,
"distribution_executions": 2,
"known_upstream_schema_gaps": [
"LifecycleRole missing from published enum; original payload preserved",
"NonFiniteValue missing from published enum; original payload preserved"
]
}
}
@@ -0,0 +1,34 @@
08b3115f4dc03a2a637b4133fd4a7ec6a27395183b8de1d778953e71c2e7df9f discussion/lessons/DSC-0093-pbs-sdk-map-keyval/evidence/conformance-debug.log.gz
5efbde06fc26364560cf5e4e717d8023769ce322edb624b01d80e957dfe1a701 discussion/lessons/DSC-0093-pbs-sdk-map-keyval/evidence/conformance-release.log.gz
effff1a1552e7210bdb0486e44996d668dfb1d1e1cd1c0f076fe7a47a837f0f6 discussion/lessons/DSC-0093-pbs-sdk-map-keyval/evidence/gradle-results.json
be95e4f4aa5c8d2718c8cc356b3eaa68cce050632f0ea6d1ed5051f482013b4e discussion/lessons/DSC-0093-pbs-sdk-map-keyval/evidence/map-results.json
5af47b805bcf2c794d95bfaf517fe592e6b24116ba87d1c46a490fbbc18fbce1 discussion/lessons/DSC-0093-pbs-sdk-map-keyval/evidence/regression.log.gz
8d5804910106bfa3f654edcefd2a7d196d70fdf19fd67b86bc80d8d766ffe25b docs/compiler/pbs/specs/generic-structs-and-free-functions.md
c82af9031ddd0143e208869a807fff5c85ee71d35799b9c841efdf8e30ed4b19 docs/compiler/pbs/specs/sdk-list-and-cursor.md
12135be9ff129493eb332f4bb9c05e36d7667e001f52669d03d90221d1232d9a docs/compiler/pbs/specs/sdk-map-and-keyval.md
4af3aa15ce6da20ae83c39ab4fcca38beba6f634ee5e55bf5f6edb0b5018ad50 docs/compiler/pbs/specs/sdk-set-and-identity.md
fe0e0d0eccfacd5b3f5ca220ec8c7a7c82f7bc527c5317d4b0651443b021f73e docs/compiler/pbs/specs/typed-arrays-and-optional-execution.md
de20f955ac4e55b3845741ea0ed6631326b4b91997f78fe7151dc094c40fe100 prometeu-app/src/test/java/p/studio/app/compiler/CompilerProjectKnowledgeTest.java
59d87f52a1d75bb2b873a5a0446ba00b50a2ae69b8e13f610467e8879ab0dbbe prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeProbe.java
dd65d8a6f7912aa1ba853a6876bf198069a88fdd75fbf53f6e83e4a2cfd29ebd prometeu-app/src/test/java/p/studio/app/compiler/InstalledKnowledgeTest.java
43fcb6def3cb51dd410d27baf67fc96f5c524d29633700d1c5192d0c7c64aabc prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsListCursorTest.java
38412eaad67ca52b591a12bad0d22064b569b2944e29d5f49e243c6243487bc5 prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsMapKeyvalTest.java
5254aad2e0001ce420fec865e518f41aeaa938e7271319a81da016aeed8ab48d prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsSetTest.java
f8336f28708de962c6dbe6e7c4927accb01c3bdc4df1a70f53921e14cea04e9d prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsGenericInstantiation.java
4231b9b16aae5e81740299a0959ca0258dedc3724ee69e82174df8218bfddfed prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsGenericTemplateValidator.java
e486759ce7a8225fdab8c793b1305dda761c7f7dc9ea542feb832432fa98efec prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/PbsSdkCollections.java
f7295c1d1430b24f3bc76f3b988c9bb19e6047334d73b274a9eca6ff2751e624 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/lowering/PbsExecutableBodyLowerer.java
e6101710926b3bb6b1d188544f505a6f6236c551c7c7667d7e0bf8cb68afeda5 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/parser/PbsDeclarationParser.java
452363ea506a17dabd68ff3bc1443066fbc9558877c8016df3fdb85465fa38dc prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/semantics/PbsEditorialSupportService.java
e0cc2c6dbd0cd2c3acfe58ebc17dba8f9fe58dd76c7820212863f851afd7cd32 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/semantics/PbsFlowSemanticSupport.java
448ea7e5650ff101ae380d0fef3f5f925fea8687ec236f32fc95ddb2b79f57fb prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/java/p/studio/compiler/pbs/semantics/PbsFlowStructuralExpressionAnalyzer.java
6fe7a8ed065c6df78af005320ece39425bb73eeb48e8451310866655ff109733 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/main.pbs
eedf6c88abc73535423edfeafa549d491c137d23f7532d3d9b72f0ca6622642c prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/game/stdlib/1/sdk/collections/mod.barrel
25d85ee0698f6e5e0ffae561ebf639a5942b4c990418fcbd5d9a4992c37109ea prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/maps.pbs
e3a5232997b2bdc2d249da07b186bbc6fb73eb8741aacd3aa00beda43537d2e6 prometeu-compiler/frontends/prometeu-frontend-pbs/src/main/resources/pbs-knowledge/examples/game/1/src/mod.barrel
6ec188158133c4632bd15a42105a6b32a8c9b5963872bdd62e7a1cd3954a2893 prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsGateUSdkInterfaceConformanceTest.java
fd9dd09edbf272ce54cb59c633df481d1d72fd1e008992b93f4a5583747f578b prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/PbsMapKeyvalTest.java
7a3ffc5e2a533966dbe069f8a5e1541005916d7be47aa65d0f4d605926945f71 prometeu-compiler/frontends/prometeu-frontend-pbs/src/test/java/p/studio/compiler/pbs/semantics/PbsMapKeyvalEditorialTest.java
01311527195d9f8513c96907adf7eb501080cd073e56f3f71b6e6c690c800b94 tools/pbx-conformance/README.md
2d3cfb43159a4c20f891562f2e196fd894e82e0d47124f09707b20f146ed505e tools/pbx-conformance/main.rs
11ac435b7c1f7a0df1961975a537da4f491d4c6fd7feeb8b7b39183ecaf039e2 tools/pbx-conformance/run.py
@@ -0,0 +1,23 @@
Owner: compiler/pbs. Completed 2026-10-26. DEC-0079 / PLN-0157, R1–R12.
Workers: Spec Editorial Worker, Code Implementation Worker, Lesson Worker.
discussion mark-plan PLN-0157 done --json
{"discussion_id":"DSC-0093","plan_id":"PLN-0157","status":"done"}
discussion validate (before housekeeping)
validation passed
discussion housekeep DSC-0093 --lesson discussion/lessons/DSC-0093-pbs-sdk-map-keyval/LSN-0094-map-presence-key-identity-and-captured-associations.md
housekept DSC-0093
removed workflow artifacts: 3
registered lesson: LSN-0094
discussion validate (after housekeeping)
validation passed
discussion inspect DSC-0093
DSC-0093 done pbs-sdk-map-keyval; sole artifact: LSN-0094 done, present.
Runtime: clean dcf6561ea73446e0a3e1cf1c73eda20923b38df0, no writes or commits.
Next lesson ID was obtained through DiscussionIndex.next_id (read only);
housekeep owned registration, counter advancement and index mutations.
@@ -129,3 +129,11 @@ explicit identity and width; common lowering resolves constant groups against
`IRVMBuiltinConstantDefinition` and checks the declared result width. PBS does
not introduce another catalog. Source execution and the full installed example
are separate consumer gates beyond CSV parity.
The [PBS hashing/Identity facade](../../pbs/specs/sdk-set-and-identity.md#hash-facade-and-typed-query-boundary)
consumes hash.i64@1, hash.float@1 and hash.string@1 with exact schema-2 profiles,
null admission and attempted costs. Reference hash/equality are typed Registry
instructions rather than intrinsic catalog rows. Consumer support includes their
common IR/emission/verification and full source-to-VM/installed qualification;
catalog parity alone does not establish that integration. Collection algorithms
remain source SDK code and do not add provider collection syscalls.
@@ -124,6 +124,8 @@ fallthrough past the end is invalid.
| CALL_TRAIT `0x58` | Trait TypeId, method index |
| DYN_PACK/UNPACK/CAN_UNPACK `0x90`–`0x92` | TypeId |
| Reference operations `0x93`–`0x96` | source TypeId, target TypeId |
| REF_IDENTITY_HASH `0x97` | source TypeId; full source group → Int64 |
| REF_IDENTITY_EQ `0x98` | left TypeId, right TypeId; full left/right groups → Bool |
| STRUCT_NEW `0xa0` | Struct TypeId |
| STRUCT_GET/SET `0xa1`–`0xa2` | Struct TypeId, field index |
| Array operations `0xa3`–`0xa6` | Array TypeId |
@@ -143,6 +145,18 @@ jump-to-end require exact result width and compatible known facts. GET/SET_LOCAL
address only parameters plus extra locals. No implicit numeric, trait or Dynamic
conversion is introduced by linking, writing or verification.
Typed identity queries accept exactly Struct, Array, Fn, Trait and builtin Random.
They do not widen older reference-conversion category rules. Trait validates its
complete receiver/ImplementationId pair, including valid zero, before querying
the receiver. Both operands validate left-to-right; absent/different left does not
short-circuit right-side validation. Scalar String/Dynamic/private value carriers
are ineligible. Hash consumes w(source) slots and costs 8+w(source); equality
consumes both complete groups and costs 4+w(left)+w(right). Immediates are static
program-local TypeIds (u32 LE after u16 LE opcode), and results preserve provider
faults, signed long bits, provenance and charged attempts, including unused results.
Set algorithms and equality policy remain frontend/SDK consumers, described for
PBS in [Set/Identity](../../pbs/specs/sdk-set-and-identity.md).
HOSTCALL references a declared canonical `(module, name, version)` identity
with exact argument/result slot ABI. Duplicate identities with conflicting ABI,
unused declarations and invalid indices are rejected. Only the runtime loader
+1 -1
View File
@@ -1,6 +1,6 @@
# PBS Generic Contracts
Owner: `compiler/pbs`. This specification extends nominal contracts with explicit type parameters. Registry, PBX, instruction, ownership and resource contracts remain governed by the runtime provider. Collections and object-identity hashing are separate consumers.
Owner: `compiler/pbs`. This specification extends nominal contracts with explicit type parameters. Registry, PBX, instruction, ownership and resource contracts remain governed by the runtime provider. [Set, SDK Identity and hashing](sdk-set-and-identity.md) are consumers with their own public policy; the examples below illustrate generic language syntax, not the SDK Identity declaration.
## Declaration and application
@@ -269,3 +269,5 @@ iteration protocols and List logical bounds/slot transfer are separate decisions
- [Typed arrays and optional execution](typed-arrays-and-optional-execution.md).
- [First-class functions and captures](first-class-functions-and-captures.md).
- [Concrete Registry and PBX handoff](../../general/specs/typed-registry-and-pbx-v0.md).
- [SDK Map and Keyval](sdk-map-and-keyval.md), a two-parameter source consumer
preserving separate key/value policies and complete optional/grouped fields.
@@ -177,10 +177,12 @@ faults. List itself rejects directly optional elements.
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
[Set](sdk-set-and-identity.md) produces Cursor<T>; Map<K,V> uses
Cursor<keyval<K,V>> as specified by [Map/Keyval](sdk-map-and-keyval.md).
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.
Set/Map and keyval are separate deliveries.
Map owns entry production and Keyval representation while reusing this protocol.
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,
@@ -0,0 +1,181 @@
# SDK Map and read-only Keyval associations
Owner: `compiler/pbs`. Map algorithms execute as PBS SDK source. This contract
builds on [generic structs](generic-structs-and-free-functions.md),
[Array/optional](typed-arrays-and-optional-execution.md),
[Set/Identity/hash](sdk-set-and-identity.md) and the
[shared snapshot Cursor](sdk-list-and-cursor.md#cursor-protocol).
Registry/PBX, storage, ownership, faults and resource limits remain provider contracts.
## Types, construction and public API
`@sdk:collections` exports invariant ordinary generic structs `map<K,V>` and
`keyval<K,V>`, alongside the nominal `Identity<T>` and shared `Cursor<T>` contracts.
Applications retain nominal origin through imports, aliases and reexports. User
homonyms and untrusted SDK attributes acquire no reserved behavior.
```pbs
import {map, keyval, Identity, Cursor} from @sdk:collections;
let scores = new map<str, int>();
let contextual: map<str, int> = new map();
let entry = new keyval<str, int>("player", 42);
let previous: optional int = scores.put(entry);
```
Map offers `new map<K,V>()` and `new map<K,V>(identity: Identity<K>)`. Keyval offers
`new keyval<K,V>(key: K, value: V)`. Diamond/omitted type arguments require a
sufficient expected application; constructor values do not infer type arguments.
Every constructor initializes its fields before returning.
Directly optional K, including grouping, is invalid for Map with either constructor.
V may be optional. Independent keyval admits all valid generic field compositions,
including optional K/V. Optional of the Map itself remains valid. Array's direct
optional-element restriction does not apply to V fields in private Map nodes.
| Operation | Result | Behavior |
| --- | --- | --- |
| m[key] | optional V | none for absent key, some(stored value) for a present key |
| m[key] = value | No previous value | Insert or replace V |
| containsKey(key: K) | bool | Membership under selected key identity |
| put(entry: keyval<K,V>) | optional V | Insert/replace; return previous V if present |
| remove(key: K) | optional V | Remove; return previous V if present |
| clear() | unit | Remove associations, release table, restore lazy empty state |
| size() | long | Number of associations |
| isEmpty() | bool | size equals zero |
| cursor() | Cursor<keyval<K,V>> | Complete independent entry snapshot |
| entry.key() | K | Captured key reference/value |
| entry.value() | V | Captured value reference/value |
Receivers and arguments evaluate once in source order, including named application.
Map indexing expects K and does not apply the additional int-to-long index promotion
of Array/List/Linked. Complete key/value groups cross source calls unchanged.
Map has stable reference identity: aliases observe the same mutations.
Only one association exists for each key equivalence class; values may repeat.
Replacement changes only V and preserves the original stored K, cached hash and
size. It never triggers growth. Replacing the representative key requires removing
the entry and reinserting the new key. put reads association components without
retaining its keyval instance as an editable internal node. remove of an absent
key returns none without changing associations/topology. Prepare previous-value
carriers before replacement or unlinking.
Insertion/iteration order is unspecified. There are no keys/values projections,
capacity constructors, capacity/reserve/trim, bulk APIs, redundant tryGet, public
nodes, universal Element or positional indexing contract.
## Presence and optional payloads
The outer optional of read/put/remove describes association existence, independently
of V. For `map<str, optional int>`, absence is none, present V=none is some(none),
and present V=some(7) is some(some(7)). containsKey is true for a stored none;
assigning none as V does not remove an association. Zero/false and complete
multi-slot groups remain present. Preserve deeper valid optional nesting.
Type/callback/resource faults never become none/false.
## Key Identity and read-only Keyval
Use the exact defaults and eligibility rules of Set: int/long/bool by static-domain
value; str by exact UTF-8 content; finite float equality with equal signed zeros;
source structs, arrays, Fn, Random and eligible services by instance; contracts by
fully validated receiver identity. Tuples and other types without a default require
an explicit Identity<K>. An unavailable default constructor must not invalidate an
otherwise valid explicit-strategy application. V requires no equality/hash capability.
Hash filters candidates and equals resolves collisions. Keep all signed-long hash
bits. Identity must define a stable equivalence relation while stored and satisfy
`equals(a,b) => hash(a) == hash(b)`. Relevant key content and strategy configuration
must remain coherent. Remove from affected collections before changing content
used by hashing/equality, then reinsert. Cached hashes do not stabilize mutable
equality. No structural fallback, freezing, purity proof or effect system is added.
Keyval has private fields and only key()/value() accessors, with no mutators.
It retains ordinary struct instance identity: separately constructed keyvals are
distinct under default Identity even when components match. Component comparison
requires explicit policy; unconstrained K/V gain no equality operator. Assignment
shares the keyval instance and payload aliases. Its association cannot be edited
through Map or follow later replacements; referenced objects remain mutable/shared.
## Private storage, rehash and costs
Map owns a private bucket array with private chained nodes containing full cached
long hash, K, V and next link. It is not implemented over Set/Linked and requires
no public keyval wrapper per internally stored association. Reuse Identity/hash,
common private arithmetic helpers and shared Cursor implementation.
Empty construction has no nonempty bucket table. First inclusion uses 8 buckets.
For capacity c and next size r, grow if r exceeds floor(3*c/4), to
min(2*c, 4294967295). Use checked long arithmetic. At maximum bucket capacity,
chaining can continue within checked signed-long size and ordinary quotas;
Array's u32 bound limits buckets rather than entry count. For positive capacity,
use signed hash remainder and add capacity when negative, without abs, narrowing
or assuming powers of two. Removal does not shrink. Clear returns to lazy state.
Rehash prepares independent nodes/topology from cached hashes, with no callbacks
or edits to published links before installation. Complete payload groups and
reference identities survive. Ordinary mutation does not reconstruct the whole
table. Old/candidate structures coexist under normal quotas and GC; preparation
does not make subsequent stores atomic. Search has worst case O(N), growing rehash
costs O(new buckets + N), and String hashing costs content-dependent work. No fixed
instruction cost, adversarial resistance or allocation-free optional is promised.
## Guard, preparation and failure boundaries
Read/write indexing, containsKey, put and remove install a private guard before
Identity callbacks. Reject callback reentry into these operations or clear on the
same Map, including aliases, through the private terminal TRAP_EXPLICIT path
before changing associations. During callback preparation, size/isEmpty/cursor
may inspect the consistent prior structure without invoking strategies.
Prepare checks, callbacks/search, next sizes, values, return carriers and candidate
structures before the first structural write. Structural stores are sequential,
without user callbacks or explicit suspension. Normal returns release the guard,
including absent reads/removals and insertion/replacement.
| Outcome | Guarantee |
| --- | --- |
| Refusal before structural writes | Prior associations/topology survive; guard, external effects and provisional allocations are not undone |
| Successful return | Size, table, groups, representative identity, cleanup, snapshot and guard invariants hold |
| Internal/resource fault after structural writes start | Earlier writes and incomplete invariants may remain; no rollback, retry, recovery or safe reuse guarantee |
Installing the guard is already a write. Callback faults can leave it active.
No catch/finally, poison flag or recovery API is introduced. Allowed callback reads
do not establish post-fault reuse. Per-instruction refusal preserves that
instruction's applicable entry state, not prior instructions. Budget/debugger
pauses are resumable instruction boundaries; a source method is not an atomic
observation region for host/debugger inspection.
## Entry snapshot and lifetime
cursor creates one new independent keyval and one immutable private snapshot node
per association, without editable Map-node/position links or hash/equals calls.
Capture completes before returning; failure returns no partial Cursor and leaves
source structure unchanged. Preserve the shared Cursor state table: nonempty-born
starts on first, hasNext tests successor, next without successor keeps last, and
only empty-born has absent current. No reset, mutation or new for-in is added.
Later insertions/replacements/removals/clear/rehash cannot rewrite captured
associations. Mutations of referenced objects remain visible; order between captures
is unspecified. Capture costs O(B+N) work and O(N) additional storage, including
N keyvals, N nodes and applicable carriers. next is O(1). Consumed prefixes may
become unreachable, while final current stays retained until its Cursor releases it.
Preserve complete groups and roots/owners across calls, allocation, hashing,
rehash, returns and snapshots. Replacement/removal/clear release obsolete Map
references; snapshots/other owners remain independent. Tracing and native owners
retain normal costs; immediate GC/quota recovery is not guaranteed.
## Qualification and distribution
Compile real source through typed IR/PBX into the production loader/verifier/VM,
using ordinary/optimized producers and independent debug/release harness profiles.
Cover the full API, optional presence, evaluation order, representative retention,
identity/collisions/negative hashes, all Cursor states, groups/aliases, resource
prefix refusals, memory coexistence and real GC. Counters establish callback-free
rehash/capture. Distinguish natural production quotas from named test-only
post-write injection, never shipped in production SDK. Missing mandatory fixtures
fail qualification. Regress existing collections, hashing and language boundaries.
Package this English specification, SDK source and executable examples in installed
knowledge. Compile actual installed examples outside the checkout using installed
JARs and execute their PBX. No collection syscall/opcode, changed ABI/catalog policy,
quota relaxation or provider source mutation is required.
@@ -0,0 +1,242 @@
# SDK Set, Identity and hashing
Owner: `compiler/pbs`. Set algorithms belong to executable PBS SDK source. Typed
reference queries, storage, admission, faults and resource limits are generic
compiler/provider mechanisms. This contract builds on [generic structs](generic-structs-and-free-functions.md),
[generic contracts](generic-contracts.md), [Array/optional](typed-arrays-and-optional-execution.md)
and the [shared snapshot Cursor](sdk-list-and-cursor.md#cursor-protocol).
## Types, construction and operations
The `@sdk:collections` module exports invariant ordinary generic `set<T>` and the
nominal contract `Identity<T>`:
```pbs
declare contract Identity<T> {
fn hash(value: T) -> long;
fn equals(left: T, right: T) -> bool;
}
```
Import the SDK declarations with `import {set, Identity, Cursor} from @sdk:collections;`.
Another user contract named Identity does not become the SDK contract through its spelling.
`new set<T>()` selects the SDK default for T. `new set<T>(identity)` takes an
explicit `Identity<T>` service/struct value. Contextual `new set()` and diamond
construction require an existing expected application; argument values do not infer
T. Type applications and strategies are invariant. Associated members such as
Enemy.identity, blanket implementations and generic reflection are not introduced.
No-strategy construction for T without a default is a localized source error.
Explicit-strategy construction remains valid for otherwise supported grouped/native
T, even though the generic owner also declares a no-argument constructor. Direct
optional T, including grouped optional, is invalid with either constructor.
`optional (set<T>)` and elements with optional fields remain valid.
| Operation | Result | Behavior |
| --- | --- | --- |
| add(value) | bool | True on inclusion; false for an equal existing element |
| contains(value) | bool | Membership under the selected policy |
| remove(value) | bool | True only when an equal element was removed |
| clear() | unit | Release table/nodes and reset size to zero |
| size() | long | Number of elements |
| isEmpty() | bool | size equals zero |
| cursor() | Cursor<T> | Complete independent snapshot |
Set has stable reference identity: aliases observe the same collection. A duplicate
add preserves the original stored reference and does not grow the table. Removing
an absent value is false. There is no positional indexer, tryGet, capacity argument,
capacity/reserve/trim API or public node. Insertion/iteration order is not guaranteed.
Receiver and arguments evaluate once in source order, including named application.
An unrelated user declaration named set gains none of the SDK-specific rules.
## Equality policy and obligations
| T | Default policy |
| --- | --- |
| int / long | Value equality in the static domain of T; int widens exactly to Int64 for hashing |
| bool | Value equality; false/true map to Int64 0/1 for hashing |
| str | Exact resident UTF-8 content, without normalization or case folding |
| float | Finite Float equality; +0 and -0 are equal and hash equally |
| Source structs, arrays, Fn and Random | Instance identity, independent of mutable fields/elements, code/captures or generator state |
| contracts | Identity of the fully validated receiver, independent of view/ImplementationId |
| Tuples and other native values | Explicit Identity required; no automatic composition |
Services represented as ordinary singleton references retain the same instance
semantics when they are valid element types. There is no public Dynamic, universal
cross-numeric equality, structural fallback or equality based on private value
carrier identity. Hash collisions do not imply equality. All signed long digest
bits, including negative values, are retained.
Two distinct gameplay objects with equal fields remain distinct under defaults.
An explicit strategy may define content equality and collapse such instances.
Actual stored references remain shared; no deep freezing/copying occurs. Identity
must define a stable equivalence relation while stored and satisfy
`equals(a,b) => hash(a) == hash(b)`. The converse is not required. Strategy
configuration and relevant content must remain coherent. Remove from every affected
collection before changing content used by equality/hash, then reinsert. A cached
old hash does not stabilize mutable equality. The SDK cannot prove these obligations
and does not introduce purity, readonly or effect-system requirements.
Every value preserves its complete logical group and reference aliases through
callbacks, nodes, calls, allocations, rehash and capture. A tuple's private storage
carrier is not that tuple's source identity. Groups/native owners retain ordinary
storage packing and quota costs.
## Private representation and growth
Set uses a private array of buckets with private chained nodes. Each node stores
the complete cached long hash, full T and its next link. It does not depend on the
public linked collection or a universal Element. Hash selects candidates and
equals resolves collisions, including deliberately identical hashes.
Empty construction has no nonempty bucket table. First successful inclusion uses
8 buckets. For current capacity c and next size r, grow when r exceeds
`floor(3*c/4)`; new capacity is `min(2*c, 4294967295)`. Arithmetic uses long and
must not overflow. At the maximum Array capacity, chaining can continue within
checked signed-long size and provider quotas, accepting increased occupancy.
Array's u32 structural bound limits buckets, not the count of chained elements.
An unrepresentable next size is rejected before addition/mutation.
For positive capacity, select the bucket with signed remainder and correction:
```text
bucket = hash % capacity
if bucket < 0: bucket += capacity
```
Do not abs the digest, narrow to int, or assume every capacity is a power of two.
Removal does not shrink. Clear releases table/node references and returns to lazy
empty state. Discarded objects and old storage follow normal tracing and other
owners; immediate collection or quota recovery is not guaranteed.
Rehash prepares independent buckets/nodes/topology from cached hashes. It preserves
published links until installation and preserves payload reference identities.
It does not invoke hash/equals. Old and candidate structures coexist under ordinary
quotas; private carriers can add allocation/ownership work. Installation uses
ordinary typed stores. Whole-table reconstruction on every mutation is not the
algorithm, and preparing a candidate does not make later source instructions atomic.
Search cost depends on hash distribution, occupancy and callback cost; the worst
case is O(N). Growing rehash costs O(new buckets + N) work and additional nodes/table.
String hashing additionally costs work proportional to content. Expected lookup
or amortized growth claims do not promise constant instruction cost, no allocation,
adversarial resistance or allocation-free optional carriers.
## Guard, mutation phases and faults
Operations calling Identity use a private guard. A callback cannot reenter
add/remove/clear/contains on the same Set, including aliases; reject before
structural changes through the private terminal TRAP_EXPLICIT path. During callback
preparation the original structure is consistent: size/isEmpty/cursor may read it
without invoking Identity. Capturing a Cursor may still fail resources.
Validation, hashing/equality/search, next sizes, nodes/tables and value/return
carriers are prepared before structural writes. No user callback or explicit
suspension occurs in the sequence that applies structural changes. Rehash/capture
never call strategies. The guard is released on successful return, including
ordinary false results.
| Outcome | Guarantee |
| --- | --- |
| Refusal before the first structural write | Prior elements/topology are unchanged; external argument/callback effects and provisional allocations are not undone |
| Successful return | Size, topology, buckets, groups, identity, cleanup and guard invariants hold |
| Internal/resource fault after structural mutation starts | Earlier writes may remain and invariants may be incomplete; no rollback, safe reuse, retry or automatic recovery |
Installing the guard is already a write. Callback fault can leave it active while
elements remain unchanged. Structural preservation is not bit-for-bit preservation
of the whole Set object. No catch/finally, poison flag or recovery API is added;
permitted callback reads do not establish post-fault safe reuse. Mutations of other
Sets/objects are not rolled back, and the guard does not prove strategy stability.
Faults are never translated into false/none.
Per-instruction refusal preserves that instruction's applicable entry state, not
earlier successful instructions. BudgetExhausted/debugger pauses are resumable
instruction boundaries; execution resumes at its PC. A source method is not an
atomic observation region for host/debugger inspection.
## Snapshot Cursor
Cursor uses the existing shared implementation and full state table. A nonempty
snapshot begins on the first captured element; hasNext tests a successor; next
without a successor keeps the final value. Only an empty-born Cursor reports
current absent. Consume current before deciding whether to advance:
```pbs
let snapshot = values.cursor();
while true {
if let value = snapshot.current() {
// Use value.
} else { break; }
if snapshot.hasNext() { snapshot.next(); }
else { break; }
}
```
Capture allocates one private immutable singly linked node per element and completes
before returning. Failure returns no partial Cursor and preserves source structure.
Snapshot nodes retain payload values, not editable Set nodes/bucket positions.
Later inclusion/removal/clear/growth cannot rewrite the capture. Referenced objects
remain shared and later field mutation remains visible; iteration order between
captures is not guaranteed. There is no live/fail-fast iterator, reset, removal
through Cursor or new for-in protocol.
For B buckets and N elements, capture is O(B+N) work/O(N) extra nodes and next O(1).
Consumed prefixes can become unreachable; final current node/value stays retained
until the Cursor releases it. Ordinary allocations, carriers, owners, GC and quotas
apply without relaxation.
## Hash facade and typed query boundary
`@sdk:hash` exports `hash.i64(long)->long`, `hash.f64(float)->long`,
`hash.text(str)->long`, `referenceHash<T>(T)->long` and
`referenceEquals<T>(T,T)->bool`. Generic reference wrappers require explicit type
arguments and accept exactly eligible reference categories; grouped/optional value
carriers and arbitrary scalar/Dynamic values do not become references.
The reference wrappers lower complete groups to REF_IDENTITY_HASH 0x0097 and
REF_IDENTITY_EQ 0x0098. Contracts validate receiver and ImplementationId, including
valid zero, before querying receiver identity. Concrete and valid trait views agree.
Both sides of equality validate left-to-right; an absent/different left cannot
suppress an invalid right. Invalid handles/groups remain faults. TypeIds are static
program metadata, not guest selectors. Reference hashes do not root objects or
provide persistent cross-session IDs.
Scalar wrappers use hash.i64@1 0x4100, hash.float@1 0x4101 and hash.string@1 0x4102.
Hash-v1 preserves the provider's XXH64 seed-zero digest bits. Numeric hashes cost
8 cycles; String costs 8 plus UTF-8 bytes. Finite Float bit hashing normalizes both
zeros, never converts numerically to integer, and rejects non-finite inputs.
Provider-level canonical null String hashes to zero and differs from empty text;
PBS adds no public null syntax or optional-element admission. Reference hash costs
8+w(T), equality 4+w(left)+w(right). Valid queries have bounded borrowed execution
without guest/scratch allocation or user callbacks; wrapper/call costs are separate.
Compiler verification, emission and optimization must preserve complete groups,
provenance/null admission, fault order, attempt costs, zero-budget/overshoot and
unused-result effects. Defaults are resolved by private trusted bindings at
concrete logical types, not by enabling == on unconstrained T. Existing user
contracts named Identity retain their own nominal identity and signatures.
## Qualification, distribution and related consumers
Qualify the hash facade from real PBS through PBX into the production VM before
Set algorithms. Test ordinary/optimized source and neutral producers, independent
provider vectors, negative hashes, complete contract groups, Float boundaries,
String owners, aliases/mutation, GC/stale handles and malformed/dead instructions.
Strategy counters and deliberate collisions must demonstrate callback-free
rehash/capture and alias-safe reentrancy. Observe measured old/candidate memory
peaks and instruction-prefix refusal state, separating guard effects and structural
writes. Controlled test-only fault injection must be identified separately from
natural production quota cases.
Ship this English specification, SDK source and executable examples in installed
knowledge; compile actual installed examples outside the checkout and execute their
PBX. Normal compilation uses versioned Studio catalogs/resources without a sibling
checkout. Runtime ownership, limits and semantics remain the provider contract;
Set adds no VM collection instruction/syscall or provider source change.
Identity/hash are reused by [Map/Keyval](sdk-map-and-keyval.md). Its API, representation
and implementation are separate consumers; sharing policy does not impose a shared
physical table or universal entry type. See the
[common typed Registry contract](../../general/specs/typed-registry-and-pbx-v0.md)
and [catalog consumer rules](../../general/specs/runtime-catalog-support.md).
@@ -88,6 +88,11 @@ policies remain separate concerns.
See [Runtime Random integration](runtime-random-integration.md) for opaque generator aliases in typed storage and captures.
[Map/Keyval](sdk-map-and-keyval.md) stores V in generic node fields and therefore
admits optional values. Its outer lookup/previous-value optional describes entry
existence independently of stored V; some(none) is distinct from absent none.
This does not change Array's direct optional-element restriction.
## Parameterized contract integration
+8 -2
View File
@@ -28,7 +28,7 @@ For an external coding agent:
### Small Game Project
The installed example has three separately indexed files:
The installed example has separately indexed files. Its basic game entry consists of:
- `pbs:example:game:1:prometeu.json`: a Game manifest with `language: pbs`,
`stdlib: 1` (serialized as a string), and no external dependencies;
@@ -36,6 +36,11 @@ The installed example has three separately indexed files:
moving rectangle through `Gfx` and constructing colors through `Color`;
- `pbs:example:game:1:src/mod.barrel`: the module's public declarations.
Additional runnable examples include `src/collections.pbs` for List/Cursor,
`src/linked.pbs` for Linked, `src/sets.pbs` for Set and `src/hashing.pbs` for
scalar hashing and typed instance queries. Read the corresponding SDK/spec
references; Set's order is unspecified and custom equality is explicit.
Copy these reference texts to the corresponding paths only when creating that
example in your editable project. The example is compiled in the test suite;
it is not a complete game or a claim that every requested gameplay abstraction
@@ -54,7 +59,8 @@ for exact rules rather than inferring compatibility with another language.
`pbs:sdk:game:1:sdk/gfx/main.pbs` exposes the actual installed Gfx source,
including documentation comments and signatures. Its `mod.barrel` reference
defines exports. Equivalent references exist for `sdk/asset`, `sdk/composer`,
`sdk/input`, `sdk/log`, `sdk/math`, `sdk/mathf`, `sdk/random`, and `core/color`. Read those interfaces before calling
`sdk/input`, `sdk/log`, `sdk/math`, `sdk/mathf`, `sdk/random`, `sdk/hash`,
`sdk/collections`, and `core/color`. Read those interfaces before calling
them. Game SDK 1 does not currently ship `audio` or `core/math` modules.
Ordinary game source uses public services such as `Gfx`; it must not manufacture
@@ -51,6 +51,8 @@ class CompilerProjectKnowledgeTest {
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(result.references().stream().anyMatch(value -> value.id().equals("pbs:spec:sdk-map-and-keyval.md")));
assertTrue(result.references().stream().anyMatch(value -> value.id().equals("pbs:example:game:1:src/maps.pbs")));
assertTrue(language.referenceIds().contains("pbs:quickstart"));
assertFalse(result.limitations().stream().anyMatch(value -> value.contains("DOCUMENTATION_UNAVAILABLE")));
assertFalse(Files.exists(root.resolve(".workspace")));
@@ -31,6 +31,12 @@ public final class InstalledKnowledgeProbe {
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");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:spec:sdk-set-and-identity.md")), "Missing installed Set/Identity specification");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/sets.pbs")), "Missing installed Set example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:spec:sdk-map-and-keyval.md")), "Missing installed Map/Keyval specification");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/maps.pbs")), "Missing installed Map/Keyval example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:example:game:1:src/hashing.pbs")), "Missing installed hashing example");
check(docs.stream().anyMatch(r -> r.id().equals("pbs:sdk:game:1:sdk/hash/main.pbs")), "Missing installed hash SDK");
for (var reference : docs) {
if (reference.id().startsWith("pbs:example:game:1:")) {
Path file = root.resolve(reference.id().substring("pbs:example:game:1:".length()));
@@ -91,6 +97,20 @@ public final class InstalledKnowledgeProbe {
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");
for(var example:java.util.Map.of("set","sets.pbs","hash","hashing.pbs","map","maps.pbs").entrySet()) {
Path exampleRoot=Files.createDirectory(Path.of(example.getKey()+"-example")).toRealPath();
Files.createDirectories(exampleRoot.resolve("src"));
Files.copy(root.resolve("prometeu.json"),exampleRoot.resolve("prometeu.json"));
Files.writeString(exampleRoot.resolve("src/mod.barrel"),"pub fn frame() -> void;");
String function=switch(example.getKey()){case "set"->"setExample";case "map"->"mapsExample";default->"hashingExample";};
Files.writeString(exampleRoot.resolve("src/main.pbs"),Files.readString(root.resolve("src/"+example.getValue()))
+"\n[Frame] fn frame()->void {if "+function+"()!=42 {let failure=1/0;}}");
var exampleResult=BuilderPipelineService.INSTANCE.compile(
BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(false,exampleRoot.toString())),LogAggregator.stdout());
check(exampleResult.diagnostics().stream().noneMatch(issue->issue.isError()),"Installed "+example.getKey()+" example must compile: "+exampleResult.diagnostics());
Files.write(fixtures.resolve("pbs-"+example.getKey()+"-installed.pbx"),exampleResult.bytecodeBytes());
System.out.println("Installed "+example.getKey()+" execution fixture generated");
}
verifyExternalSources(provider.getClass().getClassLoader());
registration.unsubscribe();
System.out.println("Installed knowledge verified: " + docs.size() + " references; example compiled");
@@ -17,6 +17,7 @@ class InstalledKnowledgeTest {
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);
assertTrue(log.contains("Installed map execution fixture generated"), log);
}
@Test void installedCompilerAndSdkCarryTheSameVerifiedCatalog() throws Exception {
@@ -0,0 +1,134 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.pbs.PbsFrontendCompiler;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.HexFormat;
import static org.junit.jupiter.api.Assertions.*;
class PbsIdentityHashingTest {
static String sdk() throws Exception {
try(var stream=PbsIdentityHashingTest.class.getResourceAsStream("/game/stdlib/1/sdk/hash/main.pbs")) {
assertNotNull(stream);return new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
}
static void compile(String name,String source) throws Exception {
compile(name,source,"");
}
static void compile(String name,String source,String extraSdk) throws Exception {
var matcher=java.util.regex.Pattern.compile("declare service Probe\\s*\\{fn main\\(\\)->(int|long)").matcher(source);
assertTrue(matcher.find(),name);String result=matcher.group(1);
String executable=matcher.replaceFirst("fn _hashProbe()->"+result);
executable=executable.substring(0,executable.lastIndexOf('}'));
executable+="declare service Probe {fn main()->"+result+" {return _hashProbe();}}";
for(var suffix:new String[]{"","-optimized"})
PbsRuntimeAlignmentTest.compile("hash-"+name+suffix,sdk()+extraSdk+executable);
}
@Test void scalarWrappersMatchIndependentProviderOracle() throws Exception {
String corpus;
try(var stream=getClass().getResourceAsStream("/hash/hash-conformance.tsv")) {
assertNotNull(stream);corpus=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
int count=0;var manifest=new StringBuilder();
for(var row:corpus.lines().skip(1).toList()) {
var parts=row.split("\t",-1);String expression;int id;int bytes=0;
switch(parts[0]) {
case "i64" -> {expression="hash.i64("+parts[1]+")";id=0x4100;}
case "float" -> {
long bits=Long.parseUnsignedLong(parts[1],16);
String value=bits==Long.MIN_VALUE?"-0.0":java.math.BigDecimal.valueOf(Double.longBitsToDouble(bits)).toPlainString();
if(!value.contains("."))value+=".0";
expression="hash.f64("+value+")";id=0x4101;
}
case "string" -> {
var content=HexFormat.of().parseHex(parts[1]);bytes=content.length;
String value=new String(content,StandardCharsets.UTF_8).replace("\\","\\\\").replace("\"","\\\"")
.replace("\n","\\n").replace("\r","\\r").replace("\t","\\t");
expression="hash.text(\""+value+"\")";id=0x4102;
}
default -> {continue;}
}
String name="vector-"+count++;
compile(name,"declare service Probe {fn main()->long {return "+expression+";}}");
for(var suffix:new String[]{"","-optimized"})
manifest.append("pbs-hash-").append(name).append(suffix).append('\t').append(parts[2])
.append('\t').append(id).append('\t').append(8+bytes).append('\n');
}
assertEquals(30,count);
Files.writeString(Path.of(System.getProperty("pbs.registryConformanceDirectory")).resolve("hash-vectors.tsv"),manifest);
}
@Test void instanceQueriesCoverMutableObjectsViewsArraysFunctionsAndGenerators() throws Exception {
compile("object","""
declare struct Item(pub mut value:int);
declare service Probe {fn main()->int {
let item=new Item(20);let other=new Item(20);let saved=referenceHash<Item>(item);
item.value=42;
if saved!=referenceHash<Item>(item) {return 0;}
if referenceEquals<Item>(item,item)==false or referenceEquals<Item>(item,other) {return 0;}
return 42;
}}
""");
compile("contract","""
declare contract Read {fn read()->int;}
declare struct Item(pub mut value:int);
implements Read for Item using item {fn read()->int {return item.value;}}
declare service Probe {fn main()->int {
let item=new Item(20);let other=new Item(20);let left:Read=item;let right:Read=item;
if referenceHash<Read>(left)!=referenceHash<Item>(item) {return 0;}
if referenceEquals<Read>(left,right)==false {return 0;}
if referenceEquals<Read>(left,other as Read) {return 0;}
item.value=42;return right.read();
}}
""");
compile("array","""
declare service Probe {fn main()->int {
let a:array<int>=[1];let b:array<int>=[1];let h=referenceHash<array<int>>(a);a[0]=42;
if h!=referenceHash<array<int>>(a) or referenceEquals<array<int>>(a,b) {return 0;}
if referenceEquals<array<int>>(a,a)==false {return 0;}return a[0] else 0;
}}
""");
compile("function","""
fn make()->Fn()->int {return fn []()->int{return 42;};}
declare service Probe {fn main()->int {
let a=make();let b=make();let alias=a;
if referenceHash<Fn()->int>(a)!=referenceHash<Fn()->int>(alias) {return 0;}
if referenceEquals<Fn()->int>(a,alias)==false or referenceEquals<Fn()->int>(a,b) {return 0;}
return alias();
}}
""");
String random;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/random/main.pbs")) {
assertNotNull(stream);random=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
compile("random","""
declare service Probe {fn main()->int {
let a=random.new(42);let b=random.new(42);let h=referenceHash<Random>(a);random.next(a);
if h!=referenceHash<Random>(a) or referenceEquals<Random>(a,b) {return 0;}
if referenceEquals<Random>(a,a)==false {return 0;}return 42;
}}
""",random);
compile("discard","declare service Probe {fn main()->int {hash.text(\"hello\");hash.i64(-1);return 42;}}");
compile("discard-reference","""
declare struct Item(pub value:int);
declare service Probe {fn main()->int{
let item=new Item(1);let other=new Item(1);
referenceHash<Item>(item);referenceEquals<Item>(item,other);return 42;
}}
""");
}
@Test void invalidReferenceSourcesAndProjectAttributesReject() throws Exception {
for(var argument:new String[]{"int","str","optional int","(x:int,y:int)"}) {
var diagnostics=DiagnosticSink.empty();
String value=switch(argument){case "int"->"1";case "str"->"\"x\"";case "optional int"->"none";default->"(x:1,y:2)";};
new PbsFrontendCompiler().compileFile(new FileId(0),sdk()+"fn probe()->long {return referenceHash<"+argument+">("+value+");}",diagnostics,SourceKind.SDK_INTERFACE);
assertTrue(diagnostics.stream().anyMatch(d->d.getCode().equals("E_SEM_IDENTITY_REFERENCE_TYPE")),argument+": "+diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList());
}
var diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),"[IdentityPrimitive(name=\"referenceHash\")] fn counterfeit<T>(x:T)->long{return 0;}",diagnostics);
assertTrue(diagnostics.hasErrors());
}
}
@@ -21,10 +21,15 @@ class PbsListCursorTest {
return compileCollection("list", name, source);
}
static BytecodeModule compileCollection(String collection,String name,String source) throws Exception {
return compileCollection(collection,name,source,null);
}
static BytecodeModule compileCollection(String collection,String name,String source,String sdkOverride) 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")) {
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
String sdk=sdkOverride;
if(sdk==null) {
try(var stream=PbsListCursorTest.class.getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
assertNotNull(stream);sdk=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
}
var ast=PbsParser.parse(PbsLexer.lex(sdk+"\n"+source,id,diagnostics),id,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
var file=new PbsFrontendCompiler().compileParsedFile(id,ast,diagnostics,source.contains("[BuiltinType")?SourceKind.SDK_INTERFACE:SourceKind.PROJECT,new ModuleId(0),
@@ -39,9 +44,10 @@ class PbsListCursorTest {
if(name.startsWith("gc"))module=PbsStructuredValuesTest.withTestSafepoint(module);
new BytecodePreloadVerifierService().verify(module);
Files.createDirectories(directory());Files.write(directory().resolve("pbs-"+collection+"-"+name+".pbx"),module.serialize());
if(!name.startsWith("gc")) {
if(!name.startsWith("gc") || collection.equals("map")) {
var optimized=new OptimizeIRVMService().optimize(new LowerToIRVMService().lower(backend));
var optimizedModule=new BytecodeEmitter().emit(optimized.coherentEmissionPlan());
if(name.startsWith("gc"))optimizedModule=PbsStructuredValuesTest.withTestSafepoint(optimizedModule);
new BytecodePreloadVerifierService().verify(optimizedModule);
Files.write(directory().resolve("pbs-"+collection+"-"+name+"-optimized.pbx"),optimizedModule.serialize());
}
@@ -0,0 +1,341 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class PbsMapKeyvalTest {
static void compile(String name,String source)throws Exception { PbsListCursorTest.compileCollection("map",name,source); }
static void probe(String name,String body)throws Exception { compile(name,"declare service Probe {fn main()->int {"+body+"}}"); }
@Test void fullApiAndConstructionPreservePreviousValues()throws Exception {
probe("basic","""
let values:map<str,int>=new map();let alias=values;
if values.isEmpty()==false or values.size()!=(0 as long){return 0;}
if values["missing"].hasSome() or values.remove("missing").hasSome() or values.containsKey("missing"){return 0;}
values["a"]=0;if values["a"].hasNone() or values.containsKey("a")==false{return 0;}
if (values.put(new keyval<str,int>("a",20)) else -1) != 0 {return 0;}
if (alias["a"] else 0) != 20 {return 0;}
if values.put(new keyval<str,int>("b",22)).hasSome(){return 0;}
if values.size()!=(2 as long){return 0;}
let total:int=(values.remove("a") else 0)+(values.remove("b") else 0);
if values.remove("a").hasSome() or values.isEmpty()==false{return 0;}
for i:int from 0 until 40 {let indexed=new map<int,int>();indexed[i]=42;if (indexed[i] else 0) != 42{return 0;}}
values["again"]=42;values.clear();values.clear();
let diamond:map<long,bool>=new map<>();diamond[9223372036854775807]=false;
if diamond[9223372036854775807].hasNone(){return 0;}
let maybe:optional (map<int,int>)=some(new map());
if let present=maybe {present[0]=total;return present[0] else 0;}return 0;
""");
}
@Test void optionalPayloadPresenceIsNeverEntryAbsence()throws Exception {
probe("optional","""
let values=new map<str,optional int>();
if values["a"].hasSome(){return 0;}
values["a"]=none;
if values.containsKey("a")==false{return 0;}
if let payload=values["a"] {if payload.hasSome(){return 0;}}else{return 0;}
if let old=values.put(new keyval<str,optional int>("a",some(42))) {if old.hasSome(){return 0;}}else{return 0;}
let capture=values.cursor();values["a"]=none;
if let entry=capture.current(){if (entry.value() else 0) != 42{return 0;}}else{return 0;}
if let removed=values.remove("a"){if removed.hasSome(){return 0;}}else{return 0;}
if values.remove("a").hasSome(){return 0;}
let nested=new map<int,optional (optional int)>();nested[0]=some(none);
if let outer=nested[0]{if let inner=outer{if inner.hasSome(){return 0;}}else{return 0;}}else{return 0;}
let independent=new keyval<optional int,optional int>(none,some(42));
if independent.key().hasSome(){return 0;}return independent.value() else 0;
""");
}
@Test void defaultIdentitiesPreserveStaticDomainsAndObjectAliases()throws Exception {
probe("scalars","""
let ints=new map<int,int>();ints[-2147483648]=20;ints[0]=22;
let longs=new map<long,int>();longs[-9223372036854775808]=20;longs[9223372036854775807]=22;
let floats=new map<float,int>();floats[0.0]=20;floats[-0.0]=42;
if floats.size()!=(1 as long) or (floats[0.0] else 0)!=42{return 0;}
let flags=new map<bool,bool>();flags[false]=false;flags[true]=true;
if flags[false].hasNone() or (flags[false] else true){return 0;}
let text=new map<str,int>();text["hel"+"lo"]=42;text[""]=0;text["ação 😀"]=20;
if (text["hello"] else 0)!=42 or text.size()!=(3 as long){return 0;}
return (ints[-2147483648] else 0)+(longs[9223372036854775807] else 0);
""");
compile("objects","""
declare struct Item(pub mut value:int);
declare contract Read{fn read()->int;}
implements Read for Item using x{fn read()->int{return x.value;}}
declare service Probe{fn main()->int{
let a=new Item(20);let b=new Item(20);let values=new map<Item,Item>();
values[a]=a;values[b]=b;a.value=42;
if values.size()!=(2 as long){return 0;}
if let stored=values[a]{if stored!=a or stored.value!=42{return 0;}}else{return 0;}
let views=new map<Read,int>();views[a as Read]=20;views[b as Read]=22;
if views.size()!=(2 as long){return 0;}
let receivers=new map<Read,Read>();receivers[a as Read]=b as Read;
let capture=receivers.cursor();receivers.clear();
if let e=capture.current(){if e.key().read()!=42 or e.value().read()!=20{return 0;}}else{return 0;}
return (views[a as Read] else 0)+(views[b as Read] else 0);
}}
""");
String random;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/random/main.pbs")) {
assertNotNull(stream);random=new String(stream.readAllBytes(),java.nio.charset.StandardCharsets.UTF_8);
}
compile("random",random+"""
fn randomMapProbe()->int{
let values=new map<Random,Random>();let a=random.new(42);let b=random.new(42);
values[a]=b;values[b]=a;random.next(a);
if values.size()!=(2 as long) or values.containsKey(a)==false{return 0;}
if let found=values[a]{if found!=b{return 0;}}else{return 0;}return 42;
}
declare service Probe{fn main()->int{return randomMapProbe();}}
""");
probe("references","""
let a=new array<int>(1);a[0]=20;let b=new array<int>(1);b[0]=20;
let arrays=new map<array<int>,int>();arrays[a]=20;arrays[b]=22;a[0]=42;
if arrays.size()!=(2 as long){return 0;}
let f:Fn(int)->int=fn[](x:int)->int{return x;};let g:Fn(int)->int=fn[](x:int)->int{return x;};
let functions=new map<Fn(int)->int,int>();functions[f]=20;functions[g]=22;
if functions.size()!=(2 as long) or (arrays[a] else 0)!=20{return 0;}
return (functions[f] else 0)+(functions[g] else 0);
""");
}
@Test void contentReplacementPreservesTheOriginalRepresentativeAndSnapshot()throws Exception {
compile("content","""
declare struct Item(pub id:int,pub mut score:int);
declare service Policy{}
implements Identity<Item> for Policy using p{
fn hash(v:Item)->long{return v.id as long;}
fn equals(a:Item,b:Item)->bool{return a.id==b.id;}
}
declare service Probe{fn main()->int{
let a=new Item(7,20);let equal=new Item(7,22);let values=new map<Item,Item>(Policy);
values[a]=a;let before=values.cursor();
let old=values.put(new keyval<Item,Item>(equal,equal));
if let item=old{if item!=a{return 0;}}else{return 0;}
if values.size()!=(1 as long){return 0;}
if let entry=values.cursor().current(){if entry.key()!=a or entry.value()!=equal{return 0;}}else{return 0;}
if let entry=before.current(){if entry.key()!=a or entry.value()!=a{return 0;}}else{return 0;}
a.score=42;if let entry=before.current(){if entry.value().score!=42{return 0;}}else{return 0;}
values.remove(equal);values[equal]=equal;
if let entry=values.cursor().current(){if entry.key()!=equal{return 0;}}else{return 0;}
let one=new keyval<Item,Item>(a,equal);let two=new keyval<Item,Item>(a,equal);
let associations=new set<keyval<Item,Item>>();associations.add(one);associations.add(two);
if associations.size()!=(2 as long){return 0;}return 42;
}}
""");
}
@Test void collisionCountersCoverRehashAndChainRemoval()throws Exception {
compile("collisions","""
declare struct Policy(pub mut hashes:int,pub mut comparisons:int);
implements Identity<int> for Policy using p{
fn hash(v:int)->long{p.hashes=p.hashes+1;return -9223372036854775808;}
fn equals(a:int,b:int)->bool{p.comparisons=p.comparisons+1;return a==b;}
}
declare service Probe{fn main()->int{
let policy=new Policy(0,0);let values=new map<int,int>(policy);
for i:int from 0 until 6{values[i]=i;}
if policy.hashes!=6 or policy.comparisons!=15{return 0;}
values[6]=6;if policy.hashes!=7 or policy.comparisons!=21{return 0;}
let capture=values.cursor();if policy.hashes!=7 or policy.comparisons!=21{return 0;}
let total:int=0;
while true{if let entry=capture.current(){total=total+entry.value();}else{break;}if capture.hasNext(){capture.next();}else{break;}}
if total!=21{return 0;}
values[6]=42;if values.size()!=(7 as long) or (values[6] else 0)!=42{return 0;}
for i:int from 7 until 40{values[i]=i;}
for i:int from 0 until 40{if values.containsKey(i)==false{return 0;}}
values.remove(20);values.remove(39);values.remove(0);
if values.containsKey(20) or values.containsKey(39) or values.containsKey(0){return 0;}
for i:int from 0 until 40{values.remove(i);}
if values.isEmpty()==false{return 0;}return 42;
}}
""");
probe("arithmetic","""
let maximum:long=4294967295;let sizeMaximum:long=9223372036854775807;
if _hashCapacity(0,1)!=(8 as long) or _hashCapacity(8,6)!=(8 as long){return 0;}
if _hashCapacity(8,7)!=(16 as long) or _hashCapacity(maximum-1,maximum)!=maximum{return 0;}
if _hashCapacity(maximum,maximum+1)!=maximum or _hashNextSize(sizeMaximum-1)!=sizeMaximum{return 0;}
if _hashBucket(-9223372036854775808,maximum)<(0 as long) or _hashBucket(-1,8)!=(7 as long){return 0;}return 42;
""");
probe("fault-overflow","_hashNextSize(9223372036854775807);return 0;");
}
@Test void groupedKeysValuesAndOptionalCarriersPreserveFullPayloads()throws Exception {
compile("tuple","""
declare service Policy{}
implements Identity<(a:int,b:int)> for Policy using p{
fn hash(v:(a:int,b:int))->long{return -1;}
fn equals(a:(a:int,b:int),b:(a:int,b:int))->bool{return a.a==b.a&&a.b==b.b;}
}
declare service Probe{fn main()->int{
let m=new map<(a:int,b:int),optional (left:int,right:int)>(Policy);
let key=(a:20,b:22);m[key]=some((left:20,right:22));
for i:int from 0 until 20{m[(a:i,b:0)]=none;}
let walk=m.cursor();m.clear();let total:int=0;
while true{if let entry=walk.current(){if let payload=entry.value(){total=total+payload.left+payload.right;}}else{break;}if walk.hasNext(){walk.next();}else{break;}}
return total;
}}
""");
compile("native","""
[BuiltinType(name="vec2",version=1)]declare builtin type Vector(pub x:float,pub y:float){}
declare service Policy{}
implements Identity<Vector> for Policy using p{
fn hash(v:Vector)->long{return -1;}
fn equals(a:Vector,b:Vector)->bool{return a.x==b.x&&a.y==b.y;}
}
fn nativeProbe()->int{
let values=new map<Vector,Vector>(Policy);let k=new Vector(1.0,2.0);values[k]=new Vector(20.0,22.0);
if let previous=values.put(new keyval<Vector,Vector>(new Vector(1.0,2.0),new Vector(21.0,21.0))){if previous.x!=20.0 or previous.y!=22.0{return 0;}}else{return 0;}
let capture=values.cursor();values.clear();if let e=capture.current(){if e.key().x!=1.0 or e.key().y!=2.0{return 0;}return (e.value().x+e.value().y) as int;}return 0;
}
declare service Probe{fn main()->int{return nativeProbe();}}
""");
compile("pixel","""
[BuiltinType(name="color",version=1)]declare builtin type Color(pub raw:long){}
[BuiltinConst(target="color",name="red",version=1)]declare const RED:Color;
[BuiltinType(name="pixel",version=1)]declare builtin type Pixel(pub x:long,pub y:long,pub color:Color){}
declare service Policy{}
implements Identity<Pixel> for Policy using p{
fn hash(v:Pixel)->long{return -1;}
fn equals(a:Pixel,b:Pixel)->bool{return a.x==b.x&&a.y==b.y&&a.color==b.color;}
}
fn pixelProbe()->int{
let values=new map<Pixel,Pixel>(Policy);let k=new Pixel(1,2,RED);values[k]=new Pixel(20,22,RED);
let capture=values.cursor();values.clear();if let e=capture.current(){if e.key().x!=(1 as long) or e.key().y!=(2 as long) or e.value().color!=RED{return 0;}return (e.value().x+e.value().y) as int;}return 0;
}
declare service Probe{fn main()->int{return pixelProbe();}}
""");
}
@Test void cursorKeepsTheSharedFullStateTableAndIndependentAssociations()throws Exception {
probe("cursor-empty","""
let values=new map<int,int>();let capture=values.cursor();
for i:int from 0 until 3{if capture.hasNext() or capture.current().hasSome(){return 0;}capture.next();}return 42;
""");
probe("cursor-single","""
let values=new map<int,int>();values[0]=42;let capture=values.cursor();values[0]=0;values.clear();
for i:int from 0 until 3{if capture.hasNext(){return 0;}if let e=capture.current(){if e.key()!=0 or e.value()!=42{return 0;}}else{return 0;}capture.next();}return 42;
""");
probe("snapshot","""
let values=new map<int,int>();values[0]=20;values[1]=22;let capture=values.cursor();
values[0]=0;values.remove(1);for i:int from 2 until 40{values[i]=i;}values.clear();
let sum:int=0;let count:int=0;
while true{if let e=capture.current(){sum=sum+e.value();count=count+1;}else{break;}if capture.hasNext(){capture.next();}else{break;}}
if count!=2{return 0;}return sum;
""");
}
@Test void receiverAndArgumentsAreEvaluatedOnceInSourceOrder()throws Exception {
compile("evaluation","""
declare struct Effects(pub mut trace:int,pub m:map<str,int>){
fn receiver()->map<str,int>{this.trace=this.trace*10+1;return this.m;}
fn key()->str{this.trace=this.trace*10+2;return "a";}
fn value()->int{this.trace=this.trace*10+3;return 42;}
}
fn insert(values:map<str,int>,key:str,value:int){values[key]=value;}
declare service Probe{fn main()->int{
let m=new map<str,int>();let e=new Effects(0,m);e.receiver()[e.key()]=e.value();
if e.trace!=123{return 0;}e.trace=0;let found=e.receiver()[e.key()];if e.trace!=12{return 0;}
e.trace=0;insert apply (value:e.value(),key:e.key(),values:e.receiver());if e.trace!=321{return 0;}
e.trace=0;let removed=e.receiver().remove(e.key());if e.trace!=12{return 0;}return removed else 0;
}}
""");
}
@Test void callbacksRespectGuardAndReadOnlyPreparation()throws Exception {
for(int action=1;action<=7;action++){
String operation=switch(action){case 1->"let found=m[0];";case 2->"m[0]=42;";case 3->"m.containsKey(0);";case 4->"m.put(new keyval<int,int>(0,42));";case 5->"m.remove(0);";case 6->"m.clear();";default->"let failure=1/0;";};
compile("fault-callback-"+action,"""
declare struct Policy(pub mut owner:optional (map<int,int>));
implements Identity<int> for Policy using p{
fn hash(v:int)->long{if let m=p.owner{OPERATION}return 0;}
fn equals(a:int,b:int)->bool{return a==b;}
}
declare service Probe{fn main()->int{let p=new Policy(none);let m=new map<int,int>(p);p.owner=some(m);m[0]=42;return 0;}}
""".replace("OPERATION",operation));
compile("fault-equals-"+action,"""
declare struct Policy(pub mut owner:optional (map<int,int>),pub mut armed:bool);
implements Identity<int> for Policy using p{
fn hash(v:int)->long{return 0;}
fn equals(a:int,b:int)->bool{if p.armed{if let m=p.owner{OPERATION}}return a==b;}
}
declare service Probe{fn main()->int{let p=new Policy(none,false);let m=new map<int,int>(p);p.owner=some(m);m[0]=20;p.armed=true;m[1]=22;return 0;}}
""".replace("OPERATION",operation));
}
compile("callback-reads","""
declare struct Policy(pub mut owner:optional (map<int,int>),pub mut calls:int);
implements Identity<int> for Policy using p{
fn hash(v:int)->long{
if let m=p.owner{if m.size()!=(p.calls as long){let failure=1/0;}let c=m.cursor();if p.calls==0&&c.current().hasSome(){let failure=1/0;}if p.calls>0&&c.current().hasNone(){let failure=1/0;}}
p.calls=p.calls+1;return 0;
}
fn equals(a:int,b:int)->bool{return a==b;}
}
declare service Probe{fn main()->int{let p=new Policy(none,0);let m=new map<int,int>(p);p.owner=some(m);m[0]=20;m[1]=22;if p.calls!=2{return 0;}return 42;}}
""");
}
@Test void resourceFixturesExposeEachStructuralPhase()throws Exception {
for(var operation:java.util.Map.of(
"growth","values[42]=42;","add","values[42]=42;","replace","values[1]=42;",
"put","let old=values.put(new keyval<int,int>(1,42));","remove","let old=values.remove(1);",
"clear","values.clear();","cursor","let capture=values.cursor();","read","let found=values[1];").entrySet()){
compile("refusal-"+operation.getKey(),"""
fn mutationMarker(){let marker:int=314160;}
fn completedMarker(){let marker:int=314161;}
fn performSecond(values:map<int,int>){OPERATION}
fn perform(values:map<int,int>){performSecond(values);}
declare service Probe{fn main()->int{let values=new map<int,int>();for i:int from 0 until COUNT{values[i]=i;}mutationMarker();perform(values);completedMarker();return 42;}}
""".replace("OPERATION",operation.getValue()).replace("COUNT",operation.getKey().equals("growth")?"6":"5"));
}
String sdk=PbsSetTest.sdk();int offset=sdk.indexOf("[SdkMap]");
String injected=sdk.substring(0,offset)+sdk.substring(offset).replace("this.buckets = published;","""
this.buckets = published;
if this.length == (6 as long) {let injectedQuotaProbe=new array<int>(1000000);}
""");
PbsListCursorTest.compileCollection("map","injected-refusal-growth","""
declare service Probe{fn main()->int{let values=new map<int,int>();for i:int from 0 until 6{values[i]=i;}values[42]=42;return 42;}}
""",injected);
}
@Test void realSafepointsObserveKeysValuesAssociationsAndSnapshots()throws Exception {
compile("gc","""
declare struct Item(pub value:int);declare struct Garbage();
fn collect(){let marker:int=314159;}
fn makeCursor()->Cursor<keyval<Item,Item>>{
let values=new map<Item,Item>();for i:int from 0 until 1200{let key=new Item(i);let value=new Item(i);values[key]=value;}return values.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();}
declare service Probe{fn main()->int{useCursor();for i:int from 0 until 1200{let x=new Garbage();}collect();return 42;}}
""");
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 x=new Garbage();}}
fn create()->map<Item,Item>{let m=new map<Item,Item>();for i:int from 0 until 64{m[new Item(i)]=new Item(i);}return m;}
fn replace(values:map<Item,Item>){let c=values.cursor();for i:int from 0 until 3{if let e=c.current(){values[e.key()]=new Item(42);}c.next();}}
fn discard(values:map<Item,Item>){let c=values.cursor();for i:int from 0 until 3{if let e=c.current(){values.remove(e.key());}c.next();}}
fn use(){let m=create();pressure();collect();replace(m);pressure();collect();discard(m);pressure();collect();m.clear();pressure();collect();}
declare service Probe{fn main()->int{use();pressure();collect();return 42;}}
""");
}
@Test void importsAndGenericReexportsUseRealSdkModules(@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 Values<K,V>;pub struct Entry<K,V>;pub contract Walk<T>;");
Files.writeString(root.resolve("src/facade/main.pbs"),"import {map as Values,keyval as Entry,Cursor as Walk} from @sdk:collections;");
Files.writeString(root.resolve("src/main.pbs"),"""
import {Values,Entry,Walk} from @app:facade;
[Frame]fn frame()->void{
let values:Values<str,optional int>=new Values();values["left"]=some(20);
let old=values.put(new Entry<str,optional int>("right",some(22)));
if old.hasSome(){let failure=1/0;}
values["empty"]=none;let c:Walk<Entry<str,optional int>>=values.cursor();values.clear();
let sum:int=0;while true{if let e=c.current(){if let v=e.value(){sum=sum+v;}}else{break;}if c.hasNext(){c.next();}else{break;}}
if sum!=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());
Files.write(Path.of(System.getProperty("pbs.registryConformanceDirectory")).resolve("pbs-map-imports.pbx"),compiled.bytecodeBytes());
}
}
@@ -0,0 +1,404 @@
package p.studio.compiler.backend.irvm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.pbs.*;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import static org.junit.jupiter.api.Assertions.*;
class PbsSetTest {
static String sdk() throws Exception {
try(var stream=PbsSetTest.class.getResourceAsStream("/game/stdlib/1/sdk/collections/main.pbs")) {
assertNotNull(stream);return new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
}
static void compile(String name,String source)throws Exception{PbsListCursorTest.compileCollection("set",name,source);}
static void probe(String name,String body)throws Exception{compile(name,"declare service Probe {fn main()->int {"+body+"}}");}
@Test void constructionEveryOperationAndGrowthUseTheSourceSdk()throws Exception {
probe("basic","""
let values:set<int>=new set();let alias=values;
if values.isEmpty()==false or values.size()!=(0 as long) {return 0;}
if values.contains(42) or values.remove(42) {return 0;}
if values.add(42)==false or values.add(42) {return 0;}
if alias.size()!=(1 as long) or alias.contains(42)==false {return 0;}
if values.remove(42)==false or values.remove(42) {return 0;}
if values.isEmpty()==false {return 0;}
for i:int from 0 until 40 {if values.add(i)==false {return 0;}}
for i:int from 0 until 40 {if alias.contains(i)==false {return 0;}}
if values.size()!=(40 as long) {return 0;}
for i:int from 0 until 40 {if values.remove(i)==false {return 0;}}
values.add(9);values.clear();values.clear();
if values.isEmpty()==false or values.contains(9) {return 0;}
values.add(42);let cursor=values.cursor();return cursor.current() else 0;
""");
probe("construction","""
let a:set<int>=new set<>();let b:optional (set<int>)=some(new set());
if let values=b {values.add(42);if values.size()!=(1 as long) {return 0;}} else {return 0;}
if a.isEmpty()==false {return 0;}return 42;
""");
probe("arithmetic","""
let maximum:long=4294967295;let sizeMaximum:long=9223372036854775807;
if _hashCapacity(0,1)!=(8 as long) or _hashCapacity(8,6)!=(8 as long) {return 0;}
if _hashCapacity(8,7)!=(16 as long) or _hashCapacity(16,13)!=(32 as long) {return 0;}
if _hashCapacity(maximum-1,maximum)!=maximum {return 0;}
if _hashCapacity(maximum,maximum+1)!=maximum {return 0;}
if _hashNextSize(sizeMaximum-1)!=sizeMaximum {return 0;}
let minimum:long=-9223372036854775808;
if _hashBucket(minimum,maximum)<(0 as long) or _hashBucket(-1,8)!=(7 as long) {return 0;}
return 42;
""");
}
@Test void basicDefaultsKeepValueDomainsAndPresence()throws Exception {
probe("scalars","""
let ints=new set<int>();ints.add(-2147483648);ints.add(0);ints.add(42);
if ints.add(-2147483648) or ints.size()!=(3 as long) {return 0;}
let longs=new set<long>();longs.add(-9223372036854775808);longs.add(9223372036854775807);
if longs.contains(-9223372036854775808)==false {return 0;}
let booleans=new set<bool>();booleans.add(false);booleans.add(true);
if booleans.add(false) or booleans.size()!=(2 as long) {return 0;}
let floats=new set<float>();floats.add(0.0);
if floats.add(-0.0) or floats.contains(-0.0)==false {return 0;}
floats.add(0.5);if floats.remove(-0.0)==false or floats.size()!=(1 as long) {return 0;}
let strings=new set<str>();strings.add("hello");let constructed="hel"+"lo";
if strings.add(constructed) or strings.contains(constructed)==false {return 0;}
strings.add("");strings.add("ação 😀");
if strings.size()!=(3 as long) {return 0;}return 42;
""");
}
@Test void objectsAndContractReceiversKeepInstanceIdentity()throws Exception {
compile("objects","""
declare contract Read {fn read()->int;}
declare struct Item(pub mut value:int);
implements Read for Item using item {fn read()->int{return item.value;}}
declare service Probe {fn main()->int {
let a=new Item(20);let b=new Item(20);let values=new set<Item>();let alias=values;
if values.add(a)==false or values.add(b)==false or values.add(a) {return 0;}
a.value=42;if alias.contains(a)==false {return 0;}
let views=new set<Read>();views.add(a as Read);
if views.add(a as Read) or views.add(b as Read)==false {return 0;}
if views.size()!=(2 as long) or views.contains(a as Read)==false {return 0;}
if values.remove(a)==false or values.contains(b)==false {return 0;}return a.value;
}}
""");
}
@Test void optionalMissingDefaultsAndPositionalAccessReject()throws Exception {
for(var source:new String[]{
"fn test(){let values=new set<optional int>();}",
"fn test(){let values=new set<(x:int,y:int)>();}",
"fn test(){let values=new set<int>();values[0]=42;}",
"fn test(){let values=new set<int>();values._hash(42);}",
"fn test(){let values=new set<int>();let hidden=values.buckets;}",
"fn test(){let values=new set<int>(8);}",
"fn test(){let values:set<long>=new set<int>();}",
"fn test(){let values=new set(42);}"}) {
var diagnostics=DiagnosticSink.empty();var id=new FileId(0);
var ast=PbsParser.parse(PbsLexer.lex(sdk()+source,id,diagnostics),id,diagnostics,PbsParser.ParseMode.INTERFACE_MODULE);
new PbsFrontendCompiler().compileParsedFile(id,ast,diagnostics,SourceKind.PROJECT,new p.studio.compiler.source.identifiers.ModuleId(0),
p.studio.utilities.structures.ReadOnlyList.empty(),p.studio.compiler.messages.HostAdmissionContext.permissiveDefault(),
p.studio.compiler.messages.FESurfaceContext.empty(),new p.studio.compiler.source.tables.NameTable());
assertTrue(diagnostics.hasErrors(),source);
if(source.contains("set<optional"))assertTrue(diagnostics.stream().anyMatch(d->d.getCode().equals("E_SEM_GENERIC_SET_OPTIONAL_ELEMENT")));
if(source.contains("set<(x:"))assertTrue(diagnostics.stream().anyMatch(d->d.getCode().equals("E_SEM_GENERIC_SET_IDENTITY_REQUIRED")));
}
}
@Test void receiverArgumentsAndNamedApplicationEvaluateOnceInSourceOrder()throws Exception {
compile("evaluation","""
declare struct Effects(pub mut trace:int,pub values:set<int>){
fn receiver()->set<int>{this.trace=this.trace*10+1;return this.values;}
fn value()->int{this.trace=this.trace*10+2;return 42;}
}
fn include(values:set<int>,value:int)->bool{return values.add(value);}
declare service Probe {fn main()->int{
let values=new set<int>.make();let effects=new Effects(0,values);
effects.receiver().add(effects.value());if effects.trace!=12{return 0;}
effects.trace=0;effects.receiver().add apply effects.value();if effects.trace!=12{return 0;}
effects.trace=0;if effects.receiver().contains(effects.value())==false or effects.trace!=12{return 0;}
effects.trace=0;if (effects.receiver().remove apply effects.value())==false {return 0;}
if effects.trace!=12 or values.isEmpty()==false{return 0;}
effects.trace=0;if (include apply (value:effects.value(),values:effects.receiver()))==false{return 0;}
if effects.trace!=21{return 0;}return 42;
}}
""");
}
@Test void collisionCountersProveRehashAndCaptureNeverCallStrategies()throws Exception {
compile("collisions","""
declare struct Policy(pub mut hashes:int,pub mut comparisons:int);
implements Identity<int> for Policy using policy {
fn hash(value:int)->long {policy.hashes=policy.hashes+1;return -9223372036854775808;}
fn equals(left:int,right:int)->bool {policy.comparisons=policy.comparisons+1;return left==right;}
}
declare service Probe {fn main()->int {
let policy=new Policy(0,0);let values=new set<int>(policy);
for i:int from 0 until 6 {values.add(i);}
if policy.hashes!=6 or policy.comparisons!=15 {return 0;}
let comparisons=policy.comparisons;
values.add(6);
if policy.hashes!=7 or policy.comparisons!=comparisons+6 {return 0;}
let captured=values.cursor();let saved=policy.comparisons;
if policy.hashes!=7 or saved!=21 {return 0;}
let total:int=0;
while true {
if let value=captured.current() {total=total+value;} else {break;}
if captured.hasNext() {captured.next();} else {break;}
}
if policy.hashes!=7 or policy.comparisons!=saved or total!=21 {return 0;}
if values.add(6) or policy.hashes!=8 {return 0;}
for i:int from 7 until 30 {values.add(i);}
for i:int from 0 until 30 {if values.contains(i)==false {return 0;}}
for i:int from 0 until 30 {if values.remove(i)==false {return 0;}}
if values.isEmpty()==false {return 0;}return 42;
}}
""");
}
@Test void explicitContentPolicyKeepsTheFirstActualReferenceAndGroupedValues()throws Exception {
compile("content","""
declare struct Item(pub mut key:int,pub mut health:int);
declare service Policy {}
implements Identity<Item> for Policy using policy {
fn hash(value:Item)->long {return value.key as long;}
fn equals(left:Item,right:Item)->bool {return left.key==right.key;}
}
declare service Probe {fn main()->int {
let first=new Item(7,20);let second=new Item(7,22);let values=new set<Item>(Policy);
if values.add(first)==false or values.add(second) or values.size()!=(1 as long) {return 0;}
first.health=42;let cursor=values.cursor();
if let value=cursor.current() {if value!=first {return 0;}} else {return 0;}
if values.remove(second)==false {return 0;}
first.key=9;values.add(first);if values.contains(first)==false {return 0;}return first.health;
}}
""");
compile("tuple","""
declare service Policy {}
implements Identity<(left:int,right:int)> for Policy using policy {
fn hash(value:(left:int,right:int))->long {return value.left as long;}
fn equals(a:(left:int,right:int),b:(left:int,right:int))->bool {return a.left==b.left && a.right==b.right;}
}
declare service Probe {fn main()->int {
let values=new set<(left:int,right:int)>(Policy);
if values.add((left:20,right:22))==false or values.add((left:20,right:22)) {return 0;}
values.add((left:20,right:1));let cursor=values.cursor();values.clear();
let total:int=0;
while true {
if let value=cursor.current() {if value.right==22 {total=value.left+value.right;}} else {break;}
if cursor.hasNext() {cursor.next();} else {break;}
}
return total;
}}
""");
}
@Test void snapshotsKeepSharedProtocolAndMutationsDoNotRewriteCapture()throws Exception {
probe("cursor-empty","let values=new set<int>();let cursor=values.cursor();values.add(42);cursor.next();if cursor.current().hasSome() or cursor.hasNext(){return 0;}return 42;");
probe("cursor-single","let values=new set<int>();values.add(42);let cursor=values.cursor();values.clear();cursor.next();cursor.next();if cursor.hasNext(){return 0;}return cursor.current() else 0;");
probe("snapshot","""
let values=new set<int>();values.add(0);values.add(20);values.add(22);let cursor=values.cursor();
values.remove(20);for i:int from 0 until 40 {values.add(i);}values.clear();
let total:int=0;let count:int=0;
while true {
if let value=cursor.current() {total=total+value;count=count+1;} else {break;}
if cursor.hasNext() {cursor.next();} else {break;}
}
if count!=3 or cursor.current().hasNone() {return 0;}return total;
""");
compile("contract-snapshot","""
declare contract Read{fn read()->int;}
declare struct Item(pub mut value:int);
implements Read for Item using item{fn read()->int{return item.value;}}
declare service Probe{fn main()->int{
let item=new Item(20);let values=new set<Read>();values.add(item as Read);
let cursor=values.cursor();values.clear();item.value=42;
if let view=cursor.current(){return view.read();}return 0;
}}
""");
probe("string-snapshot","""
let values=new set<str>();let text="hel"+"lo";values.add(text);
let cursor=values.cursor();values.clear();
if let value=cursor.current(){if value=="hello"{return 42;}}return 0;
""");
probe("false-snapshot","""
let values=new set<bool>();values.add(false);let cursor=values.cursor();values.clear();
cursor.next();if cursor.hasNext(){return 0;}
if let value=cursor.current(){if value==false{return 42;}}return 0;
""");
}
@Test void callbackReadsAreAllowedAndAliasReentrancyRefuses()throws Exception {
String policy="""
declare struct Policy(pub mut target:optional set<int>,pub mut mode:int,pub mut reads:int);
implements Identity<int> for Policy using policy {
fn hash(value:int)->long {
if let target=policy.target {
if policy.mode==1 {target.add(9);}
if policy.mode==2 {target.remove(9);}
if policy.mode==3 {target.clear();}
if policy.mode==4 {target.contains(9);}
if policy.mode==5 {let failure=1/0;}
let size=target.size();let empty=target.isEmpty();let cursor=target.cursor();
if empty && cursor.current().hasSome() {let failure=1/0;}
policy.reads=policy.reads+1;
}
return 0;
}
fn equals(left:int,right:int)->bool {return left==right;}
}
""";
compile("callback-reads",policy+"""
declare service Probe {fn main()->int {
let policy=new Policy(none,0,0);let values=new set<int>(policy);let alias=values;policy.target=some(alias);
values.add(42);if values.contains(42)==false or policy.reads!=2 {return 0;}
values.remove(42);if policy.reads!=3 or values.isEmpty()==false {return 0;}return 42;
}}
""");
for(int mode=1;mode<=5;mode++)compile("fault-callback-"+mode,policy+"declare service Probe {fn main()->int {let policy=new Policy(none,"+mode+",0);let values=new set<int>(policy);let alias=values;policy.target=some(alias);values.add(42);return 0;}}");
probe("fault-overflow","_hashNextSize(9223372036854775807);return 0;");
}
@Test void arraysFunctionsAndRandomKeepReferenceDefaults()throws Exception {
compile("references","""
fn closure()->Fn()->int {return fn []()->int{return 42;};}
declare service Probe {fn main()->int {
let arrays=new set<array<int>>();let a:array<int>=[1];let b:array<int>=[1];
arrays.add(a);arrays.add(b);a[0]=42;
if arrays.add(a) or arrays.contains(a)==false or arrays.size()!=(2 as long) {return 0;}
let calls=new set<Fn()->int>();let first=closure();let second=closure();
calls.add(first);calls.add(second);if calls.add(first) or calls.size()!=(2 as long) {return 0;}
let cursor=calls.cursor();calls.clear();if let f=cursor.current(){return f();}return 0;
}}
""");
String random;
try(var stream=getClass().getResourceAsStream("/game/stdlib/1/sdk/random/main.pbs")) {
assertNotNull(stream);random=new String(stream.readAllBytes(),StandardCharsets.UTF_8);
}
// SDK intrinsic service signatures live in an interface; the application body is a free function.
compile("random",random+"""
fn randomSetProbe()->int {
let values=new set<Random>();let a=random.new(42);let b=random.new(42);
values.add(a);values.add(b);random.next(a);
if values.add(a) or values.contains(a)==false or values.size()!=(2 as long){return 0;}return 42;
}
declare service Probe {fn main()->int{return randomSetProbe();}}
""");
}
@Test void nativeGroupStrategiesPreserveAllSlots()throws Exception {
compile("vec2","""
[BuiltinType(name="vec2",version=1)] declare builtin type Vector(pub x:float,pub y:float) {}
declare service Policy {}
implements Identity<Vector> for Policy using policy {
fn hash(value:Vector)->long {return 0;}
fn equals(a:Vector,b:Vector)->bool{return a.x==b.x && a.y==b.y;}
}
fn vectorProbe()->int {
let values=new set<Vector>(Policy);values.add(new Vector(20.0,22.0));
if values.add(new Vector(20.0,22.0)) {return 0;}
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 vectorProbe();}}
""");
compile("pixel","""
[BuiltinType(name="color",version=1)] declare builtin type Color(pub raw:long) {}
[BuiltinConst(target="color",name="red",version=1)] declare const RED:Color;
[BuiltinType(name="pixel",version=1)] declare builtin type Pixel(pub x:long,pub y:long,pub color:Color) {}
declare service Policy {}
implements Identity<Pixel> for Policy using policy {
fn hash(value:Pixel)->long{return value.x;}
fn equals(a:Pixel,b:Pixel)->bool{return a.x==b.x && a.y==b.y && a.color==b.color;}
}
fn pixelProbe()->int {
let values=new set<Pixel>(Policy);values.add(new Pixel(20,22,RED));
if values.add(new Pixel(20,22,RED)){return 0;}
let cursor=values.cursor();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 pixelProbe();}}
""");
}
@Test void importsReexportsAndHashAliasesCompileFromRealSdkModules(@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 Values<T>;pub contract Walk<T>;pub fn refHash<T>(value:T)->long;");
Files.writeString(root.resolve("src/facade/main.pbs"),"import {set as Values,Cursor as Walk} from @sdk:collections;import {referenceHash as refHash} from @sdk:hash;");
Files.writeString(root.resolve("src/main.pbs"),"""
import {Values,Walk,refHash} from @app:facade;
import {hash as Hash} from @sdk:hash;
[Frame] fn frame()->void {
let values:Values<int>=new Values();values.add(20);values.add(22);
if values.add(20){let failure=1/0;}
let before=refHash<Values<int>>(values);let cursor:Walk<int>=values.cursor();values.clear();
let expected:long=-8804195676797548855;
if before!=refHash<Values<int>>(values) or Hash.i64(-1)!=expected{let failure=1/0;}
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 result=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(result.diagnostics().stream().anyMatch(d->d.isError()),result.diagnostics().toString());
Files.write(Path.of(System.getProperty("pbs.registryConformanceDirectory")).resolve("pbs-set-imports.pbx"),result.bytecodeBytes());
}
@Test void resourceFixturesExposePreparationGrowthMutationAndCapture()throws Exception {
for(var operation:java.util.Map.of(
"growth","values.add(42);","add","values.add(42);","duplicate","values.add(1);",
"remove","values.remove(1);","clear","values.clear();","cursor","let snapshot=values.cursor();").entrySet()) {
int count=operation.getKey().equals("growth")?6:5;
compile("refusal-"+operation.getKey(),"""
fn mutationMarker(){let marker:int=314160;}
fn completedMarker(){let marker:int=314161;}
fn performSecond(values:set<int>){OPERATION}
fn perform(values:set<int>){performSecond(values);}
declare service Probe {fn main()->int {
let values=new set<int>();for i:int from 0 until COUNT {values.add(i);}
mutationMarker();perform(values);completedMarker();return 42;
}}
""".replace("COUNT",Integer.toString(count)).replace("OPERATION",operation.getValue()));
}
}
@Test void explicitPostPublicationFaultInjectionDoesNotClaimProductionRefusalFrequency()throws Exception {
// Only this fixture gets a controlled allocation after real source table installation.
// It qualifies the failure boundary; natural quota cases are measured separately.
String injected=sdk().replace("this.buckets = published;","""
this.buckets = published;
if this.length == (6 as long) {let injectedQuotaProbe=new array<int>(1000000);}
""");
PbsListCursorTest.compileCollection("set","injected-refusal-growth","""
declare service Probe {fn main()->int {
let values=new set<int>();for i:int from 0 until 6 {values.add(i);}
values.add(42);return 42;
}}
""",injected);
}
@Test void snapshotPrefixesFinalValuesAndRemovedNodesUseRealGc()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 set<Item>();for i:int from 0 until 1200 {values.add(new Item(i));}return values.cursor();
}
fn useCursor(){
let cursor=makeCursor();collect();for i:int from 0 until 1199 {cursor.next();}collect();
if let last=cursor.current(){
for i:int from 0 until 1200 {cursor.next();}collect();
if let still=cursor.current(){if still!=last {let failure=1/0;}}else{let failure=1/0;}
}else{let failure=1/0;}
}
declare service Probe {fn main()->int{useCursor();for i:int from 0 until 1200{let item=new Garbage();}collect();return 42;}}
""");
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 item=new Garbage();}}
fn create()->set<Item>{let values=new set<Item>();for i:int from 0 until 64 {values.add(new Item(i));}return values;}
fn discard(values:set<Item>){let cursor=values.cursor();for i:int from 0 until 3 {if let value=cursor.current(){values.remove(value);}cursor.next();}}
fn useCollection(){let values=create();pressure();collect();discard(values);pressure();collect();values.clear();pressure();collect();}
declare service Probe {fn main()->int{useCollection();pressure();collect();return 42;}}
""");
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,5 @@
Provider: dcf6561ea73446e0a3e1cf1c73eda20923b38df0
Source: crates/console/prometeu-vm/tests/fixtures/hash-conformance.tsv
SHA-256: a863486eaea14ab1a010f076f355eed0d5738207dad3651f7d10e92fc09f74d4
Independent XXH64 C v0.8.3 oracle; see provider tests/fixtures/hash-oracle.md.
Copied unchanged. Source producer tests select scalar/String rows; raw-byte and reference vectors remain provider evidence.
@@ -73,7 +73,11 @@ public final class PbsContractTypes {
declarations.put(name, declaration);
scopes.computeIfAbsent(declaration.span().getFileId(), ignored -> new HashMap<>()).put(name, declaration);
}
if (declaration instanceof PbsAst.ImplementsDecl implementation && implementations.stream().noneMatch(i -> i.span().equals(implementation.span()))) implementations.add(implementation);
// A generic owner family produces several concrete implementations at the
// same source span. Deduplicating by span alone loses all but its first T.
if (declaration instanceof PbsAst.ImplementsDecl implementation && implementations.stream().noneMatch(i ->
i.span().equals(implementation.span())&&key(i.ownerType()).equals(key(implementation.ownerType()))
&&key(i.contractType()).equals(key(implementation.contractType())))) implementations.add(implementation);
}
public PbsAst.TopDecl declaration(PbsAst.TypeRef type) {
@@ -221,7 +221,14 @@ public final class PbsGenericInstantiation {
if (t == null) return null;
var rewritten = super.type(t);
var d = types.declaration(t);
if (t.kind() != PbsAst.TypeRefKind.SIMPLE || d == null || PbsContractTypes.applicationOrigin(d) != null) return rewritten;
if (t.kind() != PbsAst.TypeRefKind.SIMPLE || d == null) return rewritten;
var origin=PbsContractTypes.applicationOrigin(d);
if(origin!=null) {
// A concrete argument still belongs to its defining module.
// The generic function's module must import that application.
dependency(definition,origin.declaration());
return rewritten;
}
var alias = nominalBinding(d);
types.index(new PbsAst.TypeBindingDecl(alias, d, definition));
var file = concrete.containsKey(definition.getFileId()) ? definition.getFileId() : sources.getFirst().ast().span().getFileId();
@@ -237,9 +244,9 @@ public final class PbsGenericInstantiation {
error("ARITY", name(template)+" requires "+parameters(template).size()+" explicit type argument(s)", site);
return null;
}
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);
if (PbsSdkCollections.isCollection(template) && PbsSdkCollections.directOptional(arguments.getFirst())) {
error(PbsSdkCollections.isMap(template)?"MAP_OPTIONAL_KEY":PbsSdkCollections.isSet(template)?"SET_OPTIONAL_ELEMENT":PbsSdkCollections.isLinked(template) ? "LINKED_OPTIONAL_ELEMENT" : "LIST_OPTIONAL_ELEMENT",
"SDK " + name(template) + " does not permit a directly optional " + (PbsSdkCollections.isMap(template)?"key":"element"), site);
return null;
}
var application = PbsAst.TypeRef.applied(name(template), arguments, template.span());
@@ -535,7 +542,14 @@ public final class PbsGenericInstantiation {
if (s instanceof PbsAst.ReturnStatement r) return new PbsAst.ReturnStatement(expression(r.value(), result), r.span());
if (s instanceof PbsAst.AssignStatement a) {
var target = a.target().expression();
if(target==null&&!a.target().pathSegments().isEmpty()) {
target=a.target().rootName().equals("this")?new PbsAst.ThisExpr(a.target().span())
:new PbsAst.IdentifierExpr(a.target().rootName(),a.target().span());
for(var member:a.target().pathSegments())target=new PbsAst.MemberExpr(target,member,a.target().span());
}
var expected = target == null ? locals.get(a.target().rootName()) : infer(target);
if(target instanceof PbsAst.IndexExpr && expected!=null && expected.kind()==PbsAst.TypeRefKind.OPTIONAL)
expected=expected.inner();
return new PbsAst.AssignStatement(a.target(), a.operator(), expression(a.value(), expected), a.span());
}
return super.statement(s);
@@ -543,6 +557,17 @@ public final class PbsGenericInstantiation {
@Override public PbsAst.Expression expression(PbsAst.Expression e, PbsAst.TypeRef expected) {
if (e != null) { budget.visit(e.span(), chain.size()); budget.store(e.span(), 64); }
if(e instanceof PbsAst.IndexExpr index) {
var receiver=expression(index.receiver(),null);
var owner=types.declaration(infer(receiver));
var origin=PbsContractTypes.applicationOrigin(owner);
var key=origin!=null&&PbsSdkCollections.isMap(origin.declaration())?origin.arguments().getFirst():null;
return new PbsAst.IndexExpr(receiver,expression(index.index(),key),index.span());
}
if(symbolicProbe&&e instanceof PbsAst.MemberExpr member) {
var receiver=infer(member.receiver());
if(receiver!=null)requiredShapes.add(receiver.name());
}
if (e instanceof PbsAst.CallExpr c) {
var callee = expression(c.callee());
var signatures = callableSignatures(callee);
@@ -631,8 +656,15 @@ public final class PbsGenericInstantiation {
else { error("CONTEXT", "Construction of "+n.typeName()+" requires explicit arguments or an expected application", n.span()); return n; }
}
var concreteType = type(applied);
if((PbsSdkCollections.isSet(s)||PbsSdkCollections.isMap(s))&&n.arguments().isEmpty()&&!symbolicProbe) {
var owner=types.declaration(concreteType);
var origin=PbsContractTypes.applicationOrigin(owner);
if(origin!=null&&!PbsSdkCollections.hasDefaultIdentity(origin.arguments().getFirst(),types))
error(PbsSdkCollections.isMap(s)?"MAP_IDENTITY_REQUIRED":"SET_IDENTITY_REQUIRED",
"This collection key/element type requires an explicit Identity strategy",n.span());
}
if (symbolicProbe) requiredShapes.add(concreteType.name());
var constructor = PbsSdkCollections.isIndexedCollection(s) && n.ctorName() == null ? "make" : n.ctorName();
var constructor = PbsSdkCollections.isCollection(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 "
@@ -655,6 +687,10 @@ public final class PbsGenericInstantiation {
case PbsAst.NewExpr n -> n.explicitType() != null ? n.explicitType() : PbsAst.TypeRef.simple(n.typeName(), n.span());
case PbsAst.IndexExpr i -> {
var receiver=infer(i.receiver());
var declaration=types.declaration(receiver);
var origin=PbsContractTypes.applicationOrigin(declaration);
if(origin!=null&&PbsSdkCollections.hasIndexer(origin.declaration()))
yield PbsAst.TypeRef.optional(origin.arguments().get(PbsSdkCollections.isMap(origin.declaration())?1:0),i.span());
yield receiver!=null && receiver.kind()==PbsAst.TypeRefKind.ARRAY ? PbsAst.TypeRef.optional(receiver.inner(),i.span()) : null;
}
case PbsAst.GroupExpr g -> infer(g.expression());
@@ -86,13 +86,30 @@ final class PbsGenericTemplateValidator {
}
@Override public PbsAst.Statement statement(PbsAst.Statement s) {
if (s instanceof PbsAst.LetStatement l) {
if(s instanceof PbsAst.IfStatement branch&&branch.bindingName()!=null) {
var optional=infer(branch.condition(),null);
while(optional!=null&&optional.kind()==PbsAst.TypeRefKind.GROUP)optional=optional.inner();
var previous=locals;locals=new HashMap<>(locals);
try {
if(optional!=null&&optional.kind()==PbsAst.TypeRefKind.OPTIONAL)locals.put(branch.bindingName(),optional.inner());
block(branch.thenBlock());
} finally {locals=previous;}
if(branch.elseIf()!=null)statement(branch.elseIf());
if(branch.elseBlock()!=null)block(branch.elseBlock());
return s;
} else if (s instanceof PbsAst.LetStatement l) {
var actual = infer(l.initializer(), l.explicitType());
checkCompatible(actual, l.explicitType(), l.span());
locals.put(l.name(), l.explicitType() == null ? actual : l.explicitType());
} else if (s instanceof PbsAst.ReturnStatement r) checkCompatible(infer(r.value(), result), result, r.span());
else if (s instanceof PbsAst.AssignStatement a) {
var target = a.target().expression() == null ? locals.get(a.target().rootName()) : infer(a.target().expression(), null);
var expression=a.target().expression();
if(expression==null&&!a.target().pathSegments().isEmpty()) {
expression=a.target().rootName().equals("this")?new PbsAst.ThisExpr(a.target().span())
:new PbsAst.IdentifierExpr(a.target().rootName(),a.target().span());
for(var member:a.target().pathSegments())expression=new PbsAst.MemberExpr(expression,member,a.target().span());
}
var target = expression == null ? locals.get(a.target().rootName()) : infer(expression, null);
// Indexed writes accept the element, while indexed reads are optional.
if(a.target().expression() instanceof PbsAst.IndexExpr && target!=null && target.kind()==PbsAst.TypeRefKind.OPTIONAL)
target=target.inner();
@@ -153,7 +170,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.isIndexedCollection(s) && n.ctorName()==null ? "make" : n.ctorName();
var constructor = PbsSdkCollections.isCollection(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());
@@ -192,6 +209,12 @@ final class PbsGenericTemplateValidator {
case PbsAst.UnaryExpr u -> { var value = infer(u.expression(), null); if (containsParameter(value)) capability(u.span()); yield value; }
case PbsAst.IndexExpr i -> {
var receiver = infer(i.receiver(), null);
var declaration=context.declaration(receiver);
if(declaration instanceof PbsAst.StructDecl s&&PbsSdkCollections.hasIndexer(s)) {
var method=s.methods().stream().filter(m->m.name().equals("_at")).findFirst().orElse(null);
if(method!=null&&PbsSdkCollections.isMap(s))infer(i.index(),PbsTypeSubstitution.substitute(method.parameters().getFirst().typeRef(),bindings(s,receiver)));
if(method!=null)yield PbsTypeSubstitution.substitute(method.returnType(),bindings(s,receiver));
}
yield receiver != null && receiver.kind() == PbsAst.TypeRefKind.ARRAY ? PbsAst.TypeRef.optional(receiver.inner(), i.span()) : null;
}
case PbsAst.GroupExpr g -> infer(g.expression(), expected);
@@ -15,6 +15,32 @@ public final class PbsSdkCollections {
public static boolean isIndexedCollection(PbsAst.TopDecl declaration) {
return isList(declaration) || isLinked(declaration);
}
public static boolean isSet(PbsAst.TopDecl declaration) {
return declaration instanceof PbsAst.StructDecl s && attribute(s.attributes(), "SdkSet") != null;
}
public static boolean isMap(PbsAst.TopDecl declaration) {
return declaration instanceof PbsAst.StructDecl s && attribute(s.attributes(), "SdkMap") != null;
}
public static boolean hasIndexer(PbsAst.TopDecl declaration) {
return isIndexedCollection(declaration) || isMap(declaration);
}
public static boolean isCollection(PbsAst.TopDecl declaration) {
return hasIndexer(declaration)||isSet(declaration);
}
public static boolean hasDefaultIdentity(PbsAst.TypeRef type,PbsContractTypes types) {
while(type!=null&&type.kind()==PbsAst.TypeRefKind.GROUP)type=type.inner();
if(type==null)return false;
if(type.kind()==PbsAst.TypeRefKind.ARRAY||type.kind()==PbsAst.TypeRefKind.FN)return true;
if(type.kind()!=PbsAst.TypeRefKind.SIMPLE)return false;
if(java.util.Set.of("int","long","bool","float","str").contains(type.name()))return true;
var declaration=types.declaration(type);
if(declaration instanceof PbsAst.StructDecl||declaration instanceof PbsAst.ServiceDecl||declaration instanceof PbsAst.ContractDecl)return true;
if(declaration instanceof PbsAst.BuiltinTypeDecl builtin) {
var binding=builtin.attributes().stream().filter(a->a.name().equals("BuiltinType")).findFirst().orElse(null);
return binding!=null&&argument(binding,"name").equals("Random");
}
return false;
}
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);
}
@@ -61,7 +61,11 @@ final class PbsExecutableBodyLowerer {
var binding = functionDecl.attributes().stream().filter(a -> "IntrinsicCall".equals(a.name())).findFirst();
final boolean terminated;
var storageBinding = p.studio.compiler.pbs.PbsSdkCollections.attribute(functionDecl.attributes(), "StoragePrimitive");
if (storageBinding != null) {
var identityBinding = p.studio.compiler.pbs.PbsSdkCollections.attribute(functionDecl.attributes(), "IdentityPrimitive");
if (identityBinding != null) {
lowerIdentityPrimitive(functionDecl,identityBinding,context);
terminated = true;
} else if (storageBinding != null) {
lowerStoragePrimitive(functionDecl,storageBinding,context);
terminated = true;
} else if (binding.isPresent()) {
@@ -568,7 +572,7 @@ final class PbsExecutableBodyLowerer {
var receiverType = PbsRuntimeTypes.unwrap(expressionType(member.receiver(), context));
if (receiverType != null && receiverType.kind() == PbsAst.TypeRefKind.OPTIONAL
&& (member.memberName().equals("hasSome") || member.memberName().equals("hasNone"))) {
lowerExpression(member.receiver(), context);
lowerExpected(member.receiver(), receiverType, context);
int slot = save(1, callExpr.span(), context);
optionalPresence(slot, receiverType, callExpr.span(), context);
if (member.memberName().equals("hasNone")) {
@@ -1166,7 +1170,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.isIndexedCollection(declaration)
return p.studio.compiler.pbs.PbsSdkCollections.hasIndexer(declaration)
? declaration.methods().stream().filter(m->m.name().equals("_at")).findFirst().orElseThrow().returnType()
: PbsAst.TypeRef.optional(array.inner(),index.span());
}
@@ -1234,6 +1238,11 @@ final class PbsExecutableBodyLowerer {
var struct = c.types().structs.get(owner.name());
var service = c.types().services.get(owner.name());
var methods = struct != null ? struct.methods() : service != null ? service.methods() : ReadOnlyList.<PbsAst.FunctionDecl>empty();
var applicable=methods.stream().filter(candidate -> candidate.name().equals(member.memberName())
&& parametersMatch(candidate.parameters(),call.arguments(),c)).toList();
// A chained receiver has its own result type; the outer call's expected
// bool (for example hasSome) cannot erase a uniquely resolved optional.
if(applicable.size()==1)return PbsTypeSubstitution.replaceSelf(applicable.getFirst().returnType(),owner.name());
for (var candidate : methods) if (candidate.name().equals(member.memberName()) && parametersMatch(candidate.parameters(),call.arguments(),c)
&& returnMatches(candidate.returnType(),owner.name(),c)) return PbsTypeSubstitution.replaceSelf(candidate.returnType(),owner.name());
var method=c.types().signatures.get(owner.name()+"."+member.memberName()+"#"+call.arguments().size());
@@ -1613,6 +1622,61 @@ final class PbsExecutableBodyLowerer {
span);
}
private void lowerIdentityPrimitive(PbsAst.FunctionDecl function, PbsAst.Attribute binding, PbsExecutableLoweringContext c) {
var operation=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"name");
boolean equality=operation.equals("referenceEquals")||operation.equals("defaultEquals");
boolean fallback=operation.equals("defaultHash")||operation.equals("defaultEquals");
if(!fallback&&!operation.equals("referenceHash")&&!operation.equals("referenceEquals"))
throw new IllegalArgumentException("Unknown identity binding: "+operation);
var type=PbsRuntimeTypes.unwrap(function.parameters().getFirst().typeRef());
var span=function.span();int width=c.types().slots(type);
boolean reference=c.types().identitySource(type);
var scalar=c.types().canonical(type);
if(!reference&&(!fallback||!java.util.Set.of("int","long","bool","float","str").contains(scalar))) {
// Explicit strategy Sets instantiate this unreachable default branch too.
// Public reference queries always reject ineligible source applications.
if(!fallback) p.studio.compiler.source.diagnostics.Diagnostics.error(c.diagnostics(),
"E_SEM_IDENTITY_REFERENCE_TYPE","Reference identity requires Struct, Array, Fn, contract or Random",
function.applicationOrigin()==null?span:function.applicationOrigin().requestedAt());
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.TRAP,"",null,null,span));
return;
}
emitLoadLocalSlots(0,width*(equality?2:1),span,c);
if(reference) {
var source=c.types().registryType(type);
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.REGISTRY,
null,"",null,null,null,"","",width*(equality?2:1),1,span,
new IRRegistryInstruction(equality?REF_IDENTITY_EQ:REF_IDENTITY_HASH,source,equality?source:null,null,0)));
} else if(equality) {
emitBinaryOperatorInstruction("==",span,c);
} else {
String intrinsic;
switch(scalar) {
case "int" -> {identityIntrinsic("math.i32.to_i64",span,c);intrinsic="hash.i64";}
case "long" -> intrinsic="hash.i64";
case "float" -> intrinsic="hash.float";
case "str" -> intrinsic="hash.string";
case "bool" -> {
var zero=c.nextLabel("bool_hash_zero");var done=c.nextLabel("bool_hash_done");
emitJump(IRBackendExecutableFunction.InstructionKind.JMP_IF_FALSE,zero,span,c);
emitIdentityLong(1,span,c);emitJump(IRBackendExecutableFunction.InstructionKind.JMP,done,span,c);
emitLabel(zero,span,c);emitIdentityLong(0,span,c);emitLabel(done,span,c);intrinsic="hash.i64";
}
default -> throw new AssertionError(scalar);
}
identityIntrinsic(intrinsic,span,c);
}
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.RET,"",null,null,span));
}
private void identityIntrinsic(String name,Span span,PbsExecutableLoweringContext c) {
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.CALL_INTRINSIC,
"",null,new IRBackendExecutableFunction.IntrinsicCallMetadata(name,1,c.intrinsicIdTable().register(name,1)),1,1,span));
}
private void emitIdentityLong(long value,Span span,PbsExecutableLoweringContext c) {
c.instructions().add(new IRBackendExecutableFunction.Instruction(IRBackendExecutableFunction.InstructionKind.PUSH_I64,
null,"",null,null,null,Long.toString(value),"",null,null,span));
}
private void lowerStoragePrimitive(PbsAst.FunctionDecl function, PbsAst.Attribute binding, PbsExecutableLoweringContext c) {
var operation=p.studio.compiler.pbs.PbsSdkCollections.argument(binding,"name");
var span=function.span();
@@ -1633,12 +1697,14 @@ final class PbsExecutableBodyLowerer {
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.isIndexedCollection(declaration))return false;
if(!p.studio.compiler.pbs.PbsSdkCollections.hasIndexer(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);
lowerArrayIndex(index.index(),c);
int pops=2;
var key=method.parameters().getFirst().typeRef();
if(p.studio.compiler.pbs.PbsSdkCollections.isMap(declaration))lowerExpected(index.index(),key,c);
else lowerArrayIndex(index.index(),c);
int pops=1+c.types().slots(key);
if(value!=null) {
var element=method.parameters().get(1).typeRef();
lowerExpected(value,element,c);pops+=c.types().slots(element);
@@ -1929,13 +1995,19 @@ final class PbsExecutableBodyLowerer {
&& allocation.typeName().equals("array") && allocation.explicitType() == null) continue;
if (listIndex && i==0 && "int".equals(c.types().canonical(actual)) && "long".equals(c.types().canonical(expected))) continue;
if (c.types().shapeKey(actual).equals(c.types().shapeKey(expected))) continue;
if (arguments.get(i) instanceof PbsAst.IntLiteralExpr && "long".equals(c.types().canonical(expected))) continue;
if (integerLiteral(arguments.get(i)) && "long".equals(c.types().canonical(expected))) continue;
if (c.types().isContract(expected) && c.types().contractTypes.implementers(expected).contains(actual.name())) continue;
return false;
}
return true;
}
private static boolean integerLiteral(PbsAst.Expression expression) {
if(expression instanceof PbsAst.GroupExpr group)return integerLiteral(group.expression());
if(expression instanceof PbsAst.UnaryExpr unary&&"-".equals(unary.operator()))return integerLiteral(unary.expression());
return expression instanceof PbsAst.IntLiteralExpr;
}
private void emitStructuredFunction(String name, java.util.List<PbsAst.TypeRef> parameters, PbsAst.TypeRef returnType,
int pops, int pushes, Span span, PbsExecutableLoweringContext c) {
var callableId = c.exactLocalCallable(name, parameters, returnType);
@@ -132,6 +132,14 @@ final class PbsRuntimeTypes {
return "pbs:" + category + ":file" + span.getFileId().getId() + ":" + span.getStart();
}
IRRegistry.TypeRef dynamic() { return builtin(0x15); }
boolean identitySource(PbsAst.TypeRef type) {
var t=unwrap(type);
if(t==null)return false;
return t.kind()==PbsAst.TypeRefKind.ARRAY||t.kind()==PbsAst.TypeRefKind.FN
||contractTypes.declaration(t) instanceof PbsAst.StructDecl
||contractTypes.declaration(t) instanceof PbsAst.ServiceDecl
||isContract(t)||canonical(t).equals("Random");
}
IRRegistry.TypeRef arrayStorage() {
var element = dynamic();
var ref = new IRRegistry.TypeRef("pbs:array-storage");
@@ -685,7 +685,7 @@ final class PbsDeclarationParser {
final String targetSurface) {
for (final var attribute : attributes) {
if ("Doc".equals(attribute.name()) || attribute.trustedSdkOrigin() &&
java.util.Set.of("SdkList", "SdkLinked").contains(attribute.name()) && "struct declarations".equals(targetSurface)) {
java.util.Set.of("SdkList", "SdkLinked", "SdkSet", "SdkMap").contains(attribute.name()) && "struct declarations".equals(targetSurface)) {
continue;
}
p.studio.compiler.source.diagnostics.Diagnostics.error(context.diagnostics(),
@@ -724,7 +724,7 @@ final class PbsDeclarationParser {
for (final var attribute : attributes) {
final PbsAst.LifecycleMarker nextMarker;
if ("Doc".equals(attribute.name()) || attribute.trustedSdkOrigin() &&
java.util.Set.of("StoragePrimitive","SdkPrivate").contains(attribute.name())) {
java.util.Set.of("StoragePrimitive","IdentityPrimitive","SdkPrivate").contains(attribute.name())) {
continue;
}
if (!isOrdinaryMode()) {
@@ -41,7 +41,7 @@ public final class PbsEditorialSupportService {
final Integer previousTokenIndex = context.previousSignificantTokenIndexAt(offset);
if (previousTokenIndex != null && context.token(previousTokenIndex).kind() == PbsTokenKind.DOT) {
final TypeView receiverType = context.resolveReceiverTypeBeforeDot(previousTokenIndex, scopeState);
return context.memberCompletions(receiverType);
return context.memberCompletions(receiverType, context.receiverExpressionBefore(previousTokenIndex) instanceof PbsAst.ThisExpr);
}
return context.generalCompletions(scopeState);
}
@@ -72,6 +72,7 @@ public final class PbsEditorialSupportService {
final Integer previousTokenIndex = context.previousSignificantTokenIndex(tokenIndex);
if (previousTokenIndex != null && context.token(previousTokenIndex).kind() == PbsTokenKind.DOT) {
final TypeView receiverType = context.resolveReceiverTypeBeforeDot(previousTokenIndex, scopeState);
if(!context.visibleMember(receiverType,token.lexeme(),context.receiverExpressionBefore(previousTokenIndex) instanceof PbsAst.ThisExpr))return Optional.empty();
return context.resolveMemberHover(receiverType, token.lexeme());
}
return context.resolveSymbolHover(token.lexeme(), tokenIndex, scopeState);
@@ -753,13 +754,29 @@ public final class PbsEditorialSupportService {
}
private List<PbsEditorialCompletionCandidate> memberCompletions(final TypeView receiverType) {
return memberCompletions(receiverType, false);
}
private boolean visibleMember(TypeView receiverType,String name,boolean ownFields) {
if(receiverType==null||receiverType.kind()==Kind.UNKNOWN)return false;
if(receiverType.kind()!=Kind.STRUCT)return true;
if(model.isSdkPrivateMember(receiverType,name))return false;
var symbolic=symbolicStruct(receiverType);
if(symbolic!=null) {
var field=symbolic.fields().stream().filter(f->f.name().equals(name)).findFirst();
if(field.isPresent())return ownFields||field.get().isPublic();
}
var owner=model.structs.get(receiverType.name());
var field=owner==null?null:owner.fields().get(name);
return field==null||ownFields||field.isPublic();
}
private List<PbsEditorialCompletionCandidate> memberCompletions(final TypeView receiverType, boolean ownFields) {
if (receiverType == null) {
return List.of();
}
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.isIndexedCollection(symbolic)||field.isPublic()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
for (var field : symbolic.fields()) if(ownFields||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 +795,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.isSdkIndexedCollection(receiverType)&&!field.getValue().isPublic())continue;
if(!ownFields&&!field.getValue().isPublic())continue;
completions.put(field.getKey(), new PbsEditorialCompletionCandidate(
field.getKey(),
PbsEditorialSymbolKind.FIELD,
@@ -959,6 +976,7 @@ public final class PbsEditorialSupportService {
final Integer previousTokenIndex = previousSignificantTokenIndex(tokenIndex);
if (previousTokenIndex != null && token(previousTokenIndex).kind() == PbsTokenKind.DOT) {
final TypeView receiverType = resolveReceiverTypeBeforeDot(previousTokenIndex, scopeState);
if(!visibleMember(receiverType,token.lexeme(),receiverExpressionBefore(previousTokenIndex) instanceof PbsAst.ThisExpr))return List.of();
return resolveMemberDefinition(receiverType, token.lexeme());
}
return resolveSymbolDefinition(token.lexeme(), tokenIndex, scopeState, offset);
@@ -1305,6 +1323,7 @@ public final class PbsEditorialSupportService {
private List<PbsEditorialSignature> resolveMemberSignatures(
final TypeView receiverType,
final String memberName) {
if(!visibleMember(receiverType,memberName,false))return List.of();
return resolveMemberHover(receiverType, memberName)
.map(PbsEditorialResolvedSymbol::signatures)
.orElse(List.of());
@@ -1749,6 +1768,13 @@ public final class PbsEditorialSupportService {
return new CallSite(CallSiteKind.MEMBER, calleeToken.lexeme(), activeParameter, maybeDotIndex);
}
if (maybeDotIndex != null && token(maybeDotIndex).kind() == PbsTokenKind.NEW) {
// Contextual construction records the application on the whole
// NewExpr span rather than on an explicit angular type suffix.
for(var resolution:genericResolutions.entrySet())
if(resolution.getKey().getStart()==token(maybeDotIndex).start()
&& resolution.getKey().getEnd()>=calleeToken.end()
&& concreteSymbols.get(resolution.getValue()) instanceof PbsAst.StructDecl)
return new CallSite(CallSiteKind.CONSTRUCTOR,resolution.getValue(),activeParameter,-1);
return new CallSite(CallSiteKind.CONSTRUCTOR, calleeToken.lexeme(), activeParameter, -1);
}
return new CallSite(CallSiteKind.FUNCTION, calleeToken.lexeme(), activeParameter, -1);
@@ -74,6 +74,12 @@ final class PbsFlowExpressionAnalyzer {
final PbsAst.Expression expression,
final PbsFlowExpressionContext context) {
var result = analyzeExpressionUnchecked(expression, context);
if (context.use() == ExprUse.VALUE && context.expectedType() != null
&& (context.model().isGenericStruct(result.type()) || context.model().isGenericStruct(context.expectedType()))
&& !typeOps.compatible(result.type(), context.expectedType())) {
p.studio.compiler.source.diagnostics.Diagnostics.error(context.diagnostics(), "E_SEM_GENERIC_TYPE_MISMATCH",
"Generic struct applications require the exact declared type arguments", expression.span());
}
if (context.use() == ExprUse.VALUE && context.expectedType() != null
&& (context.model().isOpaqueBuiltin(result.type()) || context.model().isOpaqueBuiltin(context.expectedType()))
&& !typeOps.compatible(result.type(), context.expectedType())) {
@@ -202,6 +202,14 @@ final class PbsFlowSemanticSupport {
return type != null && p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(contractTypes.declaration(
type.sourceType() == null ? PbsAst.TypeRef.simple(type.name(), Span.none()) : type.sourceType()));
}
boolean isSdkMap(TypeView type) {
return type != null && p.studio.compiler.pbs.PbsSdkCollections.isMap(contractTypes.declaration(
type.sourceType() == null ? PbsAst.TypeRef.simple(type.name(), Span.none()) : type.sourceType()));
}
boolean isSdkCollection(TypeView type) {
return type != null && p.studio.compiler.pbs.PbsSdkCollections.isCollection(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
@@ -271,6 +279,12 @@ final class PbsFlowSemanticSupport {
type.functionInputs(), type.functionOutput(), contractTypes.key(reference), reference, type.implementers());
}
boolean isGenericStruct(TypeView type) {
return type.kind() == Kind.STRUCT && type.sourceType() != null
&& p.studio.compiler.pbs.PbsContractTypes.applicationOrigin(
contractTypes.declaration(type.sourceType())) != null;
}
TypeView contractView(PbsAst.TypeRef reference) {
var instance = contractTypes.resolve(reference);
if (instance == null) return TypeView.unknown();
@@ -52,6 +52,13 @@ final class PbsFlowStructuralExpressionAnalyzer {
}
if (expression instanceof PbsAst.IndexExpr index) {
final var receiver = analyzeValueExpression(index.receiver(), context, null).type();
if (model.isSdkMap(receiver)) {
var read=model.structs.get(receiver.name()).methods().get("_at").getFirst();
var key=analyzeValueExpression(index.index(),context,read.inputTypes().getFirst()).type();
if(!typeOps.compatible(key,read.inputTypes().getFirst())) Diagnostics.error(diagnostics,
"E_SEM_MAP_KEY_TYPE", "Map key does not match its declared key type", index.index().span());
return ExprResult.type(read.outputType());
}
checkArrayIndex(index.index(), context);
if (model.isSdkIndexedCollection(receiver)) {
return ExprResult.type(model.structs.get(receiver.name()).methods().get("_at").getFirst().outputType());
@@ -307,3 +307,386 @@ declare struct linked<T>(head: optional _LinkedNode<T>, tail: optional _LinkedNo
return new _SnapshotCursor<T>(captured) as Cursor<T>;
}
}
// Equality policy is independent of private Set storage and reusable by Map.
declare contract Identity<T> {
fn hash(value: T) -> long;
fn equals(left: T, right: T) -> bool;
}
[IdentityPrimitive(name = "defaultHash")]
fn _collectionHash<T>(value: T) -> long { return 0; }
[IdentityPrimitive(name = "defaultEquals")]
fn _collectionEquals<T>(left: T, right: T) -> bool { return false; }
fn _hashBucket(hash: long, capacity: long) -> long {
let bucket: long = hash % capacity;
if bucket < (0 as long) { bucket = bucket + capacity; }
return bucket;
}
fn _hashNextSize(length: long) -> long {
let maximum: long = 9223372036854775807;
_bounds(length, maximum);
return length + 1;
}
fn _hashCapacity(capacity: long, required: long) -> long {
let maximum: long = 4294967295;
if capacity == (0 as long) { return 8; }
if capacity < maximum && required > capacity * (3 as long) / (4 as long) {
let candidate: long = capacity * (2 as long);
if candidate > maximum { return maximum; }
return candidate;
}
return capacity;
}
declare struct _SetNode<T>(pub hash: long, pub value: T, pub mut next: optional _SetNode<T>) {
ctor make(hash: long, value: T, next: optional _SetNode<T>) {
this.hash = hash;
this.value = value;
this.next = next;
}
}
[SdkSet]
declare struct set<T>(buckets: optional array<_SetNode<T>>, length: long, strategy: optional Identity<T>, busy: bool) {
ctor make() {
this.buckets = none;
this.length = 0;
this.strategy = none;
this.busy = false;
}
ctor make(identity: Identity<T>) {
this.buckets = none;
this.length = 0;
this.strategy = some(identity);
this.busy = false;
}
fn size() -> long { return this.length; }
fn isEmpty() -> bool { return this.length == (0 as long); }
[SdkPrivate]
fn _check() { if this.busy { _fail(); } }
[SdkPrivate]
fn _begin() { this._check(); this.busy = true; }
[SdkPrivate]
fn _hash(value: T) -> long {
if let identity = this.strategy { return identity.hash(value); }
return _collectionHash<T>(value);
}
[SdkPrivate]
fn _equals(left: T, right: T) -> bool {
if let identity = this.strategy { return identity.equals(left, right); }
return _collectionEquals<T>(left, right);
}
[SdkPrivate]
fn _find(hash: long, value: T) -> bool {
if let buckets = this.buckets {
let current: optional _SetNode<T> = buckets[_hashBucket(hash, buckets.length)];
while current.hasSome() {
if let node = current {
if node.hash == hash && this._equals(node.value, value) { return true; }
current = node.next;
}
}
}
return false;
}
[SdkPrivate]
fn _rehash(capacity: long) -> array<_SetNode<T>> {
let target: array<_SetNode<T>> = new array<>(capacity);
if let buckets = this.buckets {
let index: long = 0;
while index < buckets.length {
let current: optional _SetNode<T> = buckets[index];
while current.hasSome() {
if let node = current {
let bucket: long = _hashBucket(node.hash, capacity);
let copied = new _SetNode<T>(node.hash, node.value, target[bucket]);
target[bucket] = copied;
current = node.next;
}
}
index = index + 1;
}
}
return target;
}
fn contains(value: T) -> bool {
this._begin();
let hash: long = this._hash(value);
let found: bool = this._find(hash, value);
this.busy = false;
return found;
}
fn add(value: T) -> bool {
this._begin();
let hash: long = this._hash(value);
if this._find(hash, value) { this.busy = false; return false; }
let required: long = _hashNextSize(this.length);
let capacity: long = 0;
if let buckets = this.buckets { capacity = buckets.length; }
let nextCapacity: long = _hashCapacity(capacity, required);
if nextCapacity != capacity {
let target = this._rehash(nextCapacity);
let bucket: long = _hashBucket(hash, nextCapacity);
let inserted = new _SetNode<T>(hash, value, target[bucket]);
target[bucket] = inserted;
let published: optional array<_SetNode<T>> = some(target);
this.buckets = published;
} else {
if let buckets = this.buckets {
let bucket: long = _hashBucket(hash, capacity);
let inserted = new _SetNode<T>(hash, value, buckets[bucket]);
buckets[bucket] = inserted;
}
}
this.length = required;
this.busy = false;
return true;
}
fn remove(value: T) -> bool {
this._begin();
let hash: long = this._hash(value);
if let buckets = this.buckets {
let bucket: long = _hashBucket(hash, buckets.length);
let previous: optional _SetNode<T> = none;
let current: optional _SetNode<T> = buckets[bucket];
while current.hasSome() {
if let node = current {
if node.hash == hash && this._equals(node.value, value) {
let next: optional _SetNode<T> = node.next;
let length: long = this.length - 1;
if let predecessor = previous { predecessor.next = next; }
else {
if let successor = next { buckets[bucket] = successor; }
else { buckets.clear(bucket); }
}
this.length = length;
this.busy = false;
return true;
}
previous = current;
current = node.next;
}
}
}
this.busy = false;
return false;
}
fn clear() {
this._check();
this.buckets = none;
this.length = 0;
}
fn cursor() -> Cursor<T> {
let head: optional _SnapshotNode<T> = none;
if let buckets = this.buckets {
let index: long = 0;
while index < buckets.length {
let current: optional _SetNode<T> = buckets[index];
while current.hasSome() {
if let node = current {
head = some(new _SnapshotNode<T>(node.value, head));
current = node.next;
}
}
index = index + 1;
}
}
return new _SnapshotCursor<T>(head) as Cursor<T>;
}
}
// An association is independent of mutable Map nodes. Only payload aliases are shared.
declare struct keyval<K, V>(storedKey: K, storedValue: V) {
ctor make(key: K, value: V) { this.storedKey = key; this.storedValue = value; }
fn key() -> K { return this.storedKey; }
fn value() -> V { return this.storedValue; }
}
declare struct _MapNode<K, V>(pub hash: long, pub key: K, pub mut value: V, pub mut next: optional _MapNode<K, V>) {
ctor make(hash: long, key: K, value: V, next: optional _MapNode<K, V>) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
}
[SdkMap]
declare struct map<K, V>(buckets: optional array<_MapNode<K, V>>, length: long, strategy: optional Identity<K>, busy: bool) {
ctor make() {
this.buckets = none;
this.length = 0;
this.strategy = none;
this.busy = false;
}
ctor make(identity: Identity<K>) {
this.buckets = none;
this.length = 0;
this.strategy = some(identity);
this.busy = false;
}
fn size() -> long { return this.length; }
fn isEmpty() -> bool { return this.length == (0 as long); }
[SdkPrivate]
fn _check() { if this.busy { _fail(); } }
[SdkPrivate]
fn _begin() { this._check(); this.busy = true; }
[SdkPrivate]
fn _hash(key: K) -> long {
if let identity = this.strategy { return identity.hash(key); }
return _collectionHash<K>(key);
}
[SdkPrivate]
fn _equals(left: K, right: K) -> bool {
if let identity = this.strategy { return identity.equals(left, right); }
return _collectionEquals<K>(left, right);
}
[SdkPrivate]
fn _find(hash: long, key: K) -> optional _MapNode<K, V> {
if let buckets = this.buckets {
let current: optional _MapNode<K, V> = buckets[_hashBucket(hash, buckets.length)];
while current.hasSome() {
if let node = current {
if node.hash == hash && this._equals(node.key, key) { return some(node); }
current = node.next;
}
}
}
return none;
}
[SdkPrivate]
fn _at(key: K) -> optional V {
this._begin();
let hash: long = this._hash(key);
let retrieved: optional V = none;
if let node = this._find(hash, key) { retrieved = some(node.value); }
this.busy = false;
return retrieved;
}
[SdkPrivate]
fn _put(key: K, value: V) { let discarded: optional V = this._associate(key, value, false); }
fn containsKey(key: K) -> bool {
this._begin();
let hash: long = this._hash(key);
let found: bool = this._find(hash, key).hasSome();
this.busy = false;
return found;
}
[SdkPrivate]
fn _rehash(capacity: long) -> array<_MapNode<K, V>> {
let target: array<_MapNode<K, V>> = new array<>(capacity);
if let buckets = this.buckets {
let index: long = 0;
while index < buckets.length {
let current: optional _MapNode<K, V> = buckets[index];
while current.hasSome() {
if let node = current {
let bucket: long = _hashBucket(node.hash, capacity);
let copied = new _MapNode<K, V>(node.hash, node.key, node.value, target[bucket]);
target[bucket] = copied;
current = node.next;
}
}
index = index + 1;
}
}
return target;
}
[SdkPrivate]
fn _associate(key: K, value: V, returnPrevious: bool) -> optional V {
this._begin();
let hash: long = this._hash(key);
if let node = this._find(hash, key) {
let previous: optional V = none;
if returnPrevious { previous = some(node.value); }
node.value = value;
this.busy = false;
return previous;
}
// Typed absence can materialize a carrier; prepare it before publishing nodes.
let absent: optional V = none;
let required: long = _hashNextSize(this.length);
let capacity: long = 0;
if let buckets = this.buckets { capacity = buckets.length; }
let nextCapacity: long = _hashCapacity(capacity, required);
if nextCapacity != capacity {
let target = this._rehash(nextCapacity);
let bucket: long = _hashBucket(hash, nextCapacity);
let inserted = new _MapNode<K, V>(hash, key, value, target[bucket]);
target[bucket] = inserted;
let published: optional array<_MapNode<K, V>> = some(target);
this.buckets = published;
} else {
if let buckets = this.buckets {
let bucket: long = _hashBucket(hash, capacity);
let inserted = new _MapNode<K, V>(hash, key, value, buckets[bucket]);
buckets[bucket] = inserted;
}
}
this.length = required;
this.busy = false;
return absent;
}
fn put(entry: keyval<K, V>) -> optional V {
return this._associate(entry.key(), entry.value(), true);
}
fn remove(key: K) -> optional V {
this._begin();
let hash: long = this._hash(key);
if let buckets = this.buckets {
let bucket: long = _hashBucket(hash, buckets.length);
let previous: optional _MapNode<K, V> = none;
let current: optional _MapNode<K, V> = buckets[bucket];
while current.hasSome() {
if let node = current {
if node.hash == hash && this._equals(node.key, key) {
let retrieved: optional V = some(node.value);
let next: optional _MapNode<K, V> = node.next;
let length: long = this.length - 1;
if let predecessor = previous { predecessor.next = next; }
else {
if let successor = next { buckets[bucket] = successor; }
else { buckets.clear(bucket); }
}
this.length = length;
this.busy = false;
return retrieved;
}
previous = current;
current = node.next;
}
}
}
this.busy = false;
return none;
}
fn clear() {
this._check();
this.buckets = none;
this.length = 0;
}
fn cursor() -> Cursor<keyval<K, V>> {
let head: optional _SnapshotNode<keyval<K, V>> = none;
if let buckets = this.buckets {
let index: long = 0;
while index < buckets.length {
let current: optional _MapNode<K, V> = buckets[index];
while current.hasSome() {
if let node = current {
let captured = new keyval<K, V>(node.key, node.value);
head = some(new _SnapshotNode<keyval<K, V>>(captured, head));
current = node.next;
}
}
index = index + 1;
}
}
return new _SnapshotCursor<keyval<K, V>>(head) as Cursor<keyval<K, V>>;
}
}
@@ -1,3 +1,7 @@
pub struct list<T>;
pub contract Cursor<T>;
pub struct linked<T>;
pub struct set<T>;
pub contract Identity<T>;
pub struct map<K, V>;
pub struct keyval<K, V>;
@@ -0,0 +1,26 @@
[Doc(markdown = """
Runtime Hash-v1 operations. Results preserve all signed long digest bits.
Reference queries validate complete values and observe instance identity.
""")]
declare service hash {
[IntrinsicCall(name = "hash.i64", version = 1)]
fn i64(value: long) -> long;
[IntrinsicCall(name = "hash.float", version = 1)]
fn f64(value: float) -> long;
[IntrinsicCall(name = "hash.string", version = 1)]
fn text(value: str) -> long;
}
[Doc(markdown = """
Hashes a Struct, Array, Fn, contract receiver or Random by instance.
The hash stays stable while the object lives and does not retain that object.
""")]
[IdentityPrimitive(name = "referenceHash")]
fn referenceHash<T>(value: T) -> long { return 0; }
[Doc(markdown = """
Compares fully validated references of the same logical type by instance.
Contract views compare receivers, after validating the complete view groups.
""")]
[IdentityPrimitive(name = "referenceEquals")]
fn referenceEquals<T>(left: T, right: T) -> bool { return false; }
@@ -0,0 +1,3 @@
pub service hash;
pub fn referenceHash<T>(value: T) -> long;
pub fn referenceEquals<T>(left: T, right: T) -> bool;
@@ -0,0 +1,19 @@
import {hash, referenceHash, referenceEquals} from @sdk:hash;
declare struct HashExampleObject(pub mut value: int);
fn hashingExample() -> int {
// Hash-v1 preserves signed digest bits; negative hashes are ordinary results.
let expected: long = -8804195676797548855;
if hash.i64(-1) != expected { return 0; }
if hash.f64(0.0) != hash.f64(-0.0) { return 0; }
if hash.text("hel" + "lo") != hash.text("hello") { return 0; }
let first = new HashExampleObject(20);
let other = new HashExampleObject(20);
let saved: long = referenceHash<HashExampleObject>(first);
first.value = 42;
if saved != referenceHash<HashExampleObject>(first) { return 0; }
if referenceEquals<HashExampleObject>(first, first) == false { return 0; }
if referenceEquals<HashExampleObject>(first, other) { return 0; }
return first.value;
}
@@ -0,0 +1,27 @@
import {map, keyval} from @sdk:collections;
fn mapsExample() -> int {
let scores = new map<str, optional int>();
scores["left"] = some(20);
let previous = scores.put(new keyval<str, optional int>("right", some(22)));
if previous.hasSome() { return 0; }
scores["empty"] = none;
if scores.containsKey("empty") == false { return 0; }
if let empty = scores["empty"] {
if empty.hasSome() { return 0; }
} else { return 0; }
if scores["missing"].hasSome() { return 0; }
let captured = scores.cursor();
scores["left"] = some(0);
scores.clear();
let total: int = 0;
while true {
if let entry = captured.current() {
if let value = entry.value() { total = total + value; }
} else { break; }
if captured.hasNext() { captured.next(); }
else { break; }
}
return total;
}
@@ -4,3 +4,6 @@ pub fn contractExample() -> int;
pub fn genericsExample() -> int;
pub fn collectionsExample() -> int;
pub fn linkedExample() -> int;
pub fn setExample() -> int;
pub fn hashingExample() -> int;
pub fn mapsExample() -> int;
@@ -0,0 +1,25 @@
import {set, Cursor} from @sdk:collections;
fn sumSetSnapshot(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 setExample() -> int {
let values: set<int> = new set();
values.add(20);
values.add(22);
if values.add(20) { return 0; } // Duplicate inclusion preserves the first value.
let captured: Cursor<int> = values.cursor();
values.remove(20);
values.clear();
if values.isEmpty() == false { return 0; }
// The capture owns both values; their traversal order is unspecified.
return sumSetSnapshot(captured);
}
@@ -124,6 +124,29 @@ class PbsGateUSdkInterfaceConformanceTest {
assertTrue(copied.hasErrors());
}
@Test
void mapExportsUseOriginCheckedKeyTypingAndPrivateAssociations() throws IOException {
var positive=compileWorkspaceModule(tempDir.resolve("map-positive"),"""
import {map as Maps,keyval as Entry} from @sdk:collections;
[Frame] fn frame()->void{
let m:Maps<str,optional int>=new Maps();m["a"]=none;
let previous=m.put(new Entry<str,optional int>("a",some(42)));
if let value=m["a"]{if (value else 0)!=42{let failure=1/0;}}else{let failure=1/0;}
}
""","pub fn frame()->void;",1,null);
assertFalse(positive.diagnostics().hasErrors(),positive.diagnostics().toString());
for(var entry:java.util.Map.of(
"optional","let m=new map<optional int,int>();",
"key","let m=new map<str,int>();m[0]=42;",
"private","let m=new map<int,int>();let exposed=m.buckets;",
"association","let e=new keyval<int,int>(20,22);e.storedKey=0;",
"capacity","let m=new map<int,int>(8);").entrySet()){
var negative=compileWorkspaceModule(tempDir.resolve("map-negative-"+entry.getKey()),
"import {map,keyval} from @sdk:collections;[Frame]fn frame()->void{"+entry.getValue()+"}","pub fn frame()->void;",1,null);
assertTrue(negative.diagnostics().hasErrors(),entry.getKey());
}
}
@Test
void removedInputObjectsExplainTheirMigration() throws IOException {
final var result = compileWorkspaceModule(tempDir.resolve("removed-input"), """
@@ -6,6 +6,36 @@ import p.studio.compiler.source.identifiers.FileId;
import static org.junit.jupiter.api.Assertions.*;
class PbsGenericStructsTest {
@Test void nominalApplicationsStayInvariantInExpectedValueContexts() {
rejects(BOX+"fn main(){let value:Box<long>=new Box<int>(42);}", "E_SEM_GENERIC_TYPE_MISMATCH");
rejects(BOX+"fn main(){let value:Box<long>=new Box(42);value=new Box<int>(42);}", "E_SEM_GENERIC_TYPE_MISMATCH");
rejects(BOX+"fn main()->Box<long>{return new Box<int>(42);}", "E_SEM_GENERIC_TYPE_MISMATCH");
accepts(BOX+"fn main(){let value:Box<long>=new Box<long>(42);value=new Box(43);}");
}
@Test void optionalGenericStrategiesAndEveryConcreteOwnerFamilyKeepLogicalScope() {
accepts("""
declare contract Policy<T>{fn same(a:T,b:T)->bool;}
declare struct Compared<T>(policy:optional Policy<T>){
fn same(a:T,b:T)->bool{if let policy=this.policy{return policy.same(a,b);}return false;}
}
declare contract Read<T>{fn read()->T;}
declare struct Cell<T>(pub mut value:T){fn replace(value:T){this.value=value;}}
implements<T> Read<T> for Cell<T> using cell {fn read()->T{return cell.value;}}
fn main()->int{
let a=new Cell<int>(20);let b=new Cell<long>(22);let viewA:Read<int>=a;let viewB:Read<long>=b;
a.replace(20);b.replace(22);return viewA.read()+(viewB.read() as int);
}
""");
rejects("""
declare struct Broken<T>(value:optional T){
fn invalid()->optional T{if let value=this.value{return some(value+value);}return none;}
}
""","E_SEM_GENERIC_CAPABILITY");
accepts("""
declare struct set<T>(value:T){fn get()->T{return this.value;}}
fn main()->int{let userSet=new set<optional int>(some(42));return userSet.get() else 0;}
""");
}
@Test void recursiveOptionalBindingsAndContractViewsKeepTheirDefinitionShapes() {
accepts("""
declare contract Reader<T> {fn read()->optional T;}
@@ -41,7 +71,7 @@ class PbsGenericStructsTest {
}
private void accepts(String source) {
var diagnostics = compile(source);
assertFalse(diagnostics.hasErrors(), () -> diagnostics.stream().map(d -> d.getCode()+": "+d.getMessage()).toList().toString());
assertFalse(diagnostics.hasErrors(), () -> diagnostics.stream().map(d -> d.getCode()+": "+d.getMessage()+" at "+d.getSpan().getStart()+":"+d.getSpan().getEnd()).toList().toString());
}
private void rejects(String source, String code) {
var diagnostics = compile(source);
@@ -0,0 +1,79 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.models.SourceKind;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
class PbsMapKeyvalTest {
private DiagnosticSink compile(String source)throws Exception {
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 diagnostics=DiagnosticSink.empty();
new PbsFrontendCompiler().compileFile(new FileId(0),sdk+"\n"+source,diagnostics,SourceKind.SDK_INTERFACE);
return diagnostics;
}
private void accepts(String source)throws Exception {
var diagnostics=compile(source);assertFalse(diagnostics.hasErrors(),()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
}
private void rejects(String source,String code)throws Exception {
var diagnostics=compile(source);assertTrue(diagnostics.stream().anyMatch(d->d.getCode().contains(code)),()->diagnostics.stream().map(d->d.getCode()+":"+d.getMessage()).toList().toString());
}
@Test void keyAndValueApplicationsHaveIndependentRules()throws Exception {
accepts("""
fn main()->optional int {
let a:map<str,optional int>=new map<>();a["a"]=none;
let b:map<str,optional int>=new map();b["a"]=some(42);
let entry:keyval<str,optional int>=new keyval("a",none);
let prior:optional (optional int)=b.put(entry);
let detached=new keyval<optional int,optional int>(none,some(42));
return detached.value();
}
""");
rejects("fn main(){let m=new map<optional int,int>();}","MAP_OPTIONAL_KEY");
rejects("fn main(){let m=new map<(optional int),int>();}","MAP_OPTIONAL_KEY");
rejects("""
declare service Policy {}
implements Identity<optional int> for Policy using p {
fn hash(v:optional int)->long{return 0;}fn equals(a:optional int,b:optional int)->bool{return true;}
}
fn main(){let m=new map<optional int,int>(Policy);}
""","MAP_OPTIONAL_KEY");
rejects("fn main(){let m=new map();}","GENERIC_CONTEXT");
rejects("fn main(){let k=new keyval(1,2);}","GENERIC_CONTEXT");
rejects("fn main(){let m:map<int,long>=new map<int,int>();}","GENERIC_TYPE_MISMATCH");
rejects("fn main(){let m=new map<str,int>();m[0]=42;}","MAP_KEY_TYPE");
rejects("fn main(){let m=new map<str,int>();m[\"a\"]=false;}","ASSIGN_TYPE_MISMATCH");
}
@Test void strategiesAreNominalAndNeverRequiredForValues()throws Exception {
accepts("""
declare struct Box<T>(value:T){fn get()->T{return this.value;}}
fn main(){let m=new map<Box<int>,Box<int>>();m[new Box(20)]=new Box(22);}
""");
accepts("""
declare service Policy {}
implements Identity<(a:int,b:int)> for Policy using p {
fn hash(v:(a:int,b:int))->long{return 0;}
fn equals(a:(a:int,b:int),b:(a:int,b:int))->bool{return a.a==b.a&&a.b==b.b;}
}
fn main(){let m=new map<(a:int,b:int),optional int>(Policy);m[(a:20,b:22)]=none;}
""");
accepts("fn main(){let m=new map<int,(a:int,b:int)>();m[1]=(a:20,b:22);}");
rejects("fn main(){let m=new map<(a:int,b:int),int>();}","MAP_IDENTITY_REQUIRED");
rejects("fn main(){let k=new keyval<int,int>(20,22);k.storedKey=42;}","ACCESS_DENIED");
rejects("fn main(){let k=new keyval<int,int>(20,22);let hidden=k.storedValue;}","ACCESS_DENIED");
rejects("fn main(){let m=new map<int,int>();m._at(0);}","PRIVATE");
var source="declare struct map<K,V>(pub key:K,pub value:V);fn main(){let m=new map<optional int,optional int>(none,none);}";
var diagnostics=DiagnosticSink.empty();new PbsFrontendCompiler().compileFile(new FileId(0),source,diagnostics);
assertFalse(diagnostics.hasErrors(),diagnostics.toString());
}
@Test void reservedMapBindingCannotBeForgedByProjectCode() {
var diagnostics=DiagnosticSink.empty();new PbsFrontendCompiler().compileFile(new FileId(0),
"[SdkMap] declare struct map<K,V>(key:K,value:V);",diagnostics);
assertTrue(diagnostics.hasErrors());
}
}
@@ -12,7 +12,7 @@ import static org.junit.jupiter.api.Assertions.*;
class PbsListCursorEditorialTest {
@org.junit.jupiter.params.ParameterizedTest
@org.junit.jupiter.params.provider.ValueSource(strings={"list","linked"})
@org.junit.jupiter.params.provider.ValueSource(strings={"list","linked","set"})
void concreteApiHidesStorageAndKeepsSdkDefinitionOrigins(String collection) throws Exception {
var diagnostics=DiagnosticSink.empty();var sdkId=new FileId(1);
String sdk;
@@ -36,11 +36,11 @@ class PbsListCursorEditorialTest {
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(")+11).orElseThrow().signatures();
assertEquals(collection.equals("list")?2:1,constructors.size());
assertEquals(collection.equals("linked")?1:2,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","head","tail","_at","_put","_node").contains(c.label())));
assertFalse(completions.stream().anyMatch(c->java.util.Set.of("storage","length","head","tail","buckets","strategy","busy","_at","_put","_node","_hash","_find","_begin","_rehash").contains(c.label())));
var definition=service.definition(source,ast,supplemental,member);
assertEquals(sdkId,definition.getFirst().span().getFileId());
int current=source.indexOf("cursor.current")+7;
@@ -0,0 +1,60 @@
package p.studio.compiler.pbs.semantics;
import org.junit.jupiter.api.Test;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
class PbsMapKeyvalEditorialTest {
@Test void concretePublicApiAndIndexResultsRetainSdkOrigins()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 source="""
import {map as Maps,keyval as Entry,Cursor} from @sdk:collections;
fn sample(values:Maps<str,optional int>,entry:Entry<str,optional int>)->optional (optional int){
let created:Maps<str,optional int>=new Maps();
let association:Entry<str,optional int>=new Entry("a",none);
values.put(entry);entry.key();entry.value();return values["a"];
}
""";
var id=new FileId(2);var ast=PbsParser.parse(PbsLexer.lex(source,id,diagnostics),id,diagnostics);
var declarations=new java.util.ArrayList<PbsAst.TopDecl>(library.topDecls().stream().toList());
for(var binding:java.util.Map.of("map","Maps","keyval","Entry").entrySet()){
var origin=library.topDecls().stream().filter(d->d instanceof PbsAst.StructDecl s&&s.name().equals(binding.getKey())).findFirst().orElseThrow();
declarations.add(new PbsAst.TypeBindingDecl(binding.getValue(),origin,ast.span()));
}
var supplemental=ReadOnlyList.wrap(declarations);var service=new PbsEditorialSupportService();
assertFalse(diagnostics.hasErrors(),diagnostics.toString());
int put=source.indexOf("values.put")+7;
var methods=service.completion(source,ast,supplemental,put);
assertEquals(Set.of("containsKey","put","remove","clear","size","isEmpty","cursor"),methods.stream().map(c->c.label()).collect(Collectors.toSet()));
var hover=service.hover(source,ast,supplemental,put).orElseThrow();
assertTrue(hover.signatures().getFirst().label().contains("optional int"),hover.toString());
assertEquals(sdkId,service.definition(source,ast,supplemental,put).getFirst().span().getFileId());
int key=source.indexOf("entry.key")+6;
assertEquals(Set.of("key","value"),service.completion(source,ast,supplemental,key).stream().map(c->c.label()).collect(Collectors.toSet()));
assertTrue(service.hover(source,ast,supplemental,key).orElseThrow().signatures().getFirst().label().contains("str"));
int construction=source.indexOf("new Maps(")+9;
var ctors=service.signatureHelp(source,ast,supplemental,construction).orElseThrow().signatures();
assertEquals(2,ctors.size());assertTrue(ctors.stream().noneMatch(s->s.label().contains("buckets")||s.label().contains("$generic$")));
var entryCtor=service.signatureHelp(source,ast,supplemental,source.indexOf("new Entry(")+10).orElseThrow();
assertTrue(entryCtor.signatures().getFirst().label().contains("key: str"),entryCtor.toString());
assertTrue(service.rename(source,ast,supplemental,source.indexOf("values:Maps"),"scores").allowed());
assertTrue(service.references(source,ast,supplemental,source.indexOf("values:Maps")).size()>=3);
String invalid=source.replace("entry.key();","entry.storedKey;entry.key();");
var invalidAst=PbsParser.parse(PbsLexer.lex(invalid,id,diagnostics),id,diagnostics);
int hidden=invalid.indexOf("entry.storedKey")+6;
assertTrue(service.hover(invalid,invalidAst,supplemental,hidden).isEmpty());
assertTrue(service.definition(invalid,invalidAst,supplemental,hidden).isEmpty());
}
}
@@ -8,8 +8,8 @@ final class BytecodeOpcodeLayout {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x51,0x80 -> 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,0xa9 -> 6;
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0xa1,0xa2 -> 10;
0x90,0x91,0x92,0x97,0xa0,0xa3,0xa4,0xa5,0xa6,0xa9 -> 6;
case 0x14,0x15,0x52,0x58,0x93,0x94,0x95,0x96,0x98,0xa1,0xa2 -> 10;
default -> throw new BytecodeMarshalingException(BytecodeMarshalingErrorCode.MARSHAL_VERIFY_PRECHECK_UNKNOWN_OPCODE,
"unknown or retired opcode 0x%04X".formatted(opcode));
};
@@ -56,6 +56,7 @@ public class BytecodePreloadVerifierService {
case 0x58 -> method(r,a,b);
case 0x90,0x91,0x92 -> type(r,a);
case 0x93,0x94,0x95,0x96 -> {var src=type(r,a).body();var dst=type(r,b).body();check(src instanceof Struct||src instanceof Array||src instanceof Trait,"reference source category");check(op==0x94||op==0x95?dst instanceof Trait:dst instanceof Struct||dst instanceof Array,"reference target category");}
case 0x97,0x98 -> {identitySource(r,a);if(op==0x98)identitySource(r,b);}
case 0xa0 -> check(type(r,a).body() instanceof Struct,"Struct category");
case 0xa1,0xa2 -> field(r,a,b);
case 0xa3,0xa4,0xa5,0xa6,0xa9 -> array(r,a);
@@ -159,6 +160,8 @@ public class BytecodePreloadVerifierService {
case 0x91 -> {dynamic(pop(s,2),typed);produce(s,facts(r,type(r,a).identity()),i.pc());}
case 0x92 -> {dynamic(pop(s,2),typed);s.stack.add(carrier("bool"));}
case 0x93,0x94,0x95,0x96 -> {consume(s,facts(r,type(r,a).identity()),typed);if(op==0x93||op==0x94)s.stack.add(carrier("bool"));else produce(s,facts(r,type(r,b).identity()),i.pc());}
case 0x97 -> {consume(s,facts(r,type(r,a).identity()),typed);s.stack.add(carrier("i64"));}
case 0x98 -> {var expected=new ArrayList<>(facts(r,type(r,a).identity()));expected.addAll(facts(r,type(r,b).identity()));consume(s,expected,typed);s.stack.add(carrier("bool"));}
case 0xa0 -> produce(s,facts(r,type(r,a).identity()),i.pc());
case 0xa1 -> {consume(s,facts(r,type(r,a).identity()),typed);produce(s,facts(r,field(r,a,b).type()),i.pc());}
case 0xa2 -> {consume(s,facts(r,field(r,a,b).type()),typed);consume(s,facts(r,type(r,a).identity()),typed);}
@@ -266,6 +269,7 @@ public class BytecodePreloadVerifierService {
check((mask(actual.get(i+1).carrier())&64)!=0,"invalid null group pair");
}
}
private static void identitySource(LinkedRegistry r,int id){var body=type(r,id).body();check(body instanceof Struct||body instanceof Array||body instanceof Fn||body instanceof Trait||body instanceof Builtin builtin&&builtin.id()==0x16,"identity source category");}
private static TypeDeclaration type(LinkedRegistry r,int id){check(id>=0&&id<r.types().size(),"TypeId");return r.types().get(id);}
private static Fn fn(LinkedRegistry r,int id){check(type(r,id).body() instanceof Fn,"Fn category");return (Fn)type(r,id).body();}
private static Method method(LinkedRegistry r,int id,int member){check(type(r,id).body() instanceof Trait,"Trait category");var t=(Trait)type(r,id).body();check(member>=0&&member<t.methods().size(),"method index");return t.methods().get(member);}
@@ -42,6 +42,15 @@ public final class RegistryInstructionLowerer {
if(trait?!(target instanceof Trait):!(target instanceof Struct||target instanceof Array))throw new IllegalArgumentException("reference target category");
second=registry.id(instruction.targetType()).index();pops=registry.width(instruction.type());pushes=instruction.kind()==IRRegistryInstruction.Kind.REF_TO_TRAIT?2:1;
}
case REF_IDENTITY_HASH,REF_IDENTITY_EQ -> {
if(!identitySource(body))throw new IllegalArgumentException("identity source category");
pops=registry.width(instruction.type());pushes=1;
if(instruction.kind()==IRRegistryInstruction.Kind.REF_IDENTITY_EQ) {
if(!identitySource(registry.type(instruction.targetType()).body()))throw new IllegalArgumentException("identity right category");
pops=Math.addExact(pops,registry.width(instruction.targetType()));
second=registry.id(instruction.targetType()).index();
}
}
case STRUCT_NEW,STRUCT_GET,STRUCT_SET -> {
if(!(body instanceof Struct struct))throw new IllegalArgumentException("Struct category required");
if(instruction.kind()==IRRegistryInstruction.Kind.STRUCT_NEW)pushes=1;
@@ -64,4 +73,8 @@ public final class RegistryInstructionLowerer {
int bytes=second==null?4:8;
return new IRVMInstruction(new IRVMOp("REGISTRY_"+instruction.kind(),instruction.kind().opcode(),bytes,pops,pushes,false,false,false),first,second);
}
private static boolean identitySource(TypeBody body) {
return body instanceof Struct||body instanceof Array||body instanceof Fn||body instanceof Trait
||body instanceof Builtin builtin&&builtin.id()==0x16;
}
}
@@ -0,0 +1,108 @@
package p.studio.compiler.backend.registry;
import org.junit.jupiter.api.Test;
import p.studio.compiler.backend.bytecode.*;
import p.studio.compiler.backend.irvm.*;
import p.studio.compiler.models.*;
import p.studio.compiler.models.registry.*;
import p.studio.compiler.models.registry.IRRegistry.*;
import p.studio.compiler.source.identifiers.*;
import p.studio.utilities.structures.ReadOnlyList;
import java.nio.file.*;
import java.util.*;
import static p.studio.compiler.models.IRBackendExecutableFunction.InstructionKind.*;
import static p.studio.compiler.models.registry.IRRegistryInstruction.Kind.*;
import static org.junit.jupiter.api.Assertions.*;
/** Neutral producer: no PBS type, spelling or collection binding reaches the backend. */
class IdentityHashingPipelineTest {
private static final TypeRef LONG=TypeRef.builtin(0x11),BOOL=TypeRef.builtin(0x13),
OWNER=new TypeRef("neutral:object"),VIEW=new TypeRef("neutral:view");
private final List<IRBackendExecutableFunction.Instruction> code=new ArrayList<>();
private void emit(IRBackendExecutableFunction.InstructionKind op,String label,Integer index) {
code.add(new IRBackendExecutableFunction.Instruction(op,null,null,null,null,null,label,null,index,null,null));
}
private void typed(IRRegistryInstruction.Kind op,TypeRef left,TypeRef right,int field) {
code.add(new IRBackendExecutableFunction.Instruction(REGISTRY,null,null,null,null,null,null,null,null,null,null,
new IRRegistryInstruction(op,left,right,null,field)));
}
@Test void completeTraitViewsAndConcreteAliasesUseBothTypedQueries() throws Exception {
var registry=new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(LONG,null,new Builtin(0x11)),new TypeDeclaration(BOOL,null,new Builtin(0x13)),
new TypeDeclaration(OWNER,"Object",new Struct(ReadOnlyList.from(new NamedType(LONG,"value")))),
new TypeDeclaration(VIEW,"View",new Trait(null))),
ReadOnlyList.from(new Implementation("neutral:implementation",OWNER,VIEW,null)));
typed(STRUCT_NEW,OWNER,null,0);emit(SET_LOCAL,null,0);
emit(GET_LOCAL,null,0);typed(REF_IDENTITY_HASH,OWNER,null,0);emit(SET_LOCAL,null,1);
emit(GET_LOCAL,null,0);emit(PUSH_I64,"42",null);typed(STRUCT_SET,OWNER,null,0);
emit(GET_LOCAL,null,0);typed(REF_TO_TRAIT,OWNER,VIEW,0);typed(REF_IDENTITY_HASH,VIEW,null,0);
emit(GET_LOCAL,null,1);emit(EQ,null,null);
emit(GET_LOCAL,null,0);emit(GET_LOCAL,null,0);typed(REF_TO_TRAIT,OWNER,VIEW,0);
typed(REF_IDENTITY_EQ,OWNER,VIEW,0);emit(AND,null,null);
emit(GET_LOCAL,null,0);typed(STRUCT_NEW,OWNER,null,0);typed(REF_IDENTITY_EQ,OWNER,OWNER,0);
emit(NOT,null,null);emit(AND,null,null);emit(RET,null,null);
var f=new IRBackendExecutableFunction(new FileId(0),new ModuleId(0),"main",new CallableId(0),0,0,0,2,1,5,
ReadOnlyList.wrap(code),null,FunctionMetadata.ordinary(new Signature(null,ReadOnlyList.from(BOOL))));
var backend=IRBackend.builder().entryPointModuleId(new ModuleId(0)).entryPointCallableName("main")
.registry(registry).executableFunctions(ReadOnlyList.from(f)).build();
for(boolean optimized:new boolean[]{false,true}) {
var program=new LowerToIRVMService().lower(backend);
if(optimized)program=new OptimizeIRVMService().optimize(program);
var queries=program.module().functions().getFirst().instructions().stream()
.filter(i->i.op().opcode()==0x97||i.op().opcode()==0x98).toList();
assertEquals(4,queries.size());
assertEquals(List.of(6,6,10,10),queries.stream().map(IRVMInstruction::encodedSize).toList());
assertEquals(List.of(1,2,3,2),queries.stream().map(i->i.op().pops()).toList());
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-identity"+(optimized?"-optimized":"")+".pbx"),module.serialize());
int pc=0,hashPc=-1,eqPc=-1;
for(var instruction:program.module().functions().getFirst().instructions()) {
if(hashPc<0&&instruction.op().opcode()==0x97)hashPc=pc;
if(eqPc<0&&instruction.op().opcode()==0x98)eqPc=pc;
pc+=instruction.encodedSize();
}
int boolId=LinkedRegistry.link(registry).id(BOOL).index();
for(var negative:new String[]{"bad-category","bad-right","dead-bad-category","bad-typeid"}) {
byte[] bad=module.code().clone();var buffer=java.nio.ByteBuffer.wrap(bad).order(java.nio.ByteOrder.LITTLE_ENDIAN);
if(negative.equals("bad-right"))buffer.putInt(eqPc+6,boolId);
else buffer.putInt(hashPc+2,negative.equals("bad-typeid")?-1:boolId);
if(negative.startsWith("dead"))for(int offset=0;offset<6;offset+=2)buffer.putShort(offset,(short)1);
var malformed=new BytecodeModule(module.version(),module.constPool(),module.functions(),bad,module.debugInfo(),module.exports(),module.syscalls(),module.registry(),module.intrinsicSignatures());
assertThrows(BytecodeMarshalingException.class,()->new BytecodePreloadVerifierService().verify(malformed),negative);
Files.write(directory.resolve("common-identity-"+negative+(optimized?"-optimized":"")+".pbx"),malformed.serialize());
}
}
}
@Test void neutralUnknownInputsKeepRuntimeValidationOfTheCompleteRightView()throws Exception {
var registry=new IRRegistry(ReadOnlyList.from(
new TypeDeclaration(LONG,null,new Builtin(0x11)),new TypeDeclaration(BOOL,null,new Builtin(0x13)),
new TypeDeclaration(OWNER,"Object",new Struct(null)),new TypeDeclaration(VIEW,"View",new Trait(null))),null);
var linked=LinkedRegistry.link(registry);int owner=linked.id(OWNER).index(),view=linked.id(VIEW).index();
for(var name:new String[]{"null-views","right-after-null","right-after-distinct","partial","truncated"}) {
var bytes=new java.io.ByteArrayOutputStream();
if(name.equals("truncated")){word(bytes,0x97);}
else {
if(name.equals("right-after-distinct"))instruction(bytes,0xa0,owner);else instruction(bytes,0x10,0);
if(name.startsWith("right")) {instruction(bytes,0x40,0);instruction(bytes,0x40,1);}
else {instruction(bytes,0x10,0);if(!name.equals("partial"))instruction(bytes,0x10,0);}
instruction(bytes,0x98,owner);integer(bytes,view);word(bytes,0x51);
}
byte[] raw=bytes.toByteArray();
var module=new BytecodeModule(0,ReadOnlyList.from(new BytecodeModule.NullConstant()),
ReadOnlyList.from(new BytecodeModule.FunctionMeta(0,raw.length,0,0,1,3,
FunctionMetadata.ordinary(new Signature(null,ReadOnlyList.from(BOOL))))),raw,null,ReadOnlyList.empty(),ReadOnlyList.empty(),registry);
if(name.equals("partial")||name.equals("truncated"))assertThrows(BytecodeMarshalingException.class,()->new BytecodePreloadVerifierService().verify(module));
else new BytecodePreloadVerifierService().verify(module);
var directory=Path.of("build/registry-conformance");Files.createDirectories(directory);
Files.write(directory.resolve("common-identity-"+name+".pbx"),module.serialize());
}
}
private static void word(java.io.ByteArrayOutputStream out,int n){out.write(n);out.write(n>>>8);}
private static void integer(java.io.ByteArrayOutputStream out,int n){for(int i=0;i<4;i++)out.write(n>>>(8*i));}
private static void instruction(java.io.ByteArrayOutputStream out,int op,int n){word(out,op);integer(out,n);}
@Test void identityEqualityRequiresTwoTypeOperands() {
assertThrows(NullPointerException.class,()->new IRRegistryInstruction(REF_IDENTITY_EQ,OWNER,null,null,0));
assertThrows(IllegalArgumentException.class,()->new IRRegistryInstruction(REF_IDENTITY_HASH,OWNER,VIEW,null,0));
}
}
@@ -10,6 +10,7 @@ public record IRRegistryInstruction(Kind kind, IRRegistry.TypeRef type, IRRegist
MAKE_CLOSURE(0x52), CALL_CLOSURE(0x53), GET_CAPTURE(0x57), CALL_TRAIT(0x58),
DYN_PACK(0x90), DYN_UNPACK(0x91), DYN_CAN_UNPACK(0x92),
REF_IS_EXACT(0x93), REF_IMPLEMENTS(0x94), REF_TO_TRAIT(0x95), REF_CAST(0x96),
REF_IDENTITY_HASH(0x97), REF_IDENTITY_EQ(0x98),
STRUCT_NEW(0xa0), STRUCT_GET(0xa1), STRUCT_SET(0xa2),
ARRAY_NEW(0xa3), ARRAY_LEN(0xa4), ARRAY_GET(0xa5), ARRAY_SET(0xa6),
ARRAY_COPY(0xa9);
@@ -23,7 +24,7 @@ public record IRRegistryInstruction(Kind kind, IRRegistry.TypeRef type, IRRegist
if(kind!=Kind.GET_CAPTURE)Objects.requireNonNull(type,"type");
if(kind==Kind.MAKE_CLOSURE) {Objects.requireNonNull(function,"function");function.getIndex();}
else if(function!=null)throw new IllegalArgumentException("unexpected function operand");
boolean pair=(kind.opcode()>=0x93&&kind.opcode()<=0x96);
boolean pair=(kind.opcode()>=0x93&&kind.opcode()<=0x96)||kind==Kind.REF_IDENTITY_EQ;
if(pair)Objects.requireNonNull(targetType,"targetType");
else if(targetType!=null)throw new IllegalArgumentException("unexpected target type");
if(kind==Kind.GET_CAPTURE&&type!=null)throw new IllegalArgumentException("capture index is not a TypeId");
+94 -1
View File
@@ -3,6 +3,42 @@
Owner: compiler/general. This harness uses the production runtime loader and VM;
it does not implement a Java approximation of runtime admission.
## PBS Map and Keyval qualification
Generate the mandatory Map corpus and actual installed-JAR example, then execute
the dedicated verifier in both runtime profiles:
```sh
./gradlew :prometeu-architecture-tests:test --tests '*PbsMapKeyvalTest' :prometeu-app:installedKnowledgeTest --console=plain
CARGO_TARGET_DIR=/tmp/studio-pbx-map-target python3 tools/pbx-conformance/run.py --runtime ../runtime --map-only
CARGO_TARGET_DIR=/tmp/studio-pbx-map-target python3 tools/pbx-conformance/run.py --runtime ../runtime --map-only --release
```
Each non-GC source case has ordinary/optimized PBS producer variants, independent
of the Rust dev/release profile. The manifest names every required success/fault,
resource, injection, GC, import and installed fixture; missing files fail. The
full harness includes Map separately from its historical catch-all PBS verifier.
The corpus covers typed non-positional keys; default/explicit Identity; optional
V and previous-value presence; original-key retention under content policies;
read-only Keyval instance identity; collisions/negative full hashes; rehash and
capture callback counters; complete tuple/native/contract groups; evaluation order;
alias reentrancy and permitted callback reads; and the shared snapshot state table.
Resource fixtures compare attempted instruction prefixes with successful
instruction prefixes under lowered production quotas. They separate installed
guard writes from structural mutation and measure old/candidate table/node and
snapshot/keyval peaks. A named test-only allocation after the real candidate-table
store qualifies permitted post-write incomplete invariants; production SDK has no
injected probe. Natural quota refusal counts are reported separately.
Real safepoints check independent key/value roots, obsolete-value release after
replacement, removed-key/value reclamation, Map-node reclamation, captured Keyvals,
consumed Cursor prefixes, retained final current and release after all owners exit.
Imported/reexported and installed examples execute through the production loader
and VM and actually construct the shared entry Cursor. This is local semantic and
resource qualification, not a performance benchmark or a new provider contract.
From the Studio root:
```sh
@@ -15,7 +51,7 @@ The Java tests generate PBX in the build-pipeline module's
`build/registry-conformance`. `run.py` creates a temporary Cargo project, copies
the runtime lockfile and references its bytecode/hal/vm/system crates by path.
It never modifies the runtime checkout. Add `--online` for an uncached machine;
by default Cargo uses only cached dependencies. Java 25, Rust and the runtime
by default Cargo uses only cached dependencies. Java 21, Rust and the runtime
checkout's dependencies are required.
The output identifies runtime revision, OS/architecture and load configuration.
@@ -304,3 +340,60 @@ Evidence prints consumer/provider source hashes, selected revision and clean sta
toolchain/platform/profile, lockfile/catalog hashes, capabilities, default ceilings
and lowered refusal limits. Keep new qualification separate from historical List/
Linked evidence; prior successful providers are not relabeled as the new target.
## PBS hashing, Identity and Set
`IdentityHashingPipelineTest` qualifies neutral 0x97/0x98 production, complete
concrete/Trait groups and both optimizer paths. It also emits malformed-category,
right-TypeId, unknown-TypeId, dead-code, partial-group and truncated fixtures.
Unknown global inputs in two neutral cases reach production runtime validation:
an absent/different left cannot hide the invalid complete right view. Refusal
retains all three input slots, charges seven cycles and executes zero successful
query steps; zero budget executes nothing.
`PbsIdentityHashingTest` selects all 30 scalar/String rows from the unchanged
118-row independent provider C XXH64 oracle. Both producer paths preserve exact
signed digest bits and attempted costs. Separate source fixtures qualify Struct,
Array, Fn, Random, complete contracts, mutation, aliases and discarded scalar and
reference results. The reference-query source gate ran before Set algorithm work.
The consumer oracle copy is checked against the selected provider on every
explicit harness invocation. Ordinary Gradle compilation uses local resources.
`PbsSetTest` exercises the complete source API, contextual/default/explicit
construction, optional/default diagnostics, nominal origin, shared complete values,
content opt-in, collisions, exact growth arithmetic, callbacks, aliases and Cursor.
Counter strategies demonstrate that rehash and capture do not call hash/equals.
SDK aliases, generic-function reexports and installed examples compile through
actual workspace builders; installed compilation uses only installDist JARs in an
external directory and writes mandatory PBX fixtures for runtime execution.
```sh
./gradlew :prometeu-compiler:prometeu-build-pipeline:test --tests '*IdentityHashingPipelineTest'
./gradlew :prometeu-architecture-tests:test --tests '*PbsIdentityHashingTest' --tests '*PbsSetTest'
./gradlew :prometeu-app:installedKnowledgeTest
CARGO_TARGET_DIR=/tmp/studio-pbx-target python3 tools/pbx-conformance/run.py --runtime ../runtime --identity-hashing-only
CARGO_TARGET_DIR=/tmp/studio-pbx-target python3 tools/pbx-conformance/run.py --runtime ../runtime --set-only
```
The Set quota corpus measures old/candidate coexistence and compares every
original-object instruction prefix with a successful run. It treats guard writes
separately from structural writes. Natural quota cases cover insertion, growth,
duplicate inclusion, removal, clear and capture with no claim that all failures
preserve the entire Set or that every theoretical failure boundary occurs at the
sampled limits. Current natural runs refuse before structural mutation; this is
reported explicitly by their summaries.
A **separate test-only instrumented source fixture** inserts a controlled Array
allocation after the real candidate-table store and before size assignment. A
lowered guest quota refuses that probe. The old size six, new table capacity 16,
seven reachable nodes and active guard remain, while the refused instruction
preserves its own entry state. This demonstrates the post-publication failure
boundary; it is not a production SDK allocation, callback, algorithm or claim about
natural refusal frequency. Its ordinary/optimized fixtures are named
`pbs-set-injected-refusal-growth*` and are generated from an in-memory SDK copy.
Real safepoints qualify source Set node/candidate release, removal/clear, snapshot
prefix reclamation and sticky final retention. Set GC tests do not assume an
iteration order. Add `--release` to qualify the Rust release profile independently
of the two producer variants; omit focused flags for the complete mandatory corpus.
All harness builds remain outside the runtime checkout, which is consumed unchanged.
+563 -3
View File
@@ -41,6 +41,9 @@ 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 == "--map-only") { verify_pbs_map_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--set-only") { verify_pbs_set_fixtures(&root); return; }
if std::env::args().any(|arg| arg == "--identity-hashing-only") { verify_identity_hashing_fixtures(&root); return; }
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; }
@@ -49,6 +52,7 @@ fn main() {
verify_pbs_math_fixtures(&root);
if std::env::args().any(|arg| arg == "--math-only") { return; }
verify_pbs_random_fixtures(&root);
verify_identity_hashing_fixtures(&root);
verify_catalog_fixtures(&root);
verify_pbs_fixtures(&root);
verify_pbs_structured_fixtures(&root);
@@ -57,6 +61,8 @@ fn main() {
verify_common_storage_fixtures(&root);
verify_pbs_list_cursor_fixtures(&root);
verify_pbs_linked_fixtures(&root);
verify_pbs_set_fixtures(&root);
verify_pbs_map_fixtures(&root);
verify_pbs_resource_refusals(&root);
verify_pbs_closure_frames(&root);
for name in [
@@ -497,7 +503,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-linked-") || !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-hash-") || name.starts_with("pbs-set-") || name.starts_with("pbs-map-") || !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)
@@ -1236,8 +1242,9 @@ fn verify_collection_cursor_gc(root:&std::path::Path,collection:&str) {
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");}
if collection=="set" {assert!(named("_SetNode<Item>").is_empty(),"snapshot does not retain original Set 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");}
if items==1 && collection!="set" {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={}",collection.to_uppercase(),snapshot.gc.reclaimed);
}
let report=vm.run_budget(100_000,&mut NoNative,&mut HostContext::new(None)).unwrap();
@@ -1355,7 +1362,9 @@ fn verify_collection_distribution(root:&std::path::Path,collection:&str,last:i32
}
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");
if collection=="set" {
assert_eq!(retained.len(),1);assert!([20,22].contains(&retained[0]),"Set has no order guarantee");
}else{assert_eq!(retained,expected,"{collection} {name}: complete traversal retains each final value");}
println!("PBS {} DISTRIBUTION LOAD/EXEC PASS {name}",collection.to_uppercase());
}
}
@@ -1509,3 +1518,554 @@ fn verify_linked_collection_gc(root:&std::path::Path) {
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)]);
}
fn verify_identity_hashing_fixtures(root: &std::path::Path) {
use prometeu_vm::LogicalFrameEndingReason as End;
use prometeu_bytecode::{isa::core::CoreOpCode as Op,registry::TypeId};
let corpus=std::fs::read_to_string(root.join("hash-vectors.tsv")).expect("mandatory hashing oracle manifest");
let mut originals=std::collections::BTreeMap::new();let mut count=0;
for row in corpus.lines() {
let parts:Vec<_>=row.split_whitespace().collect();let name=parts[0];
let expected=u64::from_str_radix(parts[1],16).unwrap() as i64;
let id:u32=parts[2].parse().unwrap();let cost:u64=parts[3].parse().unwrap();
let bytes=std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let code=image.code().to_vec();let mut vm=VirtualMachine::from_image(image,Default::default());
vm.prepare_boot_call().unwrap();let mut total=0;let mut calls=0;
loop {
let pc=vm.pc();let op=Op::try_from(u16::from_le_bytes(code[pc..pc+2].try_into().unwrap())).unwrap();
let charge=if op==Op::Intrinsic {
assert_eq!(u32::from_le_bytes(code[pc+2..pc+6].try_into().unwrap()),id,"{name}");
let before=vm.operand_stack_top(1).to_vec();let zero=vm.run_budget(0,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert_eq!((vm.pc(),zero.cycles_used,zero.steps_executed),(pc,0,0));
assert_eq!(vm.operand_stack_top(1),before);calls+=1;cost
}else{op.cycles()};
let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();
assert_eq!(report.cycles_used,charge,"{name} at {pc}");total+=charge;
if matches!(report.reason,End::EndOfRom|End::Halted){break;}
assert!(matches!(report.reason,End::BudgetExhausted),"{name}: {report:?}");
assert!(total<100_000);
}
assert_eq!(calls,1,"{name}");assert_eq!(vm.operand_stack_top(1),vec![Value::Int64(expected)],"{name}");
if let Some(base)=name.strip_suffix("-optimized") {assert_eq!(originals.get(base),Some(&total),"{name}: optimizer charging");}
else{originals.insert(name.to_string(),total);}
println!("HASH ORACLE/COST/FUEL PASS {name} result={expected} cycles={total}");count+=1;
}
assert_eq!(count,60,"all 30 source scalar/String vectors in both optimizer paths");
for prefix in ["common-identity","pbs-hash-object","pbs-hash-contract","pbs-hash-array","pbs-hash-function","pbs-hash-random","pbs-hash-discard","pbs-hash-discard-reference"] {
for suffix in ["","-optimized"] {
let name=format!("{prefix}{suffix}");let bytes=std::fs::read(root.join(format!("{name}.pbx"))).expect("mandatory identity fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap_or_else(|e|panic!("{name}: {e:?}"));
let code=image.code().to_vec();let registry=image.registry().clone();
let mut vm=VirtualMachine::from_image(image,Default::default());let observer=vm.resource_observer();
vm.prepare_boot_call().unwrap();let mut steps=0;let mut queries=0;
loop {
let pc=vm.pc();let op=Op::try_from(u16::from_le_bytes(code[pc..pc+2].try_into().unwrap())).unwrap();
let width=|at:usize|registry.type_layout(TypeId(u32::from_le_bytes(code[at..at+4].try_into().unwrap()))).unwrap().storage.slots as u64;
let charge=match op {Op::RefIdentityHash=>Some(8+width(pc+2)),Op::RefIdentityEq=>Some(4+width(pc+2)+width(pc+6)),_=>None};
let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();
if let Some(charge)=charge {queries+=1;assert_eq!(report.cycles_used,charge,"{name}: typed query charge");}
if matches!(report.reason,End::EndOfRom|End::Halted){break;}
assert!(matches!(report.reason,End::BudgetExhausted),"{name}: {report:?}");steps+=1;assert!(steps<100_000);
}
if prefix=="pbs-hash-discard-reference"{assert_eq!(queries,2,"unused reference queries retain charged work");}
let expected=if prefix=="common-identity"{Value::Boolean(true)}else{Value::Int32(42)};
assert_eq!(vm.operand_stack_top(1),vec![expected],"{name}");drop(vm);
for resource in [prometeu_vm::resource_budget::RuntimeResource::GuestBytes,prometeu_vm::resource_budget::RuntimeResource::HandleSlots,prometeu_vm::resource_budget::RuntimeResource::StringBytes] {
assert_eq!(observer.used(resource),0,"{name}: owner release");
}
println!("IDENTITY COMPLETE GROUP/ALIAS/COST PASS {name}");
}
}
verify_identity_negative_fixtures(root);
}
fn verify_pbs_set_fixtures(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeResource as Q};
let successes=["basic","construction","arithmetic","scalars","objects","collisions","content","tuple",
"cursor-empty","cursor-single","snapshot","callback-reads","references","random","vec2","pixel","evaluation","contract-snapshot","string-snapshot","false-snapshot"];
let faults=[("fault-callback-1",prometeu_bytecode::TRAP_EXPLICIT),("fault-callback-2",prometeu_bytecode::TRAP_EXPLICIT),
("fault-callback-3",prometeu_bytecode::TRAP_EXPLICIT),("fault-callback-4",prometeu_bytecode::TRAP_EXPLICIT),
("fault-callback-5",prometeu_bytecode::TRAP_DIV_ZERO),("fault-overflow",prometeu_bytecode::TRAP_EXPLICIT)];
for (base,fault) in successes.into_iter().map(|n|(n,None)).chain(faults.into_iter().map(|(n,f)|(n,Some(f)))) {
for suffix in ["","-optimized"] {
let name=format!("{base}{suffix}");let bytes=std::fs::read(root.join(format!("pbs-set-{name}.pbx"))).unwrap_or_else(|e|panic!("mandatory Set {name}: {e}"));
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap_or_else(|e|panic!("Set {name}: {e:?}"));
let registry=image.registry().clone();let mut vm=VirtualMachine::from_image(image,Default::default());let observer=vm.resource_observer();
vm.prepare_boot_call().unwrap();let report=vm.run_budget(10_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:?}");
if base.starts_with("fault-callback") {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let set=objects.iter().find(|o|registry.declarations().types[o.type_id.0 as usize].name.as_deref()==Some("set<int>")).expect("faulted Set retained");
assert_eq!(set.groups[0],vec![V::Null],"{name}: unpublished buckets");
assert_eq!(set.groups[1],vec![V::Int64(0)],"{name}: old size");
assert_eq!(set.groups[3],vec![V::Boolean(true)],"{name}: guard is not cleaned after fault");
}
}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 [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes,Q::StringBytes,Q::StringAllocations] {
assert_eq!(observer.used(resource),0,"{name}: final resource release {resource:?}");
}
println!("SET SOURCE/LOAD/EXEC/GUARD PASS {name}");
}
}
verify_set_refusals(root);
verify_set_postpublication_injection(root);
verify_collection_cursor_gc(root,"set");
verify_set_collection_gc(root);
verify_collection_distribution(root,"set",0);
verify_installed_hash(root);
}
fn set_state(vm:&VirtualMachine,image:&ValidatedProgramImage)->ObservedStorage {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let owner=objects.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("set<int>")).expect("source Set");
let mut pending=vec![owner.reference];let mut state:ObservedStorage=Vec::new();
while let Some(reference)=pending.pop() {
if state.iter().any(|o|o.reference==reference){continue;}
let object=objects.iter().find(|o|o.reference==reference).expect("live complete Set group");
for value in object.groups.iter().flatten(){if let V::Reference(reference)=value{pending.push(*reference);}}
state.push(object.clone());
}
state.sort_by_key(|o|(o.reference.index,o.reference.generation));state
}
fn set_without_guard(mut state:ObservedStorage,image:&ValidatedProgramImage)->ObservedStorage {
for object in &mut state {
if image.registry().declarations().types[object.type_id.0 as usize].name.as_deref()==Some("set<int>") {
object.groups[3]=vec![prometeu_vm::resource_diagnostics::StorageValue::Boolean(false)];
}
}
state
}
fn validate_set(vm:&VirtualMachine,image:&ValidatedProgramImage,expected:i64)->usize {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let state=set_state(vm,image);
let owner=state.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("set<int>")).unwrap();
assert_eq!(owner.groups[1],vec![V::Int64(expected)]);assert_eq!(owner.groups[3],vec![V::Boolean(false)]);
let get=|reference|state.iter().find(|o|o.reference==reference).expect("reachable object");
let V::Reference(carrier)=owner.groups[0][0] else{assert_eq!(expected,0);return 0;};
let V::Reference(table)=get(carrier).groups[0][0] else{panic!("bucket carrier")};
let buckets=get(table);let capacity=buckets.groups.len();let mut nodes=std::collections::HashSet::new();
for (bucket,group) in buckets.groups.iter().enumerate() {
if *group==vec![V::Null,V::Null]{continue;}
let [V::Int64(tag),V::Reference(first)]=group.as_slice() else{panic!("complete bucket group")};
assert_eq!(image.registry().declarations().types[*tag as usize].name.as_deref(),Some("_SetNode<int>"));
let mut next=Some(*first);
while let Some(reference)=next {
assert!(nodes.insert(reference),"each node appears once, no cycles");let node=get(reference);
let V::Int64(hash)=node.groups[0][0] else{panic!("cached complete hash")};
assert_eq!(hash.rem_euclid(capacity as i64) as usize,bucket);
next=match node.groups[2][0]{V::Null=>None,V::Reference(carrier)=>{let V::Reference(value)=get(carrier).groups[0][0] else{panic!("next node carrier")};Some(value)},_=>panic!("next group")};
}
}
assert_eq!(nodes.len(),expected as usize);capacity
}
fn verify_set_refusals(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::{RuntimeLimits,RuntimeResource as Q}};
use prometeu_bytecode::{decode_next,isa::core::CoreOpCode as Op,TRAP_RESOURCE_LIMIT};
let mut node_refusal=false;let mut snapshot_refusal=false;let mut table_refusal=false;let mut guard_effect=false;
for name in ["growth","add","duplicate","remove","clear","cursor"] {
for suffix in ["","-optimized"] {
let bytes=std::fs::read(root.join(format!("pbs-set-refusal-{name}{suffix}.pbx"))).expect("mandatory Set resource 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 i=decode_next(pc,image.code()).unwrap();if i.opcode==Op::PushI32&&i.imm_i32().unwrap()==value{found.push(pc);}pc=i.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 old=set_state(&baseline,&image);let size=if name=="growth"{6}else{5};
assert_eq!(validate_set(&baseline,&image,size),8);baseline.remove_breakpoint(before_pc);
let mut successful=Vec::new();let mut max_nodes=0;let mut max_tables=0;
loop {
let objects=baseline.heap().diagnostic_typed_storage();
max_nodes=max_nodes.max(objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_SetNode<int>")).count());
max_tables=max_tables.max(objects.iter().filter(|o|matches!(image.registry().declarations().types[o.type_id.0 as usize].kind,prometeu_bytecode::registry::TypeKind::Array(_))).count());
successful.push((baseline.pc(),observe_originals(&baseline,&old)));
let report=baseline.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();
if baseline.pc()==after_pc{break;}
assert!(matches!(report.reason,End::BudgetExhausted),"Set {name}: {report:?}");assert!(successful.len()<200_000);
}
let after=baseline.resource_snapshot();let expected=match name{"growth"|"add"=>size+1,"remove"=>size-1,"clear"=>0,_=>size};
let capacity=validate_set(&baseline,&image,expected);
assert_eq!(capacity,if name=="growth"{16}else if name=="clear"{0}else{8});
if name=="growth"{assert!(max_nodes>=13,"six old + six copied + inserted");assert!(max_tables>=2);}
if name=="duplicate"||name=="cursor"{assert_eq!(set_state(&baseline,&image),old);}
println!("SET PEAK {name}{suffix} nodes={max_nodes} tables={max_tables} before={:?} after={:?}",before.budget.peaks,after.budget.peaks);
let mut refusals=0;let mut structural_refusals=0;
for resource in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes,Q::CallDepth] {
let lower=before.budget.peaks[resource as usize];let upper=after.budget.peaks[resource as usize];if lower>=upper{continue;}
let mut limits=vec![lower,lower+(upper-lower)/4,lower+(upper-lower)/2,upper-1];
if resource==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(resource,limit).unwrap());let observer=vm.resource_observer();
assert!(matches!(vm.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap().reason,End::Breakpoint),"Set setup {name} {resource:?}={limit}");
let original=set_state(&vm,&image);vm.remove_breakpoint(before_pc);
let (report,prefix,_)=trace_collection_attempt(&mut vm,&original,after_pc);assert_mutation_prefix(&prefix,&successful);
if let End::Trap(info)=report.reason {
assert_eq!(info.code,TRAP_RESOURCE_LIMIT,"Set {name} {resource:?}={limit}: {info:?}");
let touched=prefix.iter().any(|(_,state)|set_without_guard(state.clone(),&image)!=set_without_guard(original.clone(),&image));
if !touched {assert_eq!(set_without_guard(observe_originals(&vm,&original),&image),set_without_guard(original.clone(),&image));}
if name=="cursor"{assert_eq!(set_state(&vm,&image),original);}
let now=observe_originals(&vm,&original);guard_effect|=!touched&&now!=original;
structural_refusals+=usize::from(touched);
let instruction=decode_next(info.pc as usize,image.code()).unwrap();table_refusal|=name=="growth"&&instruction.opcode==Op::ArrayNew;
if instruction.opcode==Op::StructNew {
let named=image.registry().declarations().types[instruction.imm_u32().unwrap() as usize].name.as_deref();
node_refusal|=named==Some("_SetNode<int>");snapshot_refusal|=named==Some("_SnapshotNode<int>");
}
refusals+=1;println!("SET PHASE REFUSAL {name}{suffix} structural={touched} resource={resource:?} limit={limit} opcode={:?}",instruction.opcode);
}else{assert!(matches!(report.reason,End::BudgetExhausted|End::Breakpoint)&&vm.pc()==after_pc);}
drop(vm);for q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes]{assert_eq!(observer.used(q),0);}
}
}
assert!(refusals>0,"Set {name}: resource refusal evidence");
println!("SET PHASE SUMMARY {name}{suffix} refusals={refusals} after_structural_write={structural_refusals}");
}
}
assert!(node_refusal&&snapshot_refusal&&table_refusal&&guard_effect,"nodes/snapshot/table/guard refusal evidence required");
}
fn verify_set_collection_gc(root:&std::path::Path) {
use prometeu_vm::LogicalFrameEndingReason as End;
let bytes=std::fs::read(root.join("pbs-set-gc-collection.pbx")).expect("mandatory Set collection GC fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),Default::default());vm.prepare_boot_call().unwrap();let mut previous=0;
for (round,count) in [64,61,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),"Set GC {round}: {report:?}");
let collections=vm.resource_snapshot().gc.collections;assert!(collections>previous);previous=collections;
let objects=vm.heap().diagnostic_typed_storage();
for name in ["_SetNode<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,"Set GC {round}: {name}");
}
println!("SET COLLECTION/OLD-CANDIDATE GC PASS round={round} live_nodes={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)]);
}
// Controlled test-only source instrumentation, never a production SDK algorithm.
// A quota probe is inserted after the real candidate-table store and before size.
// Natural production-quota coverage and its refusal frequencies remain separate.
fn verify_set_postpublication_injection(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::{RuntimeLimits,RuntimeResource as Q},resource_diagnostics::StorageValue as V};
use prometeu_bytecode::{decode_next,isa::core::CoreOpCode as Op,TRAP_RESOURCE_LIMIT};
for suffix in ["","-optimized"] {
let bytes=std::fs::read(root.join(format!("pbs-set-injected-refusal-growth{suffix}.pbx"))).expect("explicit test-only post-publication injection");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let limits=RuntimeLimits::default().lowered(Q::GuestBytes,500_000).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),limits);let observer=vm.resource_observer();vm.prepare_boot_call().unwrap();
let mut found=false;
for _ in 0..200_000 {
let instruction=decode_next(vm.pc(),image.code()).unwrap();
if instruction.opcode==Op::ArrayNew&&vm.operand_stack_top(1)==vec![Value::Int64(1_000_000)] {
let original=vm.heap().diagnostic_typed_storage();let state=set_state(&vm,&image);
let owner=state.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("set<int>")).unwrap();
assert_eq!(owner.groups[1],vec![V::Int64(6)]);assert_eq!(owner.groups[3],vec![V::Boolean(true)]);
assert_eq!(state.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_SetNode<int>")).count(),7);
assert!(state.iter().any(|o|matches!(image.registry().declarations().types[o.type_id.0 as usize].kind,prometeu_bytecode::registry::TypeKind::Array(_))&&o.groups.len()==16));
let input=vm.operand_stack_top(1).to_vec();let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();
let End::Trap(info)=report.reason else{panic!("post-publication injection: {report:?}")};assert_eq!(info.code,TRAP_RESOURCE_LIMIT);
assert_eq!(vm.heap().diagnostic_typed_storage(),original,"refused probe preserves its entry state, retaining earlier table publication");
assert_eq!(vm.operand_stack_top(1),input);
println!("SET TEST-ONLY POST-PUBLICATION QUOTA INJECTION PASS {suffix}: new table=16 nodes=7 old size=6 guard=true");found=true;break;
}
let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();assert!(matches!(report.reason,End::BudgetExhausted),"before injection: {report:?}");
}
assert!(found);drop(vm);for q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes]{assert_eq!(observer.used(q),0);}
}
}
fn verify_identity_negative_fixtures(root:&std::path::Path) {
use prometeu_vm::LogicalFrameEndingReason as End;
use prometeu_bytecode::{decode_next,isa::core::CoreOpCode as Op};
for base in ["bad-category","bad-right","dead-bad-category","bad-typeid"] {
for suffix in ["","-optimized"] {
let name=format!("common-identity-{base}{suffix}");let bytes=std::fs::read(root.join(format!("{name}.pbx"))).unwrap();
assert!(ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).is_err(),"{name}");
println!("IDENTITY NEUTRAL ADMISSION REJECT PASS {name}");
}
}
for base in ["partial","truncated"] {
let bytes=std::fs::read(root.join(format!("common-identity-{base}.pbx"))).unwrap();
assert!(ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).is_err());
println!("IDENTITY PARTIAL/TRUNCATED ADMISSION REJECT PASS {base}");
}
for base in ["null-views","right-after-null","right-after-distinct"] {
let bytes=std::fs::read(root.join(format!("common-identity-{base}.pbx"))).unwrap();
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap_or_else(|e|panic!("{base}: {e:?}"));
let mut vm=VirtualMachine::from_image(image.clone(),Default::default());vm.prepare_boot_call().unwrap();
loop {
let instruction=decode_next(vm.pc(),image.code()).unwrap();
if instruction.opcode==Op::RefIdentityEq {
let operands=vm.operand_stack_top(3).to_vec();
let zero=vm.run_budget(0,&mut NoNative,&mut HostContext::new(None)).unwrap();assert_eq!((zero.cycles_used,zero.steps_executed),(0,0));
let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();assert_eq!(report.cycles_used,7);
if base=="null-views" {
assert!(matches!(report.reason,End::BudgetExhausted));assert_eq!(vm.operand_stack_top(1),vec![Value::Boolean(true)]);
}else {
let End::Trap(info)=report.reason else{panic!("{base}: right validation was skipped: {report:?}")};
assert_eq!(info.code,prometeu_bytecode::TRAP_TYPE);assert_eq!(report.steps_executed,0);assert_eq!(vm.operand_stack_top(3),operands);
}
println!("IDENTITY COMPLETE RIGHT/NULL/FUEL/COST PASS {base}");break;
}
let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();assert!(matches!(report.reason,End::BudgetExhausted));
}
}
}
fn verify_installed_hash(root:&std::path::Path) {
use prometeu_vm::LogicalFrameEndingReason as End;
let bytes=std::fs::read(root.join("pbs-hash-installed.pbx")).expect("mandatory installed hashing example");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image,Default::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),"installed hashing: {report:?}");
println!("HASH INSTALLED JAR/SDK/EXAMPLE LOAD/EXEC PASS");
}
fn map_state(vm:&VirtualMachine,image:&ValidatedProgramImage)->ObservedStorage {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let owner=objects.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("map<int, int>")).expect("source Map");
let mut pending=vec![owner.reference];let mut state:ObservedStorage=Vec::new();
while let Some(reference)=pending.pop() {
if state.iter().any(|o|o.reference==reference){continue;}
let object=objects.iter().find(|o|o.reference==reference).expect("live complete Map group");
for value in object.groups.iter().flatten(){if let V::Reference(reference)=value{pending.push(*reference);}}
state.push(object.clone());
}
state.sort_by_key(|o|(o.reference.index,o.reference.generation));state
}
fn map_without_guard(mut state:ObservedStorage,image:&ValidatedProgramImage)->ObservedStorage {
for object in &mut state {
if image.registry().declarations().types[object.type_id.0 as usize].name.as_deref()==Some("map<int, int>") {
object.groups[3]=vec![prometeu_vm::resource_diagnostics::StorageValue::Boolean(false)];
}
}
state
}
fn validate_map(vm:&VirtualMachine,image:&ValidatedProgramImage,expected:i64)->usize {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let state=map_state(vm,image);
let owner=state.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("map<int, int>")).unwrap();
assert_eq!(owner.groups[1],vec![V::Int64(expected)]);assert_eq!(owner.groups[3],vec![V::Boolean(false)]);
let get=|reference|state.iter().find(|o|o.reference==reference).expect("reachable object");
let V::Reference(carrier)=owner.groups[0][0] else{assert_eq!(expected,0);return 0;};
let V::Reference(table)=get(carrier).groups[0][0] else{panic!("bucket carrier")};
let buckets=get(table);let capacity=buckets.groups.len();let mut nodes=std::collections::HashSet::new();
for (bucket,group) in buckets.groups.iter().enumerate() {
if *group==vec![V::Null,V::Null]{continue;}
let [V::Int64(tag),V::Reference(first)]=group.as_slice() else{panic!("complete bucket group")};
assert_eq!(image.registry().declarations().types[*tag as usize].name.as_deref(),Some("_MapNode<int, int>"));
let mut next=Some(*first);
while let Some(reference)=next {
assert!(nodes.insert(reference),"each node appears once, no cycles");let node=get(reference);
let V::Int64(hash)=node.groups[0][0] else{panic!("cached complete hash")};
assert_eq!(hash.rem_euclid(capacity as i64) as usize,bucket);
next=match node.groups[3][0]{V::Null=>None,V::Reference(carrier)=>{let V::Reference(value)=get(carrier).groups[0][0] else{panic!("next node carrier")};Some(value)},_=>panic!("next group")};
}
}
assert_eq!(nodes.len(),expected as usize);capacity
}
fn verify_map_refusals(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::{RuntimeLimits,RuntimeResource as Q}};
use prometeu_bytecode::{decode_next,isa::core::CoreOpCode as Op,TRAP_RESOURCE_LIMIT};
let mut node_refusal=false;let mut snapshot_refusal=false;let mut table_refusal=false;let mut guard_effect=false;
for name in ["growth","add","replace","put","remove","clear","cursor","read"] {
for suffix in ["","-optimized"] {
let bytes=std::fs::read(root.join(format!("pbs-map-refusal-{name}{suffix}.pbx"))).expect("mandatory Map resource 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 i=decode_next(pc,image.code()).unwrap();if i.opcode==Op::PushI32&&i.imm_i32().unwrap()==value{found.push(pc);}pc=i.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 old=map_state(&baseline,&image);let size=if name=="growth"{6}else{5};
assert_eq!(validate_map(&baseline,&image,size),8);baseline.remove_breakpoint(before_pc);
let mut successful=Vec::new();let mut max_nodes=0;let mut max_tables=0;
loop {
let objects=baseline.heap().diagnostic_typed_storage();
max_nodes=max_nodes.max(objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_MapNode<int, int>")).count());
max_tables=max_tables.max(objects.iter().filter(|o|matches!(image.registry().declarations().types[o.type_id.0 as usize].kind,prometeu_bytecode::registry::TypeKind::Array(_))).count());
successful.push((baseline.pc(),observe_originals(&baseline,&old)));
let report=baseline.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();
if baseline.pc()==after_pc{break;}
assert!(matches!(report.reason,End::BudgetExhausted),"Map {name}: {report:?}");assert!(successful.len()<200_000);
}
let after=baseline.resource_snapshot();let expected=match name{"growth"|"add"=>size+1,"remove"=>size-1,"clear"=>0,_=>size};
let capacity=validate_map(&baseline,&image,expected);
assert_eq!(capacity,if name=="growth"{16}else if name=="clear"{0}else{8});
if name=="replace"||name=="put"{let state=map_state(&baseline,&image);assert!(state.iter().any(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_MapNode<int, int>")&&o.groups[1]==vec![prometeu_vm::resource_diagnostics::StorageValue::Int32(1)]&&o.groups[2]==vec![prometeu_vm::resource_diagnostics::StorageValue::Int32(42)]));}
if name=="growth"{assert!(max_nodes>=13,"six old + six copied + inserted");assert!(max_tables>=2);}
if name=="read"||name=="cursor"{assert_eq!(map_state(&baseline,&image),old);}
println!("MAP PEAK {name}{suffix} nodes={max_nodes} tables={max_tables} before={:?} after={:?}",before.budget.peaks,after.budget.peaks);
let mut refusals=0;let mut structural_refusals=0;
for resource in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes,Q::CallDepth] {
let lower=before.budget.peaks[resource as usize];let upper=after.budget.peaks[resource as usize];if lower>=upper{continue;}
let mut limits=vec![lower,lower+(upper-lower)/4,lower+(upper-lower)/2,upper-1];
if resource==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(resource,limit).unwrap());let observer=vm.resource_observer();
assert!(matches!(vm.run_budget(20_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap().reason,End::Breakpoint),"Map setup {name} {resource:?}={limit}");
let original=map_state(&vm,&image);vm.remove_breakpoint(before_pc);
let (report,prefix,_)=trace_collection_attempt(&mut vm,&original,after_pc);assert_mutation_prefix(&prefix,&successful);
if let End::Trap(info)=report.reason {
assert_eq!(info.code,TRAP_RESOURCE_LIMIT,"Map {name} {resource:?}={limit}: {info:?}");
let touched=prefix.iter().any(|(_,state)|map_without_guard(state.clone(),&image)!=map_without_guard(original.clone(),&image));
if !touched {assert_eq!(map_without_guard(observe_originals(&vm,&original),&image),map_without_guard(original.clone(),&image));}
if name=="cursor"{assert_eq!(map_state(&vm,&image),original);}
let now=observe_originals(&vm,&original);guard_effect|=!touched&&now!=original;
structural_refusals+=usize::from(touched);
let instruction=decode_next(info.pc as usize,image.code()).unwrap();table_refusal|=name=="growth"&&instruction.opcode==Op::ArrayNew;
if instruction.opcode==Op::StructNew {
let named=image.registry().declarations().types[instruction.imm_u32().unwrap() as usize].name.as_deref();
node_refusal|=named==Some("_MapNode<int, int>");snapshot_refusal|=named==Some("_SnapshotNode<keyval<int, int>>");
}
refusals+=1;println!("MAP PHASE REFUSAL {name}{suffix} structural={touched} resource={resource:?} limit={limit} opcode={:?}",instruction.opcode);
}else{assert!(matches!(report.reason,End::BudgetExhausted|End::Breakpoint)&&vm.pc()==after_pc);}
drop(vm);for q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes]{assert_eq!(observer.used(q),0);}
}
}
assert!(refusals>0,"Map {name}: resource refusal evidence");
println!("MAP PHASE SUMMARY {name}{suffix} refusals={refusals} after_structural_write={structural_refusals}");
}
}
assert!(node_refusal&&snapshot_refusal&&table_refusal&&guard_effect,"nodes/snapshot/table/guard refusal evidence required");
}
fn verify_map_collection_gc(root:&std::path::Path) {
for suffix in ["","-optimized"] {verify_map_collection_gc_variant(root,suffix);}
}
fn verify_map_collection_gc_variant(root:&std::path::Path,suffix:&str) {
use prometeu_vm::LogicalFrameEndingReason as End;
let bytes=std::fs::read(root.join(format!("pbs-map-gc-collection{suffix}.pbx"))).expect("mandatory Map collection GC fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),Default::default());vm.prepare_boot_call().unwrap();let mut previous=0;
for (round,count) in [64,64,61,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),"Map GC {round}: {report:?}");
let collections=vm.resource_snapshot().gc.collections;assert!(collections>previous);previous=collections;
let objects=vm.heap().diagnostic_typed_storage();
for name in ["_MapNode<Item, 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,if name=="Item"{count*2}else{count},"Map GC {round}: {name}");
}
println!("MAP COLLECTION/OLD-CANDIDATE GC PASS {suffix} round={round} live_nodes={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)]);
}
// Controlled test-only source instrumentation, never a production SDK algorithm.
// A quota probe is inserted after the real candidate-table store and before size.
// Natural production-quota coverage and its refusal frequencies remain separate.
fn verify_map_postpublication_injection(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::{RuntimeLimits,RuntimeResource as Q},resource_diagnostics::StorageValue as V};
use prometeu_bytecode::{decode_next,isa::core::CoreOpCode as Op,TRAP_RESOURCE_LIMIT};
for suffix in ["","-optimized"] {
let bytes=std::fs::read(root.join(format!("pbs-map-injected-refusal-growth{suffix}.pbx"))).expect("explicit test-only post-publication injection");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let limits=RuntimeLimits::default().lowered(Q::GuestBytes,500_000).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),limits);let observer=vm.resource_observer();vm.prepare_boot_call().unwrap();
let mut found=false;
for _ in 0..200_000 {
let instruction=decode_next(vm.pc(),image.code()).unwrap();
if instruction.opcode==Op::ArrayNew&&vm.operand_stack_top(1)==vec![Value::Int64(1_000_000)] {
let original=vm.heap().diagnostic_typed_storage();let state=map_state(&vm,&image);
let owner=state.iter().find(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("map<int, int>")).unwrap();
assert_eq!(owner.groups[1],vec![V::Int64(6)]);assert_eq!(owner.groups[3],vec![V::Boolean(true)]);
assert_eq!(state.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_MapNode<int, int>")).count(),7);
assert!(state.iter().any(|o|matches!(image.registry().declarations().types[o.type_id.0 as usize].kind,prometeu_bytecode::registry::TypeKind::Array(_))&&o.groups.len()==16));
let input=vm.operand_stack_top(1).to_vec();let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();
let End::Trap(info)=report.reason else{panic!("post-publication injection: {report:?}")};assert_eq!(info.code,TRAP_RESOURCE_LIMIT);
assert_eq!(vm.heap().diagnostic_typed_storage(),original,"refused probe preserves its entry state, retaining earlier table publication");
assert_eq!(vm.operand_stack_top(1),input);
println!("MAP TEST-ONLY POST-PUBLICATION QUOTA INJECTION PASS {suffix}: new table=16 nodes=7 old size=6 guard=true");found=true;break;
}
let report=vm.run_budget(1,&mut NoNative,&mut HostContext::new(None)).unwrap();assert!(matches!(report.reason,End::BudgetExhausted),"before injection: {report:?}");
}
assert!(found);drop(vm);for q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes]{assert_eq!(observer.used(q),0);}
}
}
fn verify_pbs_map_fixtures(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_budget::RuntimeResource as Q};
let successes=["basic","optional","scalars","objects","references","content","collisions","arithmetic",
"tuple","native","pixel","random","cursor-empty","cursor-single","snapshot","evaluation","callback-reads"];
let faults=(1..=7).map(|n|(format!("fault-callback-{n}"),if n==7{prometeu_bytecode::TRAP_DIV_ZERO}else{prometeu_bytecode::TRAP_EXPLICIT}))
.chain((1..=7).map(|n|(format!("fault-equals-{n}"),if n==7{prometeu_bytecode::TRAP_DIV_ZERO}else{prometeu_bytecode::TRAP_EXPLICIT})))
.chain(std::iter::once(("fault-overflow".to_string(),prometeu_bytecode::TRAP_EXPLICIT)));
let mut count=0;
for (base,fault) in successes.into_iter().map(|n|(n.to_string(),None)).chain(faults.map(|(n,f)|(n,Some(f)))) {
for suffix in ["","-optimized"] {
let name=format!("{base}{suffix}");let bytes=std::fs::read(root.join(format!("pbs-map-{name}.pbx"))).unwrap_or_else(|e|panic!("mandatory Map {name}: {e}"));
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap_or_else(|e|panic!("Map {name}: {e:?}"));
let registry=image.registry().clone();let mut vm=VirtualMachine::from_image(image,Default::default());let observer=vm.resource_observer();
vm.prepare_boot_call().unwrap();let report=vm.run_budget(20_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:?}");
if base.starts_with("fault-callback")||base.starts_with("fault-equals") {
use prometeu_vm::resource_diagnostics::StorageValue as V;
let objects=vm.heap().diagnostic_typed_storage();
let owner=objects.iter().find(|o|registry.declarations().types[o.type_id.0 as usize].name.as_deref()==Some("map<int, int>")).expect("faulted Map retained");
let populated=base.starts_with("fault-equals");
assert_eq!(owner.groups[1],vec![V::Int64(if populated{1}else{0})]);assert_eq!(owner.groups[3],vec![V::Boolean(true)]);
if populated{assert!(objects.iter().any(|o|registry.declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_MapNode<int, int>")&&o.groups[1]==vec![V::Int32(0)]&&o.groups[2]==vec![V::Int32(20)]));}
else{assert_eq!(owner.groups[0],vec![V::Null]);}
}
} 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 q in [Q::GuestBytes,Q::HandleSlots,Q::AuxiliaryBytes,Q::TemporaryBytes,Q::StringBytes,Q::StringAllocations]{assert_eq!(observer.used(q),0,"{name}: owner release {q:?}");}
count+=1;println!("MAP SOURCE/LOAD/EXEC/GUARD PASS {name}");
}
}
println!("MAP MANDATORY SOURCE CASES {count}");
verify_map_refusals(root);verify_map_postpublication_injection(root);verify_map_cursor_gc(root);verify_map_collection_gc(root);verify_map_distribution(root);
}
fn verify_map_cursor_gc(root:&std::path::Path) {
for suffix in ["","-optimized"] {verify_map_cursor_gc_variant(root,suffix);}
}
fn verify_map_cursor_gc_variant(root:&std::path::Path,suffix:&str) {
use prometeu_vm::LogicalFrameEndingReason as End;
let bytes=std::fs::read(root.join(format!("pbs-map-gc{suffix}.pbx"))).expect("mandatory Map snapshot GC fixture");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),Default::default());vm.prepare_boot_call().unwrap();let mut previous=0;
for (round,count) in [1200,1,1,0].into_iter().enumerate() {
let report=vm.run_budget(100_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();assert!(matches!(report.reason,End::FrameSync),"Map cursor GC {round}: {report:?}");
let snapshot=vm.resource_snapshot();assert!(snapshot.gc.collections>previous);previous=snapshot.gc.collections;
let objects=vm.heap().diagnostic_typed_storage();
assert!(!objects.iter().any(|o|matches!(image.registry().declarations().types[o.type_id.0 as usize].kind,prometeu_bytecode::registry::TypeKind::Array(_))),"snapshot retains no Map table");
for (name,expected) in [("_SnapshotNode<keyval<Item, Item>>",count),("keyval<Item, Item>",count),("Item",count*2),("_MapNode<Item, Item>",0)] {
assert_eq!(objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some(name)).count(),expected,"Map cursor GC {round}: {name}");
}
println!("MAP CURSOR/KEYVAL/KEY/VALUE GC PASS {suffix} round={round} nodes={count} items={}",count*2);
}
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)]);
}
fn verify_map_distribution(root:&std::path::Path) {
use prometeu_vm::{LogicalFrameEndingReason as End,resource_diagnostics::StorageValue as V};
for name in ["imports","installed"] {
let bytes=std::fs::read(root.join(format!("pbs-map-{name}.pbx"))).expect("mandatory Map workspace/installed example");
let image=ValidatedProgramImage::load(&bytes,&mut LoadBudget::new(LoadLimits::default()),caps::NONE).unwrap();
let mut vm=VirtualMachine::from_image(image.clone(),Default::default());vm.prepare_boot_call().unwrap();
let report=vm.run_budget(2_000_000,&mut NoNative,&mut HostContext::new(None)).unwrap();assert!(matches!(report.reason,End::FrameSync|End::Halted|End::EndOfRom),"Map distribution {name}: {report:?}");
let objects=vm.heap().diagnostic_typed_storage();
let cursors=objects.iter().filter(|o|image.registry().declarations().types[o.type_id.0 as usize].name.as_deref()==Some("_SnapshotCursor<keyval<str, optional int>>")).collect::<Vec<_>>();
assert!(!cursors.is_empty(),"installed/imported Map actually constructs the common entry Cursor");
let get=|r|objects.iter().find(|o|o.reference==r).unwrap();
for cursor in cursors {
let V::Reference(carrier)=cursor.groups[0][0] else{panic!("current carrier")};let V::Reference(node)=get(carrier).groups[0][0] else{panic!("node")};
assert_eq!(get(node).groups[1],vec![V::Null],"complete traversal retains sticky final node");
let V::Reference(entry)=get(node).groups[0][0] else{panic!("captured keyval")};
assert_eq!(image.registry().declarations().types[get(entry).type_id.0 as usize].name.as_deref(),Some("keyval<str, optional int>"));
}
println!("MAP DISTRIBUTION SOURCE/LOAD/EXEC PASS {name}");
}
}
+8 -1
View File
@@ -12,6 +12,9 @@ parser.add_argument("--runtime", type=pathlib.Path, default=pathlib.Path("../run
parser.add_argument("--fixtures", type=pathlib.Path, default=pathlib.Path("prometeu-compiler/prometeu-build-pipeline/build/registry-conformance"))
parser.add_argument("--online", action="store_true", help="Allow Cargo to fetch uncached dependencies")
parser.add_argument("--release", action="store_true", help="Execute an optimized runtime harness")
parser.add_argument("--set-only", action="store_true", help="Run mandatory PBS Set corpus")
parser.add_argument("--map-only", action="store_true", help="Run mandatory PBS Map/Keyval corpus")
parser.add_argument("--identity-hashing-only", action="store_true", help="Run mandatory neutral and PBS identity/hash corpus")
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")
@@ -25,6 +28,10 @@ if not args.math_only:
consumer_vectors = pathlib.Path("prometeu-architecture-tests/src/test/resources/random/random-conformance.tsv")
if provider_vectors.read_bytes() != consumer_vectors.read_bytes():
raise SystemExit("Random oracle differs from the selected provider; update provenance and requalify")
provider_hash_vectors = runtime / "crates/console/prometeu-vm/tests/fixtures/hash-conformance.tsv"
consumer_hash_vectors = pathlib.Path("prometeu-architecture-tests/src/test/resources/hash/hash-conformance.tsv")
if provider_hash_vectors.read_bytes() != consumer_hash_vectors.read_bytes():
raise SystemExit("Hash oracle differs from selected provider")
print(subprocess.check_output(["rustc", "-Vv"], text=True).strip(), flush=True)
for source in [pathlib.Path(__file__), pathlib.Path(__file__).with_name("main.rs"),
pathlib.Path("prometeu-architecture-tests/src/test/java/p/studio/compiler/backend/irvm/PbsRuntimeRandomTest.java"),
@@ -52,4 +59,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 []), *(["--linked-only"] if args.linked_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"), *(["--map-only"] if args.map_only else []), *(["--set-only"] if args.set_only else []), *(["--identity-hashing-only"] if args.identity_hashing_only else []), *(["--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)