use std::io::{ErrorKind, Write};
const STACK_BYTES: usize = 4 << 30;
pub fn run_program(src: &str, input: &str) -> Result<String, String> {
let (src, input) = (src.to_owned(), input.to_owned());
let run = move || run_here(&src, &input);
std::thread::Builder::new()
.stack_size(STACK_BYTES)
.spawn(run)
.map_err(|e| format!("arb: cannot start the run thread: {e}"))?
.join()
.unwrap_or_else(|_| Err("arb: run panicked".to_string()))
}
fn run_here(src: &str, input: &str) -> Result<String, String> {
let (mut out, mut err) = (Vec::new(), Vec::new());
let status = crate::hosted::run(|| {
match crate::cli::run_headless(src, input.as_bytes(), &mut out, &mut err) {
Ok(()) => 0,
Err(e) if e.kind() == ErrorKind::BrokenPipe => 0,
Err(e) => {
let _ = writeln!(err, "arb: {e}");
1
}
}
});
if status == 0 {
return Ok(String::from_utf8_lossy(&out).into_owned());
}
let msg = String::from_utf8_lossy(&err).trim_end().to_string();
Err(if msg.is_empty() {
format!("exit status {status}")
} else {
msg
})
}