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 Q4-0 rows: closures in list cells, pinned with the SBCL value next
6
//! to each row. Q4-3 made a lambda literal in a list a real cell: X1 and R2 no
7
//! longer read the caller's `n`.
8

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

            
12
use persist::{Row, assert_pinned, assert_pinned_wide, row};
13

            
14
const DEFVAR_OF_LAMBDAS: &str = "(defvar *fs* (list (lambda (x) (+ x 1))))";
15
const LET_OF_CAPTURE: &str = "(let ((n (entity-count))) (defvar *hs* (list (lambda (x) (+ x n)))))";
16
const LOOP_OVER_LAMBDAS: &str = "(let ((r 0)) (dolist (f (list (lambda (x) 1))) \
17
     (setf r (funcall f 0))) r)";
18

            
19
const L: &str = "(let ((f (lambda (x) (+ x 1)))) (list f))";
20

            
21
1
fn rows() -> Vec<Row> {
22
1
    vec![
23
1
        row(
24
1
            "L6",
25
1
            &[&format!("{DEFVAR_OF_LAMBDAS} (funcall (car *fs*) 4)")],
26
1
        ),
27
1
        row(
28
1
            "L7",
29
1
            &[
30
1
                "(let ((n (entity-count))) (defvar *fs* (list (lambda (x) (+ x n)))) \
31
1
               (funcall (car *fs*) 4))",
32
1
            ],
33
1
        ),
34
1
        row(
35
1
            "X1",
36
1
            &[&format!(
37
1
                "{LET_OF_CAPTURE} (let ((n 100)) (funcall (car *hs*) 4))"
38
1
            )],
39
1
        ),
40
1
        row(
41
1
            "X2",
42
1
            &[&format!(
43
1
                "(defvar *n* 100) {LET_OF_CAPTURE} (funcall (car *hs*) 4)"
44
1
            )],
45
1
        ),
46
1
        row("R1", &[DEFVAR_OF_LAMBDAS, "(funcall (car *fs*) 4)"]),
47
1
        row(
48
1
            "R2",
49
1
            &[LET_OF_CAPTURE, "(let ((n 100)) (funcall (car *hs*) 4))"],
50
1
        ),
51
1
        row("L10", &["(print (list (lambda (x) x)))"]),
52
1
        row("L1", &["(let ((f (lambda (x) x))) (list f \"x\"))"]),
53
1
        row("L5", &["(let ((f (lambda () 1))) (list (funcall f) f))"]),
54
1
        row(
55
1
            "L2",
56
1
            &["(let ((f (lambda (x) (+ x 1)))) (funcall (car (list f)) 2))"],
57
1
        ),
58
1
        row(
59
1
            "L3",
60
1
            &["(let ((n (entity-count))) (let ((f (lambda (x) (+ x n)))) \
61
1
               (funcall (car (list f f)) 2)))"],
62
1
        ),
63
1
        row(
64
1
            "W7",
65
1
            &["(let ((f (lambda (x) (+ x 1)))) (dolist (g (list f f)) (print (funcall g 1))))"],
66
1
        ),
67
1
        row("B1", &[&format!("(defun f (x) 100) {LOOP_OVER_LAMBDAS}")]),
68
1
        row("B1a", &[LOOP_OVER_LAMBDAS]),
69
1
        row(
70
1
            "B6",
71
1
            &[&format!("(labels ((f (x) 100)) {LOOP_OVER_LAMBDAS})")],
72
1
        ),
73
1
        row(
74
1
            "A31",
75
1
            &[
76
1
                "(let ((r 0)) (dolist (f (list (lambda (x) 1) (lambda (x) 2))) \
77
1
               (setf r (+ r (funcall f 0)))) r)",
78
1
            ],
79
1
        ),
80
1
        row(
81
1
            "W9",
82
1
            &["(mapcar (lambda (f) (funcall f 2)) (list (lambda (x) (+ x 1))))"],
83
1
        ),
84
1
        row(
85
1
            "W1",
86
1
            &[
87
1
                "(defvar *fs* (let ((n (entity-count))) (list (lambda (x) (+ x n))))) \
88
1
               (funcall (car *fs*) 4)",
89
1
            ],
90
1
        ),
91
1
        row(
92
1
            "W3",
93
1
            &[
94
1
                "(let ((fs (let ((n (entity-count))) (list (lambda (x) (+ x n)))))) \
95
1
               (let ((n 100)) (funcall (car fs) 4)))",
96
1
            ],
97
1
        ),
98
1
        row(
99
1
            "W4",
100
1
            &[
101
1
                "(defun mkfs () (let ((n (entity-count))) (list (lambda (x) (+ x n))))) \
102
1
               (let ((n 100)) (funcall (car (mkfs)) 4))",
103
1
            ],
104
1
        ),
105
1
        row(
106
1
            "W12",
107
1
            &[
108
1
                "(let ((fs (list (lambda (x) (+ x 1)) (lambda (x) (* x 2))))) \
109
1
               (funcall (car (cdr fs)) 5))",
110
1
            ],
