Lines
100 %
Functions
Branches
//! Plan 0014 PERSIST (owner decision 2A), fixtures first: what a later unit
//! sees of a runtime global today. A row marked `CL` reads differently in
//! Common Lisp and turns green with the host carry.
use super::*;
use crate::ctx::ScriptLimits;
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 {
let outcome = session.handle_request(source).await;
outcomes.push(match outcome.payload {
ResponsePayload::Error { message, .. } => message,
ResponsePayload::Value(value) => format!("{value:?}"),
});
}
outcomes
async fn units(sources: &[&str]) -> Vec<String> {
run_units(ScriptCtx::new(uuid::Uuid::nil()), sources).await
fn num(n: i64, d: i64) -> String {
format!("Number(Scalar(Ratio {{ numer: {n}, denom: {d} }}))")
fn index(n: i64) -> String {
format!("Number(Index({n}))")
#[tokio::test]
async fn a_callee_store_of_a_defvar_is_carried_across_units() {
let outcomes = units(&[
"(defvar x 7)",
"(defun g () x)",
"(defun s (v) (setf x v))",
"(s 5)",
"(g)",
])
.await;
assert_eq!(outcomes[4], index(5));
async fn the_total_accumulator_is_carried_across_units() {
"(setf total 0)",
"(defun add (n) (setf total (+ total n)))",
"(add 5)",
"total",
assert_eq!(outcomes[3], index(10));
assert_eq!(outcomes[4], index(10));
async fn a_store_through_a_chain_of_earlier_functions_is_carried() {
"(defun twice (n) (add n) (add n))",
"(twice 5)",
async fn a_unit_that_binds_and_stores_an_earlier_special_carries_the_store() {
"(progn (let ((x 1)) (g)) (setf x 9))",
"x",
assert_eq!(outcomes[3], index(9));
async fn a_created_global_is_carried_and_a_later_defvar_keeps_it() {
"(setf g 1)",
"(setf g 2)",
"g",
"(defvar g 9)",
"(let ((g 5)) g)",
assert_eq!(outcomes[2], index(2));
assert_eq!(outcomes[4], index(2));
assert_eq!(outcomes[5], index(5));
async fn a_repeated_defvar_does_not_stop_the_store_being_carried() {
"(defvar total 0)",
"(add 3)",
"(defvar y 5)",
"(progn (defvar y 6) (setf y 7))",
"y",
assert_eq!(outcomes[5], index(6));
assert_eq!(outcomes[8], index(7));
async fn a_runtime_store_before_an_uncaught_error_is_carried() {
"(progn (defvar x 1) (setf x (+ 5 (rpc-protocol-version))) (error 'boom \"m\"))",
// Before the failure policy the second unit read an expired global.
assert_eq!(outcomes, ["script raised BOOM: m".to_string(), index(6)]);
async fn a_runtime_store_before_a_fuel_trap_is_carried_with_the_value_outside_a_binding() {
let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
fuel: 100_000,
..ScriptLimits::default()
let outcomes = run_units(
ctx,
&[
"(progn (defvar z 1) (defvar y 1) (setf z (+ 5 (rpc-protocol-version))) (setf y 2) (let ((y 3)) (setf y 4) (do ((i 0 (+ i 1))) ((> i 1073741823) i))))",
"z",
],
)
assert_eq!(outcomes[0], "fuel exhausted before completion");
assert_eq!(outcomes[1], index(6));
async fn a_stored_closure_is_carried_under_its_site_in_the_next_unit() {
let first = "(progn (defvar v (lambda (x) (+ x 2))) (setf v (lambda (x) (+ x 1))))";
assert_eq!(
units(&[first, "v"]).await,
[
"Closure(Closure { code_id: Some(1), env: [] })".to_string(),
"Closure(Closure { code_id: Some(0), env: [] })".to_string(),
]
);
async fn every_stored_value_kind_is_carried() {
let int = |n| index(n);
let kinds = [
("(progn (defvar v 1) (setf v 2))", int(2)),
("(progn (defvar v 1/2) (setf v 3/4))", num(3, 4)),
("(progn (defvar v 1) (setf v nil))", "Nil".to_string()),
("(progn (defvar v nil) (setf v t))", "T".to_string()),
(
"(progn (defvar v \"a\") (setf v \"b\"))",
"String(\"b\")".to_string(),
),
"(progn (defvar v 'a) (setf v 'b))",
"Symbol(\"B\")".to_string(),
"(progn (defvar v :a) (setf v :b))",
"Symbol(\":B\")".to_string(),
"(progn (defvar v '(1)) (setf v '(1 2)))",
format!(
"Pair(Pair {{ car: {}, cdr: Pair(Pair {{ car: {}, cdr: Nil }}) }})",
int(1),
int(2)
];
for (first, stored) in kinds {
[stored.clone(), stored],
"{first}"
async fn a_type_change_in_a_later_unit_defines_a_fresh_home() {
"(progn (defvar v 1) (setf v 2))",
"(progn (defparameter v \"s\") (setf v \"t\") v)",
assert_eq!(outcomes, [index(2), "String(\"t\")".to_string()]);
async fn an_integral_scalar_divides_as_a_scalar_only_while_it_is_runtime() {
"(progn (defvar x 1/2) (setf x (* x 4/1)) (/ x 4/1))",
"(progn (defvar y 2) (setf y (+ y 0)) (/ y 4))",
"(progn (defvar z 2) (/ z 4))",
"(/ 2 4)",
// CL: 1/2 everywhere. Rows 2-4 are index division on an integer literal.
assert_eq!(outcomes, [num(1, 2), index(0), index(0), index(0)]);
async fn a_scalar_that_ends_integral_stays_a_scalar_in_the_next_unit() {
"(progn (defvar x 1/2) (setf x (* x 4/1)))",
"(/ x 4/1)",
"(+ x 1/2)",
assert_eq!(outcomes, [num(2, 1), num(1, 2), num(5, 2), num(2, 1)]);
async fn an_index_stays_an_index_in_the_next_unit() {
let outcomes = units(&["(progn (defvar n 1) (setf n 5))", "(/ n 2)"]).await;
assert_eq!(outcomes, [index(5), index(2)]);
async fn a_store_by_a_defun_called_in_the_same_unit_is_carried() {
let outcomes = units(&["(progn (defvar x 1) (defun s (v) (setf x v)) (s 5))", "x"]).await;
assert_eq!(outcomes, [index(5), index(5)]);
async fn a_carried_list_and_symbol_are_data_in_the_next_unit() {
"(progn (defvar l '(1)) (setf l '(7 8)))",
"(car l)",
"(progn (defvar s 'a) (setf s 'b))",
"(eq s 'b)",
assert_eq!(outcomes[1], index(7));
assert_eq!(outcomes[3], "T");