1
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
2
// Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
//! Runtime evaluation of HANDLER-CASE through the eval-mode (nomi-eval)
6
//! boundary wrapper: the catch actually fires, clauses run, condition
7
//! accessors read the live struct, and an unmatched raise propagates.
8
//! Codegen-level validation lives in `nomiscript/tests/codegen/handler_case.rs`.
9

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

            
13
19
fn eval_one(src: &str) -> Value {
14
19
    let mut interp = Interpreter::new(false).unwrap();
15
19
    interp
16
19
        .eval(src)
17
        .unwrap_or_else(|e| panic!("eval {src:?}: {e}"))
18
        .into_iter()
19
        .next_back()
20
        .unwrap_or_else(|| panic!("eval {src:?} produced no value"))
21
19
}
22

            
23
4
fn eval_err(src: &str) -> String {
24
4
    let mut interp = Interpreter::new(false).unwrap();
25
4
    match interp.eval(src) {
26
        Ok(v) => panic!("eval {src:?} unexpectedly succeeded: {v:?}"),
27
4
        Err(e) => e.to_string(),
28
    }
29
4
}
30

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

            
35
4
fn s(v: &str) -> Value {
36
4
    Value::String(v.to_string())
37
4
}
38

            
39
#[test]
40
1
fn basic_clause_catches_matching_raise() {
41
1
    assert_eq!(
42
1
        eval_one(r#"(handler-case (error 'oops "x") (oops (e) 7))"#),
43
1
        n(7)
44
    );
45
1
}
46

            
47
#[test]
48
1
fn no_throw_body_returns_its_value_clauses_untouched() {
49
1
    assert_eq!(eval_one("(handler-case 42 (oops (e) 7))"), n(42));
50
1
}
51

            
52
#[test]
53
1
fn first_matching_clause_wins() {
54
1
    assert_eq!(
55
1
        eval_one(r#"(handler-case (error 'two "x") (one (e) 1) (two (e) 2))"#),
56
1
        n(2),
57
    );
58
1
}
59

            
60
#[test]
61
1
fn catch_all_t_catches_unlisted_code() {
62
1
    assert_eq!(
63
1
        eval_one(r#"(handler-case (error 'whatever "x") (t (e) 99))"#),
64
1
        n(99),
65
    );
66
1
}
67

            
68
#[test]
69
1
fn unmatched_with_no_catch_all_propagates() {
70
    // The condition re-raises and escapes the handler-case; nms surfaces it
71
    // as an execution error (it carries the code in the marker chain).
72
1
    let err = eval_err(r#"(handler-case (error 'nope "x") (other (e) 1))"#);
73
1
    assert!(
74
1
        !err.is_empty(),
75
        "an unmatched handler-case must propagate the raise"
76
    );
77
1
}
78

            
79
#[test]
80
1
fn error_message_accessor_reads_the_condition() {
81
1
    assert_eq!(
82
1
        eval_one(r#"(handler-case (error 'x "the-msg") (x (e) (error-message e)))"#),
83
1
        s("the-msg"),
84
    );
85
1
}
86

            
87
#[test]
88
1
fn error_code_is_a_symbol_for_eq_and_case() {
89
1
    assert_eq!(
90
1
        eval_one(r#"(handler-case (error 'boom "m") (t (e) (eq? (error-code e) 'boom)))"#),
91
1
        Value::T,
92
    );
93
1
    assert_eq!(
94
1
        eval_one(
95
1
            r#"(handler-case (error 'boom "m") (t (e) (case (error-code e) (boom "b") (t "o"))))"#
96
1
        ),
97
1
        s("b"),
98
    );
99
1
}
100

            
101
#[test]
102
1
fn error_code_accessor_reads_the_upcased_code() {
103
    // The reader upcases the raised symbol, so the wire code is "X".
104
1
    assert_eq!(
105
1
        eval_one(r#"(handler-case (error 'x "m") (x (e) (error-code e)))"#),
106
1
        Value::Symbol("X".to_string()),
107
    );
108
1
}
109

            
110
#[test]
111
1
fn catch_all_binds_condition_for_accessors() {
112
1
    assert_eq!(
113
1
        eval_one(r#"(handler-case (error 'boom "details") (t (e) (error-message e)))"#),
114
1
        s("details"),
115
    );
116
1
}
117

            
118
#[test]
119
1
fn nested_handler_case_inner_catches_first() {
120
    // The inner handler-case catches 'inner; the outer never sees it.
121
1
    assert_eq!(
122
1
        eval_one(
123
1
            r#"(handler-case
124
1
                 (handler-case (error 'inner "x") (inner (e) 1))
125
1
                 (t (e) 2))"#
126
1
        ),
127
1
        n(1),
128
    );
129
1
}
130

            
131
#[test]
132
1
fn nested_handler_case_outer_catches_uncaught_inner() {
133
    // The inner clause doesn't match 'boom, so it re-raises; the outer t
134
    // catches it.
135
1
    assert_eq!(
136
1
        eval_one(
137
1
            r#"(handler-case
138
1
                 (handler-case (error 'boom "x") (other (e) 1))
139
1
                 (t (e) 2))"#
140
1
        ),
141
1
        n(2),
142
    );
143
1
}
144

            
145
#[test]
146
1
fn handler_case_in_effect_position_still_catches() {
147
    // In effect position (non-tail of a PROGN) the handler-case must still
148
    // emit its catch — it must not be elided as a const-folded value.
149
1
    assert_eq!(
150
1
        eval_one(r#"(progn (handler-case (error 'x "m") (x (e) 1)) 5)"#),
151
1
        n(5),
152
    );
153
1
}
154

            
155
#[test]
156
1
fn return_from_inside_matching_clause_exits_block() {
157
    // A `(return-from)` inside a matching clause body must resolve to the
158
    // enclosing BLOCK through the handler-case frames (not branch to the
159
    // wrong target). Returns 7, not the dead tail 99.
160
1
    assert_eq!(
161
1
        eval_one(r#"(block done (handler-case (error 'x "m") (x (e) (return-from done 7))) 99)"#),
162
1
        n(7),
163
    );
164
1
}
165

            
166
#[test]
167
1
fn return_from_inside_catch_all_clause_exits_block() {
168
1
    assert_eq!(
169
1
        eval_one(r#"(block done (handler-case (error 'z "m") (t (e) (return-from done 7))) 99)"#),
170
1
        n(7),
171
    );
172
1
}
173

            
174
#[test]
175
1
fn diverging_body_let_bound_takes_clause_type() {
176
    // The body always throws, so the form's runtime type comes from the
177
    // clause (StringRef), not the body's placeholder. A let binding sizes
178
    // its local from the eval-time type, so it must be the clause type or
179
    // the StringRef value mis-fits an i32 local (invalid module).
180
1
    assert_eq!(
181
1
        eval_one(r#"(let* ((x (handler-case (error 'boom "m") (boom (e) "ok")))) x)"#),
182
1
        s("ok"),
183
    );
184
1
}
185

            
186
#[test]
187
1
fn diverging_body_let_bound_ratio_clause() {
188
1
    assert_eq!(
189
1
        eval_one(r#"(let* ((x (handler-case (error 'b "m") (b (e) 11/10)))) x)"#),
190
1
        Value::Number(Number::Scalar(Fraction::new(11, 10))),
191
    );
192
1
}
193

            
194
const ALL_DIVERGING: &str = r#"(handler-case (error 'boom "m") (boom (e) (error 'bam "n")))"#;
195

            
196
#[test]
197
1
fn all_diverging_raises_the_clause_condition() {
198
1
    assert!(eval_err(ALL_DIVERGING).contains("BAM: n"));
199
1
}
200

            
201
#[test]
202
1
fn all_diverging_inside_a_defun_raises_the_clause_condition() {
203
1
    let err = eval_err(&format!("(defun f () {ALL_DIVERGING}) (f)"));
204
1
    assert!(err.contains("BAM: n"), "{err}");
205
1
}
206

            
207
#[test]
208
1
fn all_diverging_as_an_if_test_raises_the_clause_condition() {
209
1
    let err = eval_err(&format!("(if {ALL_DIVERGING} 1 2)"));
210
1
    assert!(err.contains("BAM: n"), "{err}");
211
1
}
212

            
213
#[test]
214
1
fn nested_all_diverging_inner_clause_is_caught_by_the_outer() {
215
1
    assert_eq!(
216
1
        eval_one(&format!("(handler-case {ALL_DIVERGING} (bam (e) 9))")),
217
1
        n(9),
218
    );
219
1
}
220

            
221
#[test]
222
1
fn return_from_body_and_clause_leave_the_block_with_the_body_value() {
223
1
    assert_eq!(
224
1
        eval_one(
225
1
            "(block b (progn (handler-case (return-from b 1) (boom (e) (return-from b 2))) 5))"
226
1
        ),
227
1
        n(1),
228
    );
229
1
}
230

            
231
#[test]
232
1
fn return_from_clause_leaves_the_block_after_the_body_raises() {
233
1
    assert_eq!(
234
1
        eval_one(
235
1
            r#"(block b (progn (handler-case (error 'boom "m") (boom (e) (return-from b 2))) 5))"#
236
1
        ),
237
1
        n(2),
238
    );
239
1
}