Add support for anonymous functions in Rust.

This commit is contained in:
Stephen Chung
2020-04-08 23:01:48 +08:00
parent 660ce6cc79
commit 518725e119
7 changed files with 181 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
#![cfg(not(feature = "no_function"))]
use rhai::{Engine, EvalAltResult, ParseErrorType, Scope, INT};
use rhai::{AnonymousFn, Engine, EvalAltResult, ParseErrorType, Scope, INT};
#[test]
fn test_fn() -> Result<(), EvalAltResult> {
@@ -59,3 +59,16 @@ fn test_call_fn() -> Result<(), EvalAltResult> {
Ok(())
}
#[test]
fn test_anonymous_fn() -> Result<(), EvalAltResult> {
let calc_func = AnonymousFn::<(INT, INT, INT), INT>::create_from_script(
Engine::new(),
"fn calc(x, y, z) { (x + y) * z }",
"calc",
)?;
assert_eq!(calc_func(42, 123, 9)?, 1485);
Ok(())
}