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
//! PERSIST S6 rows with their Common Lisp values: a global that holds a
6
//! closure carries it to the next unit.
7

            
8
#[path = "persist/rows.rs"]
9
mod persist;
10

            
11
use persist::{Row, assert_pinned, row};
12

            
13
const K: &str = "(defvar *k* (lambda (x) (* x 2)))";
14
const F: &str = "(defvar *f* (let ((r 1/10)) (lambda (x) (* x r))))";
15
const LOOP: &str = "(defun lp (n) (lp (+ n 1)))";
16

            
17
1
fn rows() -> Vec<Row> {
18
1
    vec![
19
1
        row("P1", &[K, "(funcall *k* 3)"]),
20
1
        row("P2", &[F, "(funcall *f* 50/1)"]),
21
1
        row("P3", &[K, "(defun twice (v) (funcall *k* v))", "(twice 4)"]),
22
1
        row("P4", &[K, "(mapcar *k* (list 1 2 3))"]),
23
1
        row(
24
1
            "P5",
25
1
            &[K, "(setf *k* (lambda (x) (+ x 1)))", "(funcall *k* 3)"],
26
1
        ),
27
1
        row(
28
1
            "P6",
29
1
            &[
30
1
                K,
31
1
                LOOP,
32
1
                "(setf *k* (lambda (x) (+ x 1))) (lp (entity-count))",
33
1
                "(funcall *k* 3)",
34
1
            ],
35
1
        ),
36
1
        row(
37
1
            "P7",
38
1
            &[
39
1
                "(defvar *c* (let ((n 0)) (lambda () (setf n (+ n 1)))))",
40
1
                "(funcall *c*)",
41
1
            ],
42
1
        ),
43
1
        row(
44
1
            "P8",
45
1
            &[
46
1
                "(defvar *fs* (list (lambda (x) x)))",
47
1
                "(funcall (car *fs*) 4)",
48
1
            ],
49
1
        ),
50
1
        row(
51
1
            "P9",
52
1
            &[
53
1
                "(defvar *rate* 1/10)",
54
1
                "(defvar *g* (lambda (x) (* x *rate*)))",
55
1
                "(setf *rate* 1/5)",
56
1
                "(funcall *g* 10/1)",
57
1
            ],
58
1
        ),
59
1
        row("P10", &[K, "(funcall *k* 3)", "(funcall *k* 4)"]),
60
1
        row(
61
1
            "P11",
62
1
            &[
63
1
                "(defun adder (n) (lambda (x) (+ x n)))",
64
1
                "(defvar *a5* (adder 5))",
65
1
                "(funcall *a5* 1)",
66
1
            ],
67
1
        ),
68
1
        row(
69
1
            "P12",
70
1
            &[
71
1
                "(defvar *g* (let ((f (lambda (x) (* x 2)))) (lambda (y) (funcall f y))))",
72
1
                "(funcall *g* 3)",
73
1
            ],
74
1
        ),
75
1
        row(
76
1
            "P13",
77
1
            &[
78
1
                "(defvar *l* (let ((xs (list 1 2 3))) (lambda () (length xs))))",
79
1
                "(funcall *l*)",
80
1
            ],
81
1
        ),
82
1
        row(
83
1
            "P14",
84
1
            &[
85
1
                "(defvar *s* (let ((s \"hi\")) (lambda () s)))",
86
1
                "(funcall *s*)",
87
1
            ],
88
1
        ),
89
1
        row(
90
1
            "P15",
91
1
            &[
92
1
                "(defvar *m* (let ((n 0)) (setf n 5) (lambda () n)))",
93
1
                "(funcall *m*)",
94
1
            ],
95
1
        ),
96
1
        row(
97
1
            "P16",
98
1
            &[K, "(defvar *k* (lambda (x) (* x 3)))", "(funcall *k* 3)"],
99
1
        ),
100
1
        row(
101
1
            "P17",
102
1
            &[
103
1
                "(defvar *i* (let ((b (> (entity-count) -1))) (lambda () b)))",
104
1
                "(funcall *i*)",
105
1
            ],
106
1
        ),
107
1
        row(
108
1
            "P18",
109
1
            &[
110
1
                "(defvar *fs* (let ((r 2)) (list (lambda (x) (* x r)))))",
111
1
                "(funcall (car *fs*) 4)",
112
1
            ],
113
1
        ),
114
1
        row(
115
1
            "P19",
116
1
            &[
117
1
                "(defvar *g* (let ((f (if (> (entity-count) -1) (lambda (x) 1) (lambda (x) 2)))) (lambda (y) (funcall f y))))",
118
1
                "(funcall *g* 3)",
119
1
            ],
120
1
        ),
121
1
        row(
122
1
            "P20",
123
1
            &["(let ((r 2)) (funcall (car (list (lambda (x) (* x r)))) 3))"],
124
1
        ),
125
1
        row(
126
1
            "P21",
127
1
            &[
128
1
                "(defvar *g* (let ((f1 (let ((f0 (let ((n 5)) (lambda () n)))) (lambda () (funcall f0))))) (lambda () (funcall f1))))",
129
1
                "(funcall *g*)",
130
1
            ],
131
1
        ),
132
1
        row(
133
1
            "P22",
134
1
            &[
135
1
                "(defvar *fs* (let ((x 100)) (list (lambda (x) (* x 2)))))",
136
1
                "(funcall (car *fs*) 3)",
137
1
            ],
138
1
        ),
139
    ]
140
1
}
141

            
142
/// What each row prints; CL is the closure's value in every row but the ones
143
/// that stay loud: P7 (CL 1, a counter the closure shares) and P15 (CL 5).
144
/// P19 is a closure over a closure only known at run time.
145
const TODAY: &[(&str, &[&str])] = &[
146
    ("P1", &["-", "6"]),
147
    ("P2", &["-", "5/1"]),
148
    ("P3", &["-", "twice", "8"]),
149
    ("P4", &["-", "(2 4 6)"]),
150
    ("P5", &["-", "#<closure:1>", "4"]),
151
    (
152
        "P6",
153
        &[
154
            "-",
155
            "lp",
156
            "err: execution trapped: error while executing at ",
157
            "4",
158
        ],
159
    ),
160
    ("P7", &["-", LOST_C]),
161
    ("P8", &["-", "4"]),
162
    ("P9", &["-", "-", "1/5", "2/1"]),
163
    ("P10", &["-", "6", "8"]),
164
    ("P11", &["adder", "-", "6"]),
165
    ("P12", &["-", "6"]),
166
    ("P13", &["-", "3"]),
167
    ("P14", &["-", "\"hi\""]),
168
    (
169
        "P15",
170
        &["-", "err: Compile error: *M*: its closure value does n"],
171
    ),
172
    ("P16", &["-", "-", "6"]),
173
    ("P17", &["-", "t"]),
174
    ("P18", &["-", "8"]),
175
    ("P19", &["-", "1"]),
176
    ("P20", &["6"]),
177
    ("P21", &["-", "5"]),
178
    ("P22", &["-", "6"]),
179
];
180

            
181
const LOST_C: &str = "err: Compile error: *C*: its closure value does n";
182

            
183
#[test]
184
1
fn every_row_reads_as_common_lisp() {
185
1
    assert_pinned(&rows(), TODAY);
186
1
}