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
//! Plan 0014 GSTORE on PERSIST: a global a `SETF` creates is a runtime cell
6
//! once stored again, lexical under binders, and carried to the next unit by
7
//! the host, with the Common Lisp value.
8

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

            
12
16
fn index(n: i64) -> Value {
13
16
    Value::Number(Number::Index(n))
14
16
}
15

            
16
1
fn number(n: i64, d: i64) -> Value {
17
1
    Value::Number(Number::Scalar(Fraction::new(n, d)))
18
1
}
19

            
20
2
fn list(items: Vec<Value>) -> Value {
21
2
    items
22
2
        .into_iter()
23
2
        .rev()
24
4
        .fold(Value::Nil, |tail, item| Pair::cons(item, tail))
25
2
}
26

            
27
13
fn last_of_each(sources: &[&str]) -> Vec<Value> {
28
13
    let mut interp = Interpreter::new(false).unwrap();
29
13
    sources
30
13
        .iter()
31
26
        .map(|source| {
32
26
            interp
33
26
                .eval(source)
34
                .unwrap_or_else(|err| panic!("{source}: {err}"))
35
                .pop()
36
26
                .unwrap_or(Value::Nil)
37
26
        })
38
        .collect()
39
13
}
40

            
41
#[test]
42
1
fn a_created_global_stored_by_a_callee_is_one_cell() {
43
1
    let shown = last_of_each(&["(setf g 1) (defun f () (setf g 2) g) (list (f) g)"]);
44
1
    assert_eq!(shown, vec![list(vec![index(2), index(2)])]);
45
1
}
46

            
47
#[test]
48
1
fn the_repl_reads_the_created_global_in_the_next_unit() {
49
1
    let shown = last_of_each(&[
50
1
        "(setf g 1) (setf g 2)",
51
1
        "g",
52
1
        "(defvar g 9)",
53
1
        "g",
54
1
        "(let ((g 5)) g)",
55
1
    ]);
56
1
    assert_eq!(shown[1], index(2));
57
1
    assert_eq!(shown[3], index(2));
58
1
    assert_eq!(shown[4], index(5));
59
1
}
60

            
61
#[test]
62
1
fn a_binding_that_stores_inside_the_unit_leaves_the_global_alone() {
63
1
    let shown = last_of_each(&["(setf g 1) (setf g 2) (let ((g 5)) (setf g 6)) g", "g"]);
64
1
    assert_eq!(shown, vec![index(2), index(2)]);
65
1
}
66

            
67
#[test]
68
1
fn a_closure_that_is_never_called_stores_nothing() {
69
1
    let shown = last_of_each(&["(setf g 1) (setf g 2) (lambda () (setf g 3)) 0", "g"]);
70
1
    assert_eq!(shown[1], index(2));
71
1
}
72

            
73
#[test]
74
1
fn a_later_defparameter_then_setf_wins() {
75
1
    let shown = last_of_each(&[
76
1
        "(setf g 1) (setf g 2)",
77
1
        "(defparameter g 7) (setf g 8)",
78
1
        "g",
79
1
    ]);
80
1
    assert_eq!(shown[2], index(8));
81
1
}
82

            
83
#[test]
84
1
fn a_store_that_widens_the_cell_keeps_the_value() {
85
1
    let shown = last_of_each(&["(setf g 1) (setf g 1/2)", "g"]);
86
1
    assert_eq!(shown[1], number(1, 2));
87
1
    let shown = last_of_each(&["(setf g nil) (setf g (list 0 0))", "g"]);
88
1
    assert_eq!(shown[1], list(vec![index(0), index(0)]));
89
1
    let shown = last_of_each(&["(setf g 1) (setf g \"b\")", "g"]);
90
1
    assert_eq!(shown[1], Value::String("b".to_string()));
91
1
}
92

            
93
#[test]
94
1
fn a_loop_variable_named_like_the_cell_leaves_it_alone() {
95
1
    let shown = last_of_each(&[
96
1
        "(setf g 1) (setf g 2) (do ((g 0 (+ g 1))) ((> g (entity-count)) g))",
97
1
        "g",
98
1
    ]);
99
1
    assert_eq!(shown[1], index(2));
100
1
}
101

            
102
3
fn single_unit(source: &str) -> Value {
103
3
    last_of_each(&[source]).remove(0)
104
3
}
105

            
106
#[test]
107
1
fn a_lambda_store_before_the_creating_setf_targets_the_same_cell() {
108
1
    let shown = single_unit("(defvar k (lambda () (setf g 2))) (setf g 1) (funcall k) g");
109
1
    assert_eq!(shown, index(2));
110
    let third =
111
1
        single_unit("(defvar k (lambda () (setf g 2))) (setf g 1) (setf g 3) (funcall k) g");
112
1
    assert_eq!(third, index(2));
113
1
    let defun = single_unit("(defun k () (setf g 2)) (setf g 1) (k) g");
114
1
    assert_eq!(defun, index(2));
115
1
}
116

            
117
#[test]
118
1
fn a_lambda_store_is_carried_to_the_next_unit() {
119
1
    let shown = last_of_each(&[
120
1
        "(defvar k (lambda () (setf g 2))) (setf g 1) (funcall k)",
121
1
        "g",
122
1
    ]);
123
1
    assert_eq!(shown[1], index(2));
124
1
}