const STACK_SIZE: usize = 512 * 1024 * 1024;
pub fn run_program(src: &str, input: &str) -> Result<String, String> {
let (src, input) = (src.to_string(), input.to_string());
std::thread::Builder::new()
.name("rlang-embed".into())
.stack_size(STACK_SIZE)
.spawn(move || {
crate::host::set_stdin(&input);
let (result, out) = crate::eval_captured(&src, &[]);
result.map(|_| out)
})
.map_err(|e| format!("cannot start interpreter thread: {e}"))?
.join()
.unwrap_or_else(|panic| Err(panic_message(panic.as_ref())))
}
fn panic_message(panic: &(dyn std::any::Any + Send)) -> String {
let reason = panic
.downcast_ref::<&str>()
.copied()
.or_else(|| panic.downcast_ref::<String>().map(String::as_str))
.unwrap_or("unknown panic");
format!("interpreter panicked: {reason}")
}