Function rune::load_sources[][src]

pub fn load_sources(
    context: &Context,
    options: &Options,
    sources: &mut Sources,
    diagnostics: &mut Diagnostics
) -> Result<Unit, LoadSourcesError>

Load and compile the given sources.

Uses the Source::name when generating diagnostics to reference the file.

Examples

Note: these must be built with the diagnostics feature enabled to give access to rune::termcolor.

use rune::termcolor::{ColorChoice, StandardStream};
use rune::EmitDiagnostics as _;

use std::path::Path;
use std::sync::Arc;
use std::error::Error;

let context = runestick::Context::with_default_modules()?;
let mut options = rune::Options::default();

let mut sources = rune::Sources::new();
sources.insert(runestick::Source::new("entry", r#"
pub fn main() {
    println("Hello World");
}
"#));

let mut diagnostics = rune::Diagnostics::new();

let result = rune::load_sources(&context, &options, &mut sources, &mut diagnostics);

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

let unit = result?;
let unit = Arc::new(unit);
let vm = runestick::Vm::new(Arc::new(context.runtime()), unit.clone());