Merge branch 'master' into master

This commit is contained in:
Stephen Chung 2020-03-13 18:27:53 +08:00 committed by GitHub
commit c7a6777b70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 22 deletions

View File

@ -167,7 +167,7 @@ use rhai::Engine;
let mut engine = Engine::new(); let mut engine = Engine::new();
let ast = engine.compile_file("hello_world.rhai")?; let ast = engine.compile_file("hello_world.rhai".into()).unwrap();
``` ```
Rhai also allows you to work _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust. Rhai also allows you to work _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust.

View File

@ -12,6 +12,7 @@ use std::{
any::{type_name, TypeId}, any::{type_name, TypeId},
fs::File, fs::File,
io::prelude::*, io::prelude::*,
path::PathBuf,
sync::Arc, sync::Arc,
}; };
@ -104,9 +105,9 @@ impl<'e> Engine<'e> {
parse(&mut tokens_stream.peekable(), self.optimize) parse(&mut tokens_stream.peekable(), self.optimize)
} }
fn read_file(filename: &str) -> Result<String, EvalAltResult> { fn read_file(path: PathBuf) -> Result<String, EvalAltResult> {
let mut f = File::open(filename) let mut f = File::open(path.clone())
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?; .map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
let mut contents = String::new(); let mut contents = String::new();
@ -116,14 +117,14 @@ impl<'e> Engine<'e> {
} }
/// Compile a file into an AST. /// Compile a file into an AST.
pub fn compile_file(&self, filename: &str) -> Result<AST, EvalAltResult> { pub fn compile_file(&self, path: PathBuf) -> Result<AST, EvalAltResult> {
Self::read_file(filename) Self::read_file(path)
.and_then(|contents| self.compile(&contents).map_err(|err| err.into())) .and_then(|contents| self.compile(&contents).map_err(|err| err.into()))
} }
/// Evaluate a file. /// Evaluate a file.
pub fn eval_file<T: Any + Clone>(&mut self, filename: &str) -> Result<T, EvalAltResult> { pub fn eval_file<T: Any + Clone>(&mut self, path: PathBuf) -> Result<T, EvalAltResult> {
Self::read_file(filename).and_then(|contents| self.eval::<T>(&contents)) Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
} }
/// Evaluate a string. /// Evaluate a string.
@ -206,17 +207,10 @@ impl<'e> Engine<'e> {
/// and not cleared from run to run. /// and not cleared from run to run.
pub fn consume_file( pub fn consume_file(
&mut self, &mut self,
filename: &str, path: PathBuf,
retain_functions: bool, retain_functions: bool,
) -> Result<(), EvalAltResult> { ) -> Result<(), EvalAltResult> {
let mut f = File::open(filename) Self::read_file(path).and_then(|_| self.consume(&contents, retain_functions))
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?;
let mut contents = String::new();
f.read_to_string(&mut contents)
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))
.and_then(|_| self.consume(&contents, retain_functions))
} }
/// Evaluate a string, but throw away the result and only return error (if any). /// Evaluate a string, but throw away the result and only return error (if any).

View File

@ -29,7 +29,7 @@
//! //!
//! engine.register_fn("compute_something", compute_something); //! engine.register_fn("compute_something", compute_something);
//! //!
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai")?, true); //! assert_eq!(engine.eval_file::<bool>("my_script.rhai".into())?, true);
//! //!
//! Ok(()) //! Ok(())
//! } //! }

View File

@ -4,7 +4,7 @@ use crate::any::Dynamic;
use crate::error::ParseError; use crate::error::ParseError;
use crate::parser::{Position, INT}; use crate::parser::{Position, INT};
use std::{error::Error, fmt}; use std::{error::Error, fmt, path::PathBuf};
/// Evaluation result. /// Evaluation result.
/// ///
@ -45,7 +45,7 @@ pub enum EvalAltResult {
/// Wrapped value is the type of the actual result. /// Wrapped value is the type of the actual result.
ErrorMismatchOutputType(String, Position), ErrorMismatchOutputType(String, Position),
/// Error reading from a script file. Wrapped value is the path of the script file. /// Error reading from a script file. Wrapped value is the path of the script file.
ErrorReadingScriptFile(String, std::io::Error), ErrorReadingScriptFile(PathBuf, std::io::Error),
/// Inappropriate member access. /// Inappropriate member access.
ErrorDotExpr(String, Position), ErrorDotExpr(String, Position),
/// Arithmetic error encountered. Wrapped value is the error message. /// Arithmetic error encountered. Wrapped value is the error message.
@ -125,8 +125,8 @@ impl fmt::Display for EvalAltResult {
} }
Self::LoopBreak => write!(f, "{}", desc), Self::LoopBreak => write!(f, "{}", desc),
Self::Return(_, pos) => write!(f, "{} ({})", desc, pos), Self::Return(_, pos) => write!(f, "{} ({})", desc, pos),
Self::ErrorReadingScriptFile(filename, err) => { Self::ErrorReadingScriptFile(path, err) => {
write!(f, "{} '{}': {}", desc, filename, err) write!(f, "{} '{}': {}", desc, path.display(), err)
} }
Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p), Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p),
Self::ErrorFunctionArgsMismatch(fun, 0, n, pos) => write!( Self::ErrorFunctionArgsMismatch(fun, 0, n, pos) => write!(