Lines
100 %
Functions
Branches
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! PERSIST Q4-0 rows: closures in list cells, pinned with the SBCL value next
//! to each row. Q4-3 made a lambda literal in a list a real cell: X1 and R2 no
//! longer read the caller's `n`.
#[path = "persist/rows.rs"]
mod persist;
use persist::{Row, assert_pinned, assert_pinned_wide, row};
const DEFVAR_OF_LAMBDAS: &str = "(defvar *fs* (list (lambda (x) (+ x 1))))";
const LET_OF_CAPTURE: &str = "(let ((n (entity-count))) (defvar *hs* (list (lambda (x) (+ x n)))))";
const LOOP_OVER_LAMBDAS: &str = "(let ((r 0)) (dolist (f (list (lambda (x) 1))) \
(setf r (funcall f 0))) r)";
const L: &str = "(let ((f (lambda (x) (+ x 1)))) (list f))";
fn rows() -> Vec<Row> {
vec![
row(
"L6",
&[&format!("{DEFVAR_OF_LAMBDAS} (funcall (car *fs*) 4)")],
),
"L7",
&[
"(let ((n (entity-count))) (defvar *fs* (list (lambda (x) (+ x n)))) \
(funcall (car *fs*) 4))",
],
"X1",
&[&format!(
"{LET_OF_CAPTURE} (let ((n 100)) (funcall (car *hs*) 4))"
)],
"X2",
"(defvar *n* 100) {LET_OF_CAPTURE} (funcall (car *hs*) 4)"
row("R1", &[DEFVAR_OF_LAMBDAS, "(funcall (car *fs*) 4)"]),
"R2",
&[LET_OF_CAPTURE, "(let ((n 100)) (funcall (car *hs*) 4))"],
row("L10", &["(print (list (lambda (x) x)))"]),
row("L1", &["(let ((f (lambda (x) x))) (list f \"x\"))"]),
row("L5", &["(let ((f (lambda () 1))) (list (funcall f) f))"]),
"L2",
&["(let ((f (lambda (x) (+ x 1)))) (funcall (car (list f)) 2))"],
"L3",
&["(let ((n (entity-count))) (let ((f (lambda (x) (+ x n)))) \
(funcall (car (list f f)) 2)))"],
"W7",
&["(let ((f (lambda (x) (+ x 1)))) (dolist (g (list f f)) (print (funcall g 1))))"],
row("B1", &[&format!("(defun f (x) 100) {LOOP_OVER_LAMBDAS}")]),
row("B1a", &[LOOP_OVER_LAMBDAS]),
"B6",
&[&format!("(labels ((f (x) 100)) {LOOP_OVER_LAMBDAS})")],
"A31",
"(let ((r 0)) (dolist (f (list (lambda (x) 1) (lambda (x) 2))) \
(setf r (+ r (funcall f 0)))) r)",
"W9",
&["(mapcar (lambda (f) (funcall f 2)) (list (lambda (x) (+ x 1))))"],
"W1",
"(defvar *fs* (let ((n (entity-count))) (list (lambda (x) (+ x n))))) \
(funcall (car *fs*) 4)",
"W3",
"(let ((fs (let ((n (entity-count))) (list (lambda (x) (+ x n)))))) \
(let ((n 100)) (funcall (car fs) 4)))",
"W4",
"(defun mkfs () (let ((n (entity-count))) (list (lambda (x) (+ x n))))) \
(let ((n 100)) (funcall (car (mkfs)) 4))",
"W12",
"(let ((fs (list (lambda (x) (+ x 1)) (lambda (x) (* x 2))))) \
(funcall (car (cdr fs)) 5))",
row("L9", &["(error 'boom \"m\" (list 1 (lambda (x) x)))"]),
"H20",
"(defvar *l* {L}) (setf *l* (let ((f (lambda (x) (+ x 2)))) (list f))) \
(funcall (car *l*) 1)"
"H20b",
"(defvar *l* (list 1))",
"(setf *l* (let ((f (lambda (x) (+ x 1)))) (list f))) (funcall (car *l*) 1)",
"U19",
"(defvar *l* {L}) (setf *l* (let ((h (lambda (x) \"s\"))) (list h))) \
"L4",
"(let ((n (entity-count))) (mapcar (lambda (g) (funcall g 5)) \
(list (lambda (x) (+ x n)) (lambda (x) (* x 2)))))",
"A31b",
"(let ((r 0)) (dolist (f (list (lambda (x) (+ x 1)) (lambda (x) (* x 2)))) \
(setf r (+ r (funcall f 3)))) r)",
"A31c",
&["(let ((r 0) (n (entity-count))) \
(dolist (f (list (lambda (x) (+ x n)) (lambda (x) (* x 2)))) \
(setf r (+ r (funcall f 3)))) r)"],
]
}
fn refused() -> Vec<Row> {
vec![row(
"L11",
&["(defstruct pt x) (funcall (pt-x (make-pt :x (lambda () 1))))"],
)]
const PINNED: &[(&str, &[&str])] = &[
("L6", &["5"]), // SBCL 5
("L7", &["4"]), // SBCL 4
("X1", &["4"]), // SBCL 4: the closure keeps its own n
("X2", &["4"]), // SBCL 4
("R1", &["-", "5"]), // SBCL *fs* 5
("R2", &["-", "4"]), // SBCL *hs* 4
("L10", &["(#<closure:0>)"]), // SBCL prints the list: stays
("L1", &["(#<closure:0> \"x\")"]), // SBCL a list of two
("L5", &["(1 #<closure:0>)"]), // SBCL a list of two
("L2", &["3"]), // SBCL 3
("L3", &["2"]), // SBCL 2
("W7", &["nil"]), // SBCL prints 2 2, returns NIL
("B1", &["1"]), // SBCL 1
("B1a", &["1"]), // SBCL 1
("B6", &["1"]), // SBCL 1
("A31", &["3"]), // SBCL 3
("W9", &["(3)"]), // SBCL (3)
("W1", &["4"]), // SBCL 4
("W3", &["4"]), // SBCL 4
("W4", &["4"]), // SBCL 4
("W12", &["10"]), // SBCL 10
("L9", &["err: BOOM: m"]), // SBCL raises BOOM
("H20", &["3"]), // SBCL 3
// SBCL 2 and "s": the home is an Index list / a list of another closure
// signature, so the call operand is an any value, checked at run time.
("H20b", &["-", "2"]),
("U19", &["\"s\""]),
// SBCL (5 10): two signatures
("L4", &["(5 10)"]),
("A31b", &["10"]), // SBCL 10
("A31c", &["9"]), // SBCL 9
];
const REFUSED: &[(&str, &[&str])] = &[
(
&["err: Compile error: cannot compile to WASM stack value: (LAMBDA () 1)"],
), // SBCL 1: Q4-4 fixes
#[test]
fn closure_list_values_read_as_pinned() {
assert_pinned(&rows(), PINNED);
fn closure_list_refusals_read_as_pinned() {
assert_pinned_wide(&refused(), REFUSED, 120);