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
//! A BLOCK whose `return-from` sits inside a loop is never folded to its
6
//! fall-through value: wherever the block is an operand, the exit's value
7
//! arrives at runtime.
8

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

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

            
23
17
fn n(v: i64) -> Value {
24
17
    Value::Number(Number::Index(v))
25
17
}
26

            
27
const EXITS_AT_SIX: &str = "(block c (dolist (y (list 5 6)) (when (= y 6) (return-from c y))) 0)";
28

            
29
#[test]
30
1
fn a_loop_exit_reaches_every_operand_position() {
31
1
    for (template, expected) in [
32
1
        ("(let ((s 0)) (setf s B) s)", 6),
33
1
        ("(let ((s 0)) (setf s B))", 6),
34
1
        ("(let ((s B)) s)", 6),
35
1
        ("(+ 1 B)", 7),
36
1
        ("(defvar *s* 0) (setf *s* B) *s*", 6),
37
1
        ("B", 6),
38
1
    ] {
39
6
        let src = template.replace('B', EXITS_AT_SIX);
40
6
        assert_eq!(eval_last(&src), n(expected), "{src}");
41
    }
42
1
}
43

            
44
#[test]
45
1
fn a_loop_exit_over_a_runtime_list_is_not_folded() {
46
1
    assert_eq!(
47
1
        eval_last(
48
1
            "(let ((s 0)) (setf s (block c (dolist (y (list 5 (entity-count))) (when (= y 0) (return-from c 6))) 0)) s)"
49
1
        ),
50
1
        n(6)
51
    );
52
1
}
53

            
54
#[test]
55
1
fn a_loop_exit_decides_a_branch_test() {
56
1
    assert_eq!(
57
1
        eval_last(
58
1
            "(if (block c (dolist (y (list 5 6)) (when (= y 6) (return-from c t))) nil) 1 2)"
59
1
        ),
60
1
        n(1)
61
    );
62
1
}
63

            
64
#[test]
65
1
fn an_exit_outside_a_loop_still_arrives() {
66
1
    assert_eq!(
67
1
        eval_last(
68
1
            "(let ((s 0)) (setf s (block c (when (= (entity-count) 0) (return-from c 6)) 0)) s)"
69
1
        ),
70
1
        n(6)
71
    );
72
1
}
73

            
74
#[test]
75
1
fn an_exit_through_a_macro_or_a_lambda_body_is_not_folded() {
76
1
    for src in [
77
1
        "(defmacro exit-c (v) (list (quote return-from) (quote c) v)) (+ 1 (block c (dolist (y (list 5 6)) (when (= y 6) (exit-c y))) 0))",
78
1
        "(+ 1 (block c (dolist (y (list 5 6)) (funcall (lambda () (when (= y 6) (return-from c y))))) 0))",
79
1
        "(+ 1 (block c (dolist (y (list 5 6)) (funcall (lambda (z) (when (= z 6) (return-from c z))) y)) 0))",
80
1
    ] {
81
3
        assert_eq!(eval_last(src), n(7), "{src}");
82
    }
83
1
}
84

            
85
#[test]
86
1
fn quoted_and_shadowed_exits_leave_the_block_foldable() {
87
1
    for src in [
88
1
        "(+ 1 (block c (dolist (y (list 5 6)) (quote (return-from c y))) 0))",
89
1
        "(+ 1 (block c (dolist (y (list 5 6)) (block c (when (= y 6) (return-from c y)))) 0))",
90
1
    ] {
91
2
        assert_eq!(eval_last(src), n(1), "{src}");
92
    }
93
1
}
94

            
95
/// A constant-list tail is the `$pair` chain every list is (plan 0014 Stage
96
/// 1), so the block's exit and its tail type alike.
97
#[test]
98
1
fn a_constant_list_tail_still_compiles() {
99
1
    for (src, expected) in [
100
1
        (
101
1
            "(let ((z (list 1 2))) (length (block c (dolist (y (list 5 6)) (when (= y 9) (return-from c z))) z)))",
102
1
            2,
103
1
        ),
104
1
        (
105
1
            "(length (block c (dolist (y (list 5 6)) (when (= y 9) (return-from c (list y)))) (list 1 2)))",
106
1
            2,
107
1
        ),
108
1
        (
109
1
            "(let ((z (list 1 2))) (length (block c (dolist (y (list 5 6)) (when (= y 9) (return-from c (list y)))) z)))",
110
1
            2,
111
1
        ),
112
1
    ] {
113
3
        assert_eq!(eval_last(src), n(expected), "{src}");
114
    }
115
1
}
116

            
117
/// A let-bound `(quote (1 2))` is data, the same value as `'(1 2)` (ISS-071,
118
/// ISS-125 3.1b.1), also when read at runtime past a BLOCK exit.
119
#[test]
120
1
fn a_quote_form_datum_read_at_runtime_is_data() {
121
1
    let src = "(let ((z (quote (1 2)))) (car (block c (dolist (y (list 5 6)) (when (= y 9) (return-from c z))) z)))";
122
1
    let results = Interpreter::new(false).unwrap().eval(src).expect(src);
123
1
    assert_eq!(results, vec![Value::Number(Number::Index(1))]);
124
1
}