Lines
100 %
Functions
66.67 %
Branches
//! A unit's runtime globals are exports of its module in both compile
//! modes, beside the probe exports `resolve_type_handles` reads (PERSIST R6).
use nomiscript::{CompileMode, Compiler, Reader, SymbolTable, global_export_name};
use wasmtime::{Linker, Store};
use crate::runtime::{EngineOpts, build_engine, compile_module, resolve_type_handles};
fn instantiate_unit(mode: CompileMode) -> (SymbolTable, Store<()>, wasmtime::Instance) {
let engine = build_engine(EngineOpts::baseline().with_fuel()).unwrap();
let mut symbols = SymbolTable::with_builtins();
let program = Reader::parse("(defvar a 1) (setf a 2)").unwrap();
let bytes = Compiler::new()
.compile_with_mode(&program, &mut symbols, mode)
.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();
(symbols, store, instance)
}
#[test]
fn the_probe_exports_and_the_unit_globals_exist_in_both_modes() {
for mode in [CompileMode::Script, CompileMode::Eval] {
let (mut symbols, mut store, instance) = instantiate_unit(mode);
resolve_type_handles(&mut store, &instance).unwrap_or_else(|err| panic!("{mode:?}: {err}"));
let manifest = symbols.take_unit_slots();
assert_eq!(manifest.len(), 1, "{mode:?}");
let (idx, _) = manifest[0].cell.expect("the global is a cell");
for idx in [idx, manifest[0].flags[0]] {
let name = global_export_name(idx);
assert!(instance.get_global(&mut store, &name).is_some(), "{mode:?}");