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
//! Which variables a loop body assigns, and the element an accumulator
6
//! store builds, both follow lexical scope: a rebinding shadows the outer
7
//! variable, quoted data assigns nothing, and disagreeing branch arms widen.
8

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

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

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

            
27
#[test]
28
1
fn a_shadowed_store_leaves_the_outer_nil_a_nil() {
29
1
    assert_eq!(
30
1
        eval_last(
31
1
            "(let ((x nil)) (dolist (i (list (entity-count))) (let ((x 0)) (setf x 5))) (if x 1 2))"
32
1
        ),
33
1
        n(2)
34
    );
35
1
}
36

            
37
#[test]
38
1
fn stores_to_a_shadowing_binding_do_not_retype_the_outer_variable() {
39
1
    for src in [
40
1
        "(let ((x nil)) (dolist (i (list (entity-count))) (let ((x 0)) (setf x 5))) (length x))",
41
1
        "(let ((x nil)) (dolist (i (list (entity-count))) (funcall (lambda (x) (setf x 5) x) 1)) (length x))",
42
1
        "(let ((x nil)) (dolist (i (list (entity-count))) (do ((x 0 (+ x 1))) ((> x 2)) (setf x (+ x 0)))) (length x))",
43
1
    ] {
44
3
        assert_eq!(eval_last(src), n(0), "{src}");
45
    }
46
1
}
47

            
48
#[test]
49
1
fn a_quoted_store_assigns_nothing() {
50
1
    assert_eq!(
51
1
        eval_last(
52
1
            "(let ((x nil)) (dolist (i (list (entity-count))) (quote (setf x 5))) (length x))"
53
1
        ),
54
1
        n(0)
55
    );
56
1
}
57

            
58
#[test]
59
1
fn a_string_accumulator_takes_its_element_from_the_car() {
60
1
    assert_eq!(
61
1
        eval_last("(let ((r nil)) (setf r (cons \"a\" r)) (length r))"),
62
1
        n(1)
63
    );
64
1
}
65

            
66
#[test]
67
1
fn disagreeing_accumulator_arms_widen_the_element() {
68
1
    assert_eq!(
69
1
        eval_last(
70
1
            "(let ((r nil)) (setf r (if (> (entity-count) 5) (cons 1 r) (list \"a\"))) (length r))"
71
1
        ),
72
1
        n(1)
73
    );
74
1
}