use std::sync::{Arc, Mutex};
use fusevm::{ChunkBuilder, Op, VMResult, Value, VM};
fn main() {
let mut b = ChunkBuilder::new();
let greeting = b.add_constant(Value::str("hello "));
b.emit(Op::LoadConst(greeting), 1);
b.emit(Op::Print(1), 1);
b.emit(Op::LoadInt(42), 1);
b.emit(Op::PrintLn(1), 1);
let chunk = b.build();
let captured = Arc::new(Mutex::new(String::new()));
let sink_buf = Arc::clone(&captured);
let mut vm = VM::new(chunk);
vm.set_output_sink(Box::new(move |s: &str| {
sink_buf.lock().unwrap().push_str(s);
}));
let mut pending = vec!["line two".to_string(), "line one".to_string()];
vm.set_input_source(Box::new(move || pending.pop()));
match vm.run() {
VMResult::Ok(_) | VMResult::Halted => {}
VMResult::Error(e) => {
eprintln!("vm error: {e}");
std::process::exit(1);
}
}
let out = captured.lock().unwrap().clone();
assert_eq!(out, "hello 42\n", "sink did not capture VM output");
print!("{out}");
}