Lines
97.14 %
Functions
81.82 %
Branches
100 %
//! Carrying a unit's globals never fails the unit that ran (PERSIST).
use nomiscript::{CompileMode, Compiler, Expr, Number, Reader, SymbolTable};
use wasmtime::{Instance, Linker, Store};
use crate::runtime::{
EngineOpts, UnitEnd, build_engine, carry_globals, carry_not_run, compile_module,
};
fn compile(symbols: &mut SymbolTable, source: &str) -> Vec<u8> {
let program = Reader::parse(source).unwrap();
Compiler::new()
.compile_with_mode(&program, symbols, CompileMode::Eval)
.unwrap()
}
fn instantiate(bytes: &[u8]) -> (Store<()>, Instance) {
let engine = build_engine(EngineOpts::baseline().with_fuel()).unwrap();
let module = compile_module(&engine, bytes).unwrap();
let mut linker: Linker<()> = Linker::new(&engine);
linker.define_unknown_imports_as_traps(&module).unwrap();
let mut store: Store<()> = Store::new(&engine, ());
store.set_fuel(10_000_000).unwrap();
store.set_epoch_deadline(1);
let instance = linker.instantiate(&mut store, &module).unwrap();
(store, instance)
fn read_error(symbols: &mut SymbolTable, name: &str) -> String {
let program = Reader::parse(name).unwrap();
.expect_err("the global has no value")
.to_string()
#[test]
fn a_unit_that_never_ran_puts_back_every_slot() {
let mut symbols = SymbolTable::with_builtins();
compile(&mut symbols, "(defvar a 1) (defun f () 1)");
carry_not_run(&mut symbols);
compile(
&mut symbols,
"(defparameter a 2) (defun f () 2) (defvar b 3)",
);
assert_eq!(symbols.take_unit_slots(), []);
let function = symbols
.lookup("F")
.and_then(|symbol| symbol.function())
.cloned();
assert!(function.is_none(), "{function:?}");
assert!(
symbols
.lookup("A")
.and_then(|symbol| symbol.value())
.is_none()
assert!(symbols.lookup("B").is_none());
fn a_manifest_naming_a_missing_export_makes_the_global_unreadable() {
compile(&mut symbols, "(defvar a 1) (setf a 2)");
let (mut store, instance) = instantiate(&compile(&mut SymbolTable::with_builtins(), "(+ 1 2)"));
carry_globals(&mut store, &instance, &mut symbols, UnitEnd::Returned);
assert_eq!(
read_error(&mut symbols, "a"),
"Compile error: A: its unreadable value does not survive the form"
fn a_unit_that_trapped_inside_a_binding_carries_the_value_outside_it() {
let bytes = compile(
"(defvar a 1) (defvar b 1) (setf a 2) (setf b 3) (let ((a 4)) (setf a 5) (do ((i 0 (+ i 1))) ((> i 1073741823) i)))",
let (mut store, instance) = instantiate(&bytes);
let main = instance.get_func(&mut store, "nomi-eval").unwrap();
main.call(&mut store, &[], &mut [wasmtime::Val::AnyRef(None)])
.unwrap_err();
carry_globals(&mut store, &instance, &mut symbols, UnitEnd::Trapped);
let held = |name: &str| symbols.lookup(name).and_then(|symbol| symbol.value());
let number = |n| Some(Expr::Number(Number::Index(n)));
assert_eq!(held("A").cloned(), number(2));
assert_eq!(held("B").cloned(), number(3));