luau 0.732.0

Safe lifetime-bound Rust embedding API for the Luau runtime
use std::sync::atomic::{AtomicUsize, Ordering};

use luau::{DebugAction, DebugHooks, Lua, Result};

static STEP_COUNT: AtomicUsize = AtomicUsize::new(0);
static LAST_LINE: AtomicUsize = AtomicUsize::new(0);

fn main() -> Result<()> {
    let mut lua = Lua::new()?;

    lua.set_debug_handler(DebugHooks::new().on_step(|context| {
        STEP_COUNT.fetch_add(1, Ordering::Relaxed);

        if let Some(line) = context.current_line() {
            LAST_LINE.store(line, Ordering::Relaxed);
        }

        Ok(DebugAction::Continue)
    }));

    lua.set_single_step(true);
    let result: Result<i32> = lua
        .load(
            r#"
            local total = 0
            for index = 1, 5 do
                total += index
            end
            return total
            "#,
        )
        .set_name("debug_step")
        .call(());
    lua.set_single_step(false);
    lua.remove_debug_handler();

    let value = result?;
    println!(
        "result={value}, steps={}, last_line={}",
        STEP_COUNT.load(Ordering::Relaxed),
        LAST_LINE.load(Ordering::Relaxed)
    );
    Ok(())
}