Fix call_fn to take &ast instead of ast.

This commit is contained in:
Stephen Chung 2020-03-07 00:29:45 +08:00
parent c917aa0a5a
commit ea82ee81d6
3 changed files with 8 additions and 8 deletions

View File

@ -146,7 +146,7 @@ let ast = Engine::compile("fn hello(x, y) { x.len() + y }")?;
// Evaluate the function in the AST, passing arguments into the script as a tuple // Evaluate the function in the AST, 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) // (beware, arguments must be of the correct types because Rhai does not have built-in type conversions)
let result: i64 = engine.call_fn("hello", ast, (&mut String::from("abc"), &mut 123_i64))?; let result: i64 = engine.call_fn("hello", &ast, (&mut String::from("abc"), &mut 123_i64))?;
``` ```
# Values and types # Values and types

View File

@ -9,7 +9,7 @@ use crate::scope::Scope;
use std::any::TypeId; use std::any::TypeId;
use std::sync::Arc; use std::sync::Arc;
impl<'a> Engine<'a> { impl<'e> Engine<'e> {
pub(crate) fn register_fn_raw( pub(crate) fn register_fn_raw(
&mut self, &mut self,
fn_name: &str, fn_name: &str,
@ -169,7 +169,7 @@ impl<'a> Engine<'a> {
let result = statements let result = statements
.iter() .iter()
.try_fold(().into_dynamic(), |_, o| self.eval_stmt(scope, o)); .try_fold(().into_dynamic(), |_, stmt| self.eval_stmt(scope, stmt));
self.script_functions.clear(); // Clean up engine self.script_functions.clear(); // Clean up engine
@ -261,7 +261,7 @@ impl<'a> Engine<'a> {
/// ///
/// let ast = Engine::compile("fn add(x, y) { x.len() + y }")?; /// let ast = Engine::compile("fn add(x, y) { x.len() + y }")?;
/// ///
/// let result: i64 = engine.call_fn("add", ast, (&mut String::from("abc"), &mut 123_i64))?; /// let result: i64 = engine.call_fn("add", &ast, (&mut String::from("abc"), &mut 123_i64))?;
/// ///
/// assert_eq!(result, 126); /// assert_eq!(result, 126);
/// # Ok(()) /// # Ok(())
@ -270,7 +270,7 @@ impl<'a> Engine<'a> {
pub fn call_fn<'f, A: FuncArgs<'f>, T: Any + Clone>( pub fn call_fn<'f, A: FuncArgs<'f>, T: Any + Clone>(
&mut self, &mut self,
name: &str, name: &str,
ast: AST, ast: &AST,
args: A, args: A,
) -> Result<T, EvalAltResult> { ) -> Result<T, EvalAltResult> {
let pos = Default::default(); let pos = Default::default();
@ -317,7 +317,7 @@ impl<'a> Engine<'a> {
/// } /// }
/// assert_eq!(result, "42"); /// assert_eq!(result, "42");
/// ``` /// ```
pub fn on_print(&mut self, callback: impl FnMut(&str) + 'a) { pub fn on_print(&mut self, callback: impl FnMut(&str) + 'e) {
self.on_print = Box::new(callback); self.on_print = Box::new(callback);
} }
@ -337,7 +337,7 @@ impl<'a> Engine<'a> {
/// } /// }
/// assert_eq!(result, "\"hello\""); /// assert_eq!(result, "\"hello\"");
/// ``` /// ```
pub fn on_debug(&mut self, callback: impl FnMut(&str) + 'a) { pub fn on_debug(&mut self, callback: impl FnMut(&str) + 'e) {
self.on_debug = Box::new(callback); self.on_debug = Box::new(callback);
} }
} }

View File

@ -6,7 +6,7 @@ fn test_engine_call_fn() -> Result<(), EvalAltResult> {
let ast = Engine::compile("fn hello(x, y) { x.len() + y }")?; let ast = Engine::compile("fn hello(x, y) { x.len() + y }")?;
let result: i64 = engine.call_fn("hello", ast, (&mut String::from("abc"), &mut 123_i64))?; let result: i64 = engine.call_fn("hello", &ast, (&mut String::from("abc"), &mut 123_i64))?;
assert_eq!(result, 126); assert_eq!(result, 126);