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
//! The syntactic scan that decides whether a BLOCK may still fold: a macro is
6
//! never judged in the wrong environment, an exit that cannot run does not
7
//! stop the fold, and a constant-list tail never folds away an effect.
8

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

            
12
12
fn eval(src: &str) -> Result<Value, String> {
13
12
    let mut interp = Interpreter::new(false).map_err(|e| e.to_string())?;
14
12
    interp
15
12
        .eval(src)
16
1
        .map_err(|e| e.to_string())?
17
        .into_iter()
18
        .next_back()
19
        .ok_or_else(|| format!("{src:?} produced no value"))
20
12
}
21

            
22
4
fn index(n: i64) -> Value {
23
4
    Value::Number(Number::Index(n))
24
4
}
25

            
26
7
fn ratio(numer: i64, denom: i64) -> Value {
27
7
    Value::Number(Number::Scalar(Fraction::new(numer, denom)))
28
7
}
29

            
30
/// The macro reads `flag`'s init, nil, at expansion time, as in CL; the
31
/// enclosing LET's dynamic binding exists only at run time.
32
#[test]
33
1
fn a_macro_reads_the_init_of_a_dynamically_bound_special() {
34
1
    let macros = "(defvar flag nil) (defmacro ex () (if flag '(return-from b 7) 0))";
35
1
    let block = "(block b (let ((flag t)) (dolist (i (list 1)) (ex))) 0)";
36
1
    assert_eq!(eval(&format!("{macros} (+ 1 {block})")), Ok(index(1)));
37
1
    assert_eq!(eval(&format!("{macros} {block}")), Ok(index(0)));
38
1
}
39

            
40
/// A macro whose expansion cannot depend on the environment is still seen
41
/// through, so a block using one keeps folding.
42
#[test]
43
1
fn a_template_macro_without_an_exit_still_folds() {
44
1
    for src in [
45
1
        "(+ 1/2 (block b (when nil (return-from b 7/1)) 1/1))",
46
1
        "(+ 1/2 (block b (unless t (return-from b 7/1)) 1/1))",
47
1
        "(defmacro m () 1/1) (+ 1/2 (block b (m) 1/1))",
48
1
    ] {
49
3
        assert_eq!(eval(src), Ok(ratio(3, 2)), "{src}");
50
    }
51
1
}
52

            
53
#[test]
54
1
fn an_exit_that_cannot_run_keeps_the_tail_a_literal() {
55
1
    for src in [
56
1
        "(+ 1/2 (block b (if nil (return-from b 7/1)) 1/1))",
57
1
        "(+ 1/2 (block b (if t 1/1 (return-from b 7/1)) 1/1))",
58
1
        "(+ 1/2 (block b (lambda () (return-from b 7/1)) 1/1))",
59
1
        "(+ 1/2 (block b (let () (lambda () (return-from b 7/1)) 1/1)))",
60
1
    ] {
61
4
        assert_eq!(eval(src), Ok(ratio(3, 2)), "{src}");
62
    }
63
1
}
64

            
65
#[test]
66
1
fn a_called_lambda_exit_still_arrives() {
67
1
    assert_eq!(
68
1
        eval("(+ 1 (block b (funcall (lambda () (return-from b 5))) 0))"),
69
1
        Ok(index(6))
70
    );
71
1
}
72

            
73
/// A constant-list tail is a `$pair` chain like any list (plan 0014 Stage
74
/// 1), so the block's exit types it and the body's store runs. An exit whose
75
/// value is a raised condition is still refused: the condition's exit is
76
/// typed as a value (ISS-030, Stage 3).
77
#[test]
78
1
fn a_constant_list_tail_never_folds_away_an_effect() {
79
1
    assert_eq!(
80
1
        eval(
81
1
            "(defvar x (entity-count)) \
82
1
             (length (block b (dolist (i (list 1)) (setf x 7) (return-from b (list 9))) (list 1 2))) \
83
1
             x"
84
1
        ),
85
1
        Ok(index(7))
86
    );
87
    let src =
88
1
        "(length (block b (dolist (i (list 1)) (return-from b (error 'boom \"bad\"))) (list 1 2)))";
89
1
    assert!(eval(src).is_err(), "{src}: {:?}", eval(src));
90
1
}