Lines
100 %
Functions
Branches
use super::*;
use scripting::nomiscript::Number;
fn interpreter() -> Interpreter {
Interpreter::new(false).expect("Interpreter::new")
}
/// ADR-0033 Phase 5 regression: a runtime-nil `StringRef` in the
/// script-mode for-effect position used to reach
/// `write_debug_string_from_stack`'s `array.len` unconditionally,
/// trapping the engine on a null array rather than serializing `nil`.
/// `(entity-count)` is 0 against the interpreter's default minimal
/// (empty) input batch, so the `if` never takes the `"x"` arm.
#[test]
fn a_runtime_nil_string_serializes_as_nil_instead_of_trapping() {
let values = interpreter()
.eval(r#"(if (> (entity-count) 5) "x")"#)
.expect("evaluating a runtime-nil string must not trap the engine");
assert_eq!(values, vec![Value::Nil]);
/// Codex review item 5: `CREATE-TAG` read `array.len` on its name/value
/// args without a null check; a nullable name (an untaken `if` branch)
/// used to trap the engine uncatchably instead of raising a catchable
/// `TYPE-ERROR`. The normal and caught arms return DISTINCT values (1
/// vs. 7) so a passing test can only mean the handler actually ran.
fn create_tag_with_a_nil_name_raises_a_catchable_type_error() {
.eval(
r#"(handler-case
(progn (create-tag 0 (if (> (entity-count) 5) "x") "v") 1)
(type-error (e) 7))"#,
)
.expect("a nil CREATE-TAG name must raise a catchable condition, not trap");
assert_eq!(values, vec![Value::Number(Number::Index(7))]);
/// Same check, nil in the value position.
fn create_tag_with_a_nil_value_raises_a_catchable_type_error() {
(progn (create-tag 0 "k" (if (> (entity-count) 5) "x")) 1)
.expect("a nil CREATE-TAG value must raise a catchable condition, not trap");
/// Codex review follow-up: a LITERAL `nil` name (not just a runtime-nil
/// `if` branch) used to fail wasm compilation outright — it folds to
/// `Bool`/i32 at `compile_for_stack`, but `CREATE-TAG` stored it into a
/// `StringRef` local, producing invalid wasm.
fn create_tag_with_a_literal_nil_name_raises_a_catchable_type_error() {
.eval(r#"(handler-case (progn (create-tag 0 nil "v") 1) (type-error (e) 7))"#)
.expect("a literal nil CREATE-TAG name must raise a catchable condition, not trap");
/// ISS-027: an uncaught condition reports its code and message, not the
/// engine's backtrace.
fn an_uncaught_condition_renders_its_code_and_message() {
let err = interpreter()
.eval(r#"(error 'boom "m")"#)
.expect_err("the condition is uncaught");
assert!(
matches!(&err, Error::Raised { code, message } if code == "BOOM" && message == "m"),
"{err:?}"
);
assert_eq!(err.render(false), "error: BOOM: m");
/// ISS-056: a program that writes no result value is not an error.
fn a_void_program_has_no_value() {
.eval("(defvar *x* 1)")
.expect("a definition-only program runs");
assert!(values.is_empty(), "{values:?}");
/// ISS-028: an input batch without a valid header is an error, not a panic.
fn an_invalid_input_batch_is_an_error() {
let interp = interpreter();
let wasm = rpc::compile_script("1").expect("compiles");
let err = interp
.run_wasm_with_input(&wasm, &[0, 1, 2])
.expect_err("a three-byte batch has no header");
assert!(err.to_string().contains("no valid header"), "{err}");
fn eval_lines(lines: &[&str]) -> Vec<Value> {
let mut interpreter = interpreter();
lines
.iter()
.map(|line| {
let mut values = interpreter.eval(line).expect("line evaluates");
values.pop().expect("line has a value")
})
.collect()
fn number(n: i64) -> Value {
Value::Number(Number::Index(n))
fn a_created_global_keeps_its_constant_across_repl_lines() {
let values = eval_lines(&["(setf g 1) (setf g 2)", "g", "(let ((g 5)) g)", "g"]);
assert_eq!(values[1..], [number(2), number(5), number(2)]);
fn a_defun_stores_to_a_created_global_in_a_later_repl_line() {
let values = eval_lines(&[
"(setf total 0) (defun add (n) (setf total (+ total n)))",
"(add 5)",
]);
assert_eq!(values[1], number(5));
fn a_defvar_of_a_created_global_keeps_the_stored_value() {
interpreter.eval("(setf g 1) (setf g 2)").expect("stores");
interpreter.eval("(defvar g 9)").expect("declares");
let values = interpreter.eval("g").expect("reads");
assert_eq!(values, vec![number(2)]);
/// PERSIST S4-4: a unit that compiled but never ran puts back what it defined.
fn a_unit_that_never_ran_puts_back_its_definitions() {
let mut interp = interpreter();
interp.eval("(defun f () 1)").expect("defines f");
let program = Reader::parse("(defun f () 2)").expect("parses");
let wasm = interp.compile(&program).expect("compiles");
let failed = interp.run_unit(&wasm, &[], true);
assert!(failed.is_err(), "an empty input batch has no header");
let values = interp.eval("(f)").expect("calls f");
assert_eq!(values, vec![Value::Number(Number::Index(1))]);