Skip to main content

nomiscript/runtime/symbol/
closure_records.rs

1use super::SymbolTable;
2use crate::ast::Expr;
3
4impl SymbolTable {
5    /// Records the parameter `WasmType`s for a closure signature id (see
6    /// [`Self::closure_params`]).
7    pub fn record_closure_params(
8        &mut self,
9        sig: crate::ast::ClosureSigId,
10        params: &[crate::ast::WasmType],
11    ) {
12        self.closure_params.insert(sig.0, params.to_vec());
13    }
14
15    /// The recorded parameter `WasmType`s for a closure signature id, or `None`
16    /// when the closure wasn't recorded.
17    pub fn closure_params(&self, sig: crate::ast::ClosureSigId) -> Option<&[crate::ast::WasmType]> {
18        self.closure_params.get(&sig.0).map(Vec::as_slice)
19    }
20
21    /// The recorded first parameter `WasmType` for a closure signature id, or
22    /// `None` when the closure takes no parameters or wasn't recorded.
23    pub fn closure_param(&self, sig: crate::ast::ClosureSigId) -> Option<crate::ast::WasmType> {
24        self.closure_params(sig)?.first().copied()
25    }
26
27    /// Records a value-position lambda's source for the closure value in `place`.
28    pub fn record_closure_body(
29        &mut self,
30        place: crate::ast::Place,
31        params: crate::ast::LambdaParams,
32        body: Expr,
33    ) {
34        self.closure_bodies.insert(place, (params, body));
35    }
36
37    /// The source of the closure value held in `place`, if one was recorded.
38    #[must_use]
39    pub fn closure_body(
40        &self,
41        place: crate::ast::Place,
42    ) -> Option<&(crate::ast::LambdaParams, Expr)> {
43        self.closure_bodies.get(&place)
44    }
45
46    /// Drops every source recorded under a local index: a function body or a
47    /// reset local pool reuses the indices.
48    pub(crate) fn forget_local_closure_bodies(&mut self) {
49        self.closure_bodies
50            .retain(|place, _| !matches!(place, crate::ast::Place::Local(_)));
51    }
52
53    /// Starts a compilation unit with no closure records. Local and global
54    /// places and closure signature ids all restart with each unit's context,
55    /// so a record outliving its unit (a failed one included) would be read
56    /// under a reused key.
57    pub(crate) fn forget_unit_closures(&mut self) {
58        self.closure_bodies.clear();
59        self.closure_results.clear();
60        self.closure_params.clear();
61    }
62
63    /// Drops the recorded source of `place`: a `setf` overwrote the closure, so
64    /// the source no longer describes the value now there.
65    pub fn forget_closure_body(&mut self, place: crate::ast::Place) {
66        self.closure_bodies.remove(&place);
67    }
68}