2021-11-27 09:28:34 +01:00
|
|
|
use rhai::{Engine, EvalAltResult, FnPtr, INT};
|
2020-07-17 04:18:07 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_ptr() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_fn("bar", |x: &mut INT, y: INT| *x += y);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let f = Fn("bar");
|
|
|
|
let x = 40;
|
|
|
|
f.call(x, 2);
|
|
|
|
x
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
40
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let f = Fn("bar");
|
|
|
|
let x = 40;
|
|
|
|
x.call(f, 2);
|
|
|
|
x
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let f = Fn("bar");
|
|
|
|
let x = 40;
|
|
|
|
call(f, x, 2);
|
|
|
|
x
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
fn foo(x) { this += x; }
|
|
|
|
|
|
|
|
let f = Fn("foo");
|
|
|
|
let x = 40;
|
|
|
|
x.call(f, 2);
|
|
|
|
x
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<INT>(
|
|
|
|
r#"
|
2022-07-29 03:42:30 +02:00
|
|
|
fn foo(x) { this += x; }
|
2020-07-17 04:18:07 +02:00
|
|
|
|
2022-07-29 03:42:30 +02:00
|
|
|
let f = Fn("foo");
|
|
|
|
call(f, 2);
|
|
|
|
x
|
|
|
|
"#
|
2020-07-17 04:18:07 +02:00
|
|
|
)
|
2023-04-10 17:23:59 +02:00
|
|
|
.unwrap_err(),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorInFunctionCall(fn_name, _, err, ..)
|
2022-02-08 02:46:14 +01:00
|
|
|
if fn_name == "foo" && matches!(*err, EvalAltResult::ErrorUnboundThis(..))
|
2020-07-17 04:18:07 +02:00
|
|
|
));
|
|
|
|
|
2022-08-05 17:30:44 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
fn foo(x) { x + 1 }
|
|
|
|
let f = foo;
|
|
|
|
let g = 42;
|
|
|
|
g = foo;
|
|
|
|
call(f, 39) + call(g, 1)
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-07-17 04:18:07 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-22 17:12:09 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_ptr_curry() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_fn("foo", |x: &mut INT, y: INT| *x + y);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let f = Fn("foo");
|
|
|
|
let f2 = f.curry(40);
|
|
|
|
f2.call(2)
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let f = Fn("foo");
|
|
|
|
let f2 = curry(f, 40);
|
|
|
|
call(f2, 2)
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-27 09:28:34 +01:00
|
|
|
|
|
|
|
#[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]
|
2021-11-28 03:49:48 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2021-11-27 09:28:34 +01:00
|
|
|
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)?;
|
|
|
|
|
2021-12-02 05:49:46 +01:00
|
|
|
move |x: INT| -> Result<String, _> { fn_ptr.call(&engine, &ast, (x,)) }
|
2021-11-27 09:28:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// 'f' captures: the Engine, the AST, and the closure
|
|
|
|
assert_eq!(f(42)?, "hello42");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|