1
//! PERSIST S6: a global that holds a closure is carried to the later units of
2
//! a session as the lambda its source and captured values make; one that
3
//! shares a variable with the unit that made it stays lost.
4

            
5
use super::*;
6
use crate::ctx::ScriptLimits;
7

            
8
4
async fn units(sources: &[&str]) -> Vec<String> {
9
4
    run_units(ScriptCtx::new(uuid::Uuid::nil()), sources).await
10
4
}
11

            
12
5
async fn run_units(ctx: ScriptCtx, sources: &[&str]) -> Vec<String> {
13
5
    let mut session = Session::new(ctx).expect("Session::new");
14
5
    let mut outcomes = Vec::new();
15
5
    for source in sources {
16
12
        outcomes.push(match session.handle_request(source).await.payload {
17
2
            ResponsePayload::Error { message, .. } => message,
18
10
            ResponsePayload::Value(value) => format!("{value:?}"),
19
        });
20
    }
21
5
    outcomes
22
5
}
23

            
24
1
fn num(n: i64) -> String {
25
1
    format!("Number(Scalar(Ratio {{ numer: {n}, denom: 1 }}))")
26
1
}
27

            
28
3
fn index(n: i64) -> String {
29
3
    format!("Number(Index({n}))")
30
3
}
31

            
32
1
fn lost(name: &str) -> String {
33
1
    format!("Compile error: {name}: its closure value does not survive the form")
34
1
}
35

            
36
#[tokio::test]
37
1
async fn a_capture_free_closure_is_called_by_the_next_unit() {
38
1
    let outcomes = units(&["(defvar *k* (lambda (x) (* x 2)))", "(funcall *k* 3)"]).await;
39
1
    assert_eq!(outcomes[1], index(6));
40
1
}
41

            
42
#[tokio::test]
43
1
async fn a_closure_over_a_scalar_is_called_by_the_next_unit() {
44
1
    let outcomes = units(&[
45
1
        "(defvar *f* (let ((r 1/10)) (lambda (x) (* x r))))",
46
1
        "(funcall *f* 50/1)",
47
1
    ])
48
1
    .await;
49
1
    assert_eq!(outcomes[1], num(5));
50
1
}
51

            
52
#[tokio::test]
53
1
async fn a_function_defined_by_a_later_unit_calls_the_closure() {
54
1
    let outcomes = units(&[
55
1
        "(defvar *k* (lambda (x) (* x 2)))",
56
1
        "(defun twice (v) (funcall *k* v))",
57
1
        "(twice 4)",
58
1
    ])
59
1
    .await;
60
1
    assert_eq!(outcomes[2], index(8));
61
1
}
62

            
63
#[tokio::test]
64
1
async fn a_closure_stored_before_a_trap_is_called_by_the_next_unit() {
65
1
    let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
66
1
        fuel: 100_000,
67
1
        ..ScriptLimits::default()
68
1
    });
69
1
    let outcomes = run_units(
70
1
        ctx,
71
1
        &[
72
1
            "(defvar *k* (lambda (x) (* x 2)))",
73
1
            "(progn (setf *k* (lambda (x) (+ x 1))) (do ((i 0 (+ i 1))) ((> i 1073741823) i)))",
74
1
            "(funcall *k* 3)",
75
1
        ],
76
1
    )
77
1
    .await;
78
1
    assert_eq!(outcomes[2], index(4));
79
1
}
80

            
81
// CL: 1, the closure shares the variable with the unit that made it
82
#[tokio::test]
83
1
async fn a_closure_over_a_mutated_variable_is_lost_to_the_next_unit() {
84
1
    let outcomes = units(&[
85
1
        "(defvar *c* (let ((n 0)) (lambda () (setf n (+ n 1)))))",
86
1
        "(funcall *c*)",
87
1
    ])
88
1
    .await;
89
1
    assert_eq!(outcomes[1], lost("*C*"));
90
1
}