1
//! The cell registry of a unit: the globals made cells at its start, what its
2
//! definitions did to them, and the closures and seeds the next attempt carries.
3

            
4
use std::collections::{BTreeSet, HashMap};
5

            
6
use super::{Asked, Cell, CellBinding, ClosureShape, Seed, locked};
7
use crate::ast::{Expr, WasmType};
8
use crate::runtime::SymbolTable;
9

            
10
impl SymbolTable {
11
    /// Records the cell `name` the unit made at its start; `flag` is its
12
    /// bound flag and `seed` what it starts with.
13
484761
    pub(crate) fn note_cell(&self, name: &str, flag: u32, seed: Seed) {
14
484761
        locked(&self.homes).cells.insert(
15
484761
            name.to_string(),
16
484761
            Cell {
17
484761
                flag,
18
484761
                seed,
19
484761
                defined: CellBinding::Unbound,
20
484761
            },
21
484761
        );
22
484761
    }
23

            
24
    /// Whether the cell `name` starts with an earlier unit's unreadable value.
25
    #[must_use]
26
107200
    pub(crate) fn is_lost_cell(&self, name: &str) -> bool {
27
107200
        locked(&self.homes)
28
107200
            .cells
29
107200
            .get(name)
30
107200
            .is_some_and(|cell| cell.seed == Seed::Lost)
31
107200
    }
32

            
33
    /// Whether the cell `name` is bound once the definitions compiled so far
34
    /// ran: a seeded one always is.
35
    #[must_use]
36
125372
    pub(crate) fn cell_binding(&self, name: &str) -> CellBinding {
37
125372
        match locked(&self.homes).cells.get(name) {
38
125371
            Some(cell) if cell.seed != Seed::Unbound => CellBinding::Bound,
39
120790
            Some(cell) => cell.defined,
40
1
            None => CellBinding::Unbound,
41
        }
42
125372
    }
43

            
44
    /// Records a DEFVAR / DEFPARAMETER of the cell `name`; one that `surely`
45
    /// runs binds it.
46
124108
    pub(crate) fn note_cell_definition(&self, name: &str, surely: bool) {
47
124108
        let binding = match surely {
48
119525
            true => CellBinding::Bound,
49
4583
            false => CellBinding::Maybe,
50
        };
51
124108
        if let Some(cell) = locked(&self.homes).cells.get_mut(name) {
52
124108
            cell.defined = cell.defined.max(binding);
53
        }
54
124108
    }
55

            
56
    /// Whether `name` is a cell of this unit.
57
    #[must_use]
58
1709908
    pub(crate) fn is_cell(&self, name: &str) -> bool {
59
1709908
        locked(&self.homes).cells.contains_key(name)
60
1709908
    }
61

            
62
    /// The global holding the cell `name`, once it is registered as the
63
    /// symbol's value.
64
    #[must_use]
65
1483096
    pub(crate) fn cell_global(&self, name: &str) -> Option<(u32, WasmType)> {
66
1483096
        match self.lookup(name)?.value()? {
67
685419
            Expr::WasmGlobal(idx, ty) if self.is_cell(name) => Some((*idx, *ty)),
68
797678
            _ => None,
69
        }
70
1483096
    }
71

            
72
    /// The bound flag of the cell `name`.
73
    #[must_use]
74
516232
    pub(crate) fn cell_flag(&self, name: &str) -> Option<u32> {
75
516232
        locked(&self.homes).cells.get(name).map(|cell| cell.flag)
76
516232
    }
77

            
78
    /// Asks the next attempt to make `name` a cell: its one definition's init
79
    /// runs at run time, where an exit may skip it. `asked` types the cell.
80
53201
    pub(crate) fn ask_cell(&self, name: &str, asked: Asked) {
81
53201
        locked(&self.homes).asked.insert(name.to_string(), asked);
82
53201
    }
83

            
84
    /// The cells the next attempt makes for definitions with a run-time init.
85
    #[must_use]
86
2431980
    pub(crate) fn forced_cells(&self) -> BTreeSet<String> {
87
2431980
        let book = locked(&self.homes);
88
2431980
        book.forced
89
2431980
            .iter()
90
2431980
            .chain(book.asked.keys())
91
2431980
            .cloned()
92
2431980
            .collect()
93
2431980
    }
94

            
95
    /// Carries the cells earlier attempts asked for into this one.
96
90046
    pub(crate) fn force_cells(&self, names: BTreeSet<String>) {
97
90046
        locked(&self.homes).forced = names;
98
90046
    }
99

            
100
    /// Asks the next attempt to start the cell `name` without the closure an
101
    /// earlier unit carried: a store of the unit has another signature.
102
9322
    pub(crate) fn refuse_seed(&self, name: &str) {
103
9322
        let mut book = locked(&self.homes);
104
9322
        if book
105
9322
            .cells
106
9322
            .get(name)
107
9322
            .is_some_and(|cell| cell.seed == Seed::Closure)
108
        {
109
209
            book.refused.insert(name.to_string());
110
9113
        }
111
9322
    }
112

            
113
    /// Whether this attempt starts the cell `name` without the carried closure.
114
    #[must_use]
115
3330
    pub(crate) fn is_seed_refused(&self, name: &str) -> bool {
116
3330
        locked(&self.homes).unseeded.contains(name)
117
3330
    }
118

            
119
    /// The cells the next attempt starts without a carried closure.
120
    #[must_use]
121
90046
    pub(crate) fn refused_seeds(&self) -> BTreeSet<String> {
122
90046
        let book = locked(&self.homes);
123
90046
        book.unseeded.union(&book.refused).cloned().collect()
124
90046
    }
125

            
126
    /// Carries the refused closure seeds of earlier attempts into this one.
127
90046
    pub(crate) fn unseed_cells(&self, names: BTreeSet<String>) {
128
90046
        locked(&self.homes).unseeded = names;
129
90046
    }
130

            
131
    /// The closure the previous attempt asked `name`'s cell to hold.
132
    #[must_use]
133
156110
    pub(crate) fn closure_hint(&self, name: &str) -> Option<ClosureShape> {
134
156110
        locked(&self.homes).closure_hints.get(name).cloned()
135
156110
    }
136

            
137
    /// The closures the next attempt allocates cells for.
138
    #[must_use]
139
90049
    pub(crate) fn closure_shapes(&self) -> HashMap<String, ClosureShape> {
140
90049
        locked(&self.homes).closure_shapes()
141
90049
    }
142

            
143
    /// Carries the closures of the previous attempt into this one.
144
90046
    pub(crate) fn hint_closures(&self, shapes: HashMap<String, ClosureShape>) {
145
90046
        locked(&self.homes).closure_hints = shapes.into_iter().collect();
146
90046
    }
147
}