Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Scripts

The simplest way to use Rune is script mode. Instead of defining a main function, a script contains top-level statements or a single expression — such as value + 40 — that runs from start to finish.

When embedding Rune in Rust, enable script mode with Options::script and declare any top-level arguments the script expects with Build::with_args:

use rune::support::Result;
use rune::sync::Arc;
use rune::termcolor::{ColorChoice, StandardStream};
use rune::{Context, ContextError, Diagnostics, Hash, Module, Options, Source, Sources, Unit, Vm};

#[rune::function]
fn calculate(value: i64) -> i64 {
    value + 10
}

pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::new();
    module.function_meta(calculate)?;
    Ok(module)
}

fn main() -> Result<()> {
    let context = context()?;
    let runtime = Arc::try_new(context.runtime()?)?;

    let unit = compile(&context, "calculate(value) / 2")?;
    let mut vm = Vm::new(runtime, unit);

    let output = vm.call(Hash::EMPTY, (5,))?;
    let output: i64 = rune::from_value(output)?;
    println!("{output:?}");
    assert_eq!(output, 7);
    Ok(())
}

fn context() -> Result<Arc<Context>, ContextError> {
    let m = module()?;
    let mut context = rune_modules::default_context()?;
    context.install(m)?;
    Ok(Arc::try_new(context)?)
}

fn compile(context: &Context, script: &str) -> Result<Arc<Unit>> {
    let mut sources = Sources::new();
    sources.insert(Source::memory(script)?)?;

    let mut diagnostics = Diagnostics::new();
    let mut options = Options::from_default_env()?;
    options.script(true);

    let result = rune::prepare(&mut sources)
        .with_args(["value"])?
        .with_options(&options)
        .with_context(context)
        .with_diagnostics(&mut diagnostics)
        .build();

    if !diagnostics.is_empty() {
        let mut writer = StandardStream::stderr(ColorChoice::Always);
        diagnostics.emit(&mut writer, &sources)?;
    }

    let unit = result?;
    let unit = Arc::try_new(unit)?;
    Ok(unit)
}

Behind the scenes, script mode wraps the top-level code in an unnamed function identified by Hash::EMPTY. The arguments registered with Build::with_args become the parameters of that function. They are local to the script body, not global variables, so other functions defined in the file cannot read them directly.