1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
use crate::compiler; use crate::{CompileError, LoadError, LoadErrorKind, Options, Warnings}; use runestick::{Context, LinkerErrors, Source, Span, Unit}; use std::cell::RefCell; use std::fs; use std::path::Path; use std::rc::Rc; /// Load the given path. /// /// The name of the loaded source will be the path as a string. /// /// If you want to load a script from memory use [load_source]. /// /// [load_source]: crate::load_source /// /// # Examples /// /// Note: these must be built with the `diagnostics` feature enabled to give /// access to `rune::termcolor`. /// /// ```rust,no_run /// use rune::termcolor::{ColorChoice, StandardStream}; /// use rune::EmitDiagnostics as _; /// /// use std::path::Path; /// use std::sync::Arc; /// use std::error::Error; /// /// # fn main() -> Result<(), Box<dyn Error>> { /// let path = Path::new("script.rn"); /// /// let context = Arc::new(rune::default_context()?); /// let mut options = rune::Options::default(); /// let mut warnings = rune::Warnings::new(); /// /// let unit = match rune::load_path(&*context, &options, &path, &mut warnings) { /// Ok(unit) => unit, /// Err(error) => { /// let mut writer = StandardStream::stderr(ColorChoice::Always); /// error.emit_diagnostics(&mut writer)?; /// return Ok(()); /// } /// }; /// /// let unit = Arc::new(unit); /// let vm = runestick::Vm::new(context.clone(), unit.clone()); /// /// if !warnings.is_empty() { /// let mut writer = StandardStream::stderr(ColorChoice::Always); /// rune::emit_warning_diagnostics(&mut writer, &warnings, &*unit)?; /// } /// /// # Ok(()) /// # } /// ``` pub fn load_path( context: &Context, options: &Options, path: &Path, warnings: &mut Warnings, ) -> Result<Unit, LoadError> { let source = fs::read_to_string(path).map_err(|error| { LoadError::from(LoadErrorKind::ReadFile { error, path: path.to_owned(), }) })?; let name = path.display().to_string(); let unit = load_source(context, options, Source::new(name, source), warnings)?; Ok(unit) } /// Load and compile the given source. /// /// 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`. /// /// ```rust /// use rune::termcolor::{ColorChoice, StandardStream}; /// use rune::EmitDiagnostics as _; /// use runestick::Source; /// /// use std::path::Path; /// use std::sync::Arc; /// use std::error::Error; /// /// # fn main() -> Result<(), Box<dyn Error>> { /// let context = Arc::new(rune::default_context()?); /// let mut options = rune::Options::default(); /// let mut warnings = rune::Warnings::new(); /// /// let source = Source::new("entry", r#" /// fn main() { /// println("Hello World"); /// } /// "#); /// /// let unit = match rune::load_source(&*context, &options, source, &mut warnings) { /// Ok(unit) => unit, /// Err(error) => { /// let mut writer = StandardStream::stderr(ColorChoice::Always); /// error.emit_diagnostics(&mut writer)?; /// return Ok(()); /// } /// }; /// /// let unit = Arc::new(unit); /// let vm = runestick::Vm::new(context.clone(), unit.clone()); /// /// if !warnings.is_empty() { /// let mut writer = StandardStream::stderr(ColorChoice::Always); /// rune::emit_warning_diagnostics(&mut writer, &warnings, &*unit)?; /// } /// /// # Ok(()) /// # } /// ``` pub fn load_source( context: &Context, options: &Options, code_source: Source, warnings: &mut Warnings, ) -> Result<Unit, LoadError> { let unit = Rc::new(RefCell::new(Unit::with_default_prelude())); if let Err(error) = compiler::compile_with_options(&*context, &code_source, &options, &unit, warnings) { return Err(LoadError::from(LoadErrorKind::CompileError { error, code_source, })); } let unit = match Rc::try_unwrap(unit) { Ok(unit) => unit.into_inner(), Err(..) => { return Err(LoadError::from(LoadErrorKind::CompileError { error: CompileError::internal("unit is not exlusively held", Span::empty()), code_source, })); } }; if options.link_checks { let mut errors = LinkerErrors::new(); if !unit.link(&*context, &mut errors) { return Err(LoadError::from(LoadErrorKind::LinkError { errors, code_source, })); } } Ok(unit) }