feat(pbs): add Set and reusable identity hashing
This commit is contained in:
+218
@@ -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
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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
|
||||
@@ -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,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
|
||||
|
||||
|
||||
@@ -177,10 +177,11 @@ 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>; future Map<K,V> uses
|
||||
Cursor<keyval<K,V>>. Set/Map gain no ordering guarantee between captures.
|
||||
A captured keyval association is read-only
|
||||
and does not follow later association replacement; referenced objects stay shared.
|
||||
Set/Map and keyval are separate deliveries.
|
||||
Map and keyval remain separate deliveries.
|
||||
|
||||
For n logical List positions and k present occurrences, capture takes O(n) work
|
||||
and O(k) additional nodes; next follows one link in O(1). Each node costs allocation,
|
||||
|
||||
@@ -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 reusable by future Map. Map/keyval 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).
|
||||
@@ -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
|
||||
|
||||
@@ -31,6 +31,10 @@ 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: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 +95,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").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=example.getKey().equals("set")?"setExample":"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");
|
||||
|
||||
+134
@@ -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());
|
||||
}
|
||||
}
|
||||
+8
-3
@@ -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),
|
||||
|
||||
+404
@@ -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 _setCapacity(0,1)!=(8 as long) or _setCapacity(8,6)!=(8 as long) {return 0;}
|
||||
if _setCapacity(8,7)!=(16 as long) or _setCapacity(16,13)!=(32 as long) {return 0;}
|
||||
if _setCapacity(maximum-1,maximum)!=maximum {return 0;}
|
||||
if _setCapacity(maximum,maximum+1)!=maximum {return 0;}
|
||||
if _setNextSize(sizeMaximum-1)!=sizeMaximum {return 0;}
|
||||
let minimum:long=-9223372036854775808;
|
||||
if _setBucket(minimum,maximum)<(0 as long) or _setBucket(-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","_setNextSize(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.
|
||||
+5
-1
@@ -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) {
|
||||
|
||||
+26
-4
@@ -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,8 +244,8 @@ 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",
|
||||
if (PbsSdkCollections.isCollection(template) && PbsSdkCollections.directOptional(arguments.getFirst())) {
|
||||
error(PbsSdkCollections.isSet(template)?"SET_OPTIONAL_ELEMENT":PbsSdkCollections.isLinked(template) ? "LINKED_OPTIONAL_ELEMENT" : "LIST_OPTIONAL_ELEMENT",
|
||||
"SDK " + name(template) + "<T> does not permit a directly optional element", site);
|
||||
return null;
|
||||
}
|
||||
@@ -535,6 +542,11 @@ 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);
|
||||
return new PbsAst.AssignStatement(a.target(), a.operator(), expression(a.value(), expected), a.span());
|
||||
}
|
||||
@@ -543,6 +555,10 @@ 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(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 +647,14 @@ 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)&&n.arguments().isEmpty()&&!symbolicProbe) {
|
||||
var owner=types.declaration(concreteType);
|
||||
var origin=PbsContractTypes.applicationOrigin(owner);
|
||||
if(origin!=null&&!PbsSdkCollections.hasDefaultIdentity(origin.arguments().getFirst(),types))
|
||||
error("SET_IDENTITY_REQUIRED","This Set element type requires an explicit Identity<T> 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 "
|
||||
|
||||
+19
-2
@@ -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();
|
||||
|
||||
+20
@@ -15,6 +15,26 @@ 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 isCollection(PbsAst.TopDecl declaration) {
|
||||
return isIndexedCollection(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);
|
||||
}
|
||||
|
||||
+67
-2
@@ -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()) {
|
||||
@@ -1613,6 +1617,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();
|
||||
@@ -1929,13 +1988,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);
|
||||
|
||||
+8
@@ -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");
|
||||
|
||||
+2
-2
@@ -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").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()) {
|
||||
|
||||
+2
-2
@@ -759,7 +759,7 @@ public final class PbsEditorialSupportService {
|
||||
final Map<String, PbsEditorialCompletionCandidate> completions = new LinkedHashMap<>();
|
||||
var symbolic = symbolicStruct(receiverType);
|
||||
if (symbolic != null) {
|
||||
for (var field : symbolic.fields()) if(!p.studio.compiler.pbs.PbsSdkCollections.isIndexedCollection(symbolic)||field.isPublic()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
|
||||
for (var field : symbolic.fields()) if(!p.studio.compiler.pbs.PbsSdkCollections.isCollection(symbolic)||field.isPublic()) completions.put(field.name(), new PbsEditorialCompletionCandidate(
|
||||
field.name(), PbsEditorialSymbolKind.FIELD, field.typeRef().surface(), symbolic.name()));
|
||||
for (var method : symbolic.methods()) if(p.studio.compiler.pbs.PbsSdkCollections.attribute(method.attributes(),"SdkPrivate")==null) completions.put(method.name(), new PbsEditorialCompletionCandidate(
|
||||
method.name(), PbsEditorialSymbolKind.METHOD, signatureFromFunction(method.name(), method.parameters(),
|
||||
@@ -778,7 +778,7 @@ public final class PbsEditorialSupportService {
|
||||
final StructInfo structInfo = model.structs.get(receiverType.name());
|
||||
if (structInfo != null) {
|
||||
for (final var field : structInfo.fields().entrySet()) {
|
||||
if(model.isSdkIndexedCollection(receiverType)&&!field.getValue().isPublic())continue;
|
||||
if(model.isSdkCollection(receiverType)&&!field.getValue().isPublic())continue;
|
||||
completions.put(field.getKey(), new PbsEditorialCompletionCandidate(
|
||||
field.getKey(),
|
||||
PbsEditorialSymbolKind.FIELD,
|
||||
|
||||
+6
@@ -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())) {
|
||||
|
||||
+10
@@ -202,6 +202,10 @@ 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 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 +275,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();
|
||||
|
||||
+193
@@ -307,3 +307,196 @@ 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 _setBucket(hash: long, capacity: long) -> long {
|
||||
let bucket: long = hash % capacity;
|
||||
if bucket < (0 as long) { bucket = bucket + capacity; }
|
||||
return bucket;
|
||||
}
|
||||
|
||||
fn _setNextSize(length: long) -> long {
|
||||
let maximum: long = 9223372036854775807;
|
||||
_bounds(length, maximum);
|
||||
return length + 1;
|
||||
}
|
||||
|
||||
fn _setCapacity(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[_setBucket(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 = _setBucket(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 = _setNextSize(this.length);
|
||||
let capacity: long = 0;
|
||||
if let buckets = this.buckets { capacity = buckets.length; }
|
||||
let nextCapacity: long = _setCapacity(capacity, required);
|
||||
if nextCapacity != capacity {
|
||||
let target = this._rehash(nextCapacity);
|
||||
let bucket: long = _setBucket(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 = _setBucket(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 = _setBucket(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>;
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -1,3 +1,5 @@
|
||||
pub struct list<T>;
|
||||
pub contract Cursor<T>;
|
||||
pub struct linked<T>;
|
||||
pub struct set<T>;
|
||||
pub contract Identity<T>;
|
||||
|
||||
+26
@@ -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; }
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
pub service hash;
|
||||
pub fn referenceHash<T>(value: T) -> long;
|
||||
pub fn referenceEquals<T>(left: T, right: T) -> bool;
|
||||
+19
@@ -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;
|
||||
}
|
||||
+2
@@ -4,3 +4,5 @@ pub fn contractExample() -> int;
|
||||
pub fn genericsExample() -> int;
|
||||
pub fn collectionsExample() -> int;
|
||||
pub fn linkedExample() -> int;
|
||||
pub fn setExample() -> int;
|
||||
pub fn hashingExample() -> int;
|
||||
|
||||
+25
@@ -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);
|
||||
}
|
||||
+31
-1
@@ -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);
|
||||
|
||||
+3
-3
@@ -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;
|
||||
|
||||
+2
-2
@@ -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));
|
||||
};
|
||||
|
||||
+4
@@ -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);}
|
||||
|
||||
+13
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+108
@@ -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));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -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");
|
||||
|
||||
@@ -15,7 +15,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 +304,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.
|
||||
|
||||
@@ -41,6 +41,8 @@ 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 == "--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 +51,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 +60,7 @@ 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_resource_refusals(&root);
|
||||
verify_pbs_closure_frames(&root);
|
||||
for name in [
|
||||
@@ -497,7 +501,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-") || 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 +1240,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 +1360,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 +1516,316 @@ 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");
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ 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("--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 +27,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 +58,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"), *(["--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)
|
||||
|
||||
Reference in New Issue
Block a user