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))]
//! Plan 0014 PERSIST (owner decision 2A), fixtures first: what a later unit
//! of one interpreter sees of a runtime global today. A row marked `CL`
//! reads differently in Common Lisp and turns green with the host carry.
use nms::interpreter::Interpreter;
fn number(n: i64, d: i64) -> String {
format!("Number(Scalar(Ratio {{ numer: {n}, denom: {d} }}))")
}
fn index_cell(n: i64) -> String {
format!("Number(Index({n}))")
fn index(n: i64) -> String {
format!("[Number(Index({n}))]")
fn num(n: i64, d: i64) -> String {
format!("[{}]", number(n, d))
fn run_units(sources: &[&str]) -> Vec<String> {
let mut interp = Interpreter::new(false).unwrap();
sources
.iter()
.map(|source| match interp.eval(source) {
Ok(values) => format!("{values:?}"),
Err(err) => err.to_string(),
})
.collect()
#[test]
fn a_callee_store_of_a_defvar_is_carried_across_units() {
let outcomes = run_units(&[
"(defvar x 7)",
"(defun g () x)",
"(defun s (v) (setf x v))",
"(s 5)",
"(g)",
]);
assert_eq!(
outcomes,
[
"[]".to_string(),
"[Symbol(\"G\")]".into(),
"[Symbol(\"S\")]".into(),
index(5),
index(5)
]
);
fn the_total_accumulator_is_carried_across_units() {
"(setf total 0)",
"(defun add (n) (setf total (+ total n)))",
"(add 5)",
"total",
index(0),
"[Symbol(\"ADD\")]".to_string(),
index(10),
index(10)
fn a_store_through_a_chain_of_earlier_functions_is_carried() {
"(defun twice (n) (add n) (add n))",
"(twice 5)",
assert_eq!(outcomes[4], index(10));
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));
fn a_runtime_store_before_an_uncaught_error_is_carried() {
"(defvar x 1) (setf x (+ 5 (entity-count))) (error 'boom \"m\")",
// Before the failure policy the second unit read an expired global.
assert_eq!(outcomes, ["BOOM: m".to_string(), index(5)]);
fn a_stored_closure_is_carried_under_its_site_in_the_next_unit() {
run_units(&[
"(defvar v (lambda (x) (+ x 2))) (setf v (lambda (x) (+ x 1)))",
"v"
]),
"[Closure(Closure { code_id: Some(1), env: [] })]".to_string(),
"[Closure(Closure { code_id: Some(0), env: [] })]".to_string(),
fn every_stored_value_kind_is_carried() {
let kinds = [
("(defvar v 1) (setf v 2)", index(2)),
("(defvar v 1/2) (setf v 3/4)", num(3, 4)),
("(defvar v 1) (setf v nil)", "[Nil]".to_string()),
("(defvar v nil) (setf v t)", "[T]".to_string()),
(
"(defvar v \"a\") (setf v \"b\")",
"[String(\"b\")]".to_string(),
),
("(defvar v 'a) (setf v 'b)", "[Symbol(\"B\")]".to_string()),
("(defvar v :a) (setf v :b)", "[Symbol(\":B\")]".to_string()),
"(defvar v '(1)) (setf v '(1 2))",
format!(
"[Pair(Pair {{ car: {}, cdr: Pair(Pair {{ car: {}, cdr: Nil }}) }})]",
index_cell(1),
index_cell(2)
];
for (first, stored) in kinds {
run_units(&[first, "v"]),
[stored.clone(), stored],
"{first}"
fn a_type_change_in_a_later_unit_defines_a_fresh_home() {
"(defvar v 1) (setf v 2)",
"(defparameter v \"s\") (setf v \"t\") v",
assert_eq!(outcomes, [index(2), "[String(\"t\")]".to_string()]);
fn an_integral_scalar_divides_as_a_scalar_only_while_it_is_runtime() {
"(defvar x 1/2) (setf x (* x 4/1)) (/ x 4/1)",
"(defvar y 2) (setf y (+ y 0)) (/ y 4)",
"(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)]);
fn a_scalar_that_ends_integral_stays_a_scalar_in_the_next_unit() {
"(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)]);
fn a_store_by_a_defun_called_in_the_same_unit_is_carried() {
let outcomes = run_units(&["(defvar x 1) (defun s (v) (setf x v)) (s 5)", "x"]);
assert_eq!(outcomes, [index(5), index(5)]);
fn a_call_of_a_stored_closure_is_made_by_the_next_unit() {
"(funcall v 1)",
assert_eq!(outcomes[1], index(2));