diff --git a/examples/arrays_and_structs.rs b/examples/arrays_and_structs.rs index 424676bd..5f7b0f0b 100644 --- a/examples/arrays_and_structs.rs +++ b/examples/arrays_and_structs.rs @@ -1,5 +1,5 @@ extern crate rhai; -use rhai::{Engine, FnRegister}; +use rhai::{Any, Engine, EvalAltResult, RegisterFn}; #[derive(Clone, Debug)] struct TestStruct { @@ -16,14 +16,27 @@ impl TestStruct { } } +fn print_ty_name(test: Vec<&mut Box>) -> Result, EvalAltResult> { + println!("Ty name: {:?}", (*test[0]).as_ref().type_name()); + + Ok(Box::new(())) +} + fn main() { let mut engine = Engine::new(); engine.register_type::(); + print_ty_name(vec![&mut (Box::new(TestStruct::new()) as Box)]).unwrap(); + engine.register_fn("update", TestStruct::update); engine.register_fn("new_ts", TestStruct::new); + engine.register_fn_raw("ty_name".to_owned(), None, Box::new(print_ty_name)); + println!( + "{:?}", + engine.eval::("let x = new_ts(); x.update(); x") + ); println!( "{:?}", engine.eval::("let x = [new_ts()]; x[0].update(); x[0]") diff --git a/examples/simple_fn.rs b/examples/simple_fn.rs index b74a45db..55b1cb72 100644 --- a/examples/simple_fn.rs +++ b/examples/simple_fn.rs @@ -2,8 +2,6 @@ extern crate rhai; use rhai::{Any, Engine, RegisterFn}; fn add(x: i64, y: i64) -> i64 { - println!("Adding: {}", x + y); - x + y } @@ -11,17 +9,7 @@ fn main() { let mut engine = Engine::new(); engine.register_fn("add", add); - engine - .call_fn_raw( - "add".to_owned(), - vec![ - &mut (Box::new(3i64) as Box), - &mut (Box::new(5i64) as Box), - ], - ) - .expect("FAIL"); - - //if let Ok(result) = engine.eval::("add(40, 2)") { - // println!("Answer: {}", result); // prints 42 - //} + if let Ok(result) = engine.eval::("add(40, 2)") { + println!("Answer: {}", result); // prints 42 + } } diff --git a/src/engine.rs b/src/engine.rs index 8be07cc8..37fafb57 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -122,28 +122,27 @@ impl Engine { args: Vec<&mut Box>, ) -> Result, EvalAltResult> { let spec = FnSpec { - ident, + ident: ident.clone(), args: Some( args.iter() .map(|a| ::type_id(a.as_ref())) .collect(), ), }; + let spec1 = FnSpec { ident, args: None }; self.fns .get(&spec) + .or_else(|| self.fns.get(&spec1)) .ok_or(EvalAltResult::ErrorFunctionNotFound) .and_then(move |f| match *f { FnIntExt::Ext(ref f) => f(args), - FnIntExt::Int(_) => unimplemented!(), + FnIntExt::Int(_) => unreachable!(), }) } - pub fn register_fn_raw(&mut self, ident: String, args: Vec, f: Box) { - let spec = FnSpec { - ident, - args: Some(args), - }; + pub fn register_fn_raw(&mut self, ident: String, args: Option>, f: Box) { + let spec = FnSpec { ident, args }; self.fns.insert(spec, FnIntExt::Ext(f)); } @@ -152,6 +151,7 @@ impl Engine { /// your type must implement Clone. pub fn register_type(&mut self) { // currently a no-op, exists for future extensibility + println!("Registering {:?}", TypeId::of::()); } /// Register a get function for a member of a registered type @@ -216,7 +216,7 @@ impl Engine { ((*val).downcast_mut() as Option<&mut Vec>>) .and_then(|arr| idx.downcast_ref::().map(|idx| (arr, *idx as usize))) - .map(|(arr, idx)| arr[idx].box_clone()) + .map(|(arr, idx)| arr[idx].clone()) .ok_or(EvalAltResult::ErrorIndexMismatch) } Expr::Dot(ref inner_lhs, ref inner_rhs) => match **inner_lhs { @@ -243,7 +243,7 @@ impl Engine { .iter_mut() .rev() .find(|&&mut (ref name, _)| *id == *name) - .map(|&mut (_, ref mut val)| val.as_ref().box_clone()) + .map(|&mut (_, ref mut val)| val.clone()) .ok_or_else(|| EvalAltResult::ErrorVariableNotFound(id.clone())) .and_then(|mut target| self.get_dot_val_helper(scope, &mut target, dot_rhs)) @@ -283,7 +283,7 @@ impl Engine { .and_then(|arr| { idx_boxed .downcast_ref::() - .map(|idx| arr[*idx as usize].box_clone()) + .map(|idx| arr[*idx as usize].clone()) }) .ok_or(EvalAltResult::ErrorIndexMismatch) }) @@ -455,7 +455,7 @@ impl Engine { Expr::Identifier(ref id) => { for &mut (ref name, ref mut val) in &mut scope.iter_mut().rev() { if *id == *name { - return Ok(val.box_clone()); + return Ok(val.clone()); } } Err(EvalAltResult::ErrorVariableNotFound(id.clone())) @@ -469,7 +469,7 @@ impl Engine { if let Some(arr_typed) = (*val).downcast_mut() as Option<&mut Vec>> { - return Ok(arr_typed[*i as usize].box_clone()); + return Ok(arr_typed[*i as usize].clone()); } else { return Err(EvalAltResult::ErrorIndexMismatch); } @@ -688,9 +688,6 @@ impl Engine { let mut x: Result, EvalAltResult> = Ok(Box::new(())); for f in fns { - if f.params.len() > 6 { - return Err(EvalAltResult::ErrorFunctionArityNotSupported); - } let name = f.name.clone(); let local_f = f.clone(); @@ -709,12 +706,11 @@ impl Engine { } } - match x { - Ok(v) => match v.downcast::() { - Ok(out) => Ok(*out), - Err(_) => Err(EvalAltResult::ErrorMismatchOutputType), - }, - Err(e) => Err(e), + let x = x?; + + match x.downcast::() { + Ok(out) => Ok(*out), + Err(_) => Err(EvalAltResult::ErrorMismatchOutputType), } } Err(_) => Err(EvalAltResult::ErrorFunctionArgMismatch), diff --git a/src/fn_register.rs b/src/fn_register.rs index 3e9aab6c..80890e71 100644 --- a/src/fn_register.rs +++ b/src/fn_register.rs @@ -45,7 +45,7 @@ macro_rules! def_register { // potentially clone the value, otherwise pass the reference. Ok(Box::new(f($(($clone)($par)),*)) as Box) }; - self.register_fn_raw(name.to_owned(), vec![$(TypeId::of::<$par>()),*], Box::new(fun)); + self.register_fn_raw(name.to_owned(), Some(vec![$(TypeId::of::<$par>()),*]), Box::new(fun)); } }