1
//! PERSIST S5: a unit that traps while a special is dynamically rebound leaves
2
//! the special as it was outside the binding, as Common Lisp unwinds it.
3

            
4
use super::*;
5
use crate::ctx::ScriptLimits;
6

            
7
const SPIN: &str = "(do ((i 0 (+ i 1))) ((> i 1073741823) i))";
8
const FUEL: &str = "fuel exhausted before completion";
9

            
10
13
async fn after_trap(units: &[&str]) -> Vec<String> {
11
13
    let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
12
13
        fuel: 100_000,
13
13
        ..ScriptLimits::default()
14
13
    });
15
13
    let mut session = Session::new(ctx).expect("Session::new");
16
13
    let mut outcomes = Vec::new();
17
13
    for source in units {
18
42
        outcomes.push(match session.handle_request(source).await.payload {
19
15
            ResponsePayload::Error { message, .. } => message,
20
27
            ResponsePayload::Value(value) => format!("{value:?}"),
21
        });
22
    }
23
13
    outcomes
24
13
}
25

            
26
8
fn ratio(numer: i64, denom: i64) -> String {
27
8
    format!("Number(Scalar(Ratio {{ numer: {numer}, denom: {denom} }}))")
28
8
}
29

            
30
3
fn index(n: i64) -> String {
31
3
    format!("Number(Index({n}))")
32
3
}
33

            
34
#[tokio::test]
35
1
async fn a_trap_inside_a_let_leaves_the_outer_value() {
36
1
    let got = after_trap(&[
37
1
        "(defvar *rate* 1/10)",
38
1
        &format!("(let ((*rate* 1/2)) {SPIN})"),
39
1
        "*rate*",
40
1
    ])
41
1
    .await;
42
1
    assert_eq!(got[1..], [FUEL.to_string(), ratio(1, 10)]);
43
1
}
44

            
45
#[tokio::test]
46
1
async fn a_trap_inside_nested_lets_leaves_the_outermost_value() {
47
1
    let got = after_trap(&[
48
1
        "(defvar *rate* 1/10)",
49
1
        &format!("(let ((*rate* 1/2)) (let ((*rate* 1/3)) {SPIN}))"),
50
1
        "*rate*",
51
1
    ])
52
1
    .await;
53
1
    assert_eq!(got[1..], [FUEL.to_string(), ratio(1, 10)]);
54
1
}
55

            
56
#[tokio::test]
57
1
async fn a_trap_inside_a_defun_with_a_special_parameter_leaves_the_outer_value() {
58
1
    let got = after_trap(&[
59
1
        "(defvar *rate* 1/10)",
60
1
        &format!("(defun slow (*rate*) {SPIN})"),
61
1
        "(slow 1/2)",
62
1
        "*rate*",
63
1
    ])
64
1
    .await;
65
1
    assert_eq!(got[2..], [FUEL.to_string(), ratio(1, 10)]);
66
1
}
67

            
68
#[tokio::test]
69
1
async fn a_trap_after_the_let_returned_keeps_the_stored_value() {
70
1
    let got = after_trap(&[
71
1
        "(defvar *rate* 1/10)",
72
1
        &format!("(progn (setf *rate* 3/10) (let ((*rate* 1/2)) *rate*) {SPIN})"),
73
1
        "*rate*",
74
1
    ])
75
1
    .await;
76
1
    assert_eq!(got[1..], [FUEL.to_string(), ratio(3, 10)]);
77
1
}
78

            
79
#[tokio::test]
80
1
async fn a_trap_inside_a_let_of_an_unbound_special_leaves_it_unbound() {
81
1
    let got = after_trap(&[
82
1
        "(defvar *rate*)",
83
1
        &format!("(let ((*rate* 1/2)) {SPIN})"),
84
1
        "*rate*",
85
1
    ])
86
1
    .await;
87
1
    assert_eq!(
88
1
        got[1..],
89
1
        [FUEL, "Compile error: symbol '*RATE*' has no value"]
90
1
    );
91
1
}
92

            
93
#[tokio::test]
94
1
async fn a_setf_inside_the_let_before_the_trap_does_not_reach_the_outer_value() {
95
1
    let got = after_trap(&[
96
1
        "(defvar *rate* 1/10)",
97
1
        &format!("(let ((*rate* 1/2)) (setf *rate* 9/10) {SPIN})"),
98
1
        "*rate*",
99
1
    ])
100
1
    .await;
101
1
    assert_eq!(got[1..], [FUEL.to_string(), ratio(1, 10)]);
102
1
}
103

            
104
#[tokio::test]
105
1
async fn a_setf_outside_any_binding_before_the_trap_is_kept() {
106
1
    let got = after_trap(&[
107
1
        "(defvar *rate* 1/10)",
108
1
        &format!("(progn (let ((*rate* 1/2)) *rate*) (setf *rate* 3/10) {SPIN})"),
109
1
        "*rate*",
110
1
    ])
111
1
    .await;
112
1
    assert_eq!(got[1..], [FUEL.to_string(), ratio(3, 10)]);
113
1
}
114

            
115
#[tokio::test]
116
1
async fn a_declaration_in_a_unit_that_trapped_in_a_binding_stays_special() {
117
1
    let got = after_trap(&[
118
1
        &format!("(progn (defvar *u*) (let ((*u* 1/2)) {SPIN}))"),
119
1
        "(setf *u* 5)",
120
1
        "(defun h () *u*)",
121
1
        "(let ((*u* 2/3)) (h))",
122
1
    ])
123
1
    .await;
124
1
    assert_eq!(
125
1
        got,
126
1
        [FUEL.to_string(), index(5), "Nil".to_string(), ratio(2, 3)]
127
1
    );
128
1
}
129

            
130
#[tokio::test]
131
1
async fn a_defvar_unit_that_trapped_in_a_binding_leaves_it_unbound() {
132
1
    let got = after_trap(&[
133
1
        &format!("(progn (defvar *u*) (let ((*u* 1/2)) {SPIN}))"),
134
1
        "*u*",
135
1
    ])
136
1
    .await;
137
1
    assert_eq!(got, [FUEL, "Compile error: symbol '*U*' has no value"]);
138
1
}
139

            
140
#[tokio::test]
141
1
async fn a_defvar_that_no_store_reached_still_declares_the_special() {
142
1
    let got = after_trap(&[
143
1
        "(progn (defvar *u*) (when (> (do ((i 0 (+ i 1))) ((> i 3) i)) 99) (setf *u* 1)))",
144
1
        "(setf *u* 5)",
145
1
        "(defun h () *u*)",
146
1
        "(let ((*u* 2/3)) (h))",
147
1
    ])
148
1
    .await;
149
1
    assert_eq!(got[3..], [ratio(2, 3)]);
150
1
}
151

            
152
#[tokio::test]
153
1
async fn a_defvar_skipped_by_an_error_is_put_back() {
154
1
    let got = after_trap(&[
155
1
        "(progn (error 'boom \"m\") (defvar *u*) (setf *u* 5))",
156
1
        "*u*",
157
1
    ])
158
1
    .await;
159
1
    assert_eq!(got[1..], ["Undefined symbol: *U*"]);
160
1
}
161

            
162
#[tokio::test]
163
1
async fn a_defvar_skipped_by_an_error_leaves_a_stored_global_lexical() {
164
1
    let got = after_trap(&[
165
1
        "(setf *u* 5)",
166
1
        "(progn (error 'boom \"m\") (defvar *u* 1))",
167
1
        "(defun h () *u*)",
168
1
        "(let ((*u* 2/3)) (h))",
169
1
    ])
170
1
    .await;
171
1
    assert_eq!(got[3..], [index(5)]);
172
1
}
173

            
174
#[tokio::test]
175
1
async fn a_defvar_skipped_by_a_trap_leaves_a_stored_global_lexical() {
176
1
    let got = after_trap(&[
177
1
        "(setf *u* 5)",
178
1
        &format!("(progn {SPIN} (defvar *u* 1) 0)"),
179
1
        "(defun h () *u*)",
180
1
        "(let ((*u* 2/3)) (h))",
181
1
    ])
182
1
    .await;
183
1
    assert_eq!(got[1..2], [FUEL]);
184
1
    assert_eq!(got[3..], [index(5)]);
185
1
}