diff --git a/README.md b/README.md index 4b5c239e..8cfe5444 100644 --- a/README.md +++ b/README.md @@ -213,8 +213,7 @@ Compiling a script file is also supported: let ast = engine.compile_file("hello_world.rhai".into())?; ``` -Rhai also allows working _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust - -via `call_fn` or its cousins `call_fn1` (one argument) and `call_fn0` (no argument). +Rhai also allows working _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust via `call_fn`. ```rust // Define functions in a script. @@ -239,20 +238,19 @@ let ast = engine.compile(true, // A custom scope can also contain any variables/constants available to the functions let mut scope = Scope::new(); -// Evaluate a function defined in the script, passing arguments into the script as a tuple -// if there are more than one. Beware, arguments must be of the correct types because -// Rhai does not have built-in type conversions. If arguments of the wrong types are passed, -// the Engine will not find the function. +// Evaluate a function defined in the script, passing arguments into the script as a tuple. +// Beware, arguments must be of the correct types because Rhai does not have built-in type conversions. +// If arguments of the wrong types are passed, the Engine will not find the function. let result: i64 = engine.call_fn(&mut scope, &ast, "hello", ( String::from("abc"), 123_i64 ) )?; // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // put arguments in a tuple -let result: i64 = engine.call_fn1(&mut scope, &ast, "hello", 123_i64)? -// ^^^^^^^^ use 'call_fn1' for one argument +let result: i64 = engine.call_fn(&mut scope, &ast, "hello", (123_i64,) )? +// ^^^^^^^^^^ tuple of one -let result: i64 = engine.call_fn0(&mut scope, &ast, "hello")? -// ^^^^^^^^ use 'call_fn0' for no arguments +let result: i64 = engine.call_fn(&mut scope, &ast, "hello", () )? +// ^^ unit = tuple of zero ``` Evaluate expressions only @@ -305,8 +303,8 @@ they even cannot be added together. This is very similar to Rust. The default integer type is `i64`. If other integer types are not needed, it is possible to exclude them and make a smaller build with the [`only_i64`] feature. -If only 32-bit integers are needed, enabling the [`only_i32`] feature will remove support for all integer types other than `i32`, -including `i64`. This is useful on some 32-bit systems where using 64-bit integers incurs a performance penalty. +If only 32-bit integers are needed, enabling the [`only_i32`] feature will remove support for all integer types other than `i32`, including `i64`. +This is useful on some 32-bit systems where using 64-bit integers incurs a performance penalty. If no floating-point is needed or supported, use the [`no_float`] feature to remove it. diff --git a/src/api.rs b/src/api.rs index 461c97e9..6df0cc8d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -831,79 +831,6 @@ impl<'e> Engine<'e> { }) } - /// Call a script function defined in an `AST` with no argument. - /// - /// # Example - /// - /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { - /// # #[cfg(not(feature = "no_stdlib"))] - /// # #[cfg(not(feature = "no_function"))] - /// # { - /// use rhai::{Engine, Scope}; - /// - /// let engine = Engine::new(); - /// - /// let ast = engine.compile("fn num() { 42 + foo }")?; - /// - /// let mut scope = Scope::new(); - /// scope.push("foo", 42_i64); - /// - /// // Call the script-defined function - /// let result: i64 = engine.call_fn0(&mut scope, &ast, "num")?; - /// - /// assert_eq!(result, 84); - /// # } - /// # Ok(()) - /// # } - /// ``` - #[cfg(not(feature = "no_function"))] - pub fn call_fn0( - &self, - scope: &mut Scope, - ast: &AST, - name: &str, - ) -> Result { - self.call_fn_internal(scope, ast, name, vec![]) - } - - /// Call a script function defined in an `AST` with one argument. - /// - /// # Example - /// - /// ``` - /// # fn main() -> Result<(), rhai::EvalAltResult> { - /// # #[cfg(not(feature = "no_stdlib"))] - /// # #[cfg(not(feature = "no_function"))] - /// # { - /// use rhai::{Engine, Scope}; - /// - /// let engine = Engine::new(); - /// - /// let ast = engine.compile("fn inc(x) { x + foo }")?; - /// - /// let mut scope = Scope::new(); - /// scope.push("foo", 42_i64); - /// - /// // Call the script-defined function - /// let result: i64 = engine.call_fn1(&mut scope, &ast, "inc", 123_i64)?; - /// - /// assert_eq!(result, 165); - /// # } - /// # Ok(()) - /// # } - /// ``` - #[cfg(not(feature = "no_function"))] - pub fn call_fn1( - &self, - scope: &mut Scope, - ast: &AST, - name: &str, - arg: A, - ) -> Result { - self.call_fn_internal(scope, ast, name, vec![arg.into_dynamic()]) - } - /// Call a script function defined in an `AST` with multiple arguments. /// /// # Example @@ -917,15 +844,25 @@ impl<'e> Engine<'e> { /// /// let engine = Engine::new(); /// - /// let ast = engine.compile("fn add(x, y) { len(x) + y + foo }")?; + /// let ast = engine.compile(r" + /// fn add(x, y) { len(x) + y + foo } + /// fn add1(x) { len(x) + 1 + foo } + /// fn bar() { foo/2 } + /// ")?; /// /// let mut scope = Scope::new(); /// scope.push("foo", 42_i64); /// /// // Call the script-defined function - /// let result: i64 = engine.call_fn(&mut scope, &ast, "add", (String::from("abc"), 123_i64))?; - /// + /// let result: i64 = engine.call_fn(&mut scope, &ast, "add", ( String::from("abc"), 123_i64 ) )?; /// assert_eq!(result, 168); + /// + /// let result: i64 = engine.call_fn(&mut scope, &ast, "add1", ( String::from("abc"), ) )?; + /// // ^^^^^^^^^^^^^^^^^^^^^^^^ tuple of one + /// assert_eq!(result, 46); + /// + /// let result: i64 = engine.call_fn(&mut scope, &ast, "bar", () )?; + /// assert_eq!(result, 21); /// # } /// # Ok(()) /// # } @@ -938,17 +875,7 @@ impl<'e> Engine<'e> { name: &str, args: A, ) -> Result { - self.call_fn_internal(scope, ast, name, args.into_vec()) - } - - #[cfg(not(feature = "no_function"))] - fn call_fn_internal( - &self, - scope: &mut Scope, - ast: &AST, - name: &str, - mut arg_values: Vec, - ) -> Result { + let mut arg_values = args.into_vec(); let mut args: Vec<_> = arg_values.iter_mut().map(Dynamic::as_mut).collect(); let fn_lib = Some(ast.1.as_ref()); let pos = Position::none(); diff --git a/src/call.rs b/src/call.rs index ac33e2c7..c81b25bc 100644 --- a/src/call.rs +++ b/src/call.rs @@ -36,6 +36,7 @@ macro_rules! impl_args { (@pop) => { }; (@pop $head:ident) => { + impl_args!(); }; (@pop $head:ident $(, $tail:ident)+) => { impl_args!($($tail),*); diff --git a/tests/call_fn.rs b/tests/call_fn.rs index b0b3793d..4ee2c3a6 100644 --- a/tests/call_fn.rs +++ b/tests/call_fn.rs @@ -44,10 +44,10 @@ fn test_call_fn() -> Result<(), EvalAltResult> { let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (42 as INT, 123 as INT))?; assert_eq!(r, 165); - let r: i64 = engine.call_fn1(&mut scope, &ast, "hello", 123 as INT)?; + let r: i64 = engine.call_fn(&mut scope, &ast, "hello", (123 as INT,))?; assert_eq!(r, 5166); - let r: i64 = engine.call_fn0(&mut scope, &ast, "hello")?; + let r: i64 = engine.call_fn(&mut scope, &ast, "hello", ())?; assert_eq!(r, 42); assert_eq!(