Lines
98.86 %
Functions
94.12 %
Branches
100 %
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! Plan 0014 GSTORE on PERSIST: a global a `SETF` creates is a runtime cell
//! once stored again, lexical under binders, and carried to the next unit by
//! the host, with the Common Lisp value.
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Fraction, Number, Pair, Value};
fn index(n: i64) -> Value {
Value::Number(Number::Index(n))
}
fn number(n: i64, d: i64) -> Value {
Value::Number(Number::Scalar(Fraction::new(n, d)))
fn list(items: Vec<Value>) -> Value {
items
.into_iter()
.rev()
.fold(Value::Nil, |tail, item| Pair::cons(item, tail))
fn last_of_each(sources: &[&str]) -> Vec<Value> {
let mut interp = Interpreter::new(false).unwrap();
sources
.iter()
.map(|source| {
interp
.eval(source)
.unwrap_or_else(|err| panic!("{source}: {err}"))
.pop()
.unwrap_or(Value::Nil)
})
.collect()
#[test]
fn a_created_global_stored_by_a_callee_is_one_cell() {
let shown = last_of_each(&["(setf g 1) (defun f () (setf g 2) g) (list (f) g)"]);
assert_eq!(shown, vec![list(vec![index(2), index(2)])]);
fn the_repl_reads_the_created_global_in_the_next_unit() {
let shown = last_of_each(&[
"(setf g 1) (setf g 2)",
"g",
"(defvar g 9)",
"(let ((g 5)) g)",
]);
assert_eq!(shown[1], index(2));
assert_eq!(shown[3], index(2));
assert_eq!(shown[4], index(5));
fn a_binding_that_stores_inside_the_unit_leaves_the_global_alone() {
let shown = last_of_each(&["(setf g 1) (setf g 2) (let ((g 5)) (setf g 6)) g", "g"]);
assert_eq!(shown, vec![index(2), index(2)]);
fn a_closure_that_is_never_called_stores_nothing() {
let shown = last_of_each(&["(setf g 1) (setf g 2) (lambda () (setf g 3)) 0", "g"]);
fn a_later_defparameter_then_setf_wins() {
"(defparameter g 7) (setf g 8)",
assert_eq!(shown[2], index(8));
fn a_store_that_widens_the_cell_keeps_the_value() {
let shown = last_of_each(&["(setf g 1) (setf g 1/2)", "g"]);
assert_eq!(shown[1], number(1, 2));
let shown = last_of_each(&["(setf g nil) (setf g (list 0 0))", "g"]);
assert_eq!(shown[1], list(vec![index(0), index(0)]));
let shown = last_of_each(&["(setf g 1) (setf g \"b\")", "g"]);
assert_eq!(shown[1], Value::String("b".to_string()));
fn a_loop_variable_named_like_the_cell_leaves_it_alone() {
"(setf g 1) (setf g 2) (do ((g 0 (+ g 1))) ((> g (entity-count)) g))",
fn single_unit(source: &str) -> Value {
last_of_each(&[source]).remove(0)
fn a_lambda_store_before_the_creating_setf_targets_the_same_cell() {
let shown = single_unit("(defvar k (lambda () (setf g 2))) (setf g 1) (funcall k) g");
assert_eq!(shown, index(2));
let third =
single_unit("(defvar k (lambda () (setf g 2))) (setf g 1) (setf g 3) (funcall k) g");
assert_eq!(third, index(2));
let defun = single_unit("(defun k () (setf g 2)) (setf g 1) (k) g");
assert_eq!(defun, index(2));
fn a_lambda_store_is_carried_to_the_next_unit() {
"(defvar k (lambda () (setf g 2))) (setf g 1) (funcall k)",