Add FnPtr::call.
This commit is contained in:
@@ -340,7 +340,7 @@ fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
|
||||
let fn_name = fn_ptr.fn_name().to_string();
|
||||
let context = NativeCallContext::new(&engine, &fn_name, &lib);
|
||||
|
||||
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
||||
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
||||
let f = move |x: INT| fn_ptr.call_dynamic(&context, None, [x.into()]);
|
||||
|
||||
assert_eq!(f(42)?.into_string(), Ok("hello42".to_string()));
|
||||
|
@@ -1,4 +1,4 @@
|
||||
use rhai::{Engine, EvalAltResult, INT};
|
||||
use rhai::{Engine, EvalAltResult, FnPtr, INT};
|
||||
|
||||
#[test]
|
||||
fn test_fn_ptr() -> Result<(), Box<EvalAltResult>> {
|
||||
@@ -111,3 +111,43 @@ fn test_fn_ptr_curry() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
fn test_fn_ptr_call() -> Result<(), Box<EvalAltResult>> {
|
||||
let engine = Engine::new();
|
||||
|
||||
let ast = engine.compile("private fn foo(x, y) { len(x) + y }")?;
|
||||
|
||||
let mut fn_ptr = FnPtr::new("foo")?;
|
||||
fn_ptr.set_curry(vec!["abc".into()]);
|
||||
let result: INT = fn_ptr.call(&engine, &ast, (39 as INT,))?;
|
||||
|
||||
assert_eq!(result, 42);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
fn test_fn_ptr_make_closure() -> Result<(), Box<EvalAltResult>> {
|
||||
let f = {
|
||||
let engine = Engine::new();
|
||||
|
||||
let ast = engine.compile(
|
||||
r#"
|
||||
let test = "hello";
|
||||
|x| test + x // this creates a closure
|
||||
"#,
|
||||
)?;
|
||||
|
||||
let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
|
||||
|
||||
move |x: INT| -> Result<String, Box<EvalAltResult>> { fn_ptr.call(&engine, &ast, (x,)) }
|
||||
};
|
||||
|
||||
// 'f' captures: the Engine, the AST, and the closure
|
||||
assert_eq!(f(42)?, "hello42");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user