1
// Skipped under Miri: these tests compile+run wasm via wasmtime.
2
#![cfg(not(miri))]
3

            
4
//! A compile-time-only DEFVAR inside a branch that a runtime test selects is
5
//! refused: the symbol table cannot make it conditional. A SETF of a global an
6
//! earlier unit defined is a runtime cell, so it runs only when its branch is
7
//! taken. The same effects inside the runtime test itself run unconditionally,
8
//! as does a SETF of a runtime-backed local.
9

            
10
use nms::interpreter::Interpreter;
11
use scripting::nomiscript::{Fraction, Number, Value};
12

            
13
const DEFVAR_REFUSED: &str = "DEFVAR: a definition must be statically executable";
14

            
15
4
fn eval_error(interp: &mut Interpreter, src: &str) -> String {
16
4
    match interp.eval(src) {
17
        Ok(values) => panic!("expected {src:?} to be refused, got {values:?}"),
18
4
        Err(e) => e.to_string(),
19
    }
20
4
}
21

            
22
46
fn eval_last(interp: &mut Interpreter, src: &str) -> Value {
23
46
    interp
24
46
        .eval(src)
25
        .unwrap_or_else(|e| panic!("eval {src:?}: {e}"))
26
        .into_iter()
27
        .next_back()
28
        .unwrap_or_else(|| panic!("eval {src:?} produced no value"))
29
46
}
30

            
31
17
fn n(v: i64) -> Value {
32
17
    Value::Number(Number::Index(v))
33
17
}
34

            
35
/// Runs `form` after `(defvar *x* 1)` and asserts `*x*` is `after`.
36
12
fn assert_setf_conditional(form: &str, after: i64) {
37
12
    let mut interp = Interpreter::new(false).unwrap();
38
12
    eval_last(&mut interp, "(defvar *x* 1) *x*");
39
12
    eval_last(&mut interp, form);
40
12
    assert_eq!(eval_last(&mut interp, "*x*"), n(after), "{form}");
41
12
}
42

            
43
4
fn assert_defvar_refused(form: &str, name: &str) {
44
4
    let mut interp = Interpreter::new(false).unwrap();
45
4
    let err = eval_error(&mut interp, form);
46
4
    assert!(err.contains(DEFVAR_REFUSED), "{form}: {err}");
47
4
    assert!(interp.eval(name).is_err(), "{name} must stay unbound");
48
4
}
49

            
50
#[test]
51
1
fn cond_deferred_nil_arm_setf_is_conditional() {
52
1
    assert_setf_conditional(
53
1
        "(block b (cond ((= (entity-count) 9) (setf *x* 42) nil) (t 1)))",
54
1
        1,
55
1
    );
56
1
}
57

            
58
#[test]
59
1
fn cond_deferred_literal_arm_setf_is_conditional() {
60
1
    assert_setf_conditional(
61
1
        "(block b (cond ((= (entity-count) 9) (setf *x* 42) 5) (t 1)))",
62
1
        1,
63
1
    );
64
1
}
65

            
66
#[test]
67
1
fn cond_constant_list_arm_setf_is_conditional() {
68
1
    assert_setf_conditional(
69
1
        "(block b (cond ((= (entity-count) 9) (setf *x* 42) '(1 2)) (t nil)))",
70
1
        1,
71
1
    );
72
1
}
73

            
74
#[test]
75
1
fn cond_tail_arm_setf_is_conditional() {
76
1
    assert_setf_conditional(
77
1
        "(block b (cond ((= (entity-count) 9) 1) (t (setf *x* 42) 2)))",
78
1
        42,
79
1
    );
80
1
}
81

            
82
#[test]
83
1
fn cond_later_clause_body_setf_is_conditional() {
84
1
    assert_setf_conditional(
85
1
        "(block b (cond ((= (entity-count) 9) 1) ((= (entity-count) 8) (setf *x* 42) nil) (t 3)))",
86
1
        1,
87
1
    );
88
1
}
89

            
90
#[test]
91
1
fn cond_later_clause_test_setf_is_conditional() {
92
1
    assert_setf_conditional(
93
1
        "(block b (cond ((= (entity-count) 9) 1) ((progn (setf *x* 42) (= (entity-count) 8)) 2) (t 3)))",
94
1
        42,
95
1
    );
96
1
}
97

            
98
#[test]
99
1
fn cond_tail_arm_defvar_is_refused_in_a_repl_session() {
100
1
    assert_defvar_refused(
101
1
        "(cond ((= (entity-count) 5) 1) (t (defvar *z* 7) nil))",
102
1
        "*z*",
103
1
    );
104
1
}
105

            
106
#[test]
107
1
fn cond_first_clause_body_defvar_is_refused() {
108
1
    assert_defvar_refused(
109
1
        "(cond ((= (entity-count) 5) (defvar *z* 7) nil) (t 1))",
110
1
        "*z*",
111
1
    );
112
1
}
113

            
114
#[test]
115
1
fn if_then_arm_setf_in_effect_position_is_conditional() {
116
1
    assert_setf_conditional("(if (= (entity-count) 5) (setf *x* 42) 0) 1", 1);
117
1
}
118

            
119
#[test]
120
1
fn if_then_arm_setf_in_stack_position_is_conditional() {
121
1
    assert_setf_conditional("(block b (if (= (entity-count) 5) (setf *x* 42) 0))", 1);
122
1
}
123

            
124
#[test]
125
1
fn if_else_arm_setf_is_conditional() {
126
1
    assert_setf_conditional("(block b (if (= (entity-count) 0) 1 (setf *x* 42)))", 1);
127
1
}
128

            
129
#[test]
130
1
fn if_arm_setf_in_a_let_initializer_is_conditional() {
131
1
    assert_setf_conditional("(let ((v (if (= (entity-count) 5) (setf *x* 42) 0))) v)", 1);
132
1
}
133

            
134
#[test]
135
1
fn when_body_setf_is_conditional() {
136
1
    assert_setf_conditional("(when (= (entity-count) 5) (setf *x* 42)) 1", 1);
137
1
}
138

            
139
#[test]
140
1
fn if_arm_defvar_is_refused() {
141
1
    assert_defvar_refused(
142
1
        "(if (= (entity-count) 5) (progn (defvar *w* 5) nil) 3)",
143
1
        "*w*",
144
1
    );
145
1
}
146

            
147
#[test]
148
1
fn a_defvar_in_the_first_cond_test_still_applies() {
149
1
    let mut interp = Interpreter::new(false).unwrap();
150
1
    eval_last(
151
1
        &mut interp,
152
1
        "(cond ((progn (defvar *q* 3) (= (entity-count) 5)) 1) (t 2))",
153
1
    );
154
1
    assert_eq!(eval_last(&mut interp, "*q*"), n(3));
155
1
}
156

            
157
#[test]
158
1
fn a_setf_in_the_first_cond_test_still_applies() {
159
1
    let mut interp = Interpreter::new(false).unwrap();
160
1
    eval_last(&mut interp, "(defvar *x* 1) *x*");
161
1
    eval_last(
162
1
        &mut interp,
163
1
        "(block b (cond ((progn (setf *x* 9) (= (entity-count) 5)) 1) (t 2)))",
164
1
    );
165
1
    assert_eq!(eval_last(&mut interp, "*x*"), n(9));
166
1
}
167

            
168
#[test]
169
1
fn a_defvar_in_the_if_test_still_applies() {
170
1
    let mut interp = Interpreter::new(false).unwrap();
171
1
    eval_last(
172
1
        &mut interp,
173
1
        "(if (progn (defvar *q* 4) (= (entity-count) 5)) 1 2)",
174
1
    );
175
1
    assert_eq!(eval_last(&mut interp, "*q*"), n(4));
176
1
}
177

            
178
#[test]
179
1
fn a_runtime_backed_setf_in_a_branch_still_runs() {
180
1
    let mut interp = Interpreter::new(false).unwrap();
181
1
    let value = eval_last(
182
1
        &mut interp,
183
1
        "(let ((s 0)) (if (= (entity-count) 0) (setf s 5) 0) s)",
184
1
    );
185
1
    assert_eq!(value, n(5));
186
1
}
187

            
188
#[test]
189
1
fn integer_literal_cond_arms_adopt_a_sibling_scalar() {
190
1
    let mut interp = Interpreter::new(false).unwrap();
191
1
    let value = eval_last(
192
1
        &mut interp,
193
1
        "(let ((v (cond ((= (entity-count) 5) 1/2) ((= (entity-count) 0) 1) (t 2)))) (+ v 1/2))",
194
1
    );
195
1
    assert_eq!(value, Value::Number(Number::Scalar(Fraction::new(3, 2))));
196
1
}
197

            
198
const HANDLER_BODY: &str = "(if (= (entity-count) 0) (error 'boom \"x\") 5)";
199

            
200
#[test]
201
1
fn handler_clause_setf_runs_when_taken() {
202
1
    assert_setf_conditional(
203
1
        &format!("(block b (handler-case {HANDLER_BODY} (t (e) (setf *x* 42) 2)))"),
204
1
        42,
205
1
    );
206
1
}
207

            
208
#[test]
209
1
fn handler_clause_defvar_is_refused() {
210
1
    assert_defvar_refused(
211
1
        &format!("(block b (handler-case {HANDLER_BODY} (boom (e) (defvar *h* 3) 2)))"),
212
1
        "*h*",
213
1
    );
214
1
}
215

            
216
#[test]
217
1
fn a_runtime_backed_setf_in_a_handler_clause_still_runs() {
218
1
    let mut interp = Interpreter::new(false).unwrap();
219
1
    let value = eval_last(
220
1
        &mut interp,
221
1
        &format!("(let ((s 0)) (handler-case {HANDLER_BODY} (t (e) (setf s 7) 2)) s)"),
222
1
    );
223
1
    assert_eq!(value, n(7));
224
1
}