Lines
100 %
Functions
Branches
//! PERSIST S6: a global that holds a closure is carried to the later units of
//! a session as the lambda its source and captured values make; one that
//! shares a variable with the unit that made it stays lost.
use super::*;
use crate::ctx::ScriptLimits;
async fn units(sources: &[&str]) -> Vec<String> {
run_units(ScriptCtx::new(uuid::Uuid::nil()), sources).await
}
async fn run_units(ctx: ScriptCtx, sources: &[&str]) -> Vec<String> {
let mut session = Session::new(ctx).expect("Session::new");
let mut outcomes = Vec::new();
for source in sources {
outcomes.push(match session.handle_request(source).await.payload {
ResponsePayload::Error { message, .. } => message,
ResponsePayload::Value(value) => format!("{value:?}"),
});
outcomes
fn num(n: i64) -> String {
format!("Number(Scalar(Ratio {{ numer: {n}, denom: 1 }}))")
fn index(n: i64) -> String {
format!("Number(Index({n}))")
fn lost(name: &str) -> String {
format!("Compile error: {name}: its closure value does not survive the form")
#[tokio::test]
async fn a_capture_free_closure_is_called_by_the_next_unit() {
let outcomes = units(&["(defvar *k* (lambda (x) (* x 2)))", "(funcall *k* 3)"]).await;
assert_eq!(outcomes[1], index(6));
async fn a_closure_over_a_scalar_is_called_by_the_next_unit() {
let outcomes = units(&[
"(defvar *f* (let ((r 1/10)) (lambda (x) (* x r))))",
"(funcall *f* 50/1)",
])
.await;
assert_eq!(outcomes[1], num(5));
async fn a_function_defined_by_a_later_unit_calls_the_closure() {
"(defvar *k* (lambda (x) (* x 2)))",
"(defun twice (v) (funcall *k* v))",
"(twice 4)",
assert_eq!(outcomes[2], index(8));
async fn a_closure_stored_before_a_trap_is_called_by_the_next_unit() {
let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
fuel: 100_000,
..ScriptLimits::default()
let outcomes = run_units(
ctx,
&[
"(progn (setf *k* (lambda (x) (+ x 1))) (do ((i 0 (+ i 1))) ((> i 1073741823) i)))",
"(funcall *k* 3)",
],
)
assert_eq!(outcomes[2], index(4));
// CL: 1, the closure shares the variable with the unit that made it
async fn a_closure_over_a_mutated_variable_is_lost_to_the_next_unit() {
"(defvar *c* (let ((n 0)) (lambda () (setf n (+ n 1)))))",
"(funcall *c*)",
assert_eq!(outcomes[1], lost("*C*"));