1
//! Carrying a unit's globals never fails the unit that ran (PERSIST).
2

            
3
use nomiscript::{CompileMode, Compiler, Expr, Number, Reader, SymbolTable};
4
use wasmtime::{Instance, Linker, Store};
5

            
6
use crate::runtime::{
7
    EngineOpts, UnitEnd, build_engine, carry_globals, carry_not_run, compile_module,
8
};
9

            
10
5
fn compile(symbols: &mut SymbolTable, source: &str) -> Vec<u8> {
11
5
    let program = Reader::parse(source).unwrap();
12
5
    Compiler::new()
13
5
        .compile_with_mode(&program, symbols, CompileMode::Eval)
14
5
        .unwrap()
15
5
}
16

            
17
2
fn instantiate(bytes: &[u8]) -> (Store<()>, Instance) {
18
2
    let engine = build_engine(EngineOpts::baseline().with_fuel()).unwrap();
19
2
    let module = compile_module(&engine, bytes).unwrap();
20
2
    let mut linker: Linker<()> = Linker::new(&engine);
21
2
    linker.define_unknown_imports_as_traps(&module).unwrap();
22
2
    let mut store: Store<()> = Store::new(&engine, ());
23
2
    store.set_fuel(10_000_000).unwrap();
24
2
    store.set_epoch_deadline(1);
25
2
    let instance = linker.instantiate(&mut store, &module).unwrap();
26
2
    (store, instance)
27
2
}
28

            
29
1
fn read_error(symbols: &mut SymbolTable, name: &str) -> String {
30
1
    let program = Reader::parse(name).unwrap();
31
1
    Compiler::new()
32
1
        .compile_with_mode(&program, symbols, CompileMode::Eval)
33
1
        .expect_err("the global has no value")
34
1
        .to_string()
35
1
}
36

            
37
#[test]
38
1
fn a_unit_that_never_ran_puts_back_every_slot() {
39
1
    let mut symbols = SymbolTable::with_builtins();
40
1
    compile(&mut symbols, "(defvar a 1) (defun f () 1)");
41
1
    carry_not_run(&mut symbols);
42
1
    compile(
43
1
        &mut symbols,
44
1
        "(defparameter a 2) (defun f () 2) (defvar b 3)",
45
1
    );
46
1
    carry_not_run(&mut symbols);
47
1
    assert_eq!(symbols.take_unit_slots(), []);
48
1
    let function = symbols
49
1
        .lookup("F")
50
        .and_then(|symbol| symbol.function())
51
        .cloned();
52
1
    assert!(function.is_none(), "{function:?}");
53
1
    assert!(
54
1
        symbols
55
1
            .lookup("A")
56
            .and_then(|symbol| symbol.value())
57
            .is_none()
58
    );
59
1
    assert!(symbols.lookup("B").is_none());
60
1
}
61

            
62
#[test]
63
1
fn a_manifest_naming_a_missing_export_makes_the_global_unreadable() {
64
1
    let mut symbols = SymbolTable::with_builtins();
65
1
    compile(&mut symbols, "(defvar a 1) (setf a 2)");
66
1
    let (mut store, instance) = instantiate(&compile(&mut SymbolTable::with_builtins(), "(+ 1 2)"));
67
1
    carry_globals(&mut store, &instance, &mut symbols, UnitEnd::Returned);
68
1
    assert_eq!(symbols.take_unit_slots(), []);
69
1
    assert_eq!(
70
1
        read_error(&mut symbols, "a"),
71
1
        "Compile error: A: its unreadable value does not survive the form"
72
    );
73
1
}
74

            
75
#[test]
76
1
fn a_unit_that_trapped_inside_a_binding_carries_the_value_outside_it() {
77
1
    let mut symbols = SymbolTable::with_builtins();
78
1
    let bytes = compile(
79
1
        &mut symbols,
80
1
        "(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)))",
81
1
    );
82
1
    let (mut store, instance) = instantiate(&bytes);
83
1
    let main = instance.get_func(&mut store, "nomi-eval").unwrap();
84
1
    main.call(&mut store, &[], &mut [wasmtime::Val::AnyRef(None)])
85
1
        .unwrap_err();
86
1
    carry_globals(&mut store, &instance, &mut symbols, UnitEnd::Trapped);
87
2
    let held = |name: &str| symbols.lookup(name).and_then(|symbol| symbol.value());
88
2
    let number = |n| Some(Expr::Number(Number::Index(n)));
89
1
    assert_eq!(held("A").cloned(), number(2));
90
1
    assert_eq!(held("B").cloned(), number(3));
91
1
}