1
//! Rollback is asserted on the whole `SymbolTable` and `CompileContext` state
2
//! but the budget, so a mutation to a field nobody journalled still fails
3
//! these tests.
4

            
5
use super::{Trial, recompile};
6
use crate::ast::{Expr, WasmType};
7
use crate::compiler::context::{CompileContext, TagFrame};
8
use crate::compiler::join::{Arm, Exit};
9
use crate::error::{Error, Result};
10
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
11

            
12
6
fn setup() -> (CompileContext, SymbolTable) {
13
6
    let ctx = CompileContext::new_with_host_fns(&[]).expect("context");
14
6
    let symbols = SymbolTable::with_builtins_for_wasm();
15
6
    (ctx, symbols)
16
6
}
17

            
18
/// Touches every journalled piece of state, plus the budget.
19
5
fn mutate(ctx: &mut CompileContext, symbols: &mut SymbolTable) -> Result<()> {
20
5
    ctx.alloc_local(WasmType::I32)?;
21
5
    ctx.add_data(b"trial")?;
22
5
    symbols.define(Symbol::new("TRIAL-ONLY", SymbolKind::Variable).with_value(Expr::T));
23
5
    symbols.define(Symbol::new("+", SymbolKind::Variable).with_value(Expr::Nil));
24
5
    symbols
25
5
        .define_struct("TRIAL-STRUCT", vec!["A".to_string()])
26
5
        .expect("TRIAL-STRUCT defines");
27
5
    symbols.register_test("TRIAL-TEST", Expr::Nil);
28
5
    symbols.mark_native_referenced("TRIAL-NATIVE");
29
5
    symbols.charge_work(1000)
30
5
}
31

            
32
15
fn digests(ctx: &CompileContext, symbols: &SymbolTable) -> (String, String) {
33
15
    let digest = symbols.state_digest();
34
15
    let symbols: Vec<&str> = digest
35
15
        .lines()
36
465
        .filter(|line| !line.starts_with("charged:"))
37
        .collect();
38
15
    (ctx.state_digest(), symbols.join("\n"))
39
15
}
40

            
41
#[test]
42
1
fn a_discarded_trial_restores_the_whole_state() {
43
1
    let (mut ctx, mut symbols) = setup();
44
1
    let before = digests(&ctx, &symbols);
45
1
    let trial = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
46
1
        mutate(ctx, symbols)?;
47
1
        assert_ne!(digests(ctx, symbols), before);
48
1
        Ok(Trial::<(), ()>::Discard(()))
49
1
    })
50
1
    .expect("trial");
51
1
    assert!(matches!(trial, Trial::Discard(())));
52
1
    assert_eq!(digests(&ctx, &symbols), before);
53
1
}
54

            
55
#[test]
56
1
fn a_failed_trial_restores_the_whole_state_and_reports_its_error() {
57
1
    let (mut ctx, mut symbols) = setup();
58
1
    let before = digests(&ctx, &symbols);
59
1
    let err = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
60
1
        mutate(ctx, symbols)?;
61
1
        Err::<Trial<(), ()>, _>(Error::Compile("trial failed".to_string()))
62
1
    })
63
1
    .expect_err("error");
64
1
    assert!(err.to_string().contains("trial failed"));
65
1
    assert_eq!(digests(&ctx, &symbols), before);
66
1
}
67

            
68
#[test]
69
1
fn a_committed_trial_keeps_its_changes() {
70
1
    let (mut ctx, mut symbols) = setup();
71
1
    let before = digests(&ctx, &symbols);
72
1
    let trial = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
73
1
        mutate(ctx, symbols)?;
74
1
        Ok(Trial::<u32, ()>::Commit(7))
75
1
    })
76
1
    .expect("trial");
77
1
    assert!(matches!(trial, Trial::Commit(7)));
78
1
    assert_ne!(digests(&ctx, &symbols), before);
79
1
    assert!(symbols.lookup("TRIAL-ONLY").is_some());
