diff --git a/examples/custom_types_and_methods.rs b/examples/custom_types_and_methods.rs index 66a9b2ea..65cf3225 100644 --- a/examples/custom_types_and_methods.rs +++ b/examples/custom_types_and_methods.rs @@ -1,5 +1,5 @@ extern crate rhai; -use rhai::{Engine, FnRegister}; +use rhai::{Engine, RegisterFn}; #[derive(Clone)] struct TestStruct { diff --git a/examples/repl.rs b/examples/repl.rs index cfc56596..145c288b 100644 --- a/examples/repl.rs +++ b/examples/repl.rs @@ -3,7 +3,7 @@ extern crate rhai; use std::fmt::Display; use std::process::exit; use std::io::{stdin, stdout, Write}; -use rhai::{Engine, FnRegister, Scope}; +use rhai::{Engine, RegisterFn, Scope}; fn showit(x: &mut T) -> () { println!("{}", x) diff --git a/examples/rhai_runner.rs b/examples/rhai_runner.rs index e4c4063d..6637844a 100644 --- a/examples/rhai_runner.rs +++ b/examples/rhai_runner.rs @@ -2,7 +2,7 @@ use std::env; use std::fmt::Display; extern crate rhai; -use rhai::{Engine, FnRegister}; +use rhai::{Engine, RegisterFn}; fn showit(x: &mut T) -> () { println!("{}", x) diff --git a/examples/simple_fn.rs b/examples/simple_fn.rs index 55b1cb72..04b55b68 100644 --- a/examples/simple_fn.rs +++ b/examples/simple_fn.rs @@ -1,5 +1,5 @@ extern crate rhai; -use rhai::{Any, Engine, RegisterFn}; +use rhai::{Engine, RegisterFn}; fn add(x: i64, y: i64) -> i64 { x + y diff --git a/src/engine.rs b/src/engine.rs index 43c8137d..7abbd746 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -27,6 +27,28 @@ pub enum EvalAltResult { Return(Box), } +impl PartialEq for EvalAltResult { + fn eq(&self, other: &Self) -> bool { + use EvalAltResult::*; + + match (self, other) { + (&ErrorFunctionNotFound, &ErrorFunctionNotFound) => true, + (&ErrorFunctionArgMismatch, &ErrorFunctionArgMismatch) => true, + (&ErrorFunctionCallNotSupported, &ErrorFunctionCallNotSupported) => true, + (&ErrorIndexMismatch, &ErrorIndexMismatch) => true, + (&ErrorIfGuardMismatch, &ErrorIfGuardMismatch) => true, + (&ErrorVariableNotFound(ref a), &ErrorVariableNotFound(ref b)) => a == b, + (&ErrorFunctionArityNotSupported, &ErrorFunctionArityNotSupported) => true, + (&ErrorAssignmentToUnknownLHS, &ErrorAssignmentToUnknownLHS) => true, + (&ErrorMismatchOutputType, &ErrorMismatchOutputType) => true, + (&ErrorCantOpenScriptFile, &ErrorCantOpenScriptFile) => true, + (&InternalErrorMalformedDotExpression, &InternalErrorMalformedDotExpression) => true, + (&LoopBreak, &LoopBreak) => true, + _ => false, + } + } +} + impl Error for EvalAltResult { fn description(&self) -> &str { match *self { @@ -121,6 +143,12 @@ impl Engine { ident: String, args: Vec<&mut Box>, ) -> Result, EvalAltResult> { + println!( + "Trying to call function {:?} with args {:?}", + ident, + args.iter().map(|x| (***x).type_id()).collect::>() + ); + let spec = FnSpec { ident: ident.clone(), args: Some( @@ -137,11 +165,26 @@ impl Engine { .ok_or(EvalAltResult::ErrorFunctionNotFound) .and_then(move |f| match *f { FnIntExt::Ext(ref f) => f(args), - FnIntExt::Int(_) => unreachable!(), + FnIntExt::Int(ref f) => { + let mut scope = Scope::new(); + scope.extend( + f.params + .iter() + .cloned() + .zip(args.iter().map(|x| (&**x).clone())), + ); + + match self.eval_stmt(&mut scope, &*f.body) { + Err(EvalAltResult::Return(x)) => Ok(x), + other => other, + } + } }) } pub fn register_fn_raw(&mut self, ident: String, args: Option>, f: Box) { + println!("Register; {:?} with args {:?}", ident, args,); + let spec = FnSpec { ident, args }; self.fns.insert(spec, FnIntExt::Ext(f)); @@ -247,7 +290,12 @@ impl Engine { .and_then(move |(idx, &mut (_, ref mut val))| map(val).map(|val| (idx, val))) } - fn array_value(&self, scope: &mut Scope, id: &str, idx: &Expr) -> Result<(usize, usize, Box), EvalAltResult> { + fn array_value( + &self, + scope: &mut Scope, + id: &str, + idx: &Expr, + ) -> Result<(usize, usize, Box), EvalAltResult> { let idx_boxed = self.eval_expr(scope, idx)? .downcast::() .map_err(|_| EvalAltResult::ErrorIndexMismatch)?; @@ -307,7 +355,10 @@ impl Engine { Expr::Identifier(ref id) => { let get_fn_name = "get$".to_string() + id; self.call_fn_raw(get_fn_name, vec![this_ptr]) - .and_then(|mut v| self.set_dot_val_helper(&mut v, inner_rhs, source_val)) + .and_then(|mut v| { + self.set_dot_val_helper(&mut v, inner_rhs, source_val) + .map(|_| v) // Discard Ok return value + }) .and_then(|mut v| { let set_fn_name = "set$".to_string() + id; diff --git a/src/lib.rs b/src/lib.rs index 10ac21f6..2e5947f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ //! And the Rust part: //! //! ```rust -//! use rhai::{FnRegister, Engine}; +//! use rhai::{Engine, RegisterFn}; //! //! fn compute_something(x: i64) -> bool { //! (x % 40) == 0 diff --git a/src/tests/arrays.rs b/src/tests/arrays.rs index 4d87728b..07e2cd30 100644 --- a/src/tests/arrays.rs +++ b/src/tests/arrays.rs @@ -1,5 +1,5 @@ use engine::Engine; -use fn_register::FnRegister; +use fn_register::RegisterFn; #[test] fn test_arrays() { diff --git a/src/tests/float.rs b/src/tests/float.rs index 6030a5b2..573077ff 100644 --- a/src/tests/float.rs +++ b/src/tests/float.rs @@ -1,5 +1,5 @@ use engine::Engine; -use fn_register::FnRegister; +use fn_register::RegisterFn; #[test] fn test_float() { diff --git a/src/tests/get_set.rs b/src/tests/get_set.rs index 46790e81..6315ad2c 100644 --- a/src/tests/get_set.rs +++ b/src/tests/get_set.rs @@ -1,5 +1,5 @@ use engine::Engine; -use fn_register::FnRegister; +use fn_register::RegisterFn; #[test] fn test_get_set() { @@ -86,9 +86,5 @@ fn test_big_get_set() { engine.register_fn("new_tp", TestParent::new); - if let Ok(result) = engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x") { - assert_eq!(result, 500); - } else { - assert!(false); - } + assert_eq!(engine.eval::("let a = new_tp(); a.child.x = 500; a.child.x"), Ok(500)); } diff --git a/src/tests/method_call.rs b/src/tests/method_call.rs index a2c36ed9..72ed3064 100644 --- a/src/tests/method_call.rs +++ b/src/tests/method_call.rs @@ -1,5 +1,5 @@ use engine::Engine; -use fn_register::FnRegister; +use fn_register::RegisterFn; #[test] fn test_method_call() { diff --git a/src/tests/mismatched_op.rs b/src/tests/mismatched_op.rs index 381e2b39..a3e04999 100644 --- a/src/tests/mismatched_op.rs +++ b/src/tests/mismatched_op.rs @@ -4,8 +4,8 @@ use engine::{Engine, EvalAltResult}; fn test_mismatched_op() { let mut engine = Engine::new(); - match engine.eval::("60 + \"hello\"") { - Err(EvalAltResult::ErrorFunctionArgMismatch) => (), - _ => assert!(false), - } + assert_eq!( + engine.eval::("60 + \"hello\""), + Err(EvalAltResult::ErrorFunctionNotFound) + ); }