git clone https://garage44.eu/jeroen/frits.git.. /crates/aot-check/src/lib.rs
//! Natives generated at build time from `scripts/`, checked against the
//! interpreter.
pub mod work {
include!(concat!(env!("OUT_DIR"), "/work.rs"));
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use frits::{Cx, Platform, Runtime, Status, Value};
fn platform() -> Platform<Vec<String>> {
let mut p = Platform::new(include_str!("../scripts/platform.frits")).unwrap();
p.bind("Out.say", |cx: &mut Cx<Vec<String>>, a| {
let s = cx.str(a[0]).to_string();
cx.state.push(s);
Ok(Value::Unit)
});
p
}
/// Run `work.frits` to the end; returns what it said and how long it took.
fn run(native: bool) -> (Vec<String>, f64) {
let mut rt = Runtime::new(platform());
rt.limits.memory = 64 << 20;
let m = rt
.platform
.compile(include_str!("../scripts/work.frits"))
.unwrap();
let pid = rt.spawn(m, Vec::new());
if native {
let ok = rt.get_mut(pid).unwrap().set_natives(
crate::work::frits_native::natives(),
crate::work::frits_native::FINGERPRINT,
);
assert!(ok, "the natives do not match the compiled script");
}
rt.post(pid, "go", Vec::new());
let t = Instant::now();
while rt.get(pid).unwrap().has_work() {
let died = rt.tick();
assert!(died.is_empty(), "{died:?}");
}
assert_eq!(rt.get(pid).unwrap().status, Status::Idle);
(rt.get(pid).unwrap().state.clone(), t.elapsed().as_secs_f64())
}
#[test]
fn compiled_code_does_what_the_interpreter_does() {
let (a, ti) = run(false);
let (b, tn) = run(true);
assert_eq!(
a,
[
"fib 17711 after 57313 calls",
"area 899992.0",
"6+9+12+15 n12,n15,n6,n9",
"bad x",
"42"
]
);
assert_eq!(a, b);
eprintln!(
"interpreted {:.1} ms, compiled {:.1} ms ({:.1}×)",
ti * 1e3,
tn * 1e3,
ti / tn
);
}
#[test]
fn natives_from_other_code_are_refused() {
let rt = Runtime::new(platform());
let m = rt
.platform
.compile("script / Out\non go() { Out.say(\"other\") }")
.unwrap();
let mut p = frits::Process::new(m, Vec::<String>::new());
assert!(!p.set_natives(
crate::work::frits_native::natives(),
crate::work::frits_native::FINGERPRINT
));
}
}