80
1
}
81

            
82
#[test]
83
1
fn an_inner_commit_is_undone_by_an_outer_discard() {
84
1
    let (mut ctx, mut symbols) = setup();
85
1
    let before = digests(&ctx, &symbols);
86
1
    recompile(&mut ctx, &mut symbols, |ctx, symbols| {
87
1
        recompile(ctx, symbols, |ctx, symbols| {
88
1
            mutate(ctx, symbols)?;
89
1
            Ok(Trial::<(), ()>::Commit(()))
90
1
        })?;
91
1
        Ok(Trial::<(), ()>::Discard(()))
92
1
    })
93
1
    .expect("nested");
94
1
    assert_eq!(digests(&ctx, &symbols), before);
95
1
}
96

            
97
#[test]
98
1
fn an_inner_discard_leaves_the_outer_trials_changes() {
99
1
    let (mut ctx, mut symbols) = setup();
100
1
    recompile(&mut ctx, &mut symbols, |ctx, symbols| {
101
1
        symbols.define(Symbol::new("OUTER", SymbolKind::Variable).with_value(Expr::T));
102
1
        let after_outer = digests(ctx, symbols);
103
1
        recompile(ctx, symbols, |ctx, symbols| {
104
1
            mutate(ctx, symbols)?;
105
1
            Ok(Trial::<(), ()>::Discard(()))
106
1
        })?;
107
1
        assert_eq!(digests(ctx, symbols), after_outer);
108
1
        Ok(Trial::<(), ()>::Commit(()))
109
1
    })
110
1
    .expect("nested");
111
1
    assert!(symbols.lookup("OUTER").is_some());
112
1
    assert!(symbols.lookup("TRIAL-ONLY").is_none());
113
1
}
114

            
115
/// Records an exit into the enclosing `OUTER` block and leaves one frame of
116
/// each kind pushed, as a trial abandoned mid-body would.
117
2
fn mutate_frames(ctx: &mut CompileContext, exit: WasmType, depth: u32) {
118
2
    ctx.record_block_exit(
119
2
        "OUTER",
120
2
        Exit {
121
2
            arm: Arm::Value(exit),
122
2
            emitted: exit,
123
2
        },
124
2
    );
125
2
    ctx.push_block_label("TRIAL-BLOCK", depth, None);
126
2
    ctx.push_unwind_frame(depth, Vec::new());
127
2
    ctx.push_tag_frame(TagFrame {
128
2
        tags: Vec::new(),
129
2
        pc_local: depth,
130
2
        loop_depth: depth,
131
2
    });
132
2
}
133

            
134
2
fn outer_exits(ctx: &CompileContext) -> Vec<Arm> {
135
2
    ctx.lookup_block_label("OUTER")
136
2
        .map(|label| label.recorded_exits.iter().map(|exit| exit.arm).collect())
137
        .unwrap_or_default()
138
2
}
139

            
140
#[test]
141
1
fn a_nested_discard_restores_the_frames() {
142
1
    let (mut ctx, mut symbols) = setup();
143
1
    ctx.push_block_label("OUTER", 0, None);
144
1
    let before = digests(&ctx, &symbols);
145
1
    recompile(&mut ctx, &mut symbols, |ctx, symbols| {
146
1
        mutate_frames(ctx, WasmType::I32, 1);
147
1
        let after_outer = digests(ctx, symbols);
148
1
        recompile(ctx, symbols, |ctx, _| {
149
1
            mutate_frames(ctx, WasmType::Ratio, 2);
150
1
            Ok(Trial::<(), ()>::Discard(()))
151
1
        })?;
152
1
        assert_eq!(outer_exits(ctx), vec![Arm::Value(WasmType::I32)]);
153
1
        assert_eq!(digests(ctx, symbols), after_outer);
154
1
        Ok(Trial::<(), ()>::Discard(()))
155
1
    })
156
1
    .expect("nested");
157
1
    assert!(outer_exits(&ctx).is_empty());
158
1
    assert_eq!(digests(&ctx, &symbols), before);
159
1
}