Skip to main content

hello_call/
hello_call.rs

1//! Basic execution, host-to-Lua calls, and bounded host profiling.
2use moonseed::*;
3mod support;
4use support::*;
5fn main() -> ExampleResult {
6    let events = std::rc::Rc::new(std::cell::Cell::new(0_u64));
7    let count = events.clone();
8    let mut registry = HostRegistry::new();
9    registry.register_hook("example.profiler", move |_| {
10        count.set(count.get() + 1);
11        Ok(HookAction::Continue)
12    });
13    let mut rt = Runtime::builder().registry(registry).build()?;
14    rt.set_hook(
15        None,
16        "example.profiler",
17        HookMask::CALL | HookMask::RETURN,
18        0,
19    )?;
20    assert_eq!(eval::<i64>(&mut rt, b"return 6 * 7")?, 42);
21    let add: Function = eval(&mut rt, b"return function(a, b) return a + b, 'ok' end")?;
22    let result: (i64, String) = done(rt.call(&add, (20i64, 22i64), &mut Journal::new(), 100)?)?;
23    assert_eq!(result, (42, "ok".into()));
24    assert!(events.get() > 0);
25    rt.clear_hook(None)?;
26    // The counter is host state; persist it separately if needed on restore.
27    Ok(())
28}
29
30#[cfg(test)]
31#[test]
32fn example_runs_and_asserts_its_result() {
33    main().unwrap();
34}