Lines
100 %
Functions
Branches
//! PERSIST S5: a unit that traps while a special is dynamically rebound leaves
//! the special as it was outside the binding, as Common Lisp unwinds it.
use super::*;
use crate::ctx::ScriptLimits;
const SPIN: &str = "(do ((i 0 (+ i 1))) ((> i 1073741823) i))";
const FUEL: &str = "fuel exhausted before completion";
async fn after_trap(units: &[&str]) -> Vec<String> {
let ctx = ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
fuel: 100_000,
..ScriptLimits::default()
});
let mut session = Session::new(ctx).expect("Session::new");
let mut outcomes = Vec::new();
for source in units {
outcomes.push(match session.handle_request(source).await.payload {
ResponsePayload::Error { message, .. } => message,
ResponsePayload::Value(value) => format!("{value:?}"),
}
outcomes
fn ratio(numer: i64, denom: i64) -> String {
format!("Number(Scalar(Ratio {{ numer: {numer}, denom: {denom} }}))")
fn index(n: i64) -> String {
format!("Number(Index({n}))")
#[tokio::test]
async fn a_trap_inside_a_let_leaves_the_outer_value() {
let got = after_trap(&[
"(defvar *rate* 1/10)",
&format!("(let ((*rate* 1/2)) {SPIN})"),
"*rate*",
])
.await;
assert_eq!(got[1..], [FUEL.to_string(), ratio(1, 10)]);
async fn a_trap_inside_nested_lets_leaves_the_outermost_value() {
&format!("(let ((*rate* 1/2)) (let ((*rate* 1/3)) {SPIN}))"),
async fn a_trap_inside_a_defun_with_a_special_parameter_leaves_the_outer_value() {
&format!("(defun slow (*rate*) {SPIN})"),
"(slow 1/2)",
assert_eq!(got[2..], [FUEL.to_string(), ratio(1, 10)]);
async fn a_trap_after_the_let_returned_keeps_the_stored_value() {
&format!("(progn (setf *rate* 3/10) (let ((*rate* 1/2)) *rate*) {SPIN})"),
assert_eq!(got[1..], [FUEL.to_string(), ratio(3, 10)]);
async fn a_trap_inside_a_let_of_an_unbound_special_leaves_it_unbound() {
"(defvar *rate*)",
assert_eq!(
got[1..],
[FUEL, "Compile error: symbol '*RATE*' has no value"]
);
async fn a_setf_inside_the_let_before_the_trap_does_not_reach_the_outer_value() {
&format!("(let ((*rate* 1/2)) (setf *rate* 9/10) {SPIN})"),
async fn a_setf_outside_any_binding_before_the_trap_is_kept() {
&format!("(progn (let ((*rate* 1/2)) *rate*) (setf *rate* 3/10) {SPIN})"),
async fn a_declaration_in_a_unit_that_trapped_in_a_binding_stays_special() {
&format!("(progn (defvar *u*) (let ((*u* 1/2)) {SPIN}))"),
"(setf *u* 5)",
"(defun h () *u*)",
"(let ((*u* 2/3)) (h))",
got,
[FUEL.to_string(), index(5), "Nil".to_string(), ratio(2, 3)]
async fn a_defvar_unit_that_trapped_in_a_binding_leaves_it_unbound() {
"*u*",
assert_eq!(got, [FUEL, "Compile error: symbol '*U*' has no value"]);
async fn a_defvar_that_no_store_reached_still_declares_the_special() {
"(progn (defvar *u*) (when (> (do ((i 0 (+ i 1))) ((> i 3) i)) 99) (setf *u* 1)))",
assert_eq!(got[3..], [ratio(2, 3)]);
async fn a_defvar_skipped_by_an_error_is_put_back() {
"(progn (error 'boom \"m\") (defvar *u*) (setf *u* 5))",
assert_eq!(got[1..], ["Undefined symbol: *U*"]);
async fn a_defvar_skipped_by_an_error_leaves_a_stored_global_lexical() {
"(progn (error 'boom \"m\") (defvar *u* 1))",
assert_eq!(got[3..], [index(5)]);
async fn a_defvar_skipped_by_a_trap_leaves_a_stored_global_lexical() {
&format!("(progn {SPIN} (defvar *u* 1) 0)"),
assert_eq!(got[1..2], [FUEL]);