2020-07-29 16:43:57 +02:00
|
|
|
#![cfg(not(feature = "no_function"))]
|
2020-10-18 11:02:17 +02:00
|
|
|
use rhai::{Engine, EvalAltResult, FnPtr, ParseErrorType, RegisterFn, Scope, INT};
|
2020-07-31 07:08:14 +02:00
|
|
|
use std::any::TypeId;
|
2020-08-22 05:08:27 +02:00
|
|
|
use std::cell::RefCell;
|
2020-08-09 13:35:29 +02:00
|
|
|
use std::mem::take;
|
2020-08-22 05:08:27 +02:00
|
|
|
use std::rc::Rc;
|
2020-07-31 07:08:14 +02:00
|
|
|
|
2020-08-22 17:01:25 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
use rhai::Map;
|
|
|
|
|
2020-07-29 16:43:57 +02:00
|
|
|
#[test]
|
|
|
|
fn test_fn_ptr_curry_call() -> Result<(), Box<EvalAltResult>> {
|
2020-08-08 16:59:05 +02:00
|
|
|
let mut engine = Engine::new();
|
2020-07-29 16:43:57 +02:00
|
|
|
|
2020-08-08 16:59:05 +02:00
|
|
|
#[allow(deprecated)]
|
|
|
|
engine.register_raw_fn(
|
2020-07-29 16:43:57 +02:00
|
|
|
"call_with_arg",
|
|
|
|
&[TypeId::of::<FnPtr>(), TypeId::of::<INT>()],
|
2020-10-18 11:02:17 +02:00
|
|
|
|context, args| {
|
2020-07-29 16:43:57 +02:00
|
|
|
let fn_ptr = std::mem::take(args[0]).cast::<FnPtr>();
|
2020-10-18 11:02:17 +02:00
|
|
|
fn_ptr.call_dynamic(context, None, [std::mem::take(args[1])])
|
2020-07-29 16:43:57 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let addition = |x, y| { x + y };
|
|
|
|
let curried = addition.curry(2);
|
|
|
|
|
|
|
|
call_with_arg(curried, 40)
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-03 06:10:20 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-07-29 16:43:57 +02:00
|
|
|
fn test_closures() -> Result<(), Box<EvalAltResult>> {
|
2020-08-08 14:09:18 +02:00
|
|
|
let mut engine = Engine::new();
|
2020-07-29 16:43:57 +02:00
|
|
|
|
2020-08-22 05:08:27 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.compile_expression("let f = |x| {};")
|
|
|
|
.expect_err("should error")
|
|
|
|
.0,
|
|
|
|
ParseErrorType::BadInput(_)
|
|
|
|
));
|
|
|
|
|
2020-07-29 16:43:57 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let x = 8;
|
|
|
|
|
|
|
|
let res = |y, z| {
|
|
|
|
let w = 12;
|
|
|
|
|
|
|
|
return (|| x + y + z + w).call();
|
|
|
|
}.curry(15).call(2);
|
|
|
|
|
|
|
|
res + (|| x - 3).call()
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-08-03 06:10:20 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let a = 41;
|
|
|
|
let foo = |x| { a += x };
|
|
|
|
foo.call(1);
|
|
|
|
a
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(engine.eval::<bool>(
|
|
|
|
r#"
|
|
|
|
let a = 41;
|
|
|
|
let foo = |x| { a += x };
|
|
|
|
a.is_shared()
|
|
|
|
"#
|
|
|
|
)?);
|
|
|
|
|
2020-08-08 16:59:05 +02:00
|
|
|
engine.register_fn("plus_one", |x: INT| x + 1);
|
2020-08-08 14:09:18 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let a = 41;
|
|
|
|
let f = || plus_one(a);
|
|
|
|
f.call()
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-08-09 16:12:50 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let a = 40;
|
|
|
|
let f = |x| {
|
|
|
|
let f = |x| {
|
|
|
|
let f = |x| plus_one(a) + x;
|
|
|
|
f.call(x)
|
|
|
|
};
|
|
|
|
f.call(x)
|
|
|
|
};
|
|
|
|
f.call(1)
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
|
|
|
let a = 21;
|
|
|
|
let f = |x| a += x;
|
|
|
|
f.call(a);
|
|
|
|
a
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
2020-08-09 13:35:29 +02:00
|
|
|
engine.register_raw_fn(
|
|
|
|
"custom_call",
|
|
|
|
&[TypeId::of::<INT>(), TypeId::of::<FnPtr>()],
|
2020-10-18 11:02:17 +02:00
|
|
|
|context, args| {
|
2020-08-09 13:35:29 +02:00
|
|
|
let func = take(args[1]).cast::<FnPtr>();
|
|
|
|
|
2020-10-18 11:02:17 +02:00
|
|
|
func.call_dynamic(context, None, [])
|
2020-08-09 13:35:29 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
2020-08-09 13:55:01 +02:00
|
|
|
let a = 41;
|
2020-08-09 13:35:29 +02:00
|
|
|
let b = 0;
|
2020-08-09 13:55:01 +02:00
|
|
|
let f = || b.custom_call(|| a + 1);
|
2020-08-09 13:35:29 +02:00
|
|
|
|
|
|
|
f.call()
|
|
|
|
"#
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
2020-08-03 06:10:20 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-08-05 16:53:01 +02:00
|
|
|
#[cfg(not(feature = "sync"))]
|
2020-08-03 06:10:20 +02:00
|
|
|
fn test_closures_data_race() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
r#"
|
2020-08-09 16:12:50 +02:00
|
|
|
let a = 1;
|
|
|
|
let b = 40;
|
|
|
|
let foo = |x| { this += a + x };
|
|
|
|
b.call(foo, 1);
|
|
|
|
b
|
|
|
|
"#
|
2020-08-03 06:10:20 +02:00
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
|
|
|
.eval::<INT>(
|
|
|
|
r#"
|
2020-08-09 16:12:50 +02:00
|
|
|
let a = 20;
|
|
|
|
let foo = |x| { this += a + x };
|
|
|
|
a.call(foo, 1);
|
|
|
|
a
|
|
|
|
"#
|
2020-08-03 06:10:20 +02:00
|
|
|
)
|
|
|
|
.expect_err("should error"),
|
|
|
|
EvalAltResult::ErrorDataRace(_, _)
|
|
|
|
));
|
|
|
|
|
2020-07-29 16:43:57 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-22 05:08:27 +02:00
|
|
|
|
|
|
|
type MyType = Rc<RefCell<INT>>;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
#[cfg(not(feature = "sync"))]
|
2020-08-23 10:29:15 +02:00
|
|
|
fn test_closures_shared_obj() -> Result<(), Box<EvalAltResult>> {
|
2020-08-22 05:08:27 +02:00
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
// Register API on MyType
|
|
|
|
engine
|
|
|
|
.register_type_with_name::<MyType>("MyType")
|
|
|
|
.register_get_set(
|
|
|
|
"data",
|
|
|
|
|p: &mut MyType| *p.borrow(),
|
|
|
|
|p: &mut MyType, value: INT| *p.borrow_mut() = value,
|
|
|
|
)
|
|
|
|
.register_fn("+=", |p1: &mut MyType, p2: MyType| {
|
|
|
|
*p1.borrow_mut() += *p2.borrow()
|
|
|
|
})
|
|
|
|
.register_fn("-=", |p1: &mut MyType, p2: MyType| {
|
|
|
|
*p1.borrow_mut() -= *p2.borrow()
|
|
|
|
});
|
|
|
|
|
|
|
|
let engine = engine; // Make engine immutable
|
|
|
|
|
|
|
|
let code = r#"
|
|
|
|
#{
|
|
|
|
name: "A",
|
|
|
|
description: "B",
|
|
|
|
cost: 1,
|
|
|
|
health_added: 0,
|
|
|
|
action: |p1, p2| { p1 += p2 }
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let ast = engine.compile(code)?;
|
|
|
|
let res = engine.eval_ast::<Map>(&ast)?;
|
|
|
|
|
|
|
|
// Make closure
|
|
|
|
let f = move |p1: MyType, p2: MyType| -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let action_ptr = res["action"].clone().cast::<FnPtr>();
|
|
|
|
let name = action_ptr.fn_name();
|
|
|
|
engine.call_fn::<_, ()>(&mut Scope::new(), &ast, name, (p1, p2))
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test closure
|
|
|
|
let p1 = Rc::new(RefCell::new(41));
|
|
|
|
let p2 = Rc::new(RefCell::new(1));
|
|
|
|
|
|
|
|
f(p1.clone(), p2.clone())?;
|
|
|
|
|
|
|
|
assert_eq!(*p1.borrow(), 42);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-23 10:29:15 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
fn test_closures_external() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let engine = Engine::new();
|
|
|
|
|
|
|
|
let mut ast = engine.compile(
|
|
|
|
r#"
|
|
|
|
let test = "hello";
|
|
|
|
|x| test + x
|
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Save the function pointer together with captured variables
|
|
|
|
let fn_ptr = engine.eval_ast::<FnPtr>(&ast)?;
|
|
|
|
|
|
|
|
// Get rid of the script, retaining only functions
|
|
|
|
ast.retain_functions(|_, _, _| true);
|
|
|
|
|
|
|
|
// Closure 'f' captures: the engine, the AST, and the curried function pointer
|
2020-10-20 04:54:32 +02:00
|
|
|
let f = move |x: INT| fn_ptr.call_dynamic((&engine, &[ast.as_ref()]).into(), None, [x.into()]);
|
2020-08-23 10:29:15 +02:00
|
|
|
|
|
|
|
assert_eq!(f(42)?.as_str(), Ok("hello42"));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|