rhai/doc/src/engine/call-fn.md
2020-06-20 12:06:17 +08:00

2.4 KiB

Calling Rhai Functions from Rust

{{#include ../links.md}}

Rhai also allows working backwards from the other direction - i.e. calling a Rhai-scripted function from Rust via Engine::call_fn.

Functions declared with private are hidden and cannot be called from Rust (see also [modules]).

// Define functions in a script.
let ast = engine.compile(true,
    r#"
        // a function with two parameters: string and i64
        fn hello(x, y) {
            x.len + y
        }

        // functions can be overloaded: this one takes only one parameter
        fn hello(x) {
            x * 2
        }

        // this one takes no parameters
        fn hello() {
            42
        }

        // this one is private and cannot be called by 'call_fn'
        private hidden() {
            throw "you shouldn't see me!";
        }
    "#)?;

// 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.
// 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_fn(&mut scope, &ast, "hello", (123_i64,) )?;
//                                                          ^^^^^^^^^^ tuple of one

let result: i64 = engine.call_fn(&mut scope, &ast, "hello", () )?;
//                                                          ^^ unit = tuple of zero

// The following call will return a function-not-found error because
// 'hidden' is declared with 'private'.
let result: () = engine.call_fn(&mut scope, &ast, "hidden", ())?;

For more control, construct all arguments as Dynamic values and use Engine::call_fn_dynamic, passing it anything that implements IntoIterator<Item = Dynamic> (such as a simple Vec<Dynamic>):

let result: Dynamic = engine.call_fn_dynamic(&mut scope, &ast, "hello",
                            vec![ String::from("abc").into(), 123_i64.into() ])?;