From ba2aac4960194aec4e9d40f94576ff3a35903cd9 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 4 Mar 2020 23:06:05 +0800 Subject: [PATCH] General code cleanup. --- Cargo.toml | 2 +- src/api.rs | 35 ++++++++++++++++++----------------- src/engine.rs | 12 ++++++------ src/fn_register.rs | 10 ++++------ src/parser.rs | 11 +++++------ src/scope.rs | 18 ++++++++---------- 6 files changed, 42 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54678e04..b4ff53e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rhai" -version = "0.10.1" +version = "0.10.2" edition = "2018" authors = ["Jonathan Turner", "Lukáš Hozda", "Stephen Chung"] description = "Embedded scripting for Rust" diff --git a/src/api.rs b/src/api.rs index 49cadfcd..49553624 100644 --- a/src/api.rs +++ b/src/api.rs @@ -31,7 +31,8 @@ impl<'a> Engine<'a> { args, }; - self.fns.insert(spec, Arc::new(FnIntExt::Ext(f))); + self.external_functions + .insert(spec, Arc::new(FnIntExt::Ext(f))); } /// Register a custom type for use with the `Engine`. @@ -62,20 +63,20 @@ impl<'a> Engine<'a> { pub fn register_get( &mut self, name: &str, - get_fn: impl Fn(&mut T) -> U + 'static, + callback: impl Fn(&mut T) -> U + 'static, ) { let get_name = "get$".to_string() + name; - self.register_fn(&get_name, get_fn); + self.register_fn(&get_name, callback); } /// Register a setter function for a member of a registered type with the `Engine`. pub fn register_set( &mut self, name: &str, - set_fn: impl Fn(&mut T, U) -> () + 'static, + callback: impl Fn(&mut T, U) -> () + 'static, ) { let set_name = "set$".to_string() + name; - self.register_fn(&set_name, set_fn); + self.register_fn(&set_name, callback); } /// Shorthand for registering both getter and setter functions @@ -154,10 +155,10 @@ impl<'a> Engine<'a> { scope: &mut Scope, ast: &AST, ) -> Result { - let AST(os, fns) = ast; + let AST(statements, functions) = ast; - fns.iter().for_each(|f| { - self.script_fns.insert( + functions.iter().for_each(|f| { + self.script_functions.insert( FnSpec { name: f.name.clone().into(), args: None, @@ -166,11 +167,11 @@ impl<'a> Engine<'a> { ); }); - let result = os + let result = statements .iter() .try_fold(().into_dynamic(), |_, o| self.eval_stmt(scope, o)); - self.script_fns.clear(); // Clean up engine + self.script_functions.clear(); // Clean up engine match result { Err(EvalAltResult::Return(out, pos)) => out.downcast::().map(|v| *v).map_err(|a| { @@ -227,9 +228,9 @@ impl<'a> Engine<'a> { parse(&mut tokens.peekable()) .map_err(|err| EvalAltResult::ErrorParsing(err)) - .and_then(|AST(ref os, ref fns)| { - for f in fns { - self.script_fns.insert( + .and_then(|AST(ref statements, ref functions)| { + for f in functions { + self.script_functions.insert( FnSpec { name: f.name.clone().into(), args: None, @@ -238,12 +239,12 @@ impl<'a> Engine<'a> { ); } - let val = os + let val = statements .iter() .try_fold(().into_dynamic(), |_, o| self.eval_stmt(scope, o)) .map(|_| ()); - self.script_fns.clear(); // Clean up engine + self.script_functions.clear(); // Clean up engine val }) @@ -275,7 +276,7 @@ impl<'a> Engine<'a> { let pos = Default::default(); ast.1.iter().for_each(|f| { - self.script_fns.insert( + self.script_functions.insert( FnSpec { name: f.name.clone().into(), args: None, @@ -295,7 +296,7 @@ impl<'a> Engine<'a> { }) }); - self.script_fns.clear(); // Clean up engine + self.script_functions.clear(); // Clean up engine result } diff --git a/src/engine.rs b/src/engine.rs index 4f4396b0..b2e62bd6 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -41,9 +41,9 @@ type IteratorFn = dyn Fn(&Dynamic) -> Box>; /// ``` pub struct Engine<'a> { /// A hashmap containing all compiled functions known to the engine - pub(crate) fns: HashMap, Arc>, + pub(crate) external_functions: HashMap, Arc>, /// A hashmap containing all script-defined functions - pub(crate) script_fns: HashMap, Arc>, + pub(crate) script_functions: HashMap, Arc>, /// A hashmap containing all iterators known to the engine pub(crate) type_iterators: HashMap>, pub(crate) type_names: HashMap, @@ -87,11 +87,11 @@ impl Engine<'_> { // First search in script-defined functions (can override built-in), // then in built-in's let fn_def = self - .script_fns + .script_functions .get(&spec) .or_else(|| { spec.args = Some(args.iter().map(|a| Any::type_id(&**a)).collect()); - self.fns.get(&spec) + self.external_functions.get(&spec) }) .map(|f| f.clone()); @@ -741,8 +741,8 @@ impl Engine<'_> { // Create the new scripting Engine let mut engine = Engine { - fns: HashMap::new(), - script_fns: HashMap::new(), + external_functions: HashMap::new(), + script_functions: HashMap::new(), type_iterators: HashMap::new(), type_names, on_print: Box::new(|x| println!("{}", x)), // default print/debug implementations diff --git a/src/fn_register.rs b/src/fn_register.rs index ac73060b..9e84492f 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -61,8 +61,8 @@ pub struct Ref(A); pub struct Mut(A); macro_rules! count_args { - () => {0usize}; - ($head:ident $($tail:ident)*) => {1usize + count_args!($($tail)*)}; + () => { 0_usize }; + ( $head:ident $($tail:ident)* ) => { 1_usize + count_args!($($tail)*) }; } macro_rules! def_register { @@ -80,8 +80,7 @@ macro_rules! def_register { let fn_name = name.to_string(); let fun = move |mut args: FnCallArgs, pos: Position| { - // Check for length at the beginning to avoid - // per-element bound checks. + // Check for length at the beginning to avoid per-element bound checks. const NUM_ARGS: usize = count_args!($($par)*); if args.len() != NUM_ARGS { @@ -113,8 +112,7 @@ macro_rules! def_register { let fn_name = name.to_string(); let fun = move |mut args: FnCallArgs, pos: Position| { - // Check for length at the beginning to avoid - // per-element bound checks. + // Check for length at the beginning to avoid per-element bound checks. const NUM_ARGS: usize = count_args!($($par)*); if args.len() != NUM_ARGS { diff --git a/src/parser.rs b/src/parser.rs index ad68bc77..0b2c347c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -51,11 +51,8 @@ impl Position { /// Panics if already at beginning of a line - cannot rewind to a previous line. /// pub(crate) fn rewind(&mut self) { - if self.pos == 0 { - panic!("cannot rewind at position 0"); - } else { - self.pos -= 1; - } + assert!(self.pos > 0, "cannot rewind at position 0"); + self.pos -= 1; } /// Advance to the next line. @@ -159,7 +156,9 @@ impl Expr { | Expr::False(pos) | Expr::Unit(pos) => *pos, - Expr::Assignment(_, _) | Expr::Dot(_, _) | Expr::And(_, _) | Expr::Or(_, _) => panic!(), + Expr::Assignment(e, _) | Expr::Dot(e, _) | Expr::And(e, _) | Expr::Or(e, _) => { + e.position() + } } } } diff --git a/src/scope.rs b/src/scope.rs index a08e912f..bafba6ed 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -61,8 +61,8 @@ impl Scope { .iter() .enumerate() .rev() // Always search a Scope in reverse order - .find(|(_, (n, _))| n == key) - .map(|(i, (n, v))| (i, n.clone(), v.clone())) + .find(|(_, (name, _))| name == key) + .map(|(i, (name, value))| (i, name.clone(), value.clone())) } /// Get the value of a variable in the Scope, starting from the last. @@ -71,18 +71,16 @@ impl Scope { .iter() .enumerate() .rev() // Always search a Scope in reverse order - .find(|(_, (n, _))| n == key) - .and_then(|(_, (_, v))| v.downcast_ref::()) - .map(|v| v.clone()) + .find(|(_, (name, _))| name == key) + .and_then(|(_, (_, value))| value.downcast_ref::()) + .map(|value| value.clone()) } /// Get a mutable reference to a variable in the Scope. pub(crate) fn get_mut(&mut self, key: &str, index: usize) -> &mut Dynamic { let entry = self.0.get_mut(index).expect("invalid index in Scope"); - if entry.0 != key { - panic!("incorrect key at Scope entry"); - } + assert_eq!(entry.0, key, "incorrect key at Scope entry"); &mut entry.1 } @@ -92,7 +90,7 @@ impl Scope { self.0 .iter() .rev() // Always search a Scope in reverse order - .map(|(key, val)| (key.as_str(), val)) + .map(|(key, value)| (key.as_str(), value)) } /// Get a mutable iterator to variables in the Scope. @@ -100,7 +98,7 @@ impl Scope { self.0 .iter_mut() .rev() // Always search a Scope in reverse order - .map(|(key, val)| (key.as_str(), val)) + .map(|(key, value)| (key.as_str(), value)) } }