rlang 0.1.11

R as a fusevm frontend: a lexer, parser, and compiler to fusevm bytecode on an RHost vector heap, with no bespoke VM or JIT
Documentation
//! The uniform embedding entry point shared by the fusevm frontends.

/// Interpreter stack for the run thread, the same size `Rscript` uses
/// (`main.rs`): every R call runs its body on a nested VM, so R recursion
/// consumes Rust stack.
const STACK_SIZE: usize = 512 * 1024 * 1024;

/// Run `src` as a complete program with `input` as its stdin and return
/// everything it wrote to stdout.
///
/// Each call runs on its own thread, so it gets a fresh host, its own output
/// capture and its own stdin (all thread-local), and never touches the
/// process stdout. `input` is what `readLines("stdin")` reads. Output is the
/// `Rscript` transcript: top-level autoprint, `print`, `cat`. A parse,
/// compile or runtime error (including `stop()`) is `Err` with its message;
/// a panic in the runtime is caught at the thread boundary and also becomes
/// `Err`. Warnings still go to the process stderr.
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}")
}