Allow call_fn with only one parameter; consume can retain functions, eval cannot.

This commit is contained in:
Stephen Chung
2020-03-12 14:54:14 +08:00
parent e24d3a7ade
commit 560da5fdc6
11 changed files with 176 additions and 156 deletions

View File

@@ -167,28 +167,35 @@ let ast = engine.compile_file("hello_world.rhai")?;
```
Rhai also allows you to work _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust.
You do this via `call_fn`, which takes a compiled AST (output from `compile`) and the
function call arguments:
You do this via `call_fn`:
```rust
use rhai::Engine;
let mut engine = Engine::new();
// Define a function in a script and compile to AST
let ast = engine.compile(
// Define a function in a script and load it into the Engine.
engine.consume(
r"
fn hello(x, y) { // a function with two arguments: String and i64
x.len() + y // returning i64
}
")?;
fn hello(x, y) { // a function with two parameters: String and i64
x.len() + y // returning i64
}
// Evaluate the function in the AST, passing arguments into the script as a tuple.
fn hello(x) { // script-functions can be overloaded: this one takes only one parameter
x * 2 // returning i64
}
", true)?; // pass true to 'retain_functions' otherwise these functions will be cleared
// at the end of consume()
// Evaluate the function in the AST, 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 you pass in arguments of the wrong type, the Engine will not find the function.
let result: i64 = engine.call_fn("hello", &ast, ( String::from("abc"), 123_i64 ) )?;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ put arguments in a tuple
let result: i64 = engine.call_fn("hello", 123_i64)?
// ^^^^^^^ this one calls the 'hello' function with one parameter (so need for tuple)
```
Values and types
@@ -318,7 +325,7 @@ fn main()
}
```
You can also see in this example how you can register multiple functions (or in this case multiple instances of the same function) to the same name in script. This gives you a way to overload functions the correct one, based on the types of the arguments, from your script.
You can also see in this example how you can register multiple functions (or in this case multiple instances of the same function) to the same name in script. This gives you a way to overload functions the correct one, based on the types of the parameters, from your script.
Fallible functions
------------------
@@ -975,8 +982,8 @@ fn add(x, y) {
print(add(2, 3));
```
Functions defined in script always take `Dynamic` arguments (i.e. the arguments can be of any type).
It is important to remember that all arguments are passed by _value_, so all functions are _pure_ (i.e. they never modify their arguments).
Functions defined in script always take `Dynamic` parameters (i.e. the parameter can be of any type).
It is important to remember that all parameters are passed by _value_, so all functions are _pure_ (i.e. they never modify their parameters).
Any update to an argument will **not** be reflected back to the caller. This can introduce subtle bugs, if you are not careful.
```rust