Skip to main content

sim_kernel/library/
registry.rs

1mod catalog;
2mod commit;
3mod commit_plan;
4mod load;
5mod overlay;
6#[cfg(test)]
7mod overlay_tests;
8mod query;
9#[cfg(test)]
10mod query_tests;
11mod register;
12mod unload;
13#[cfg(test)]
14mod unload_tests;
15
16use std::collections::{BTreeMap, BTreeSet};
17
18use crate::{
19    catalog::CatalogStore,
20    id::{
21        ClassId, CodecId, FunctionId, LibId, MacroId, NumberDomainId, RuntimeId, ShapeId, SiteId,
22        Symbol,
23    },
24    number_domain::{
25        NumberBinaryOp, NumberReductionOp, NumberUnaryOp, ValueNumberBinaryOp,
26        ValueNumberReductionOp, ValueNumberUnaryOp, ValuePromotionRule,
27    },
28    value::Value,
29};
30
31use super::{LoadedLib, RegisteredTest};
32
33#[derive(Clone, Debug, Default, PartialEq, Eq)]
34pub(crate) struct LoadDelta {
35    sequence_after: BTreeMap<Symbol, u64>,
36    number_unary_ops: DeltaRange,
37    number_reduction_ops: DeltaRange,
38    number_binary_ops: DeltaRange,
39    value_number_unary_ops: DeltaRange,
40    value_number_reduction_ops: DeltaRange,
41    value_number_binary_ops: DeltaRange,
42    promotion_rules: DeltaRange,
43    value_promotion_rules: DeltaRange,
44}
45
46#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
47pub(crate) struct DeltaRange {
48    start: usize,
49    len: usize,
50}
51
52impl DeltaRange {
53    pub(crate) fn new(start: usize, len: usize) -> Self {
54        Self { start, len }
55    }
56
57    pub(crate) fn is_empty(self) -> bool {
58        self.len == 0
59    }
60
61    pub(crate) fn end(self) -> usize {
62        self.start + self.len
63    }
64
65    pub(crate) fn adjust_after_removed(&mut self, removed: Self) {
66        if self.is_empty() || removed.is_empty() || self.start <= removed.start {
67            return;
68        }
69        self.start = self.start.saturating_sub(removed.len);
70    }
71}
72
73/// The kernel's library registry: the authoritative store of loaded libraries
74/// and their resolved exports.
75///
76/// The registry is catalog-backed for identity (the `catalog` field) and keeps
77/// projection caches over that catalog for fast lookup by symbol, id, and kind.
78/// It owns stable-id allocation, export registration, querying, transactional
79/// loading, and catalog overlays. The kernel defines this contract; libraries
80/// supply the behavior that gets registered. See the README "Library system"
81/// section.
82#[derive(Clone)]
83pub struct Registry {
84    catalog: CatalogStore,
85    // Projection cache for `libs`.
86    // Authoritative source: `registry/libs` and `registry/exports`.
87    // Updated by: `commit_loaded_lib`.
88    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
89    libs: Vec<LoadedLib>,
90    // Projection cache for `lib` and `manifest_by_symbol`.
91    // Authoritative source: `registry/libs`.
92    // Updated by: `commit_loaded_lib` and `rebuild_projection_caches_from_catalog`.
93    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
94    libs_by_symbol: BTreeMap<Symbol, LibId>,
95    // Recorded load deltas keyed by loaded library id.
96    // Authoritative source: committed load transactions.
97    // Updated by: `commit_loaded_lib` and `Registry::unload`.
98    // Covered by: unload roundtrip tests.
99    load_deltas: BTreeMap<LibId, LoadDelta>,
100    // Load-dependency edges keyed by dependent library id.
101    // Authoritative source: manifest dependencies resolved at commit time.
102    // Updated by: `commit_loaded_lib` and `Registry::unload`.
103    // Covered by: unload refusal and cascade tests.
104    load_dependencies: BTreeMap<LibId, BTreeSet<LibId>>,
105    // Projection cache for `export_symbols`.
106    // Authoritative source: `registry/exports`.
107    // Updated by: `insert_runtime_export` and `rebuild_projection_caches_from_catalog`.
108    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
109    export_symbols: BTreeMap<super::ExportKind, BTreeMap<Symbol, RuntimeId>>,
110    // Projection cache for `classes`.
111    // Authoritative source: `registry/exports`.
112    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
113    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
114    class_symbol_cache: BTreeMap<Symbol, ClassId>,
115    // Projection cache for `functions`.
116    // Authoritative source: `registry/exports`.
117    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
118    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
119    function_symbol_cache: BTreeMap<Symbol, FunctionId>,
120    // Projection cache for `macros`.
121    // Authoritative source: `registry/exports`.
122    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
123    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
124    macro_symbol_cache: BTreeMap<Symbol, MacroId>,
125    // Projection cache for `shapes`.
126    // Authoritative source: `registry/exports`.
127    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
128    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
129    shape_symbol_cache: BTreeMap<Symbol, ShapeId>,
130    // Projection cache for `codecs`.
131    // Authoritative source: `registry/exports`.
132    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
133    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
134    codec_symbol_cache: BTreeMap<Symbol, CodecId>,
135    // Projection cache for `number_domains`.
136    // Authoritative source: `registry/exports`.
137    // Updated by: `insert_number_domain_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
138    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
139    number_domain_symbol_cache: BTreeMap<Symbol, NumberDomainId>,
140    // Projection cache for `sites`.
141    // Authoritative source: `registry/exports`.
142    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
143    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
144    site_symbol_cache: BTreeMap<Symbol, SiteId>,
145    // Projection cache for `export_symbol_for_value`.
146    // Authoritative source: `registry/runtime`.
147    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
148    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
149    plain_value_cache: BTreeMap<Symbol, Value>,
150    // Projection cache for `class_value`.
151    // Authoritative source: `registry/runtime`.
152    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
153    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
154    class_value_cache: BTreeMap<ClassId, Value>,
155    // Projection cache for `function_value`.
156    // Authoritative source: `registry/runtime`.
157    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
158    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
159    function_value_cache: BTreeMap<FunctionId, Value>,
160    // Projection cache for `macro_value`.
161    // Authoritative source: `registry/runtime`.
162    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
163    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
164    macro_value_cache: BTreeMap<MacroId, Value>,
165    // Projection cache for `shape_value`.
166    // Authoritative source: `registry/runtime`.
167    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
168    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
169    shape_value_cache: BTreeMap<ShapeId, Value>,
170    // Projection cache for `codec_value`.
171    // Authoritative source: `registry/runtime`.
172    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
173    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
174    codec_value_cache: BTreeMap<CodecId, Value>,
175    // Projection cache for `number_domain_value`.
176    // Authoritative source: `registry/runtime`.
177    // Updated by: `insert_number_domain_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
178    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
179    number_domain_value_cache: BTreeMap<NumberDomainId, Value>,
180    // Projection cache for `site_value`.
181    // Authoritative source: `registry/runtime`.
182    // Updated by: `register_runtime_value`, `commit_loaded_lib`, and `rebuild_projection_caches_from_catalog`.
183    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
184    site_value_cache: BTreeMap<SiteId, Value>,
185    number_domain_order: Option<Vec<NumberDomainId>>,
186    // Projection cache for `tests` and `registered_test`.
187    // Authoritative source: `registry/tests`.
188    // Updated by: `register_test` and `rebuild_projection_caches_from_catalog`.
189    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
190    tests: BTreeMap<Symbol, RegisteredTest>,
191    // Projection cache for `tests_for_lib`.
192    // Authoritative source: `registry/tests`.
193    // Updated by: `register_test` and `rebuild_projection_caches_from_catalog`.
194    // Covered by: `rebuilding_projection_caches_from_catalog_preserves_query_results`.
195    tests_by_lib: BTreeMap<Symbol, Vec<Symbol>>,
196    promotion_rules: Vec<crate::number_domain::PromotionRule>,
197    value_promotion_rules: Vec<ValuePromotionRule>,
198    number_unary_ops: Vec<NumberUnaryOp>,
199    number_reduction_ops: Vec<NumberReductionOp>,
200    number_binary_ops: Vec<NumberBinaryOp>,
201    value_number_unary_ops: Vec<ValueNumberUnaryOp>,
202    value_number_reduction_ops: Vec<ValueNumberReductionOp>,
203    value_number_binary_ops: Vec<ValueNumberBinaryOp>,
204}
205
206impl Default for Registry {
207    fn default() -> Self {
208        Self {
209            catalog: catalog::new_registry_catalog(),
210            libs: Vec::new(),
211            libs_by_symbol: BTreeMap::new(),
212            load_deltas: BTreeMap::new(),
213            load_dependencies: BTreeMap::new(),
214            export_symbols: BTreeMap::new(),
215            class_symbol_cache: BTreeMap::new(),
216            function_symbol_cache: BTreeMap::new(),
217            macro_symbol_cache: BTreeMap::new(),
218            shape_symbol_cache: BTreeMap::new(),
219            codec_symbol_cache: BTreeMap::new(),
220            number_domain_symbol_cache: BTreeMap::new(),
221            site_symbol_cache: BTreeMap::new(),
222            plain_value_cache: BTreeMap::new(),
223            class_value_cache: BTreeMap::new(),
224            function_value_cache: BTreeMap::new(),
225            macro_value_cache: BTreeMap::new(),
226            shape_value_cache: BTreeMap::new(),
227            codec_value_cache: BTreeMap::new(),
228            number_domain_value_cache: BTreeMap::new(),
229            site_value_cache: BTreeMap::new(),
230            number_domain_order: None,
231            tests: BTreeMap::new(),
232            tests_by_lib: BTreeMap::new(),
233            promotion_rules: Vec::new(),
234            value_promotion_rules: Vec::new(),
235            number_unary_ops: Vec::new(),
236            number_reduction_ops: Vec::new(),
237            number_binary_ops: Vec::new(),
238            value_number_unary_ops: Vec::new(),
239            value_number_reduction_ops: Vec::new(),
240            value_number_binary_ops: Vec::new(),
241        }
242    }
243}