Lines
100 %
Functions
95.83 %
Branches
//! Struct instances a global holds are carried with their identity (rstruct S5).
use nomiscript::{CompileMode, Compiler, Expr, Number, Reader, StructInstance, SymbolTable};
use wasmtime::{Instance, Linker, Store, Val};
use crate::runtime::heap::{MAX_REACHED, settle_heap};
use crate::runtime::{
EngineOpts, UnitEnd, build_engine, carry_globals, carry_not_run, compile_module,
resolve_type_handles,
};
fn compile(symbols: &mut SymbolTable, source: &str) -> Result<Vec<u8>, String> {
let program = Reader::parse(source).map_err(|err| err.to_string())?;
Compiler::new()
.compile_with_mode(&program, symbols, CompileMode::Eval)
.map_err(|err| err.to_string())
}
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)
/// Compiles, runs and carries one unit, then reads its heap again within
/// `ceiling` objects; the compile error when it has one.
fn run_within(symbols: &mut SymbolTable, source: &str, ceiling: usize) -> Result<(), String> {
let bytes = compile(symbols, source).inspect_err(|_| carry_not_run(symbols))?;
let (mut store, instance) = instantiate(&bytes);
let main = instance.get_func(&mut store, "nomi-eval").unwrap();
let result = main.call(&mut store, &[], &mut [Val::AnyRef(None)]);
let end = result
.as_ref()
.err()
.map_or(UnitEnd::Returned, UnitEnd::of_failure);
carry_globals(&mut store, &instance, symbols, end);
let handles = resolve_type_handles(&mut store, &instance).ok();
settle_heap(&mut store, &instance, symbols, handles.as_ref(), ceiling);
Ok(())
fn run(symbols: &mut SymbolTable, source: &str) -> Result<(), String> {
run_within(symbols, source, MAX_REACHED)
fn number(n: i64) -> Expr {
Expr::Number(Number::Index(n))
fn heap(symbols: &SymbolTable) -> Vec<(u32, StructInstance)> {
symbols
.instances()
.map(|(serial, instance)| (serial, instance.clone()))
.collect()
fn pt(symbols: &mut SymbolTable, x: Expr) -> StructInstance {
let layout = symbols.define_struct("PT", vec!["X".to_string()]).unwrap();
StructInstance {
layout,
fields: vec![x],
fn session() -> SymbolTable {
let mut symbols = SymbolTable::with_builtins();
run(&mut symbols, "(defstruct pt x)").unwrap();
#[test]
fn an_instance_a_global_holds_enters_the_heap_with_a_serial_past_the_last() {
let mut symbols = session();
run(&mut symbols, "(defvar *a* (make-pt :x 1))").unwrap();
assert_eq!(heap(&symbols), [(0, pt(&mut symbols, number(1)))]);
assert_eq!(symbols.next_serial(), 1);
run(&mut symbols, "(defvar *b* (make-pt :x 2))").unwrap();
assert_eq!(
heap(&symbols),
[
(0, pt(&mut symbols, number(1))),
(1, pt(&mut symbols, number(2)))
]
);
assert_eq!(symbols.next_serial(), 2);
fn an_instance_no_global_holds_any_more_is_swept_and_its_serial_not_reused() {
run(
&mut symbols,
"(defvar *a* (make-pt :x 1)) (defvar *b* (make-pt :x 2))",
)
.unwrap();
run(&mut symbols, "(setf *a* nil)").unwrap();
assert_eq!(heap(&symbols), [(1, pt(&mut symbols, number(2)))]);
run(&mut symbols, "(defvar *c* (make-pt :x 3))").unwrap();
(1, pt(&mut symbols, number(2))),
(2, pt(&mut symbols, number(3)))
fn a_mutation_through_a_global_the_unit_never_wrote_is_read_back() {
run(&mut symbols, "(setf (pt-x *a*) 9)").unwrap();
assert_eq!(heap(&symbols), [(0, pt(&mut symbols, number(9)))]);
fn a_trapped_unit_keeps_the_mutations_it_made_first() {
"(setf (pt-x *a*) 5) (do ((i 0 (+ i 1))) ((> i 1073741823) i))",
assert_eq!(heap(&symbols), [(0, pt(&mut symbols, number(5)))]);
fn a_cycle_is_carried_as_an_instance_holding_itself() {
run(&mut symbols, "(defstruct box p) (defvar *c* (make-box))").unwrap();
run(&mut symbols, "(setf (box-p *c*) *c*)").unwrap();
let found = heap(&symbols);
assert_eq!(found.len(), 1);
let layout = symbols.define_struct("BOX", vec!["P".to_string()]).unwrap();
assert_eq!(found[0].1.fields, [Expr::Instance(found[0].0, layout)]);
fn an_instance_holding_a_closure_is_opaque_and_so_is_its_global() {
run(&mut symbols, "(setf (pt-x *a*) (lambda () 1))").unwrap();
assert_eq!(heap(&symbols), []);
run(&mut symbols, "*a*").unwrap_err(),
"Compile error: *A*: its struct value does not survive the form"
fn a_unit_that_fails_to_compile_leaves_the_heap_as_it_was() {
let before = heap(&symbols);
"(setf (pt-x *a*) 2) (undefined-function-here)",
.unwrap_err();
assert_eq!(heap(&symbols), before);
fn a_closure_cell_cycle_leaves_the_instances_beside_it_readable() {
run(&mut symbols, "(defvar *p* (make-pt :x 1))").unwrap();
"(defvar *h* (let ((g (lambda () 0))) (setf g (lambda () (funcall g))) g))",
fn a_heap_past_the_ceiling_loses_only_the_instances_it_did_not_reach() {
let items = (0..40).map(|n| n.to_string()).collect::<Vec<_>>().join(" ");
run_within(
&format!("(defvar *a* (list {items} (make-pt :x 2))) (defvar *z* (make-pt :x 1))"),
10,
assert_eq!(heap(&symbols), [(1, pt(&mut symbols, number(1)))]);
fn a_unit_whose_prologue_never_finished_leaves_the_heap_as_it_was() {
let bytes = compile(&mut symbols, "(setf (pt-x *a*) 5)").unwrap();
carry_globals(&mut store, &instance, &mut symbols, UnitEnd::Trapped);
fn a_unit_whose_prologue_finished_settles_the_heap() {
main.call(&mut store, &[], &mut [Val::AnyRef(None)])
carry_globals(&mut store, &instance, &mut symbols, UnitEnd::Returned);