111
1
        ),
112
1
        row("L9", &["(error 'boom \"m\" (list 1 (lambda (x) x)))"]),
113
1
        row(
114
1
            "H20",
115
1
            &[&format!(
116
1
                "(defvar *l* {L}) (setf *l* (let ((f (lambda (x) (+ x 2)))) (list f))) \
117
1
               (funcall (car *l*) 1)"
118
1
            )],
119
1
        ),
120
1
        row(
121
1
            "H20b",
122
1
            &[
123
1
                "(defvar *l* (list 1))",
124
1
                "(setf *l* (let ((f (lambda (x) (+ x 1)))) (list f))) (funcall (car *l*) 1)",
125
1
            ],
126
1
        ),
127
1
        row(
128
1
            "U19",
129
1
            &[&format!(
130
1
                "(defvar *l* {L}) (setf *l* (let ((h (lambda (x) \"s\"))) (list h))) \
131
1
               (funcall (car *l*) 1)"
132
1
            )],
133
1
        ),
134
1
        row(
135
1
            "L4",
136
1
            &[
137
1
                "(let ((n (entity-count))) (mapcar (lambda (g) (funcall g 5)) \
138
1
               (list (lambda (x) (+ x n)) (lambda (x) (* x 2)))))",
139
1
            ],
140
1
        ),
141
1
        row(
142
1
            "A31b",
143
1
            &[
144
1
                "(let ((r 0)) (dolist (f (list (lambda (x) (+ x 1)) (lambda (x) (* x 2)))) \
145
1
               (setf r (+ r (funcall f 3)))) r)",
146
1
            ],
147
1
        ),
148
1
        row(
149
1
            "A31c",
150
1
            &["(let ((r 0) (n (entity-count))) \
151
1
               (dolist (f (list (lambda (x) (+ x n)) (lambda (x) (* x 2)))) \
152
1
               (setf r (+ r (funcall f 3)))) r)"],
153
1
        ),
154
    ]
155
1
}
156

            
157
1
fn refused() -> Vec<Row> {
158
1
    vec![row(
159
1
        "L11",
160
1
        &["(defstruct pt x) (funcall (pt-x (make-pt :x (lambda () 1))))"],
161
1
    )]
162
1
}
163

            
164
const PINNED: &[(&str, &[&str])] = &[
165
    ("L6", &["5"]),                    // SBCL 5
166
    ("L7", &["4"]),                    // SBCL 4
167
    ("X1", &["4"]),                    // SBCL 4: the closure keeps its own n
168
    ("X2", &["4"]),                    // SBCL 4
169
    ("R1", &["-", "5"]),               // SBCL *fs* 5
170
    ("R2", &["-", "4"]),               // SBCL *hs* 4
171
    ("L10", &["(#<closure:0>)"]),      // SBCL prints the list: stays
172
    ("L1", &["(#<closure:0> \"x\")"]), // SBCL a list of two
173
    ("L5", &["(1 #<closure:0>)"]),     // SBCL a list of two
174
    ("L2", &["3"]),                    // SBCL 3
175
    ("L3", &["2"]),                    // SBCL 2
176
    ("W7", &["nil"]),                  // SBCL prints 2 2, returns NIL
177
    ("B1", &["1"]),                    // SBCL 1
178
    ("B1a", &["1"]),                   // SBCL 1
179
    ("B6", &["1"]),                    // SBCL 1
180
    ("A31", &["3"]),                   // SBCL 3
181
    ("W9", &["(3)"]),                  // SBCL (3)
182
    ("W1", &["4"]),                    // SBCL 4
183
    ("W3", &["4"]),                    // SBCL 4
184
    ("W4", &["4"]),                    // SBCL 4
185
    ("W12", &["10"]),                  // SBCL 10
186
    ("L9", &["err: BOOM: m"]),         // SBCL raises BOOM
187
    ("H20", &["3"]),                   // SBCL 3
188
    // SBCL 2 and "s": the home is an Index list / a list of another closure
189
    // signature, so the call operand is an any value, checked at run time.
190
    ("H20b", &["-", "2"]),
191
    ("U19", &["\"s\""]),
192
    // SBCL (5 10): two signatures
193
    ("L4", &["(5 10)"]),
194
    ("A31b", &["10"]), // SBCL 10
195
    ("A31c", &["9"]),  // SBCL 9
196
];
197

            
198
const REFUSED: &[(&str, &[&str])] = &[
199
    (
200
        "L11",
201
        &["err: Compile error: cannot compile to WASM stack value: (LAMBDA () 1)"],
202
    ), // SBCL 1: Q4-4 fixes
203
];
204

            
205
#[test]
206
1
fn closure_list_values_read_as_pinned() {
207
1
    assert_pinned(&rows(), PINNED);
208
1
}
209

            
210
#[test]
211
1
fn closure_list_refusals_read_as_pinned() {
212
1
    assert_pinned_wide(&refused(), REFUSED, 120);
213
1
}