1
//! Snapshot/restore of `CompileContext`'s wasm-section + counter state.
2
//!
3
//! Used by `recompile`: a discarded pass rolls every wasm-side mutation it
4
//! made back to the state captured when the scope opened.
5
//!
6
//! What participates:
7
//! - `types` / `functions` / `data` sections + their counters and
8
//!   name-lookup maps (`type_count` / `local_func_count` / `data_count`,
9
//!   `type_cache` / `func_names`).
10
//! - `closures` registry — env-struct + closure-sig + helper-name
11
//!   counters.
12
//! - `declared_funcs` — funcref declarations queued for the elements
13
//!   section.
14
//! - `pending_helpers` length — bodies queued during the trial emit are
15
//!   discarded by truncation.
16
//! - `local_types` length + `next_local` counter + `serializer` cursor
17
//!   — the local-pool state local-to the trial body.
18
//! - the BLOCK / TAGBODY / UNWIND-PROTECT frame stacks, including the exit
19
//!   types a trial recorded into an enclosing BLOCK.
20
//! - the BLOCK joins a trial recorded or consumed (`block_joins.rs`); the
21
//!   open BLOCK probe does not participate.
22
//!
23
//! What does *not* participate:
24
//! - `imports`, `exports`, `memories`, `codes` — set during bootstrap or
25
//!   on the `add_should_apply` / `add_process` drain, never during body
26
//!   emit.
27
//! - `host_fns` — registered once at construction time.
28
//! - `inlining_stack` / `inlining_set` — push/pop is already balanced
29
//!   around the trial emit.
30
//! - the budget — a discarded scope's work stays charged.
31

            
32
use std::collections::HashMap;
33

            
34
use wasm_encoder::{DataSection, FunctionSection, TypeSection, ValType};
35

            
36
use super::super::layout::OutputSerializer;
37
use super::block_joins::BlockJoinsMark;
38
use super::closure::ClosureRegistry;
39
use super::frames::FrameJournal;
40
use super::monomorph::MonomorphCache;
41
use super::{CompileContext, LocalSlot};
42

            
43
/// Captured snapshot returned by [`CompileContext::snapshot`]. Restored
44
/// via [`CompileContext::restore`] (consumes the snapshot).
45
pub(crate) struct ContextSnapshot {
46
    types: TypeSection,
47
    functions: FunctionSection,
48
    data: DataSection,
49
    type_count: u32,
50
    local_func_count: u32,
51
    data_count: u32,
52
    type_cache: HashMap<Vec<ValType>, HashMap<Vec<ValType>, u32>>,
53
    func_names: HashMap<String, u32>,
54
    closures: ClosureRegistry,
55
    declared_funcs: Vec<u32>,
56
    monomorphs: MonomorphCache,
57
    pending_helpers_len: usize,
58
    next_local: u32,
59
    local_types: Vec<LocalSlot>,
60
    serializer: OutputSerializer,
61
    frames: FrameJournal,
62
    block_joins: BlockJoinsMark,
63
}
64

            
65
impl CompileContext {
66
    /// Captures the wasm-side mutable state so a trial body emit can be
67
    /// rolled back. See module docs for which fields participate.
68
161154
    pub(crate) fn snapshot(&self) -> ContextSnapshot {
69
161154
        ContextSnapshot {
70
161154
            types: self.types.clone(),
71
161154
            functions: self.functions.clone(),
72
161154
            data: self.data.clone(),
73
161154
            type_count: self.type_count,
74
161154
            local_func_count: self.local_func_count,
75
161154
            data_count: self.data_count,
76
161154
            type_cache: self.type_cache.clone(),
77
161154
            func_names: self.func_names.clone(),
78
161154
            closures: self.closures.clone(),
79
161154
            declared_funcs: self.declared_funcs.clone(),
80
161154
            monomorphs: self.monomorphs.clone(),
81
161154
            pending_helpers_len: self.pending_helpers.len(),
82
161154
            next_local: self.next_local,
83
161154
            local_types: self.local_types.clone(),
84
161154
            serializer: self.serializer.clone(),
85
161154
            frames: self.frame_journal(),
86
161154
            block_joins: self.block_joins_mark(),
87
161154
        }
88
161154
    }
89

            
90
    /// Restores the wasm-side state to a captured [`ContextSnapshot`].
91
    /// Truncates `pending_helpers` to the snapshot's length so any helper
92
    /// queued by the trial emit is discarded; helpers queued *before*
93
    /// the snapshot stay in place.
94
102652
    pub(crate) fn restore(&mut self, snap: ContextSnapshot) {
95
102652
        self.types = snap.types;
96
102652
        self.functions = snap.functions;
97
102652
        self.data = snap.data;
98
102652
        self.type_count = snap.type_count;
99
102652
        self.local_func_count = snap.local_func_count;
100
102652
        self.data_count = snap.data_count;
101
102652
        self.type_cache = snap.type_cache;
102
102652
        self.func_names = snap.func_names;
103
102652
        self.closures = snap.closures;
104
102652
        self.declared_funcs = snap.declared_funcs;
105
102652
        self.monomorphs = snap.monomorphs;
106
102652
        self.pending_helpers.truncate(snap.pending_helpers_len);
107
102652
        self.next_local = snap.next_local;
108
102652
        self.local_types = snap.local_types;
109
102652
        self.serializer = snap.serializer;
110
102652
        self.restore_frames(snap.frames);
111
102652
        self.restore_block_joins(snap.block_joins);
112
102652
    }
113
}