FnPtr::call_dynamic shortcut function that enriches call arguments with curry-ed data automatically

This commit is contained in:
Ilya Lakhin
2020-07-23 04:53:40 +07:00
parent 6d551f1596
commit 82685b7df2
2 changed files with 61 additions and 1 deletions

View File

@@ -2,6 +2,8 @@
use rhai::{
Dynamic, Engine, EvalAltResult, FnPtr, Func, Module, ParseError, ParseErrorType, Scope, INT,
};
use std::any::TypeId;
use std::rc::Rc;
#[test]
fn test_fn() -> Result<(), Box<EvalAltResult>> {
@@ -157,3 +159,35 @@ fn test_fn_ptr_raw() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_currying_with_registered_fn() -> Result<(), Box<EvalAltResult>> {
let mut module = Module::new();
module.set_raw_fn(
"call_with_arg",
&[TypeId::of::<FnPtr>(), TypeId::of::<INT>()],
|engine: &Engine, module: &Module, args: &mut [&mut Dynamic]| {
std::mem::take(args[0])
.cast::<FnPtr>()
.call_dynamic(engine, module, None, [std::mem::take(args[1])])
},
);
let mut engine = Engine::new();
engine.load_package(Rc::new(module));
assert_eq!(
engine.eval::<INT>(
r#"
let addition = |x, y| { x + y };
let curryed = addition.curry(100);
call_with_arg(curryed, 5)
"#
)?,
105
);
Ok(())
}